|
| 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 './resourceTypes'; |
| 6 | + |
| 7 | +interface RawUnfreezeContract { |
| 8 | + parameter: { |
| 9 | + value: { |
| 10 | + resource?: string; |
| 11 | + unfreeze_balance?: number; |
| 12 | + owner_address?: string; |
| 13 | + }; |
| 14 | + }; |
| 15 | + type: string; |
| 16 | +} |
| 17 | + |
| 18 | +export class UnfreezeBuilder extends TransactionBuilder { |
| 19 | + /** @inheritdoc */ |
| 20 | + protected get transactionType(): TransactionType { |
| 21 | + return TransactionType.StakingUnlock; |
| 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.validateUnfreezeTransaction(rawTx); |
| 34 | + } else { |
| 35 | + this.validateUnfreezeTransaction(transaction as TransactionReceipt); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Validates if the transaction is a valid unfreeze transaction |
| 41 | + * @param transaction The transaction to validate |
| 42 | + * @throws {InvalidTransactionError} when the transaction is invalid |
| 43 | + */ |
| 44 | + private validateUnfreezeTransaction(transaction: TransactionReceipt): void { |
| 45 | + if ( |
| 46 | + !transaction || |
| 47 | + !transaction.raw_data || |
| 48 | + !transaction.raw_data.contract || |
| 49 | + transaction.raw_data.contract.length === 0 |
| 50 | + ) { |
| 51 | + throw new InvalidTransactionError('Invalid transaction: missing or empty contract array'); |
| 52 | + } |
| 53 | + |
| 54 | + const contract = transaction.raw_data.contract[0] as RawUnfreezeContract; |
| 55 | + |
| 56 | + // Validate contract type |
| 57 | + if (contract.type !== 'UnfreezeBalanceV2Contract') { |
| 58 | + throw new InvalidTransactionError( |
| 59 | + `Invalid unfreeze transaction: expected contract type UnfreezeBalanceV2Contract but got ${contract.type}` |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // Validate parameter value |
| 64 | + if (!contract.parameter || !contract.parameter.value) { |
| 65 | + throw new InvalidTransactionError('Invalid unfreeze transaction: missing parameter value'); |
| 66 | + } |
| 67 | + |
| 68 | + const value = contract.parameter.value; |
| 69 | + |
| 70 | + // Validate resource |
| 71 | + if (!Object.values(TronResource).includes(value.resource as TronResource)) { |
| 72 | + throw new InvalidTransactionError( |
| 73 | + `Invalid unfreeze transaction: resource must be ${Object.values(TronResource).join(' or ')}, got ${ |
| 74 | + value.resource |
| 75 | + }` |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + // Validate unfreeze_balance |
| 80 | + if (!value.unfreeze_balance || value.unfreeze_balance <= 0) { |
| 81 | + throw new InvalidTransactionError('Invalid unfreeze transaction: unfreeze_balance must be positive'); |
| 82 | + } |
| 83 | + |
| 84 | + // Validate owner_address |
| 85 | + if (!value.owner_address || typeof value.owner_address !== 'string' || value.owner_address.length === 0) { |
| 86 | + throw new InvalidTransactionError('Invalid unfreeze transaction: missing or invalid owner_address'); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Check if the transaction is a valid unfreeze transaction |
| 92 | + * @param transaction Transaction to check |
| 93 | + * @returns True if the transaction is a valid unfreeze transaction |
| 94 | + */ |
| 95 | + canSign(transaction: TransactionReceipt): boolean { |
| 96 | + try { |
| 97 | + this.validateUnfreezeTransaction(transaction); |
| 98 | + return true; |
| 99 | + } catch (e) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments