X: NFC - Remove code duplication
This commit is contained in:
+30
-78
@@ -46,67 +46,14 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
||||
|
||||
JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo);
|
||||
|
||||
JupiterPerpsPosition pos;
|
||||
if(perpsPositionInfo == 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);
|
||||
if (perpsPositionInfo == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
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(
|
||||
JupiterPerpsPosition pos = buildPosition(
|
||||
positionAccount,
|
||||
mintAddress,
|
||||
perpsPositionInfo.direction(),
|
||||
value,
|
||||
perpsPositionInfo.positionSize(),
|
||||
pnl,
|
||||
pnlPercent,
|
||||
leverage,
|
||||
perpsPositionInfo.entryPrice(),
|
||||
marketPrice,
|
||||
perpsPositionInfo.collateral(),
|
||||
totalFees,
|
||||
borrowFeesDue,
|
||||
closeFeePending,
|
||||
accountRent
|
||||
perpsPositionInfo
|
||||
);
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
@@ -135,13 +82,28 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
||||
}
|
||||
|
||||
JupiterPerpsPositionInfo perpsPositionInfo = positionDecoder.decode(accountInfo);
|
||||
if(perpsPositionInfo == null || perpsPositionInfo.positionSize().compareTo(ZERO) == 0) {
|
||||
if (perpsPositionInfo == null || perpsPositionInfo.positionSize().compareTo(ZERO) == 0) {
|
||||
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();
|
||||
ΩJupiterCustodyAccountInfoEncodedΩ encodedCustodyAccountInfo
|
||||
= solanaBlockChain.getAccountInfo(custodyAccountAddress);
|
||||
|
||||
ΩJupiterCustodyAccountInfoEncodedΩ encodedCustodyAccountInfo = solanaBlockChain.getAccountInfo(custodyAccountAddress);
|
||||
|
||||
if (encodedCustodyAccountInfo == null) {
|
||||
String errorMsg = "Jupiter Perps custody account does not exist: " + custodyAccountAddress;
|
||||
@@ -149,23 +111,13 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
||||
}
|
||||
|
||||
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()
|
||||
ΩJupiterPostionInterestΩ positionInterest = custodyAccountInfo.currentCumulativeInterestRate()
|
||||
.subtract(perpsPositionInfo.cumulativeInterestSnapshot());
|
||||
|
||||
ΩUSDCAmountΩ borrowFeesDue =
|
||||
calculateBorrowFeesDue(
|
||||
ΩUSDCAmountΩ borrowFeesDue = calculateBorrowFeesDue(
|
||||
positionInterest,
|
||||
perpsPositionInfo.positionSize()
|
||||
);
|
||||
@@ -177,7 +129,10 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
||||
Ω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
|
||||
ΩSolanaAmountΩ accountRent = new MoneyAmount( // TODO - Dummy
|
||||
ZERO,
|
||||
WellKnownCurrencyTypes.SOLANA.getCurrencyType()
|
||||
);
|
||||
|
||||
JupiterPerpsPosition pos = new JupiterPerpsPosition(
|
||||
positionAccount,
|
||||
@@ -197,10 +152,7 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
||||
accountRent
|
||||
);
|
||||
|
||||
positions.add(pos);
|
||||
}
|
||||
|
||||
return Set.copyOf(positions);
|
||||
return pos;
|
||||
}
|
||||
|
||||
private static ΩUSDCAmountΩ calculateBorrowFeesDue(
|
||||
|
||||
Reference in New Issue
Block a user