|
| 1 | +import assert from 'assert'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { TransactionType, BuildTransactionError } from '@bitgo/sdk-core'; |
| 4 | +import { decodeFlushTokensData, flushTokensData } from '@bitgo/abstract-eth'; |
| 5 | +import { TransactionClause } from '@vechain/sdk-core'; |
| 6 | + |
| 7 | +import { TransactionBuilder } from './transactionBuilder'; |
| 8 | +import { Transaction } from '../transaction/transaction'; |
| 9 | +import { FlushTokenTransaction } from '../transaction/flushTokenTransaction'; |
| 10 | +import utils from '../utils'; |
| 11 | + |
| 12 | +export class FlushTokenTransactionBuilder extends TransactionBuilder { |
| 13 | + /** |
| 14 | + * Creates a new FlushTokenTransactionBuilder instance. |
| 15 | + * |
| 16 | + * @param {Readonly<CoinConfig>} _coinConfig - The coin configuration object |
| 17 | + */ |
| 18 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 19 | + super(_coinConfig); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Initializes the builder with an existing FlushTokenTransaction. |
| 24 | + * |
| 25 | + * @param {FlushTokenTransaction} tx - The transaction to initialize the builder with |
| 26 | + */ |
| 27 | + initBuilder(tx: FlushTokenTransaction): void { |
| 28 | + this._transaction = tx; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the flush token transaction instance. |
| 33 | + * |
| 34 | + * @returns {FlushTokenTransaction} The flush token transaction |
| 35 | + */ |
| 36 | + get flushTokenTransaction(): FlushTokenTransaction { |
| 37 | + return this._transaction as FlushTokenTransaction; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets the transaction type for flush token. |
| 42 | + * |
| 43 | + * @returns {TransactionType} The transaction type |
| 44 | + */ |
| 45 | + protected get transactionType(): TransactionType { |
| 46 | + return TransactionType.FlushTokens; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Validates the transaction clauses for flush token transaction. |
| 51 | + * @param {TransactionClause[]} clauses - The transaction clauses to validate. |
| 52 | + * @returns {boolean} - Returns true if the clauses are valid, false otherwise. |
| 53 | + */ |
| 54 | + protected isValidTransactionClauses(clauses: TransactionClause[]): boolean { |
| 55 | + try { |
| 56 | + if (!clauses || !Array.isArray(clauses) || clauses.length === 0) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + const clause = clauses[0]; |
| 61 | + |
| 62 | + if (!clause.to || !utils.isValidAddress(clause.to)) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + // For address init transactions, value must be exactly 0 |
| 67 | + if (clause.value !== 0) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + const { tokenAddress } = decodeFlushTokensData(clause.data, clause.to); |
| 72 | + |
| 73 | + if (!utils.isValidAddress(tokenAddress as string)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } catch (e) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Sets the token address for this token flush tx. |
| 85 | + * |
| 86 | + * @param {string} address - The token address to be set for the token flush transaction |
| 87 | + * @returns {FlushTokenTransaction} This transaction builder |
| 88 | + */ |
| 89 | + tokenAddress(address: string): this { |
| 90 | + this.validateAddress({ address }); |
| 91 | + this.flushTokenTransaction.tokenAddress = address; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + forwarderVersion(version: number): this { |
| 96 | + if (version < 4) { |
| 97 | + throw new BuildTransactionError(`Invalid forwarder version: ${version}`); |
| 98 | + } |
| 99 | + |
| 100 | + this.flushTokenTransaction.forwarderVersion = version; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /** @inheritdoc */ |
| 105 | + validateTransaction(transaction?: FlushTokenTransaction): void { |
| 106 | + if (!transaction) { |
| 107 | + throw new Error('transaction not defined'); |
| 108 | + } |
| 109 | + assert(transaction.contract, 'Contract address is required'); |
| 110 | + assert(transaction.tokenAddress, 'Token address is required'); |
| 111 | + |
| 112 | + this.validateAddress({ address: transaction.contract }); |
| 113 | + this.validateAddress({ address: transaction.tokenAddress }); |
| 114 | + } |
| 115 | + |
| 116 | + /** @inheritdoc */ |
| 117 | + protected async buildImplementation(): Promise<Transaction> { |
| 118 | + const transactionData = this.getFlushTokenTransactionData(); |
| 119 | + this.transaction.type = this.transactionType; |
| 120 | + this.flushTokenTransaction.transactionData = transactionData; |
| 121 | + await this.flushTokenTransaction.build(); |
| 122 | + return this.transaction; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Generates the transaction data for flush token transaction |
| 127 | + * |
| 128 | + * @private |
| 129 | + * @returns {string} The encoded transaction data as a hex string |
| 130 | + */ |
| 131 | + private getFlushTokenTransactionData(): string { |
| 132 | + const flushTokenData = flushTokensData( |
| 133 | + this.flushTokenTransaction.contract, |
| 134 | + this.flushTokenTransaction.tokenAddress, |
| 135 | + this.flushTokenTransaction.forwarderVersion |
| 136 | + ); |
| 137 | + return flushTokenData; |
| 138 | + } |
| 139 | +} |
0 commit comments