From 9376208591a7afa269173ba585a066e93584ef697f35c29dd9f17e53e3edad8f Mon Sep 17 00:00:00 2001 From: Minimons Date: Thu, 25 Jun 2026 10:50:43 +0200 Subject: [PATCH] 13: Decode Jupiter Perps position - entry price only --- ...AnchorIdlJupiterPerpsPositionDecoder.tjava | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) 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 7f501f4..3748edb 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 @@ -3,14 +3,55 @@ package com.r35157.libs.jupiter.perps.impl.anchoridl; import com.r35157.libs.jupiter.perps.JupiterPerpsPosition; import com.r35157.libs.solana.SolanaAccountInfo; +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Base64; + class AnchorIdlJupiterPerpsPositionDecoder { JupiterPerpsPosition decode( ΩJupiterPerpsPositionAccountΩ positionAccount, SolanaAccountInfo accountInfo ) { - throw new UnsupportedOperationException( - "Jupiter Perps position decoding is not implemented yet." + byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64()); + + if (data.length < PRICE_OFFSET + U64_LENGTH) { + throw new IllegalArgumentException( + "Jupiter Perps position account data is too short: " + data.length + ); + } + + long rawEntryPrice = ByteBuffer + .wrap(data, PRICE_OFFSET, U64_LENGTH) + .order(ByteOrder.LITTLE_ENDIAN) + .getLong(); + + ΩUSDCPriceΩ entryPrice = BigDecimal + .valueOf(rawEntryPrice) + .movePointLeft(6); + + JupiterPerpsPosition pos = new JupiterPerpsPosition( + positionAccount, + entryPrice ); + + return pos; } + + private static final int ANCHOR_DISCRIMINATOR_LENGTH = 8; + private static final int PUBLIC_KEY_LENGTH = 32; + private static final int I64_LENGTH = 8; + private static final int U64_LENGTH = 8; + private static final int SIDE_ENUM_LENGTH = 1; + + private static final int PRICE_OFFSET = + ANCHOR_DISCRIMINATOR_LENGTH + + PUBLIC_KEY_LENGTH // owner + + PUBLIC_KEY_LENGTH // pool + + PUBLIC_KEY_LENGTH // custody + + PUBLIC_KEY_LENGTH // collateralCustody + + I64_LENGTH // openTime + + I64_LENGTH // updateTime + + SIDE_ENUM_LENGTH; // side } \ No newline at end of file