15: Check returned data before decoding
This commit is contained in:
+25
-15
@@ -41,26 +41,36 @@ public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPos
|
||||
@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<SolanaAccountInfo> accountInfos = solanaBlockChain.getProgramAccounts(
|
||||
JUPITER_PERPS_PROGRAM_ID,
|
||||
Set.of(new SolanaProgramAccountMemcmpFilter(
|
||||
POSITION_OWNER_OFFSET,
|
||||
owner
|
||||
))
|
||||
);
|
||||
|
||||
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
||||
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
||||
|
||||
for (SolanaAccountInfo accountInfo : accountInfos) {
|
||||
JupiterPerpsPosition position = positionDecoder.decode(
|
||||
accountInfo.address(),
|
||||
accountInfo
|
||||
);
|
||||
positions.add(position);
|
||||
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);
|
||||
}
|
||||
|
||||
return Set.copyOf(positions);
|
||||
}
|
||||
private static final ΩJupiterPerpsProgramIdΩ JUPITER_PERPS_PROGRAM_ID =
|
||||
"PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
|
||||
private static final int POSITION_OWNER_OFFSET = 8;
|
||||
|
||||
Reference in New Issue
Block a user