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