|
| 1 | +import { getCreateAccountInstruction } from '@solana-program/system'; |
| 2 | +import { |
| 3 | + BaseTransactionMessage, |
| 4 | + Commitment, |
| 5 | + Instruction, |
| 6 | + Rpc, |
| 7 | + RpcSubscriptions, |
| 8 | + SolanaRpcApi, |
| 9 | + SolanaRpcSubscriptionsApi, |
| 10 | + TransactionMessageWithBlockhashLifetime, |
| 11 | + TransactionMessageWithFeePayer, |
| 12 | + TransactionSigner, |
| 13 | + airdropFactory, |
| 14 | + appendTransactionMessageInstructions, |
| 15 | + assertIsSendableTransaction, |
| 16 | + assertIsTransactionWithBlockhashLifetime, |
| 17 | + createSolanaRpc, |
| 18 | + createSolanaRpcSubscriptions, |
| 19 | + createTransactionMessage, |
| 20 | + generateKeyPairSigner, |
| 21 | + getSignatureFromTransaction, |
| 22 | + lamports, |
| 23 | + pipe, |
| 24 | + sendAndConfirmTransactionFactory, |
| 25 | + setTransactionMessageFeePayerSigner, |
| 26 | + setTransactionMessageLifetimeUsingBlockhash, |
| 27 | + signTransactionMessageWithSigners, |
| 28 | +} from '@solana/kit'; |
| 29 | +import { getInitializeBufferInstruction, LOADER_V3_PROGRAM_ADDRESS } from './src'; |
| 30 | + |
| 31 | +export const BUFFER_HEADER_SIZE = 37n; |
| 32 | + |
| 33 | +type Client = { |
| 34 | + rpc: Rpc<SolanaRpcApi>; |
| 35 | + rpcSubscriptions: RpcSubscriptions<SolanaRpcSubscriptionsApi>; |
| 36 | +}; |
| 37 | + |
| 38 | +export const createDefaultSolanaClient = (): Client => { |
| 39 | + const rpc = createSolanaRpc('http://127.0.0.1:8899'); |
| 40 | + const rpcSubscriptions = createSolanaRpcSubscriptions('ws://127.0.0.1:8900'); |
| 41 | + return { rpc, rpcSubscriptions }; |
| 42 | +}; |
| 43 | + |
| 44 | +export const generateKeyPairSignerWithSol = async (client: Client, putativeLamports: bigint = 1_000_000_000n) => { |
| 45 | + const signer = await generateKeyPairSigner(); |
| 46 | + await airdropFactory(client)({ |
| 47 | + recipientAddress: signer.address, |
| 48 | + lamports: lamports(putativeLamports), |
| 49 | + commitment: 'confirmed', |
| 50 | + }); |
| 51 | + return signer; |
| 52 | +}; |
| 53 | + |
| 54 | +export const createDefaultTransactionMessage = async ( |
| 55 | + client: Client, |
| 56 | + feePayer: TransactionSigner, |
| 57 | + instructions?: Instruction[], |
| 58 | +) => { |
| 59 | + const { value: latestBlockhash } = await client.rpc.getLatestBlockhash().send(); |
| 60 | + return pipe( |
| 61 | + createTransactionMessage({ version: 0 }), |
| 62 | + tx => setTransactionMessageFeePayerSigner(feePayer, tx), |
| 63 | + tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), |
| 64 | + tx => (instructions ? appendTransactionMessageInstructions(instructions, tx) : tx), |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +export const signAndSendTransaction = async ( |
| 69 | + client: Client, |
| 70 | + transactionMessage: BaseTransactionMessage & |
| 71 | + TransactionMessageWithFeePayer & |
| 72 | + TransactionMessageWithBlockhashLifetime, |
| 73 | + commitment: Commitment = 'confirmed', |
| 74 | +) => { |
| 75 | + const signedTransaction = await signTransactionMessageWithSigners(transactionMessage); |
| 76 | + const signature = getSignatureFromTransaction(signedTransaction); |
| 77 | + assertIsSendableTransaction(signedTransaction); |
| 78 | + assertIsTransactionWithBlockhashLifetime(signedTransaction); |
| 79 | + await sendAndConfirmTransactionFactory(client)(signedTransaction, { |
| 80 | + commitment, |
| 81 | + }); |
| 82 | + return signature; |
| 83 | +}; |
| 84 | + |
| 85 | +export async function getCreateBufferInstructions( |
| 86 | + client: Client, |
| 87 | + input: { |
| 88 | + payer: TransactionSigner; |
| 89 | + buffer: TransactionSigner; |
| 90 | + dataLength: number | bigint; |
| 91 | + }, |
| 92 | +) { |
| 93 | + const bufferSize = BUFFER_HEADER_SIZE + BigInt(input.dataLength); |
| 94 | + const bufferLamports = await client.rpc.getMinimumBalanceForRentExemption(bufferSize).send(); |
| 95 | + return [ |
| 96 | + getCreateAccountInstruction({ |
| 97 | + payer: input.payer, |
| 98 | + newAccount: input.buffer, |
| 99 | + lamports: bufferLamports, |
| 100 | + space: bufferSize, |
| 101 | + programAddress: LOADER_V3_PROGRAM_ADDRESS, |
| 102 | + }), |
| 103 | + getInitializeBufferInstruction({ |
| 104 | + sourceAccount: input.buffer.address, |
| 105 | + bufferAuthority: input.payer.address, |
| 106 | + }), |
| 107 | + ]; |
| 108 | +} |
0 commit comments