X: Clean-up decoding
This commit is contained in:
+6
-35
@@ -2,26 +2,17 @@ package com.r35157.libs.jupiter.perps.impl.anchoridl;
|
|||||||
|
|
||||||
import com.r35157.libs.codec.Base58Codec;
|
import com.r35157.libs.codec.Base58Codec;
|
||||||
import com.r35157.libs.codec.impl.ref.Base58CodecImpl;
|
import com.r35157.libs.codec.impl.ref.Base58CodecImpl;
|
||||||
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
|
|
||||||
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionDirection;
|
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionDirection;
|
||||||
import com.r35157.libs.solana.SolanaAccountInfo;
|
import com.r35157.libs.solana.SolanaAccountInfo;
|
||||||
import com.r35157.libs.valuetypes.basic.MoneyAmount;
|
|
||||||
import com.r35157.libs.valuetypes.basic.WellKnownCurrencyTypes;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
import static java.math.BigDecimal.ZERO;
|
|
||||||
|
|
||||||
class AnchorIdlJupiterPerpsPositionDecoder {
|
class AnchorIdlJupiterPerpsPositionDecoder {
|
||||||
|
|
||||||
JupiterPerpsPosition decode(
|
JupiterPerpsPositionInfo decode(SolanaAccountInfo accountInfo) {
|
||||||
ΩJupiterPerpsPositionAccountΩ positionAccount,
|
|
||||||
SolanaAccountInfo accountInfo,
|
|
||||||
ΩSPLMintAddressΩ tradedTokenMint
|
|
||||||
) {
|
|
||||||
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
|
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
|
||||||
|
|
||||||
if (data.length < PRICE_OFFSET + U64_LENGTH) {
|
if (data.length < PRICE_OFFSET + U64_LENGTH) {
|
||||||
@@ -60,35 +51,15 @@ class AnchorIdlJupiterPerpsPositionDecoder {
|
|||||||
.valueOf(rawSizeUsd)
|
.valueOf(rawSizeUsd)
|
||||||
.movePointLeft(6);
|
.movePointLeft(6);
|
||||||
|
|
||||||
Ω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(
|
JupiterPerpsPositionInfo posInfo = new JupiterPerpsPositionInfo(
|
||||||
positionAccount,
|
|
||||||
tradedTokenMint,
|
|
||||||
direction,
|
|
||||||
value,
|
|
||||||
sizeUsd,
|
|
||||||
pnl,
|
|
||||||
pnlPercent,
|
|
||||||
leverage,
|
|
||||||
entryPrice,
|
entryPrice,
|
||||||
marketPrice,
|
direction,
|
||||||
collateralUsd,
|
sizeUsd,
|
||||||
totalFees,
|
collateralUsd
|
||||||
borrowFeesDue,
|
|
||||||
closeFeePending,
|
|
||||||
accountRent
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return pos;
|
return posInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) {
|
ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) {
|
||||||
|
|||||||
+68
-5
@@ -5,11 +5,16 @@ import com.r35157.libs.jupiter.perps.JupiterPerpsService;
|
|||||||
import com.r35157.libs.solana.SolanaAccountInfo;
|
import com.r35157.libs.solana.SolanaAccountInfo;
|
||||||
import com.r35157.libs.solana.SolanaBlockChain;
|
import com.r35157.libs.solana.SolanaBlockChain;
|
||||||
import com.r35157.libs.solana.SolanaProgramAccountMemcmpFilter;
|
import com.r35157.libs.solana.SolanaProgramAccountMemcmpFilter;
|
||||||
|
import com.r35157.libs.solana.valuetypes.WellKnownCurrencyTypes;
|
||||||
|
import com.r35157.libs.valuetypes.basic.MoneyAmount;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static java.math.BigDecimal.ZERO;
|
||||||
|
|
||||||
public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
||||||
|
|
||||||
public AnchorIdlJupiterPerpsServiceImpl(
|
public AnchorIdlJupiterPerpsServiceImpl(
|
||||||
@@ -36,7 +41,36 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo);
|
ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo);
|
||||||
JupiterPerpsPosition pos = positionDecoder.decode(positionAccount, accountInfo, tradedTokenMint);
|
|
||||||
|
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 = new JupiterPerpsPosition(
|
||||||
|
positionAccount,
|
||||||
|
tradedTokenMint,
|
||||||
|
info.direction(),
|
||||||
|
value,
|
||||||
|
info.sizeUsd(),
|
||||||
|
pnl,
|
||||||
|
pnlPercent,
|
||||||
|
leverage,
|
||||||
|
info.entryPrice(),
|
||||||
|
marketPrice,
|
||||||
|
info.collateralUsd(),
|
||||||
|
totalFees,
|
||||||
|
borrowFeesDue,
|
||||||
|
closeFeePending,
|
||||||
|
accountRent
|
||||||
|
);
|
||||||
|
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
@@ -55,19 +89,48 @@ public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
|
|||||||
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
Set<JupiterPerpsPosition> positions = new HashSet<>();
|
||||||
|
|
||||||
for (SolanaAccountInfo accountInfo : accountInfos) {
|
for (SolanaAccountInfo accountInfo : accountInfos) {
|
||||||
ΩSolanaAddressΩ address = accountInfo.address();
|
|
||||||
ΩSolanaProgramIdΩ programId = accountInfo.owner();
|
ΩSolanaProgramIdΩ programId = accountInfo.owner();
|
||||||
|
ΩJupiterPerpsPositionAccountΩ positionAccount = accountInfo.address();
|
||||||
|
|
||||||
if (!JUPITER_PERPS_PROGRAM_ID.equals(programId)) {
|
if (!JUPITER_PERPS_PROGRAM_ID.equals(programId)) {
|
||||||
String errorMsg = "Account '" + address + "' is not owned by Jupiter Perps program '" +
|
String errorMsg = "Account '" + positionAccount + "' is not owned by Jupiter Perps program '" +
|
||||||
programId + "'";
|
programId + "'";
|
||||||
throw new IllegalArgumentException(errorMsg);
|
throw new IllegalArgumentException(errorMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo);
|
ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo);
|
||||||
|
|
||||||
JupiterPerpsPosition position = positionDecoder.decode(address, accountInfo, tradedTokenMint);
|
JupiterPerpsPositionInfo info = positionDecoder.decode(accountInfo);
|
||||||
positions.add(position);
|
|
||||||
|
Ω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
|
||||||
|
);
|
||||||
|
|
||||||
|
positions.add(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Set.copyOf(positions);
|
return Set.copyOf(positions);
|
||||||
|
|||||||
Reference in New Issue
Block a user