13: Decode Jupiter Perps position - entry price only
This commit is contained in:
+43
-2
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user