From f8387f603442d92c9681410268b225bbcf0bd3014ae5b2e4d8206ed7239f6783 Mon Sep 17 00:00:00 2001 From: Minimons Date: Sun, 5 Jul 2026 01:06:52 +0200 Subject: [PATCH] X: Do not return old closed positions --- .../jupiter/perps/JupiterPerpsService.tjava | 7 +- ...AnchorIdlJupiterPerpsPositionDecoder.tjava | 14 ++-- .../AnchorIdlJupiterPerpsServiceImpl.tjava | 70 +++++++++++-------- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/main/tjava/com/r35157/libs/jupiter/perps/JupiterPerpsService.tjava b/src/main/tjava/com/r35157/libs/jupiter/perps/JupiterPerpsService.tjava index c513c2d..f8a41ea 100644 --- a/src/main/tjava/com/r35157/libs/jupiter/perps/JupiterPerpsService.tjava +++ b/src/main/tjava/com/r35157/libs/jupiter/perps/JupiterPerpsService.tjava @@ -1,5 +1,8 @@ package com.r35157.libs.jupiter.perps; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.io.IOException; import java.util.Set; @@ -26,7 +29,7 @@ public interface JupiterPerpsService { * @throws InterruptedException if the calling thread is interrupted while fetching * the position account */ - JupiterPerpsPosition getPosition(ΩJupiterPerpsPositionAccountΩ positionAccount) + @Nullable JupiterPerpsPosition getPosition(@NotNull ΩJupiterPerpsPositionAccountΩ positionAccount) throws IOException, InterruptedException; /** @@ -40,6 +43,6 @@ public interface JupiterPerpsService { * @throws IOException if the position accounts could not be fetched or decoded * @throws InterruptedException if the calling thread is interrupted while fetching positions */ - Set getOpenPositions(ΩSolanaWalletIdΩ owner) + @NotNull Set getOpenPositions(@NotNull ΩSolanaWalletIdΩ owner) throws IOException, InterruptedException; } \ No newline at end of file diff --git a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava index c910f84..2224bc1 100644 --- a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava +++ b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava @@ -4,15 +4,19 @@ import com.r35157.libs.codec.Base58Codec; import com.r35157.libs.codec.impl.ref.Base58CodecImpl; import com.r35157.libs.jupiter.perps.JupiterPerpsPositionDirection; import com.r35157.libs.solana.SolanaAccountInfo; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Base64; +import static java.math.BigDecimal.ZERO; + class AnchorIdlJupiterPerpsPositionDecoder { - JupiterPerpsPositionInfo decode(SolanaAccountInfo accountInfo) { + @Nullable JupiterPerpsPositionInfo decode(@NotNull SolanaAccountInfo accountInfo) { byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64()); if (data.length < PRICE_OFFSET + U64_LENGTH) { @@ -51,13 +55,7 @@ class AnchorIdlJupiterPerpsPositionDecoder { .valueOf(rawSizeUsd) .movePointLeft(6); - - JupiterPerpsPositionInfo posInfo = new JupiterPerpsPositionInfo( - entryPrice, - direction, - sizeUsd, - collateralUsd - ); + JupiterPerpsPositionInfo posInfo = new JupiterPerpsPositionInfo(entryPrice, direction, sizeUsd, collateralUsd); return posInfo; } diff --git a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsServiceImpl.tjava b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsServiceImpl.tjava index 9d4d0d5..f3ed880 100644 --- a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsServiceImpl.tjava +++ b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsServiceImpl.tjava @@ -7,6 +7,8 @@ import com.r35157.libs.solana.SolanaBlockChain; import com.r35157.libs.solana.SolanaProgramAccountMemcmpFilter; import com.r35157.libs.solana.valuetypes.WellKnownCurrencyTypes; import com.r35157.libs.valuetypes.basic.MoneyAmount; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.math.BigDecimal; @@ -26,7 +28,7 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService { } @Override - public JupiterPerpsPosition getPosition(ΩJupiterPerpsPositionAccountΩ positionAccount) + public @Nullable JupiterPerpsPosition getPosition(@NotNull ΩJupiterPerpsPositionAccountΩ positionAccount) throws IOException, InterruptedException { SolanaAccountInfo accountInfo = solanaBlockChain.getAccountInfo(positionAccount); @@ -44,39 +46,44 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService { JupiterPerpsPositionInfo info = positionDecoder.decode(accountInfo); - ΩUSDCAmountΩ value = ZERO; // TODO - Dummy - ΩUSDCAmountΩ pnl = ZERO; // TODO - Dummy - BigDecimal pnlPercent = ZERO; // TODO - Dummy - BigDecimal leverage = ZERO; // TODO - Dummy - ΩUSDCPriceΩ marketPrice = ZERO; // TODO - Dummy - ΩUSDCAmountΩ totalFees = ZERO; // TODO - Dummy - ΩUSDCAmountΩ borrowFeesDue = ZERO; // TODO - Dummy - ΩUSDCAmountΩ closeFeePending = ZERO; // TODO - Dummy - ΩSolanaAmountΩ accountRent = new MoneyAmount(ZERO, WellKnownCurrencyTypes.SOLANA.getCurrencyType()); // TODO - Dummy + JupiterPerpsPosition pos; + if(info == null) { + pos = null; + } else { + ΩUSDCAmountΩ value = ZERO; // TODO - Dummy + ΩUSDCAmountΩ pnl = ZERO; // TODO - Dummy + BigDecimal pnlPercent = ZERO; // TODO - Dummy + BigDecimal leverage = ZERO; // TODO - Dummy + ΩUSDCPriceΩ marketPrice = ZERO; // TODO - Dummy + ΩUSDCAmountΩ totalFees = ZERO; // TODO - Dummy + ΩUSDCAmountΩ borrowFeesDue = ZERO; // TODO - Dummy + ΩUSDCAmountΩ closeFeePending = ZERO; // TODO - Dummy + ΩSolanaAmountΩ accountRent = new MoneyAmount(ZERO, WellKnownCurrencyTypes.SOLANA.getCurrencyType()); // TODO - Dummy - JupiterPerpsPosition pos = new JupiterPerpsPosition( - positionAccount, - tradedTokenMint, - info.direction(), - value, - info.sizeUsd(), - pnl, - pnlPercent, - leverage, - info.entryPrice(), - marketPrice, - info.collateralUsd(), - totalFees, - borrowFeesDue, - closeFeePending, - accountRent - ); + pos = new JupiterPerpsPosition( + positionAccount, + tradedTokenMint, + info.direction(), + value, + info.sizeUsd(), + pnl, + pnlPercent, + leverage, + info.entryPrice(), + marketPrice, + info.collateralUsd(), + totalFees, + borrowFeesDue, + closeFeePending, + accountRent + ); + } return pos; } @Override - public Set getOpenPositions(ΩSolanaWalletIdΩ owner) + public @NotNull Set getOpenPositions(@NotNull ΩSolanaWalletIdΩ owner) throws IOException, InterruptedException { Set accountInfos = solanaBlockChain.getProgramAccounts( JUPITER_PERPS_PROGRAM_ID, @@ -98,9 +105,12 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService { throw new IllegalArgumentException(errorMsg); } - ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo); - JupiterPerpsPositionInfo info = positionDecoder.decode(accountInfo); + if(info == null || info.sizeUsd().compareTo(ZERO) == 0) { + continue; + } + + ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo); ΩUSDCAmountΩ value = ZERO; // TODO - Dummy ΩUSDCAmountΩ pnl = ZERO; // TODO - Dummy