Compare commits
6 Commits
| Author | SHA256 | Date | |
|---|---|---|---|
| 41fdf09b7b | |||
| ae5172802e | |||
| 646fbe7947 | |||
| 932b2589e4 | |||
| 3e61f35c98 | |||
| 3a739bde57 |
+64
-4
@@ -7,6 +7,7 @@ import com.r35157.libs.jupiter.perps.JupiterPerpsPositionDirection;
|
|||||||
import com.r35157.libs.solana.SolanaAccountInfo;
|
import com.r35157.libs.solana.SolanaAccountInfo;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
@@ -38,16 +39,57 @@ class AnchorIdlJupiterPerpsPositionDecoder {
|
|||||||
.valueOf(rawEntryPrice)
|
.valueOf(rawEntryPrice)
|
||||||
.movePointLeft(6);
|
.movePointLeft(6);
|
||||||
|
|
||||||
|
long rawCollateralUsd = ByteBuffer
|
||||||
|
.wrap(data, COLLATERAL_USD_OFFSET, U64_LENGTH)
|
||||||
|
.order(ByteOrder.LITTLE_ENDIAN)
|
||||||
|
.getLong();
|
||||||
|
|
||||||
|
ΩUSDCAmountΩ collateralUsd = BigDecimal
|
||||||
|
.valueOf(rawCollateralUsd)
|
||||||
|
.movePointLeft(6);
|
||||||
|
|
||||||
|
long rawSizeUsd = ByteBuffer
|
||||||
|
.wrap(data, SIZE_USD_OFFSET, U64_LENGTH)
|
||||||
|
.order(ByteOrder.LITTLE_ENDIAN)
|
||||||
|
.getLong();
|
||||||
|
|
||||||
|
ΩUSDCAmountΩ sizeUsd = BigDecimal
|
||||||
|
.valueOf(rawSizeUsd)
|
||||||
|
.movePointLeft(6);
|
||||||
|
|
||||||
|
BigInteger cumulativeInterestSnapshot = readU128(
|
||||||
|
data,
|
||||||
|
CUMULATIVE_INTEREST_SNAPSHOT_OFFSET
|
||||||
|
);
|
||||||
|
|
||||||
JupiterPerpsPosition pos = new JupiterPerpsPosition(
|
JupiterPerpsPosition pos = new JupiterPerpsPosition(
|
||||||
positionAccount,
|
positionAccount,
|
||||||
entryPrice,
|
entryPrice,
|
||||||
direction,
|
direction,
|
||||||
tradedTokenMint
|
tradedTokenMint,
|
||||||
|
sizeUsd,
|
||||||
|
collateralUsd
|
||||||
);
|
);
|
||||||
|
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BigInteger decodeCumulativeInterestSnapshot(SolanaAccountInfo accountInfo) {
|
||||||
|
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
|
||||||
|
|
||||||
|
if (data.length < CUMULATIVE_INTEREST_SNAPSHOT_OFFSET + U128_LENGTH) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Jupiter Perps position account data is too short: " + data.length
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
BigInteger value = readU128(
|
||||||
|
data,
|
||||||
|
CUMULATIVE_INTEREST_SNAPSHOT_OFFSET
|
||||||
|
);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) {
|
ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) {
|
||||||
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
|
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
|
||||||
|
|
||||||
@@ -95,20 +137,38 @@ class AnchorIdlJupiterPerpsPositionDecoder {
|
|||||||
return base58.encode(publicKeyBytes);
|
return base58.encode(publicKeyBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static BigInteger readU128(
|
||||||
|
byte[] data,
|
||||||
|
int offset
|
||||||
|
) {
|
||||||
|
byte[] bytes = new byte[U128_LENGTH];
|
||||||
|
|
||||||
|
for (int i = 0; i < U128_LENGTH; i++) {
|
||||||
|
bytes[i] = data[offset + U128_LENGTH - 1 - i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BigInteger(1, bytes);
|
||||||
|
}
|
||||||
|
|
||||||
private static final int ANCHOR_DISCRIMINATOR_LENGTH = 8;
|
private static final int ANCHOR_DISCRIMINATOR_LENGTH = 8;
|
||||||
private static final int PUBLIC_KEY_LENGTH = 32;
|
private static final int PUBLIC_KEY_LENGTH = 32;
|
||||||
private static final int I64_LENGTH = 8;
|
|
||||||
private static final int SIDE_ENUM_LENGTH = 1;
|
private static final int SIDE_ENUM_LENGTH = 1;
|
||||||
|
private static final int I64_LENGTH = 8;
|
||||||
private static final int U64_LENGTH = 8;
|
private static final int U64_LENGTH = 8;
|
||||||
|
private static final int U128_LENGTH = 16;
|
||||||
|
|
||||||
private static final int OWNER_OFFSET = ANCHOR_DISCRIMINATOR_LENGTH;
|
private static final int OWNER_OFFSET = ANCHOR_DISCRIMINATOR_LENGTH;
|
||||||
private static final int POOL_OFFSET = OWNER_OFFSET + PUBLIC_KEY_LENGTH;
|
private static final int POOL_OFFSET = OWNER_OFFSET + PUBLIC_KEY_LENGTH;
|
||||||
private static final int CUSTODY_OFFSET = POOL_OFFSET + PUBLIC_KEY_LENGTH;
|
private static final int CUSTODY_OFFSET = POOL_OFFSET + PUBLIC_KEY_LENGTH;
|
||||||
private static final int COLLATERAL_CUSTODY_OFFSET = CUSTODY_OFFSET + PUBLIC_KEY_LENGTH; // custody
|
private static final int COLLATERAL_CUSTODY_OFFSET = CUSTODY_OFFSET + PUBLIC_KEY_LENGTH;
|
||||||
private static final int OPEN_TIME_OFFSET = COLLATERAL_CUSTODY_OFFSET + PUBLIC_KEY_LENGTH;
|
private static final int OPEN_TIME_OFFSET = COLLATERAL_CUSTODY_OFFSET + PUBLIC_KEY_LENGTH;
|
||||||
private static final int UPDATE_TIME_OFFSET = OPEN_TIME_OFFSET + I64_LENGTH; // openTime
|
private static final int UPDATE_TIME_OFFSET = OPEN_TIME_OFFSET + I64_LENGTH;
|
||||||
private static final int SIDE_OFFSET = UPDATE_TIME_OFFSET + I64_LENGTH;
|
private static final int SIDE_OFFSET = UPDATE_TIME_OFFSET + I64_LENGTH;
|
||||||
private static final int PRICE_OFFSET = SIDE_OFFSET + SIDE_ENUM_LENGTH;
|
private static final int PRICE_OFFSET = SIDE_OFFSET + SIDE_ENUM_LENGTH;
|
||||||
|
private static final int SIZE_USD_OFFSET = PRICE_OFFSET + U64_LENGTH;
|
||||||
|
private static final int COLLATERAL_USD_OFFSET = SIZE_USD_OFFSET + U64_LENGTH;
|
||||||
|
private static final int REALISED_PNL_USD_OFFSET = COLLATERAL_USD_OFFSET + U64_LENGTH;
|
||||||
|
private static final int CUMULATIVE_INTEREST_SNAPSHOT_OFFSET = REALISED_PNL_USD_OFFSET + I64_LENGTH;
|
||||||
|
|
||||||
private static final Base58Codec base58 = new Base58CodecImpl();
|
private static final Base58Codec base58 = new Base58CodecImpl();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user