15: Add getOpenPositions() to Jupiter Perps API (real implementation)
This commit is contained in:
+22
-5
@@ -4,8 +4,10 @@ import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
|
||||
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionService;
|
||||
import com.r35157.libs.solana.SolanaAccountInfo;
|
||||
import com.r35157.libs.solana.SolanaBlockChain;
|
||||
import com.r35157.libs.solana.SolanaProgramAccountMemcmpFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPositionService {
|
||||
@@ -37,16 +39,31 @@ public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPos
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<JupiterPerpsPosition> getOpenPositions(
|
||||
ΩSolanaWalletIdΩ owner
|
||||
) throws IOException, InterruptedException {
|
||||
throw new UnsupportedOperationException(
|
||||
"Finding Jupiter Perps positions by owner is not implemented yet."
|
||||
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) {
|
||||
JupiterPerpsPosition position = positionDecoder.decode(
|
||||
accountInfo.address(),
|
||||
accountInfo
|
||||
);
|
||||
positions.add(position);
|
||||
}
|
||||
|
||||
return Set.copyOf(positions);
|
||||
}
|
||||
private static final ΩJupiterPerpsProgramIdΩ JUPITER_PERPS_PROGRAM_ID =
|
||||
"PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
|
||||
private static final int POSITION_OWNER_OFFSET = 8;
|
||||
|
||||
private final SolanaBlockChain solanaBlockChain;
|
||||
private final AnchorIdlJupiterPerpsPositionDecoder positionDecoder;
|
||||
|
||||
Reference in New Issue
Block a user