|
| 1 | +import { |
| 2 | + Address, |
| 3 | + AddressesByLookupTableAddress, |
| 4 | + appendTransactionMessageInstruction, |
| 5 | + appendTransactionMessageInstructions, |
| 6 | + Commitment, |
| 7 | + CompilableTransactionMessage, |
| 8 | + compressTransactionMessageUsingAddressLookupTables as compressTxWithAlt, |
| 9 | + createTransactionMessage, |
| 10 | + getSignatureFromTransaction, |
| 11 | + IInstruction, |
| 12 | + KeyPairSigner, |
| 13 | + pipe, |
| 14 | + sendAndConfirmTransactionFactory, |
| 15 | + setTransactionMessageFeePayerSigner, |
| 16 | + setTransactionMessageLifetimeUsingBlockhash, |
| 17 | + signTransactionMessageWithSigners, |
| 18 | + TransactionMessageWithBlockhashLifetime, |
| 19 | + TransactionSigner, |
| 20 | +} from "@solana/kit"; |
| 21 | + |
| 22 | +import { |
| 23 | + fetchAddressLookupTable, |
| 24 | + findAddressLookupTablePda, |
| 25 | + getCreateLookupTableInstructionAsync, |
| 26 | + getExtendLookupTableInstruction, |
| 27 | +} from "@solana-program/address-lookup-table"; |
| 28 | +import { RpcClient } from "./types"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Signs and sends a transaction. |
| 32 | + */ |
| 33 | +export const signAndSendTransaction = async ( |
| 34 | + rpcClient: RpcClient, |
| 35 | + transactionMessage: CompilableTransactionMessage & TransactionMessageWithBlockhashLifetime, |
| 36 | + commitment: Commitment = "confirmed" |
| 37 | +) => { |
| 38 | + const signedTransaction = await signTransactionMessageWithSigners(transactionMessage); |
| 39 | + const signature = getSignatureFromTransaction(signedTransaction); |
| 40 | + await sendAndConfirmTransactionFactory(rpcClient)(signedTransaction, { |
| 41 | + commitment, |
| 42 | + }); |
| 43 | + return signature; |
| 44 | +}; |
| 45 | + |
| 46 | +export const createDefaultTransaction = async (rpcClient: RpcClient, signer: TransactionSigner) => { |
| 47 | + const { value: latestBlockhash } = await rpcClient.rpc.getLatestBlockhash().send(); |
| 48 | + return pipe( |
| 49 | + createTransactionMessage({ version: 0 }), |
| 50 | + (tx) => setTransactionMessageFeePayerSigner(signer, tx), |
| 51 | + (tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx) |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Sends a transaction with an Address Lookup Table. |
| 57 | + */ |
| 58 | +export async function sendTransactionWithLookupTable( |
| 59 | + client: RpcClient, |
| 60 | + payer: KeyPairSigner, |
| 61 | + instructions: IInstruction[], |
| 62 | + addressesByLookupTableAddress: AddressesByLookupTableAddress |
| 63 | +) { |
| 64 | + return pipe( |
| 65 | + await createDefaultTransaction(client, payer), |
| 66 | + (tx) => appendTransactionMessageInstructions(instructions, tx), |
| 67 | + (tx) => compressTxWithAlt(tx, addressesByLookupTableAddress), |
| 68 | + (tx) => signTransactionMessageWithSigners(tx), |
| 69 | + async (tx) => { |
| 70 | + const signedTx = await tx; |
| 71 | + await sendAndConfirmTransactionFactory(client)(signedTx, { |
| 72 | + commitment: "confirmed", |
| 73 | + skipPreflight: false, |
| 74 | + }); |
| 75 | + return getSignatureFromTransaction(signedTx); |
| 76 | + } |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Creates an Address Lookup Table. |
| 82 | + */ |
| 83 | +export async function createLookupTable(client: RpcClient, authority: KeyPairSigner): Promise<Address> { |
| 84 | + const recentSlot = await client.rpc.getSlot({ commitment: "finalized" }).send(); |
| 85 | + |
| 86 | + const [alt] = await findAddressLookupTablePda({ |
| 87 | + authority: authority.address, |
| 88 | + recentSlot, |
| 89 | + }); |
| 90 | + |
| 91 | + const createAltIx = await getCreateLookupTableInstructionAsync({ |
| 92 | + authority, |
| 93 | + recentSlot, |
| 94 | + }); |
| 95 | + |
| 96 | + await pipe( |
| 97 | + await createDefaultTransaction(client, authority), |
| 98 | + (tx) => appendTransactionMessageInstruction(createAltIx, tx), |
| 99 | + (tx) => signAndSendTransaction(client, tx) |
| 100 | + ); |
| 101 | + |
| 102 | + return alt; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Extends an Address Lookup Table. |
| 107 | + */ |
| 108 | +export async function extendLookupTable( |
| 109 | + client: RpcClient, |
| 110 | + authority: KeyPairSigner, |
| 111 | + alt: Address, |
| 112 | + addresses: Address[] |
| 113 | +) { |
| 114 | + const extendAltIx = getExtendLookupTableInstruction({ |
| 115 | + address: alt, |
| 116 | + authority, |
| 117 | + payer: authority, |
| 118 | + addresses, |
| 119 | + }); |
| 120 | + |
| 121 | + await pipe( |
| 122 | + await createDefaultTransaction(client, authority), |
| 123 | + (tx) => appendTransactionMessageInstruction(extendAltIx, tx), |
| 124 | + (tx) => signAndSendTransaction(client, tx) |
| 125 | + ); |
| 126 | + |
| 127 | + const altAccount = await fetchAddressLookupTable(client.rpc, alt); |
| 128 | + |
| 129 | + const addressesByLookupTableAddress: AddressesByLookupTableAddress = {}; |
| 130 | + addressesByLookupTableAddress[alt] = altAccount.data.addresses; |
| 131 | + |
| 132 | + // Delay a second here to let lookup table warm up |
| 133 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 134 | + |
| 135 | + return addressesByLookupTableAddress; |
| 136 | +} |
0 commit comments