import {
createPhoenixClient,
type Authority,
type RegisterIxInstruction,
} from "@ellipsis-labs/rise";
import { createKeyPairSignerFromBytes } from "@solana/signers";
import {
AccountRole,
address,
appendTransactionMessageInstructions,
compileTransaction,
createSolanaRpc,
createTransactionMessage,
getBase64EncodedWireTransaction,
partiallySignTransaction,
pipe,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
type Blockhash,
} from "@solana/kit";
declare const traderAuthority: Authority;
declare const feePayerKeypairBytes: Uint8Array;
const toInstruction = (ix: RegisterIxInstruction) => ({
programAddress: address(ix.programId),
accounts: ix.keys.map((account) => ({
address: address(account.pubkey),
role: account.isSigner
? account.isWritable
? AccountRole.WRITABLE_SIGNER
: AccountRole.READONLY_SIGNER
: account.isWritable
? AccountRole.WRITABLE
: AccountRole.READONLY,
})),
data: Uint8Array.from(ix.data),
});
const client = createPhoenixClient({
apiUrl: "https://perp-api.phoenix.trade",
rpcUrl: "https://api.mainnet-beta.solana.com",
exchangeMetadata: { stream: false },
});
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
const feePayerSigner = await createKeyPairSignerFromBytes(feePayerKeypairBytes);
const txFeePayer = feePayerSigner.address as Authority;
const latestBlockhash = await rpc
.getLatestBlockhash({ commitment: "finalized" })
.send();
const built = await client.api.exchange().buildRegisterIxs({
traderAuthority,
txFeePayer,
maxPositions: 128,
});
const message = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayer(txFeePayer, tx),
(tx) =>
setTransactionMessageLifetimeUsingBlockhash(
{
blockhash: latestBlockhash.value.blockhash as Blockhash,
lastValidBlockHeight: latestBlockhash.value.lastValidBlockHeight,
},
tx,
),
(tx) => appendTransactionMessageInstructions(
built.instructions.map(toInstruction),
tx,
),
);
const signed = await partiallySignTransaction(
[feePayerSigner.keyPair],
compileTransaction(message),
);
const response = await client.api.exchange().sendRegisterIxs({
transaction: getBase64EncodedWireTransaction(signed),
traderAuthority,
txFeePayer,
maxPositions: 128,
traderPdaIndex: 0,
traderSubaccountIndex: 0,
});
console.log(response.signature, response.traderPda);