|
| 1 | +import BigNumber from 'bignumber.js'; |
| 2 | +import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate'; |
| 3 | +import { DecodedSignedTx, DecodedSigningPayload, UnsignedTransaction } from '@substrate/txwrapper-core'; |
| 4 | +import { methods } from '@substrate/txwrapper-polkadot'; |
| 5 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 6 | +import { BaseAddress, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 7 | +import { StakingTransactionSchema } from './txnSchema'; |
| 8 | +import utils from './utils'; |
| 9 | +import { BondArgs, BondExtraArgs } from './iface'; |
| 10 | + |
| 11 | +export interface StakingParams { |
| 12 | + amount: string; |
| 13 | + controller?: string; |
| 14 | + payee?: string; |
| 15 | + addToStake?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +export class StakingBuilder extends TransactionBuilder { |
| 19 | + protected _amount: string; |
| 20 | + protected _controller: string; |
| 21 | + protected _payee: string; |
| 22 | + protected _addToStake: boolean; |
| 23 | + |
| 24 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 25 | + super(_coinConfig); |
| 26 | + this.material(utils.getMaterial(_coinConfig.network.type)); |
| 27 | + } |
| 28 | + |
| 29 | + protected get transactionType(): TransactionType { |
| 30 | + return TransactionType.StakingActivate; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Build the appropriate staking transaction based on whether we're doing |
| 35 | + * initial bonding or adding to an existing stake. |
| 36 | + * |
| 37 | + * @returns {UnsignedTransaction} The constructed unsigned staking transaction |
| 38 | + */ |
| 39 | + protected buildTransaction(): UnsignedTransaction { |
| 40 | + const baseTxInfo = this.createBaseTxInfo(); |
| 41 | + |
| 42 | + if (this._addToStake) { |
| 43 | + return methods.staking.bondExtra( |
| 44 | + { |
| 45 | + maxAdditional: this._amount, |
| 46 | + }, |
| 47 | + baseTxInfo.baseTxInfo, |
| 48 | + baseTxInfo.options |
| 49 | + ); |
| 50 | + } else { |
| 51 | + return methods.staking.bond( |
| 52 | + { |
| 53 | + controller: this._controller || this._sender, |
| 54 | + value: this._amount, |
| 55 | + payee: this._payee || 'Staked', |
| 56 | + }, |
| 57 | + baseTxInfo.baseTxInfo, |
| 58 | + baseTxInfo.options |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set the staking amount. |
| 65 | + * |
| 66 | + * @param {string} amount - Amount to stake in smallest unit |
| 67 | + * @returns {StakingBuilder} This staking builder |
| 68 | + */ |
| 69 | + amount(amount: string): this { |
| 70 | + this.validateValue(new BigNumber(amount)); |
| 71 | + this._amount = amount; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set whether this is adding to an existing stake. |
| 77 | + * |
| 78 | + * @param {boolean} addToStake - true for bondExtra, false for initial bond |
| 79 | + * @returns {StakingBuilder} This staking builder |
| 80 | + */ |
| 81 | + addToStake(addToStake: boolean): this { |
| 82 | + this._addToStake = addToStake; |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * The controller of the staked amount. |
| 88 | + * Only applies to initial bonding. |
| 89 | + * |
| 90 | + * @param {BaseAddress} controller - Controller account address |
| 91 | + * @returns {StakingBuilder} This staking builder |
| 92 | + */ |
| 93 | + controller(controller: BaseAddress): this { |
| 94 | + this.validateAddress(controller); |
| 95 | + this._controller = controller.address; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * The rewards destination of the staked amount. |
| 101 | + * Only applies to initial bonding. |
| 102 | + * |
| 103 | + * @param {string} payee - Where rewards should be directed (e.g. 'Staked') |
| 104 | + * @returns {StakingBuilder} This staking builder |
| 105 | + */ |
| 106 | + payee(payee: string): this { |
| 107 | + this._payee = payee; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /** @inheritdoc */ |
| 112 | + validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx): void { |
| 113 | + const methodName = decodedTxn.method?.name as string; |
| 114 | + |
| 115 | + if (methodName === 'staking.bond') { |
| 116 | + const txMethod = decodedTxn.method.args as unknown as BondArgs; |
| 117 | + const value = txMethod.value; |
| 118 | + const controller = txMethod.controller; |
| 119 | + const payee = txMethod.payee; |
| 120 | + |
| 121 | + const validationResult = StakingTransactionSchema.validate({ |
| 122 | + value, |
| 123 | + controller, |
| 124 | + payee, |
| 125 | + }); |
| 126 | + |
| 127 | + if (validationResult.error) { |
| 128 | + throw new InvalidTransactionError(`Invalid transaction: ${validationResult.error.message}`); |
| 129 | + } |
| 130 | + } else if (methodName === 'staking.bondExtra') { |
| 131 | + const txMethod = decodedTxn.method.args as unknown as BondExtraArgs; |
| 132 | + const value = txMethod.maxAdditional; |
| 133 | + |
| 134 | + const validationResult = StakingTransactionSchema.validate({ |
| 135 | + value, |
| 136 | + addToStake: true, |
| 137 | + }); |
| 138 | + |
| 139 | + if (validationResult.error) { |
| 140 | + throw new InvalidTransactionError(`Invalid transaction: ${validationResult.error.message}`); |
| 141 | + } |
| 142 | + } else { |
| 143 | + throw new InvalidTransactionError(`Invalid transaction type: ${methodName}`); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** @inheritdoc */ |
| 148 | + protected fromImplementation(rawTransaction: string): Transaction { |
| 149 | + const tx = super.fromImplementation(rawTransaction); |
| 150 | + |
| 151 | + const methodName = this._method?.name as string; |
| 152 | + |
| 153 | + if (methodName === 'staking.bond' && this._method) { |
| 154 | + const txMethod = this._method.args as unknown as BondArgs; |
| 155 | + this.amount(txMethod.value); |
| 156 | + this.controller({ address: txMethod.controller }); |
| 157 | + this.payee(txMethod.payee); |
| 158 | + } else if (methodName === 'staking.bondExtra' && this._method) { |
| 159 | + const txMethod = this._method.args as unknown as BondExtraArgs; |
| 160 | + this.amount(txMethod.maxAdditional); |
| 161 | + this.addToStake(true); |
| 162 | + } else { |
| 163 | + throw new InvalidTransactionError( |
| 164 | + `Invalid Transaction Type: ${methodName}. Expected staking.bond or staking.bondExtra` |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + return tx; |
| 169 | + } |
| 170 | + |
| 171 | + /** @inheritdoc */ |
| 172 | + validateTransaction(tx: Transaction): void { |
| 173 | + super.validateTransaction(tx); |
| 174 | + this.validateFields(); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Validate the builder fields. |
| 179 | + */ |
| 180 | + private validateFields(): void { |
| 181 | + const validationResult = StakingTransactionSchema.validate({ |
| 182 | + value: this._amount, |
| 183 | + controller: this._controller, |
| 184 | + payee: this._payee, |
| 185 | + addToStake: this._addToStake, |
| 186 | + }); |
| 187 | + |
| 188 | + if (validationResult.error) { |
| 189 | + throw new InvalidTransactionError(`Invalid transaction: ${validationResult.error.message}`); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments