15: Add getOpenPositions() to Jupiter Perps API
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.r35157.libs.jupiter.perps;
|
package com.r35157.libs.jupiter.perps;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service for reading Jupiter Perps positions.
|
* Service for reading Jupiter Perps positions.
|
||||||
@@ -27,4 +28,18 @@ public interface JupiterPerpsPositionService {
|
|||||||
*/
|
*/
|
||||||
JupiterPerpsPosition getPosition(ΩJupiterPerpsPositionAccountΩ positionAccount)
|
JupiterPerpsPosition getPosition(ΩJupiterPerpsPositionAccountΩ positionAccount)
|
||||||
throws IOException, InterruptedException;
|
throws IOException, InterruptedException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds open Jupiter Perps positions owned by a wallet.
|
||||||
|
*
|
||||||
|
* <p>This method returns decoded Jupiter Perps position objects. It does not return
|
||||||
|
* raw Solana accounts or account ids.</p>
|
||||||
|
*
|
||||||
|
* @param owner the wallet address that owns the Jupiter Perps positions
|
||||||
|
* @return the open Jupiter Perps positions owned by the wallet
|
||||||
|
* @throws IOException if the position accounts could not be fetched or decoded
|
||||||
|
* @throws InterruptedException if the calling thread is interrupted while fetching positions
|
||||||
|
*/
|
||||||
|
Set<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
|
||||||
|
throws IOException, InterruptedException;
|
||||||
}
|
}
|
||||||
+37
@@ -4,8 +4,11 @@ import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
|
|||||||
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionService;
|
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionService;
|
||||||
import com.r35157.libs.solana.SolanaAccountInfo;
|
import com.r35157.libs.solana.SolanaAccountInfo;
|
||||||
import com.r35157.libs.solana.SolanaBlockChain;
|
import com.r35157.libs.solana.SolanaBlockChain;
|
||||||
|
import com.r35157.libs.solana.SolanaProgramAccountMemcmpFilter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPositionService {
|
public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPositionService {
|
||||||
|
|
||||||
@@ -35,8 +38,42 @@ public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPos
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
|
||||||
|
throws IOException, InterruptedException {
|
||||||
|
Set<SolanaAccountInfo> accountInfos = solanaBlockChain.getProgramAccounts(
|
||||||
|
JUPITER_PERPS_PROGRAM_ID,
|
||||||
|
Set.of(new SolanaProgramAccountMemcmpFilter(
|
||||||
|
POSITION_OWNER_OFFSET,
|
||||||
|
owner
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
|
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
||||||
|
|
||||||
|
for (SolanaAccountInfo accountInfo : accountInfos) {
|
||||||
|
ΩSolanaAddressΩ address = accountInfo.address();
|
||||||
|
ΩSolanaProgramIdΩ programId = accountInfo.owner();
|
||||||
|
|
||||||
|
if (!JUPITER_PERPS_PROGRAM_ID.equals(programId)) {
|
||||||
|
String errorMsg = "Account '" + address + "' is not owned by Jupiter Perps program '" +
|
||||||
|
programId + "'";
|
||||||
|
throw new IllegalArgumentException(errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
JupiterPerpsPosition position = positionDecoder.decode(
|
||||||
|
address,
|
||||||
|
accountInfo
|
||||||
|
);
|
||||||
|
positions.add(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Set.copyOf(positions);
|
||||||
|
}
|
||||||
|
|
||||||
private static final ΩJupiterPerpsProgramIdΩ JUPITER_PERPS_PROGRAM_ID =
|
private static final ΩJupiterPerpsProgramIdΩ JUPITER_PERPS_PROGRAM_ID =
|
||||||
"PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
|
"PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
|
||||||
|
private static final int POSITION_OWNER_OFFSET = 8;
|
||||||
|
|
||||||
private final SolanaBlockChain solanaBlockChain;
|
private final SolanaBlockChain solanaBlockChain;
|
||||||
private final AnchorIdlJupiterPerpsPositionDecoder positionDecoder;
|
private final AnchorIdlJupiterPerpsPositionDecoder positionDecoder;
|
||||||
|
|||||||
Reference in New Issue
Block a user