X: Do not return old closed positions

This commit is contained in:
2026-07-05 01:06:52 +02:00
parent dac76a2686
commit f8387f6034
3 changed files with 51 additions and 40 deletions
@@ -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<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
@NotNull Set<JupiterPerpsPosition> getOpenPositions(@NotNull ΩSolanaWalletIdΩ owner)
throws IOException, InterruptedException;
}
@@ -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;
}
@@ -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<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
public @NotNull Set<JupiterPerpsPosition> getOpenPositions(@NotNull ΩSolanaWalletIdΩ owner)
throws IOException, InterruptedException {
Set<SolanaAccountInfo> 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