|
| 1 | +import { |
| 2 | + Address, |
| 3 | + generateKeyPairSigner, |
| 4 | + GetBalanceApi, |
| 5 | + GetMinimumBalanceForRentExemptionApi, |
| 6 | + Instruction, |
| 7 | + KeyPairSigner, |
| 8 | + Rpc, |
| 9 | + TransactionSigner, |
| 10 | +} from "@solana/kit"; |
| 11 | +import { |
| 12 | + getCreateAccountInstruction, |
| 13 | + getTransferSolInstruction, |
| 14 | +} from "@solana-program/system"; |
| 15 | +import { |
| 16 | + getCloseAccountInstruction, |
| 17 | + getInitializeInstruction, |
| 18 | + getReallocateInstruction, |
| 19 | + getSetAuthorityInstruction, |
| 20 | + getWriteInstruction, |
| 21 | + SPL_RECORD_PROGRAM_ADDRESS, |
| 22 | +} from "./generated"; |
| 23 | +import { RECORD_META_DATA_SIZE } from "./constants"; |
| 24 | + |
| 25 | +export interface CreateRecordArgs { |
| 26 | + rpc: Rpc<GetMinimumBalanceForRentExemptionApi>; |
| 27 | + payer: KeyPairSigner; |
| 28 | + authority: Address; |
| 29 | + dataLength: number | bigint; |
| 30 | + programId?: Address; |
| 31 | + /** Optional: Provide your own keypair for the record account. If not provided, one is generated. */ |
| 32 | + recordKeypair?: KeyPairSigner; |
| 33 | +} |
| 34 | + |
| 35 | +export interface CreateRecordResult { |
| 36 | + recordKeypair: KeyPairSigner; |
| 37 | + ixs: Instruction[]; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * High-level function to create and initialize a Record Account. |
| 42 | + * Handles rent calculation and system account creation. |
| 43 | + */ |
| 44 | +export async function createRecord({ |
| 45 | + rpc, |
| 46 | + payer, |
| 47 | + authority, |
| 48 | + dataLength, |
| 49 | + programId = SPL_RECORD_PROGRAM_ADDRESS, |
| 50 | + recordKeypair, |
| 51 | +}: CreateRecordArgs): Promise<CreateRecordResult> { |
| 52 | + const recordSigner = recordKeypair ?? (await generateKeyPairSigner()); |
| 53 | + const space = RECORD_META_DATA_SIZE + BigInt(dataLength); |
| 54 | + const lamports = await rpc.getMinimumBalanceForRentExemption(space).send(); |
| 55 | + |
| 56 | + const createAccountIx = getCreateAccountInstruction({ |
| 57 | + payer: payer, |
| 58 | + newAccount: recordSigner, |
| 59 | + lamports, |
| 60 | + space, |
| 61 | + programAddress: programId, |
| 62 | + }); |
| 63 | + |
| 64 | + const initializeIx = getInitializeInstruction( |
| 65 | + { |
| 66 | + recordAccount: recordSigner.address, |
| 67 | + authority, |
| 68 | + }, |
| 69 | + { programAddress: programId }, |
| 70 | + ); |
| 71 | + |
| 72 | + return { |
| 73 | + recordKeypair: recordSigner, |
| 74 | + ixs: [createAccountIx, initializeIx], |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +export interface WriteRecordArgs { |
| 79 | + recordAccount: Address; |
| 80 | + authority: TransactionSigner; |
| 81 | + offset: number | bigint; |
| 82 | + data: Uint8Array; |
| 83 | + programId?: Address; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Creates a Write instruction. |
| 88 | + * Note: For large data, you should manually chunk this or use a loop helper. |
| 89 | + */ |
| 90 | +export function createWriteInstruction(args: WriteRecordArgs): Instruction { |
| 91 | + return getWriteInstruction( |
| 92 | + { |
| 93 | + recordAccount: args.recordAccount, |
| 94 | + authority: args.authority, |
| 95 | + offset: BigInt(args.offset), |
| 96 | + data: args.data, |
| 97 | + }, |
| 98 | + { programAddress: args.programId }, |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +export interface ReallocateRecordArgs { |
| 103 | + rpc: Rpc<GetBalanceApi & GetMinimumBalanceForRentExemptionApi>; |
| 104 | + payer: KeyPairSigner; |
| 105 | + recordAccount: Address; |
| 106 | + authority: TransactionSigner; |
| 107 | + newDataLength: number | bigint; |
| 108 | + programId?: Address; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * High-level function to reallocate a Record Account. |
| 113 | + * Checks if additional lamports are needed for the new size and adds a transfer instruction if so. |
| 114 | + */ |
| 115 | +export async function reallocateRecord({ |
| 116 | + rpc, |
| 117 | + payer, |
| 118 | + recordAccount, |
| 119 | + authority, |
| 120 | + newDataLength, |
| 121 | + programId = SPL_RECORD_PROGRAM_ADDRESS, |
| 122 | +}: ReallocateRecordArgs): Promise<Instruction[]> { |
| 123 | + const ixs: Instruction[] = []; |
| 124 | + const newSpace = RECORD_META_DATA_SIZE + BigInt(newDataLength); |
| 125 | + const requiredRent = await rpc |
| 126 | + .getMinimumBalanceForRentExemption(newSpace) |
| 127 | + .send(); |
| 128 | + const currentBalance = await rpc.getBalance(recordAccount).send(); |
| 129 | + |
| 130 | + if (requiredRent > currentBalance.value) { |
| 131 | + const lamportsNeeded = requiredRent - currentBalance.value; |
| 132 | + ixs.push( |
| 133 | + getTransferSolInstruction({ |
| 134 | + source: payer, |
| 135 | + destination: recordAccount, |
| 136 | + amount: lamportsNeeded, |
| 137 | + }), |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + ixs.push( |
| 142 | + getReallocateInstruction( |
| 143 | + { |
| 144 | + recordAccount, |
| 145 | + authority, |
| 146 | + dataLength: BigInt(newDataLength), |
| 147 | + }, |
| 148 | + { programAddress: programId }, |
| 149 | + ), |
| 150 | + ); |
| 151 | + |
| 152 | + return ixs; |
| 153 | +} |
| 154 | + |
| 155 | +export interface SetAuthorityArgs { |
| 156 | + recordAccount: Address; |
| 157 | + authority: TransactionSigner; |
| 158 | + newAuthority: Address; |
| 159 | + programId?: Address; |
| 160 | +} |
| 161 | + |
| 162 | +export function createSetAuthorityInstruction( |
| 163 | + args: SetAuthorityArgs, |
| 164 | +): Instruction { |
| 165 | + return getSetAuthorityInstruction( |
| 166 | + { |
| 167 | + recordAccount: args.recordAccount, |
| 168 | + authority: args.authority, |
| 169 | + newAuthority: args.newAuthority, |
| 170 | + }, |
| 171 | + { programAddress: args.programId }, |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +export interface CloseRecordArgs { |
| 176 | + recordAccount: Address; |
| 177 | + authority: TransactionSigner; |
| 178 | + receiver: Address; |
| 179 | + programId?: Address; |
| 180 | +} |
| 181 | + |
| 182 | +export function createCloseRecordInstruction( |
| 183 | + args: CloseRecordArgs, |
| 184 | +): Instruction { |
| 185 | + return getCloseAccountInstruction( |
| 186 | + { |
| 187 | + recordAccount: args.recordAccount, |
| 188 | + authority: args.authority, |
| 189 | + receiver: args.receiver, |
| 190 | + }, |
| 191 | + { programAddress: args.programId }, |
| 192 | + ); |
| 193 | +} |
0 commit comments