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