|
| 1 | +/** |
| 2 | + * Transaction building from high-level intents. |
| 3 | + * |
| 4 | + * Provides types and functions for building Solana transactions from a |
| 5 | + * declarative intent structure, without requiring the full @solana/web3.js dependency. |
| 6 | + */ |
| 7 | + |
| 8 | +import { BuilderNamespace } from "./wasm/wasm_solana.js"; |
| 9 | + |
| 10 | +// ============================================================================= |
| 11 | +// Nonce Types |
| 12 | +// ============================================================================= |
| 13 | + |
| 14 | +/** Use a recent blockhash for the transaction */ |
| 15 | +export interface BlockhashNonceSource { |
| 16 | + type: "blockhash"; |
| 17 | + /** The recent blockhash value (base58) */ |
| 18 | + value: string; |
| 19 | +} |
| 20 | + |
| 21 | +/** Use a durable nonce account for the transaction */ |
| 22 | +export interface DurableNonceSource { |
| 23 | + type: "durable"; |
| 24 | + /** The nonce account address (base58) */ |
| 25 | + address: string; |
| 26 | + /** The nonce authority address (base58) */ |
| 27 | + authority: string; |
| 28 | + /** The nonce value stored in the account (base58) - this becomes the blockhash */ |
| 29 | + value: string; |
| 30 | +} |
| 31 | + |
| 32 | +/** Nonce source for the transaction */ |
| 33 | +export type NonceSource = BlockhashNonceSource | DurableNonceSource; |
| 34 | + |
| 35 | +// ============================================================================= |
| 36 | +// Instruction Types |
| 37 | +// ============================================================================= |
| 38 | + |
| 39 | +/** SOL transfer instruction */ |
| 40 | +export interface TransferInstruction { |
| 41 | + type: "transfer"; |
| 42 | + /** Source account (base58) */ |
| 43 | + from: string; |
| 44 | + /** Destination account (base58) */ |
| 45 | + to: string; |
| 46 | + /** Amount in lamports (as string for BigInt compatibility) */ |
| 47 | + lamports: string; |
| 48 | +} |
| 49 | + |
| 50 | +/** Create new account instruction */ |
| 51 | +export interface CreateAccountInstruction { |
| 52 | + type: "createAccount"; |
| 53 | + /** Funding account (base58) */ |
| 54 | + from: string; |
| 55 | + /** New account address (base58) */ |
| 56 | + newAccount: string; |
| 57 | + /** Lamports to transfer (as string) */ |
| 58 | + lamports: string; |
| 59 | + /** Space to allocate in bytes */ |
| 60 | + space: number; |
| 61 | + /** Owner program (base58) */ |
| 62 | + owner: string; |
| 63 | +} |
| 64 | + |
| 65 | +/** Advance durable nonce instruction */ |
| 66 | +export interface NonceAdvanceInstruction { |
| 67 | + type: "nonceAdvance"; |
| 68 | + /** Nonce account address (base58) */ |
| 69 | + nonce: string; |
| 70 | + /** Nonce authority (base58) */ |
| 71 | + authority: string; |
| 72 | +} |
| 73 | + |
| 74 | +/** Initialize nonce account instruction */ |
| 75 | +export interface NonceInitializeInstruction { |
| 76 | + type: "nonceInitialize"; |
| 77 | + /** Nonce account address (base58) */ |
| 78 | + nonce: string; |
| 79 | + /** Nonce authority (base58) */ |
| 80 | + authority: string; |
| 81 | +} |
| 82 | + |
| 83 | +/** Allocate space instruction */ |
| 84 | +export interface AllocateInstruction { |
| 85 | + type: "allocate"; |
| 86 | + /** Account to allocate (base58) */ |
| 87 | + account: string; |
| 88 | + /** Space to allocate in bytes */ |
| 89 | + space: number; |
| 90 | +} |
| 91 | + |
| 92 | +/** Assign account to program instruction */ |
| 93 | +export interface AssignInstruction { |
| 94 | + type: "assign"; |
| 95 | + /** Account to assign (base58) */ |
| 96 | + account: string; |
| 97 | + /** New owner program (base58) */ |
| 98 | + owner: string; |
| 99 | +} |
| 100 | + |
| 101 | +/** Memo instruction */ |
| 102 | +export interface MemoInstruction { |
| 103 | + type: "memo"; |
| 104 | + /** The memo message */ |
| 105 | + message: string; |
| 106 | +} |
| 107 | + |
| 108 | +/** Compute budget instruction */ |
| 109 | +export interface ComputeBudgetInstruction { |
| 110 | + type: "computeBudget"; |
| 111 | + /** Compute unit limit (optional) */ |
| 112 | + unitLimit?: number; |
| 113 | + /** Compute unit price in micro-lamports (optional) */ |
| 114 | + unitPrice?: number; |
| 115 | +} |
| 116 | + |
| 117 | +/** Union of all instruction types */ |
| 118 | +export type Instruction = |
| 119 | + | TransferInstruction |
| 120 | + | CreateAccountInstruction |
| 121 | + | NonceAdvanceInstruction |
| 122 | + | NonceInitializeInstruction |
| 123 | + | AllocateInstruction |
| 124 | + | AssignInstruction |
| 125 | + | MemoInstruction |
| 126 | + | ComputeBudgetInstruction; |
| 127 | + |
| 128 | +// ============================================================================= |
| 129 | +// TransactionIntent |
| 130 | +// ============================================================================= |
| 131 | + |
| 132 | +/** |
| 133 | + * A declarative intent to build a Solana transaction. |
| 134 | + * |
| 135 | + * @example |
| 136 | + * ```typescript |
| 137 | + * const intent: TransactionIntent = { |
| 138 | + * feePayer: 'DgT9qyYwYKBRDyDw3EfR12LHQCQjtNrKu2qMsXHuosmB', |
| 139 | + * nonce: { |
| 140 | + * type: 'blockhash', |
| 141 | + * value: 'GWaQEymC3Z9SHM2gkh8u12xL1zJPMHPCSVR3pSDpEXE4' |
| 142 | + * }, |
| 143 | + * instructions: [ |
| 144 | + * { type: 'transfer', from: '...', to: '...', lamports: '1000000' } |
| 145 | + * ] |
| 146 | + * }; |
| 147 | + * ``` |
| 148 | + */ |
| 149 | +export interface TransactionIntent { |
| 150 | + /** The fee payer's public key (base58) */ |
| 151 | + feePayer: string; |
| 152 | + /** The nonce source (blockhash or durable nonce) */ |
| 153 | + nonce: NonceSource; |
| 154 | + /** List of instructions to include */ |
| 155 | + instructions: Instruction[]; |
| 156 | +} |
| 157 | + |
| 158 | +// ============================================================================= |
| 159 | +// buildTransaction function |
| 160 | +// ============================================================================= |
| 161 | + |
| 162 | +/** |
| 163 | + * Build a Solana transaction from a high-level intent. |
| 164 | + * |
| 165 | + * This function takes a declarative TransactionIntent and produces serialized |
| 166 | + * transaction bytes that can be signed and submitted to the network. |
| 167 | + * |
| 168 | + * The returned transaction is unsigned - signatures should be added before |
| 169 | + * broadcasting. |
| 170 | + * |
| 171 | + * @param intent - The transaction intent describing what to build |
| 172 | + * @returns Serialized unsigned transaction bytes (Uint8Array) |
| 173 | + * @throws Error if the intent cannot be built (e.g., invalid addresses) |
| 174 | + * |
| 175 | + * @example |
| 176 | + * ```typescript |
| 177 | + * import { buildTransaction } from '@bitgo/wasm-solana'; |
| 178 | + * |
| 179 | + * // Build a simple SOL transfer |
| 180 | + * const txBytes = buildTransaction({ |
| 181 | + * feePayer: sender, |
| 182 | + * nonce: { type: 'blockhash', value: blockhash }, |
| 183 | + * instructions: [ |
| 184 | + * { type: 'transfer', from: sender, to: recipient, lamports: '1000000' } |
| 185 | + * ] |
| 186 | + * }); |
| 187 | + * |
| 188 | + * // The returned bytes can be signed and broadcast |
| 189 | + * ``` |
| 190 | + * |
| 191 | + * @example |
| 192 | + * ```typescript |
| 193 | + * // Build with durable nonce and priority fee |
| 194 | + * const txBytes = buildTransaction({ |
| 195 | + * feePayer: sender, |
| 196 | + * nonce: { type: 'durable', address: nonceAccount, authority: sender, value: nonceValue }, |
| 197 | + * instructions: [ |
| 198 | + * { type: 'computeBudget', unitLimit: 200000, unitPrice: 5000 }, |
| 199 | + * { type: 'transfer', from: sender, to: recipient, lamports: '1000000' }, |
| 200 | + * { type: 'memo', message: 'BitGo transfer' } |
| 201 | + * ] |
| 202 | + * }); |
| 203 | + * ``` |
| 204 | + */ |
| 205 | +export function buildTransaction(intent: TransactionIntent): Uint8Array { |
| 206 | + return BuilderNamespace.build_transaction(intent); |
| 207 | +} |
0 commit comments