|
| 1 | +/** |
| 2 | + * Transaction building from high-level intents. |
| 3 | + * |
| 4 | + * Provides the `buildTransaction()` function for building DOT transactions. |
| 5 | + * Follows wallet-platform pattern: buildTransaction(intent, context) |
| 6 | + */ |
| 7 | + |
| 8 | +import { BuilderNamespace } from "./wasm/wasm_dot"; |
| 9 | +import { DotTransaction } from "./transaction"; |
| 10 | +import type { TransactionIntent, BuildContext } from "./types"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Build a DOT transaction from an intent and context. |
| 14 | + * |
| 15 | + * This function takes a declarative TransactionIntent and BuildContext, |
| 16 | + * producing a Transaction object that can be inspected, signed, and serialized. |
| 17 | + * |
| 18 | + * The returned transaction is unsigned - signatures should be added via |
| 19 | + * `addSignature()` before serializing with `toBytes()` and broadcasting. |
| 20 | + * |
| 21 | + * @param intent - What to do (transfer, stake, etc.) |
| 22 | + * @param context - How to build it (sender, nonce, material, validity, referenceBlock) |
| 23 | + * @returns A Transaction object that can be inspected, signed, and serialized |
| 24 | + * @throws Error if the intent cannot be built (e.g., invalid addresses) |
| 25 | + * |
| 26 | + * @example |
| 27 | + * ```typescript |
| 28 | + * import { buildTransaction } from '@bitgo/wasm-dot'; |
| 29 | + * |
| 30 | + * // Build a simple DOT transfer |
| 31 | + * const tx = buildTransaction( |
| 32 | + * { type: 'transfer', to: '5FHneW46...', amount: 1000000000000n, keepAlive: true }, |
| 33 | + * { |
| 34 | + * sender: '5EGoFA95omzemRssELLDjVenNZ68aXyUeqtKQScXSEBvVJkr', |
| 35 | + * nonce: 5, |
| 36 | + * material: { |
| 37 | + * genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', |
| 38 | + * chainName: 'Polkadot', |
| 39 | + * specName: 'polkadot', |
| 40 | + * specVersion: 9150, |
| 41 | + * txVersion: 9 |
| 42 | + * }, |
| 43 | + * validity: { firstValid: 1000, maxDuration: 2400 }, |
| 44 | + * referenceBlock: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3' |
| 45 | + * } |
| 46 | + * ); |
| 47 | + * |
| 48 | + * // Inspect the transaction |
| 49 | + * console.log(tx.nonce); |
| 50 | + * |
| 51 | + * // Get the signable payload for signing |
| 52 | + * const payload = tx.signablePayload(); |
| 53 | + * |
| 54 | + * // Add signature and serialize |
| 55 | + * tx.addSignature(signerPubkey, signature); |
| 56 | + * const txBytes = tx.toBytes(); |
| 57 | + * ``` |
| 58 | + * |
| 59 | + * @example |
| 60 | + * ```typescript |
| 61 | + * // Build with batch (multiple operations) |
| 62 | + * const tx = buildTransaction( |
| 63 | + * { |
| 64 | + * type: 'batch', |
| 65 | + * calls: [ |
| 66 | + * { type: 'transfer', to: recipient, amount: 1000000000000n }, |
| 67 | + * { type: 'stake', amount: 5000000000000n, payee: { type: 'staked' } } |
| 68 | + * ], |
| 69 | + * atomic: true |
| 70 | + * }, |
| 71 | + * context |
| 72 | + * ); |
| 73 | + * ``` |
| 74 | + */ |
| 75 | +export function buildTransaction(intent: TransactionIntent, context: BuildContext): DotTransaction { |
| 76 | + const inner = BuilderNamespace.buildTransaction(intent, context); |
| 77 | + return DotTransaction.fromInner(inner as any); |
| 78 | +} |
| 79 | + |
| 80 | +// Re-export types for convenience |
| 81 | +export type { TransactionIntent, BuildContext } from "./types"; |
0 commit comments