X: NFC - Remove code duplication

This commit is contained in:
2026-08-05 10:12:38 +02:00
parent b2278b12d5
commit eda12a12fd
@@ -46,67 +46,14 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo); JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo);
JupiterPerpsPosition pos;
if (perpsPositionInfo == null) { if (perpsPositionInfo == null) {
pos = null; return null;
} else {
ΩJupiterPerpsCustodyAccountAddressΩ custodyAccountAddress = positionDecoder.decodeCustodyAccountAddress(accountInfo);
ΩJupiterCustodyAccountInfoEncodedΩ encodedCustodyAccountInfo = solanaBlockChain.getAccountInfo(custodyAccountAddress);
if (encodedCustodyAccountInfo == null) {
String errorMsg = "Jupiter Perps custody account does not exist: " + custodyAccountAddress;
throw new IllegalArgumentException(errorMsg);
} }
JupiterCustodyAccountInfo custodyAccountInfo = custodyDecoder.decode(encodedCustodyAccountInfo); JupiterPerpsPosition pos = buildPosition(
ΩSPLMintAddressΩ mintAddress = custodyAccountInfo.mintAddress();
ΩJupiterPerpsCollateralCustodyAccountAddressΩ collateralCustodyAccountAddress =
perpsPositionInfo.collateralCustodyAccountAddress();
ΩJupiterCustodyAccountInfoEncodedΩ encodedCollateralCustodyAccountInfo =
solanaBlockChain.getAccountInfo(collateralCustodyAccountAddress);
JupiterCustodyAccountInfo collateralCustodyAccountInfo =
custodyDecoder.decode(encodedCollateralCustodyAccountInfo);
ΩJupiterPostionInterestΩ positionInterest =
collateralCustodyAccountInfo.currentCumulativeInterestRate()
.subtract(perpsPositionInfo.cumulativeInterestSnapshot());
ΩUSDCAmountΩ borrowFeesDue =
calculateBorrowFeesDue(
positionInterest,
perpsPositionInfo.positionSize()
);
Ω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Ω closeFeePending = ZERO; // TODO - Dummy
ΩSolanaAmountΩ accountRent = new MoneyAmount(ZERO, WellKnownCurrencyTypes.SOLANA.getCurrencyType()); // TODO - Dummy
pos = new JupiterPerpsPosition(
positionAccount, positionAccount,
mintAddress, perpsPositionInfo
perpsPositionInfo.direction(),
value,
perpsPositionInfo.positionSize(),
pnl,
pnlPercent,
leverage,
perpsPositionInfo.entryPrice(),
marketPrice,
perpsPositionInfo.collateral(),
totalFees,
borrowFeesDue,
closeFeePending,
accountRent
); );
}
return pos; return pos;
} }
@@ -139,9 +86,24 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
continue; continue;
} }
JupiterPerpsPosition pos = buildPosition(
positionAccount,
perpsPositionInfo
);
positions.add(pos);
}
return Set.copyOf(positions);
}
private JupiterPerpsPosition buildPosition(
ΩJupiterPerpsPositionAccountΩ positionAccount,
JupiterPerpsPositionInfo perpsPositionInfo
) throws IOException, InterruptedException {
ΩJupiterPerpsCustodyAccountAddressΩ custodyAccountAddress = perpsPositionInfo.custodyAccountAddress(); ΩJupiterPerpsCustodyAccountAddressΩ custodyAccountAddress = perpsPositionInfo.custodyAccountAddress();
ΩJupiterCustodyAccountInfoEncodedΩ encodedCustodyAccountInfo
= solanaBlockChain.getAccountInfo(custodyAccountAddress); ΩJupiterCustodyAccountInfoEncodedΩ encodedCustodyAccountInfo = solanaBlockChain.getAccountInfo(custodyAccountAddress);
if (encodedCustodyAccountInfo == null) { if (encodedCustodyAccountInfo == null) {
String errorMsg = "Jupiter Perps custody account does not exist: " + custodyAccountAddress; String errorMsg = "Jupiter Perps custody account does not exist: " + custodyAccountAddress;
@@ -149,23 +111,13 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
} }
JupiterCustodyAccountInfo custodyAccountInfo = custodyDecoder.decode(encodedCustodyAccountInfo); JupiterCustodyAccountInfo custodyAccountInfo = custodyDecoder.decode(encodedCustodyAccountInfo);
ΩSPLMintAddressΩ mintAddress = custodyAccountInfo.mintAddress(); ΩSPLMintAddressΩ mintAddress = custodyAccountInfo.mintAddress();
ΩJupiterPerpsCollateralCustodyAccountAddressΩ collateralCustodyAccountAddress = ΩJupiterPostionInterestΩ positionInterest = custodyAccountInfo.currentCumulativeInterestRate()
perpsPositionInfo.collateralCustodyAccountAddress();
ΩJupiterCustodyAccountInfoEncodedΩ encodedCollateralCustodyAccountInfo =
solanaBlockChain.getAccountInfo(collateralCustodyAccountAddress);
JupiterCustodyAccountInfo collateralCustodyAccountInfo =
custodyDecoder.decode(encodedCollateralCustodyAccountInfo);
ΩJupiterPostionInterestΩ positionInterest =
collateralCustodyAccountInfo.currentCumulativeInterestRate()
.subtract(perpsPositionInfo.cumulativeInterestSnapshot()); .subtract(perpsPositionInfo.cumulativeInterestSnapshot());
ΩUSDCAmountΩ borrowFeesDue = ΩUSDCAmountΩ borrowFeesDue = calculateBorrowFeesDue(
calculateBorrowFeesDue(
positionInterest, positionInterest,
perpsPositionInfo.positionSize() perpsPositionInfo.positionSize()
); );
@@ -177,7 +129,10 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
ΩUSDCPriceΩ marketPrice = ZERO; // TODO - Dummy ΩUSDCPriceΩ marketPrice = ZERO; // TODO - Dummy
ΩUSDCAmountΩ totalFees = ZERO; // TODO - Dummy ΩUSDCAmountΩ totalFees = ZERO; // TODO - Dummy
ΩUSDCAmountΩ closeFeePending = ZERO; // TODO - Dummy ΩUSDCAmountΩ closeFeePending = ZERO; // TODO - Dummy
ΩSolanaAmountΩ accountRent = new MoneyAmount(ZERO, WellKnownCurrencyTypes.SOLANA.getCurrencyType()); // TODO - Dummy ΩSolanaAmountΩ accountRent = new MoneyAmount( // TODO - Dummy
ZERO,
WellKnownCurrencyTypes.SOLANA.getCurrencyType()
);
JupiterPerpsPosition pos = new JupiterPerpsPosition( JupiterPerpsPosition pos = new JupiterPerpsPosition(
positionAccount, positionAccount,
@@ -197,10 +152,7 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
accountRent accountRent
); );
positions.add(pos); return pos;
}
return Set.copyOf(positions);
} }
private static ΩUSDCAmountΩ calculateBorrowFeesDue( private static ΩUSDCAmountΩ calculateBorrowFeesDue(