|
| 1 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 2 | +import { defineMethod, UnsignedTransaction, DecodedSignedTx, DecodedSigningPayload } from '@substrate/txwrapper-core'; |
| 3 | +import BigNumber from 'bignumber.js'; |
| 4 | +import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 5 | +import { TransactionBuilder, Interface, Schema, Transaction } from '@bitgo/abstract-substrate'; |
| 6 | + |
| 7 | +export class TokenTransferBuilder extends TransactionBuilder { |
| 8 | + protected _destinationColdkey: string; |
| 9 | + protected _hotkey: string; |
| 10 | + protected _originNetuid: string; |
| 11 | + protected _destinationNetuid: string; |
| 12 | + protected _alphaAmount: string; |
| 13 | + |
| 14 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 15 | + super(_coinConfig); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Construct a transaction to transfer stake |
| 20 | + * @returns {UnsignedTransaction} an unsigned transfer stake transaction |
| 21 | + */ |
| 22 | + protected buildTransaction(): UnsignedTransaction { |
| 23 | + const baseTxInfo = this.createBaseTxInfo(); |
| 24 | + return this.transferStake( |
| 25 | + { |
| 26 | + destinationColdkey: this._destinationColdkey, |
| 27 | + hotkey: this._hotkey, |
| 28 | + originNetuid: this._originNetuid, |
| 29 | + destinationNetuid: this._destinationNetuid, |
| 30 | + alphaAmount: this._alphaAmount, |
| 31 | + }, |
| 32 | + baseTxInfo |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /** @inheritdoc */ |
| 37 | + protected get transactionType(): TransactionType { |
| 38 | + return TransactionType.SendToken; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Set the amount to transfer |
| 43 | + * @param {string} amount to transfer |
| 44 | + * @returns {TokenTransferBuilder} This builder. |
| 45 | + */ |
| 46 | + amount(amount: string): this { |
| 47 | + this.validateValue(new BigNumber(amount)); |
| 48 | + this._alphaAmount = amount; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Set the validator hot key address |
| 54 | + * @param {string} hotkey address of validator |
| 55 | + * @returns {TokenTransferBuilder} This builder. |
| 56 | + */ |
| 57 | + hotkey(address: string): this { |
| 58 | + this.validateAddress({ address }); |
| 59 | + this._hotkey = address; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set the destination cold key address |
| 65 | + * @param {string} address of the destination cold key |
| 66 | + * @returns {TokenTransferBuilder} This builder. |
| 67 | + */ |
| 68 | + |
| 69 | + destinationColdkey(address: string): this { |
| 70 | + this.validateAddress({ address }); |
| 71 | + this._destinationColdkey = address; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set the origin netuid of the subnet (root network is 0) |
| 77 | + * @param {string} netuid of subnet |
| 78 | + * @returns {TokenTransferBuilder} This builder. |
| 79 | + */ |
| 80 | + originNetuid(netuid: string): this { |
| 81 | + this._originNetuid = netuid; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set the destination netuid of the subnet (root network is 0) |
| 87 | + * @param {string} netuid of subnet |
| 88 | + * @returns {TokenTransferBuilder} This builder. |
| 89 | + */ |
| 90 | + destinationNetuid(netuid: string): this { |
| 91 | + this._destinationNetuid = netuid; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + /** @inheritdoc */ |
| 96 | + protected fromImplementation(rawTransaction: string): Transaction { |
| 97 | + const tx = super.fromImplementation(rawTransaction); |
| 98 | + if (this._method?.name === Interface.MethodNames.TransferStake) { |
| 99 | + const txMethod = this._method.args as Interface.TransferStakeArgs; |
| 100 | + this.amount(txMethod.alphaAmount); |
| 101 | + this.hotkey(txMethod.hotkey); |
| 102 | + this.destinationColdkey(txMethod.destinationColdkey); |
| 103 | + this.originNetuid(txMethod.originNetuid); |
| 104 | + this.destinationNetuid(txMethod.destinationNetuid); |
| 105 | + } else { |
| 106 | + throw new InvalidTransactionError( |
| 107 | + `Invalid Transaction Type: ${this._method?.name}. Expected ${Interface.MethodNames.TransferStake}` |
| 108 | + ); |
| 109 | + } |
| 110 | + return tx; |
| 111 | + } |
| 112 | + |
| 113 | + /** @inheritdoc */ |
| 114 | + validateTransaction(_: Transaction): void { |
| 115 | + super.validateTransaction(_); |
| 116 | + this.validateFields( |
| 117 | + this._destinationColdkey, |
| 118 | + this._hotkey, |
| 119 | + this._originNetuid, |
| 120 | + this._destinationNetuid, |
| 121 | + this._alphaAmount |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Helper method to validate whether tx params have the correct type and format |
| 127 | + * @param {string} destinationColdkey destination cold key address |
| 128 | + * @param {string} hotkey hotkey address of the validator |
| 129 | + * @param {string} originNetuid netuid of the origin subnet |
| 130 | + * @param {string} destinationNetuid netuid of the destination subnet |
| 131 | + * @param {string} alphaAmount amount to transfer |
| 132 | + * @throws {InvalidTransactionError} if validation fails |
| 133 | + */ |
| 134 | + private validateFields( |
| 135 | + destinationColdkey: string, |
| 136 | + hotkey: string, |
| 137 | + originNetuid: string, |
| 138 | + destinationNetuid: string, |
| 139 | + alphaAmount: string |
| 140 | + ): void { |
| 141 | + const validationResult = Schema.TransferStakeTransactionSchema.validate({ |
| 142 | + destinationColdkey, |
| 143 | + hotkey, |
| 144 | + originNetuid, |
| 145 | + destinationNetuid, |
| 146 | + alphaAmount, |
| 147 | + }); |
| 148 | + |
| 149 | + if (validationResult.error) { |
| 150 | + throw new InvalidTransactionError(`Transaction validation failed: ${validationResult.error.message}`); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** @inheritdoc */ |
| 155 | + validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx, rawTransaction: string): void { |
| 156 | + if (decodedTxn.method?.name === Interface.MethodNames.TransferStake) { |
| 157 | + const txMethod = decodedTxn.method.args as unknown as Interface.TransferStakeArgs; |
| 158 | + |
| 159 | + const validationResult = Schema.TransferStakeTransactionSchema.validate(txMethod); |
| 160 | + if (validationResult.error) { |
| 161 | + throw new InvalidTransactionError(`Transfer Transaction validation failed: ${validationResult.error.message}`); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Construct a transaction to transfer stake |
| 168 | + * |
| 169 | + * @param {Interface.TransferStakeArgs} args arguments to be passed to the transferStake method |
| 170 | + * @param {Interface.CreateBaseTxInfo} info txn info required to construct the transferStake txn |
| 171 | + * @returns {UnsignedTransaction} an unsigned stake transaction |
| 172 | + */ |
| 173 | + |
| 174 | + private transferStake(args: Interface.TransferStakeArgs, info: Interface.CreateBaseTxInfo): UnsignedTransaction { |
| 175 | + return defineMethod( |
| 176 | + { |
| 177 | + method: { |
| 178 | + args, |
| 179 | + name: 'transferStake', |
| 180 | + pallet: 'subtensorModule', |
| 181 | + }, |
| 182 | + ...info.baseTxInfo, |
| 183 | + }, |
| 184 | + info.options |
| 185 | + ); |
| 186 | + } |
| 187 | +} |
0 commit comments