|
| 1 | +import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { TransactionBuilder } from './transactionBuilder'; |
| 3 | +import { Transaction } from './transaction'; |
| 4 | +import { TransactionReceipt } from './iface'; |
| 5 | +import { TronResource } from './resourcesTypes'; |
| 6 | + |
| 7 | +interface RawFreezeContract { |
| 8 | + parameter: { |
| 9 | + value: { |
| 10 | + resource?: string; |
| 11 | + frozen_balance?: number; |
| 12 | + owner_address?: string; |
| 13 | + }; |
| 14 | + }; |
| 15 | + type: string; |
| 16 | +} |
| 17 | + |
| 18 | +export class FreezeBalanceTxBuilder extends TransactionBuilder { |
| 19 | + /** @inheritdoc */ |
| 20 | + protected get transactionType(): TransactionType { |
| 21 | + return TransactionType.StakingActivate; |
| 22 | + } |
| 23 | + |
| 24 | + initBuilder(rawTransaction: TransactionReceipt | string): void { |
| 25 | + this.transaction = this.fromImplementation(rawTransaction); |
| 26 | + this.transaction.setTransactionType(this.transactionType); |
| 27 | + } |
| 28 | + |
| 29 | + validateTransaction(transaction: Transaction | TransactionReceipt): void { |
| 30 | + if (transaction && typeof (transaction as Transaction).toJson === 'function') { |
| 31 | + super.validateTransaction(transaction as Transaction); |
| 32 | + const rawTx = (transaction as Transaction).toJson(); |
| 33 | + this.validateFreezeTransaction(rawTx); |
| 34 | + } else { |
| 35 | + this.validateFreezeTransaction(transaction as TransactionReceipt); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Validates if the transaction is a valid freeze transaction |
| 41 | + * @param {TransactionReceipt} transaction - The transaction to validate |
| 42 | + * @throws {InvalidTransactionError} when the transaction is invalid |
| 43 | + */ |
| 44 | + private validateFreezeTransaction(transaction: TransactionReceipt): void { |
| 45 | + if (!transaction?.raw_data?.contract?.length) { |
| 46 | + throw new InvalidTransactionError('Invalid transaction: missing or empty contract array'); |
| 47 | + } |
| 48 | + |
| 49 | + const contract = transaction.raw_data.contract[0] as RawFreezeContract; |
| 50 | + |
| 51 | + // Validate contract type |
| 52 | + if (contract.type !== 'FreezeBalanceV2Contract') { |
| 53 | + throw new InvalidTransactionError( |
| 54 | + `Invalid freeze transaction: expected contract type FreezeBalanceV2Contract but got ${contract.type}` |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + // Validate parameter value |
| 59 | + if (!contract?.parameter?.value) { |
| 60 | + throw new InvalidTransactionError('Invalid freeze transaction: missing parameter value'); |
| 61 | + } |
| 62 | + |
| 63 | + const value = contract.parameter.value; |
| 64 | + |
| 65 | + // Validate resource - Using TronResource instead of FreezeResource |
| 66 | + if (!Object.values(TronResource).includes(value.resource as TronResource)) { |
| 67 | + throw new InvalidTransactionError( |
| 68 | + `Invalid freeze transaction: resource must be ${Object.values(TronResource).join(' or ')}, got ${ |
| 69 | + value.resource |
| 70 | + }` |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + // Validate frozen_balance |
| 75 | + if (!value.frozen_balance || value.frozen_balance <= 0) { |
| 76 | + throw new InvalidTransactionError('Invalid freeze transaction: frozen_balance must be positive'); |
| 77 | + } |
| 78 | + |
| 79 | + // Validate owner_address |
| 80 | + if (!value.owner_address || typeof value.owner_address !== 'string' || value.owner_address.length === 0) { |
| 81 | + throw new InvalidTransactionError('Invalid freeze transaction: missing or invalid owner_address'); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check if the transaction is a valid freeze transaction |
| 87 | + * @param {TransactionReceipt} transaction - The transaction to check |
| 88 | + * @returns True if the transaction is a valid freeze transaction |
| 89 | + */ |
| 90 | + canSign(transaction: TransactionReceipt): boolean { |
| 91 | + try { |
| 92 | + this.validateFreezeTransaction(transaction); |
| 93 | + return true; |
| 94 | + } catch (e) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments