6 Commits

4 changed files with 31 additions and 53 deletions
@@ -1,45 +0,0 @@
package com.r35157.libs.jupiter.perps;
import java.io.IOException;
import java.util.Set;
/**
* Service for reading Jupiter Perps positions.
*
* <p>This service is read-only. It does not open, close, modify, or sign transactions
* for Jupiter Perps positions.</p>
*
* <p>The first supported operation is reading a known Jupiter Perps position account
* and returning its decoded position data.</p>
*/
public interface JupiterPerpsPositionService {
/**
* Reads a Jupiter Perps position from a known position account.
*
* <p>The supplied account must be the Solana account that stores the Jupiter Perps
* position state. It is not the wallet address, token account, custody account, pool
* account, or position request account.</p>
*
* @param positionAccount the Solana account address of the Jupiter Perps position
* @return the decoded Jupiter Perps position
* @throws IOException if the position account could not be fetched or decoded
* @throws InterruptedException if the calling thread is interrupted while fetching
* the position account
*/
JupiterPerpsPosition getPosition(ΩJupiterPerpsPositionAccountΩ positionAccount)
throws IOException, InterruptedException;
/**
* Finds open Jupiter Perps positions owned by a wallet.
*
* <p>This method returns decoded Jupiter Perps position objects. It does not return
* raw Solana accounts or account ids.</p>
*
* @param owner the wallet address that owns the Jupiter Perps positions
* @return the open Jupiter Perps positions owned by the wallet
* @throws IOException if the position accounts could not be fetched or decoded
* @throws InterruptedException if the calling thread is interrupted while fetching positions
*/
Set<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
throws IOException, InterruptedException;
}
@@ -1,6 +1,7 @@
package com.r35157.libs.jupiter.perps.impl.anchoridl; package com.r35157.libs.jupiter.perps.impl.anchoridl;
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition; import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
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;
@@ -22,6 +23,9 @@ class AnchorIdlJupiterPerpsPositionDecoder {
); );
} }
JupiterPerpsPositionDirection direction =
decodeDirection(data[SIDE_OFFSET]);
long rawEntryPrice = ByteBuffer long rawEntryPrice = ByteBuffer
.wrap(data, PRICE_OFFSET, U64_LENGTH) .wrap(data, PRICE_OFFSET, U64_LENGTH)
.order(ByteOrder.LITTLE_ENDIAN) .order(ByteOrder.LITTLE_ENDIAN)
@@ -33,25 +37,44 @@ class AnchorIdlJupiterPerpsPositionDecoder {
JupiterPerpsPosition pos = new JupiterPerpsPosition( JupiterPerpsPosition pos = new JupiterPerpsPosition(
positionAccount, positionAccount,
entryPrice entryPrice,
direction
); );
return pos; return pos;
} }
private JupiterPerpsPositionDirection decodeDirection(
byte rawSide
) {
// Jupiter Perps position side values are encoded as 1 = LONG, 2 = SHORT.
JupiterPerpsPositionDirection direction = switch (rawSide) {
case 1 -> JupiterPerpsPositionDirection.LONG;
case 2 -> JupiterPerpsPositionDirection.SHORT;
default -> throw new IllegalArgumentException(
"Unknown Jupiter Perps position side: " + rawSide
);
};
return direction;
}
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 I64_LENGTH = 8;
private static final int U64_LENGTH = 8; private static final int U64_LENGTH = 8;
private static final int SIDE_ENUM_LENGTH = 1; private static final int SIDE_ENUM_LENGTH = 1;
private static final int PRICE_OFFSET = private static final int SIDE_OFFSET =
ANCHOR_DISCRIMINATOR_LENGTH ANCHOR_DISCRIMINATOR_LENGTH
+ PUBLIC_KEY_LENGTH // owner + PUBLIC_KEY_LENGTH // owner
+ PUBLIC_KEY_LENGTH // pool + PUBLIC_KEY_LENGTH // pool
+ PUBLIC_KEY_LENGTH // custody + PUBLIC_KEY_LENGTH // custody
+ PUBLIC_KEY_LENGTH // collateralCustody + PUBLIC_KEY_LENGTH // collateralCustody
+ I64_LENGTH // openTime + I64_LENGTH // openTime
+ I64_LENGTH // updateTime + I64_LENGTH; // updateTime
private static final int PRICE_OFFSET =
SIDE_OFFSET
+ SIDE_ENUM_LENGTH; // side + SIDE_ENUM_LENGTH; // side
} }
@@ -1,7 +1,7 @@
package com.r35157.libs.jupiter.perps.impl.anchoridl; package com.r35157.libs.jupiter.perps.impl.anchoridl;
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition; import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionService; 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;
@@ -10,9 +10,9 @@ import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class AnchorIdlJupiterPerpsPositionServiceImpl implements JupiterPerpsPositionService { public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
public AnchorIdlJupiterPerpsPositionServiceImpl( public AnchorIdlJupiterPerpsServiceImpl(
SolanaBlockChain solanaBlockChain SolanaBlockChain solanaBlockChain
) { ) {
this.solanaBlockChain = solanaBlockChain; this.solanaBlockChain = solanaBlockChain;
@@ -1,8 +1,8 @@
package com.r35157.nenjim.hubd.impl.ref; package com.r35157.nenjim.hubd.impl.ref;
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition; import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionService; import com.r35157.libs.jupiter.perps.JupiterPerpsService;
import com.r35157.libs.jupiter.perps.impl.anchoridl.AnchorIdlJupiterPerpsPositionServiceImpl; import com.r35157.libs.jupiter.perps.impl.anchoridl.AnchorIdlJupiterPerpsServiceImpl;
import com.r35157.libs.solana.SolanaBlockChain; import com.r35157.libs.solana.SolanaBlockChain;
import com.r35157.libs.solana.impl.ref.SolanaBlockChainImpl; import com.r35157.libs.solana.impl.ref.SolanaBlockChainImpl;
import com.r35157.nenjim.hubd.ctx.Context; import com.r35157.nenjim.hubd.ctx.Context;