diff --git a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsCustodyDecoder.tjava b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsCustodyDecoder.tjava index 7829bf3..78b86c4 100644 --- a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsCustodyDecoder.tjava +++ b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsCustodyDecoder.tjava @@ -7,9 +7,9 @@ import com.r35157.libs.solana.SolanaAccountInfo; import java.math.BigInteger; import java.util.Base64; -class AnchorIdlJupiterPerpsCustodyDecoder { +public class AnchorIdlJupiterPerpsCustodyDecoder { - ΩSPLMintAddressΩ decodeMint( + public ΩSPLMintAddressΩ decodeMint( SolanaAccountInfo custodyAccountInfo ) { byte[] data = Base64.getDecoder().decode(custodyAccountInfo.dataBase64()); @@ -23,7 +23,7 @@ class AnchorIdlJupiterPerpsCustodyDecoder { return readPublicKey(data, MINT_OFFSET); } - BigInteger decodeCurrentCumulativeInterestRate( + public BigInteger decodeCurrentCumulativeInterestRate( SolanaAccountInfo custodyAccountInfo ) { byte[] data = Base64.getDecoder().decode(custodyAccountInfo.dataBase64()); diff --git a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava index a6bdba7..288e5e2 100644 --- a/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava +++ b/src/main/tjava/com/r35157/libs/jupiter/perps/impl/anchoridl/AnchorIdlJupiterPerpsPositionDecoder.tjava @@ -12,7 +12,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Base64; -class AnchorIdlJupiterPerpsPositionDecoder { +public class AnchorIdlJupiterPerpsPositionDecoder { JupiterPerpsPosition decode( ΩJupiterPerpsPositionAccountΩ positionAccount, @@ -57,10 +57,7 @@ class AnchorIdlJupiterPerpsPositionDecoder { .valueOf(rawSizeUsd) .movePointLeft(6); - BigInteger cumulativeInterestSnapshot = readU128( - data, - CUMULATIVE_INTEREST_SNAPSHOT_OFFSET - ); + BigInteger cumulativeInterestSnapshot = decodeCumulativeInterestSnapshot(accountInfo); JupiterPerpsPosition pos = new JupiterPerpsPosition( positionAccount, @@ -74,7 +71,7 @@ class AnchorIdlJupiterPerpsPositionDecoder { return pos; } - BigInteger decodeCumulativeInterestSnapshot(SolanaAccountInfo accountInfo) { + public BigInteger decodeCumulativeInterestSnapshot(SolanaAccountInfo accountInfo) { byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64()); if (data.length < CUMULATIVE_INTEREST_SNAPSHOT_OFFSET + U128_LENGTH) { @@ -90,13 +87,7 @@ class AnchorIdlJupiterPerpsPositionDecoder { return value; } - BigInteger decodeCurrentCumulativeInterestRate( - SolanaAccountInfo collateralCustodyAccountInfo - ) { - return null; - } - - ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) { + public ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) { byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64()); if (data.length < CUSTODY_OFFSET + PUBLIC_KEY_LENGTH) { @@ -111,6 +102,23 @@ class AnchorIdlJupiterPerpsPositionDecoder { ); } + public ΩSolanaAddressΩ decodeCollateralCustodyAccount( + SolanaAccountInfo accountInfo + ) { + byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64()); + + if (data.length < COLLATERAL_CUSTODY_OFFSET + PUBLIC_KEY_LENGTH) { + throw new IllegalArgumentException( + "Jupiter Perps position account data is too short: " + data.length + ); + } + + return readPublicKey( + data, + COLLATERAL_CUSTODY_OFFSET + ); + } + private JupiterPerpsPositionDirection decodeDirection( byte rawSide ) { diff --git a/src/main/tjava/com/r35157/nenjim/hubd/impl/ref/Main.tjava b/src/main/tjava/com/r35157/nenjim/hubd/impl/ref/Main.tjava index ed7957e..61be688 100644 --- a/src/main/tjava/com/r35157/nenjim/hubd/impl/ref/Main.tjava +++ b/src/main/tjava/com/r35157/nenjim/hubd/impl/ref/Main.tjava @@ -2,6 +2,7 @@ package com.r35157.nenjim.hubd.impl.ref; import com.r35157.libs.jupiter.perps.JupiterPerpsPosition; import com.r35157.libs.jupiter.perps.JupiterPerpsService; +import com.r35157.libs.jupiter.perps.impl.anchoridl.AnchorIdlJupiterPerpsPositionDecoder; import com.r35157.libs.jupiter.perps.impl.anchoridl.AnchorIdlJupiterPerpsServiceImpl; import com.r35157.libs.solana.SolanaBlockChain; import com.r35157.libs.solana.impl.ref.SolanaBlockChainImpl; @@ -11,7 +12,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; - +import com.r35157.libs.jupiter.perps.impl.anchoridl.AnchorIdlJupiterPerpsCustodyDecoder; +import com.r35157.libs.solana.SolanaAccountInfo; +import java.math.BigInteger; import com.r35157.nenjim.hubd.ctx.ContextManager; import com.r35157.nenjim.hubd.journal.JournalManager; import com.r35157.nenjim.hubd.impl.ref.JournalManagerImpl; @@ -23,16 +26,34 @@ public class Main { // TODO: Consider if we really need a Main class or we just need to move the main method to NenjimHubImpl? static void main(String[] args) throws Exception { - NenjimHubImpl nenjimHub = new NenjimHubImpl(); + //NenjimHubImpl nenjimHub = new NenjimHubImpl(); - /* SolanaBlockChain sbc = new SolanaBlockChainImpl(); JupiterPerpsService jupiter = new AnchorIdlJupiterPerpsServiceImpl(sbc); ΩSolanaWalletIdΩ walletId = "vj98roDZ7744EBfxyuDFkKpEGCsKQLr7K8UFRumJNHf"; + Set positions = jupiter.getOpenPositions(walletId); + + AnchorIdlJupiterPerpsPositionDecoder positionDecoder = new AnchorIdlJupiterPerpsPositionDecoder(); + AnchorIdlJupiterPerpsCustodyDecoder custodyDecoder = new AnchorIdlJupiterPerpsCustodyDecoder(); + + for (JupiterPerpsPosition position : positions) { + SolanaAccountInfo positionAccountInfo = sbc.getAccountInfo(position.positionAccount()); + ΩSolanaAddressΩ collateralCustodyAccount = positionDecoder.decodeCollateralCustodyAccount(positionAccountInfo); + SolanaAccountInfo collateralCustodyAccountInfo = sbc.getAccountInfo(collateralCustodyAccount); + BigInteger currentCumulativeInterestRate =custodyDecoder.decodeCurrentCumulativeInterestRate(collateralCustodyAccountInfo); + BigInteger cumulativeInterestSnapshot = positionDecoder.decodeCumulativeInterestSnapshot(positionAccountInfo); + BigInteger difference = currentCumulativeInterestRate.subtract(cumulativeInterestSnapshot); + + System.out.println("positionAccount: " + position.positionAccount()); + System.out.println("currentCumulativeInterestRate: " + currentCumulativeInterestRate); + System.out.println("cumulativeInterestSnapshot: " + cumulativeInterestSnapshot); + System.out.println("difference: " + difference); + System.out.println(); + } + int a=0; - */ - nenjimHub.awaitShutdown(); + //nenjimHub.awaitShutdown(); /* try { log.info("Auto-starting 2 Nenjim application(s)...");