X: NFC - Remove code duplication

This commit is contained in:
2026-08-05 10:12:38 +02:00
parent b2278b12d5
commit eda12a12fd
@@ -46,68 +46,15 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo); JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo);
JupiterPerpsPosition pos; if (perpsPositionInfo == null) {
if(perpsPositionInfo == null) { return null;
pos = 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);
Ω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,
mintAddress,
perpsPositionInfo.direction(),
value,
perpsPositionInfo.positionSize(),
pnl,
pnlPercent,
leverage,
perpsPositionInfo.entryPrice(),
marketPrice,
perpsPositionInfo.collateral(),
totalFees,
borrowFeesDue,
closeFeePending,
accountRent
);
} }
JupiterPerpsPosition pos = buildPosition(
positionAccount,
perpsPositionInfo
);
return pos; return pos;
} }
@@ -135,66 +82,13 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
} }
JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo); JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo);
if(perpsPositionInfo == null || perpsPositionInfo.positionSize().compareTo(ZERO) == 0) { if (perpsPositionInfo == null || perpsPositionInfo.positionSize().compareTo(ZERO) == 0) {
continue; continue;
} }
ΩJupiterPerpsCustodyAccountAddressΩ custodyAccountAddress = perpsPositionInfo.custodyAccountAddress(); JupiterPerpsPosition pos = buildPosition(
Ω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);
Ω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
JupiterPerpsPosition pos = new JupiterPerpsPosition(
positionAccount, positionAccount,
mintAddress, perpsPositionInfo
perpsPositionInfo.direction(),
value,
perpsPositionInfo.positionSize(),
pnl,
pnlPercent,
leverage,
perpsPositionInfo.entryPrice(),
marketPrice,
perpsPositionInfo.collateral(),
totalFees,
borrowFeesDue,
closeFeePending,
accountRent
); );
positions.add(pos); positions.add(pos);
@@ -203,6 +97,64 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
return Set.copyOf(positions); return Set.copyOf(positions);
} }
private JupiterPerpsPosition buildPosition(
ΩJupiterPerpsPositionAccountΩ positionAccount,
JupiterPerpsPositionInfo perpsPositionInfo
) throws IOException, InterruptedException {
ΩJupiterPerpsCustodyAccountAddressΩ custodyAccountAddress = perpsPositionInfo.custodyAccountAddress();
Ω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);
ΩSPLMintAddressΩ mintAddress = custodyAccountInfo.mintAddress();
ΩJupiterPostionInterestΩ positionInterest = custodyAccountInfo.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( // TODO - Dummy
ZERO,
WellKnownCurrencyTypes.SOLANA.getCurrencyType()
);
JupiterPerpsPosition pos = new JupiterPerpsPosition(
positionAccount,
mintAddress,
perpsPositionInfo.direction(),
value,
perpsPositionInfo.positionSize(),
pnl,
pnlPercent,
leverage,
perpsPositionInfo.entryPrice(),
marketPrice,
perpsPositionInfo.collateral(),
totalFees,
borrowFeesDue,
closeFeePending,
accountRent
);
return pos;
}
private static ΩUSDCAmountΩ calculateBorrowFeesDue( private static ΩUSDCAmountΩ calculateBorrowFeesDue(
ΩJupiterPostionInterestΩ positionInterest, ΩJupiterPostionInterestΩ positionInterest,
ΩUSDCAmountΩ size ΩUSDCAmountΩ size