|
| 1 | +import { TransactionType, InvalidTransactionError } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { Transaction as VetTransaction, Secp256k1 } from '@vechain/sdk-core'; |
| 4 | +import { Transaction } from './transaction'; |
| 5 | +import { VetTransactionData } from '../iface'; |
| 6 | +import EthereumAbi from 'ethereumjs-abi'; |
| 7 | +import utils from '../utils'; |
| 8 | +import BigNumber from 'bignumber.js'; |
| 9 | +import { addHexPrefix } from 'ethereumjs-util'; |
| 10 | +import { ZERO_VALUE_AMOUNT } from '../constants'; |
| 11 | + |
| 12 | +export class DelegateClauseTransaction extends Transaction { |
| 13 | + private _stakingContractAddress: string; |
| 14 | + private _tokenId: number; |
| 15 | + private _delegateForever = true; |
| 16 | + |
| 17 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 18 | + super(_coinConfig); |
| 19 | + this._type = TransactionType.StakingDelegate; |
| 20 | + } |
| 21 | + |
| 22 | + get stakingContractAddress(): string { |
| 23 | + return this._stakingContractAddress; |
| 24 | + } |
| 25 | + |
| 26 | + set stakingContractAddress(address: string) { |
| 27 | + this._stakingContractAddress = address; |
| 28 | + } |
| 29 | + |
| 30 | + get tokenId(): number { |
| 31 | + return this._tokenId; |
| 32 | + } |
| 33 | + |
| 34 | + set tokenId(tokenId: number) { |
| 35 | + this._tokenId = tokenId; |
| 36 | + } |
| 37 | + |
| 38 | + get delegateForever(): boolean { |
| 39 | + return this._delegateForever; |
| 40 | + } |
| 41 | + |
| 42 | + set delegateForever(delegateForever: boolean) { |
| 43 | + this._delegateForever = delegateForever; |
| 44 | + } |
| 45 | + |
| 46 | + buildClauses(): void { |
| 47 | + if (!this.stakingContractAddress) { |
| 48 | + throw new Error('Staking contract address is not set'); |
| 49 | + } |
| 50 | + |
| 51 | + utils.validateDelegationContractAddress(this.stakingContractAddress, this._coinConfig); |
| 52 | + |
| 53 | + if (this.tokenId === undefined || this.tokenId === null) { |
| 54 | + throw new Error('Token ID is not set'); |
| 55 | + } |
| 56 | + |
| 57 | + const data = this.getDelegateData(this.tokenId, this.delegateForever); |
| 58 | + this._transactionData = data; |
| 59 | + |
| 60 | + // Create the clause for delegation |
| 61 | + this._clauses = [ |
| 62 | + { |
| 63 | + to: this.stakingContractAddress, |
| 64 | + value: ZERO_VALUE_AMOUNT, |
| 65 | + data: this._transactionData, |
| 66 | + }, |
| 67 | + ]; |
| 68 | + |
| 69 | + // Set recipients based on the clauses |
| 70 | + this._recipients = [ |
| 71 | + { |
| 72 | + address: this.stakingContractAddress, |
| 73 | + amount: ZERO_VALUE_AMOUNT, |
| 74 | + }, |
| 75 | + ]; |
| 76 | + } |
| 77 | + /** |
| 78 | + * Encodes delegation transaction data using ethereumjs-abi for delegate method |
| 79 | + * |
| 80 | + * @param {number} tokenId - The Token ID for delegation |
| 81 | + * @returns {string} - The encoded transaction data |
| 82 | + */ |
| 83 | + getDelegateData(levelId: number, delegateForever = true): string { |
| 84 | + const methodName = 'delegate'; |
| 85 | + const types = ['uint256', 'bool']; |
| 86 | + const params = [levelId, delegateForever]; |
| 87 | + |
| 88 | + const method = EthereumAbi.methodID(methodName, types); |
| 89 | + const args = EthereumAbi.rawEncode(types, params); |
| 90 | + |
| 91 | + return addHexPrefix(Buffer.concat([method, args]).toString('hex')); |
| 92 | + } |
| 93 | + |
| 94 | + toJson(): VetTransactionData { |
| 95 | + const json: VetTransactionData = { |
| 96 | + id: this.id, |
| 97 | + chainTag: this.chainTag, |
| 98 | + blockRef: this.blockRef, |
| 99 | + expiration: this.expiration, |
| 100 | + gasPriceCoef: this.gasPriceCoef, |
| 101 | + gas: this.gas, |
| 102 | + dependsOn: this.dependsOn, |
| 103 | + nonce: this.nonce, |
| 104 | + data: this.transactionData, |
| 105 | + value: ZERO_VALUE_AMOUNT, |
| 106 | + sender: this.sender, |
| 107 | + to: this.stakingContractAddress, |
| 108 | + stakingContractAddress: this.stakingContractAddress, |
| 109 | + amountToStake: ZERO_VALUE_AMOUNT, |
| 110 | + nftTokenId: this.tokenId, |
| 111 | + autorenew: this.delegateForever, |
| 112 | + }; |
| 113 | + |
| 114 | + return json; |
| 115 | + } |
| 116 | + |
| 117 | + fromDeserializedSignedTransaction(signedTx: VetTransaction): void { |
| 118 | + try { |
| 119 | + if (!signedTx || !signedTx.body) { |
| 120 | + throw new InvalidTransactionError('Invalid transaction: missing transaction body'); |
| 121 | + } |
| 122 | + |
| 123 | + // Store the raw transaction |
| 124 | + this.rawTransaction = signedTx; |
| 125 | + |
| 126 | + // Set transaction body properties |
| 127 | + const body = signedTx.body; |
| 128 | + this.chainTag = typeof body.chainTag === 'number' ? body.chainTag : 0; |
| 129 | + this.blockRef = body.blockRef || '0x0'; |
| 130 | + this.expiration = typeof body.expiration === 'number' ? body.expiration : 64; |
| 131 | + this.clauses = body.clauses || []; |
| 132 | + this.gasPriceCoef = typeof body.gasPriceCoef === 'number' ? body.gasPriceCoef : 128; |
| 133 | + this.gas = typeof body.gas === 'number' ? body.gas : Number(body.gas) || 0; |
| 134 | + this.dependsOn = body.dependsOn || null; |
| 135 | + this.nonce = String(body.nonce); |
| 136 | + |
| 137 | + // Set delegation-specific properties |
| 138 | + if (body.clauses.length > 0) { |
| 139 | + const clause = body.clauses[0]; |
| 140 | + if (clause.to) { |
| 141 | + this.stakingContractAddress = clause.to; |
| 142 | + } |
| 143 | + if (clause.data) { |
| 144 | + this.transactionData = clause.data; |
| 145 | + const decoded = utils.decodeDelegateClauseData(clause.data); |
| 146 | + this.tokenId = decoded.tokenId; |
| 147 | + this.delegateForever = decoded.delegateForever; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Set recipients from clauses |
| 152 | + this.recipients = body.clauses.map((clause) => ({ |
| 153 | + address: (clause.to || '0x0').toString().toLowerCase(), |
| 154 | + amount: new BigNumber(clause.value || 0).toString(), |
| 155 | + })); |
| 156 | + this.loadInputsAndOutputs(); |
| 157 | + |
| 158 | + // Set sender address |
| 159 | + if (signedTx.signature && signedTx.origin) { |
| 160 | + this.sender = signedTx.origin.toString().toLowerCase(); |
| 161 | + } |
| 162 | + |
| 163 | + // Set signatures if present |
| 164 | + if (signedTx.signature) { |
| 165 | + // First signature is sender's signature |
| 166 | + this.senderSignature = Buffer.from(signedTx.signature.slice(0, Secp256k1.SIGNATURE_LENGTH)); |
| 167 | + |
| 168 | + // If there's additional signature data, it's the fee payer's signature |
| 169 | + if (signedTx.signature.length > Secp256k1.SIGNATURE_LENGTH) { |
| 170 | + this.feePayerSignature = Buffer.from(signedTx.signature.slice(Secp256k1.SIGNATURE_LENGTH)); |
| 171 | + } |
| 172 | + } |
| 173 | + } catch (e) { |
| 174 | + throw new InvalidTransactionError(`Failed to deserialize transaction: ${e.message}`); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments