|
| 1 | +/** |
| 2 | + * TIP-20 Transaction |
| 3 | + * |
| 4 | + * Represents a Tempo Account Abstraction (AA) transaction (type 0x76) |
| 5 | + * Supports single or batch TIP-20 token transfers with memos |
| 6 | + */ |
| 7 | + |
| 8 | +import { BaseTransaction, ParseTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 9 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 10 | +import type { Address, Hex } from 'viem'; |
| 11 | +import { Tip20Operation } from './types'; |
| 12 | + |
| 13 | +/** |
| 14 | + * TIP-20 Transaction Request Structure |
| 15 | + * Represents the raw transaction data for Tempo Account Abstraction (EIP-7702) |
| 16 | + */ |
| 17 | +export interface Tip20TransactionRequest { |
| 18 | + /** Transaction type (0x76 for Tempo AA) */ |
| 19 | + type: number | string; |
| 20 | + /** Chain ID for the Tempo network */ |
| 21 | + chainId: number; |
| 22 | + /** Transaction nonce */ |
| 23 | + nonce: number; |
| 24 | + /** Maximum fee per gas (EIP-1559) */ |
| 25 | + maxFeePerGas: bigint; |
| 26 | + /** Maximum priority fee per gas (EIP-1559) */ |
| 27 | + maxPriorityFeePerGas: bigint; |
| 28 | + /** Gas limit for the transaction */ |
| 29 | + gas: bigint; |
| 30 | + /** Array of calls to execute in this transaction */ |
| 31 | + calls: { to: Address; data: Hex; value: bigint }[]; |
| 32 | + /** Access list (optional, typically empty for TIP-20) */ |
| 33 | + accessList?: unknown[]; |
| 34 | + /** Optional TIP-20 token to use for paying fees */ |
| 35 | + feeToken?: Address; |
| 36 | +} |
| 37 | + |
| 38 | +export class Tip20Transaction extends BaseTransaction { |
| 39 | + private txRequest: Tip20TransactionRequest; |
| 40 | + private _operations: Tip20Operation[]; |
| 41 | + private _signature?: { r: Hex; s: Hex; yParity: number }; |
| 42 | + |
| 43 | + constructor(_coinConfig: Readonly<CoinConfig>, request: Tip20TransactionRequest, operations: Tip20Operation[] = []) { |
| 44 | + super(_coinConfig); |
| 45 | + this.txRequest = request; |
| 46 | + this._operations = operations; |
| 47 | + } |
| 48 | + |
| 49 | + get type(): TransactionType { |
| 50 | + return TransactionType.Send; |
| 51 | + } |
| 52 | + |
| 53 | + canSign(): boolean { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + async serialize(signature?: { r: Hex; s: Hex; yParity: number }): Promise<Hex> { |
| 58 | + // TODO: Implement viem EIP-7702 transaction serialization |
| 59 | + throw new ParseTransactionError('Transaction serialization not yet implemented'); |
| 60 | + } |
| 61 | + |
| 62 | + getOperations(): Tip20Operation[] { |
| 63 | + return [...this._operations]; |
| 64 | + } |
| 65 | + |
| 66 | + getFeeToken(): Address | undefined { |
| 67 | + return this.txRequest.feeToken; |
| 68 | + } |
| 69 | + |
| 70 | + getOperationCount(): number { |
| 71 | + return this.txRequest.calls.length; |
| 72 | + } |
| 73 | + |
| 74 | + isBatch(): boolean { |
| 75 | + return this.txRequest.calls.length > 1; |
| 76 | + } |
| 77 | + |
| 78 | + setSignature(signature: { r: Hex; s: Hex; yParity: number }): void { |
| 79 | + this._signature = signature; |
| 80 | + } |
| 81 | + |
| 82 | + getSignature(): { r: Hex; s: Hex; yParity: number } | undefined { |
| 83 | + return this._signature; |
| 84 | + } |
| 85 | + |
| 86 | + toJson(): Record<string, unknown> { |
| 87 | + return { |
| 88 | + type: this.txRequest.type, |
| 89 | + chainId: this.txRequest.chainId, |
| 90 | + nonce: this.txRequest.nonce, |
| 91 | + maxFeePerGas: this.txRequest.maxFeePerGas.toString(), |
| 92 | + maxPriorityFeePerGas: this.txRequest.maxPriorityFeePerGas.toString(), |
| 93 | + gas: this.txRequest.gas.toString(), |
| 94 | + callCount: this.txRequest.calls.length, |
| 95 | + feeToken: this.txRequest.feeToken, |
| 96 | + operations: this._operations, |
| 97 | + signature: this._signature, |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + async toBroadcastFormat(): Promise<string> { |
| 102 | + return await this.serialize(this._signature); |
| 103 | + } |
| 104 | + |
| 105 | + get id(): string { |
| 106 | + return 'pending'; |
| 107 | + } |
| 108 | + |
| 109 | + toString(): string { |
| 110 | + return JSON.stringify(this.toJson(), null, 2); |
| 111 | + } |
| 112 | + |
| 113 | + canBroadcast(): boolean { |
| 114 | + return this.txRequest.calls.length > 0 && this.txRequest.chainId > 0; |
| 115 | + } |
| 116 | +} |
0 commit comments