15: Check returned data before decoding
This commit is contained in:
+25
-15
@@ -41,26 +41,36 @@ public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPos
|
|||||||
@Override
|
@Override
|
||||||
public Set<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
|
public Set<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException {
|
||||||
Set<SolanaAccountInfo> accountInfos = solanaBlockChain.getProgramAccounts(
|
Set<SolanaAccountInfo> accountInfos = solanaBlockChain.getProgramAccounts(
|
||||||
JUPITER_PERPS_PROGRAM_ID,
|
JUPITER_PERPS_PROGRAM_ID,
|
||||||
Set.of(new SolanaProgramAccountMemcmpFilter(
|
Set.of(new SolanaProgramAccountMemcmpFilter(
|
||||||
POSITION_OWNER_OFFSET,
|
POSITION_OWNER_OFFSET,
|
||||||
owner
|
owner
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
||||||
|
|
||||||
for (SolanaAccountInfo accountInfo : accountInfos) {
|
for (SolanaAccountInfo accountInfo : accountInfos) {
|
||||||
JupiterPerpsPosition position = positionDecoder.decode(
|
ΩSolanaAddressΩ address = accountInfo.address();
|
||||||
accountInfo.address(),
|
ΩSolanaProgramIdΩ programId = accountInfo.owner();
|
||||||
accountInfo
|
|
||||||
);
|
if (!JUPITER_PERPS_PROGRAM_ID.equals(programId)) {
|
||||||
positions.add(position);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 static final int POSITION_OWNER_OFFSET = 8;
|
||||||
|
|||||||
Reference in New Issue
Block a user