|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import BigNumber from 'bignumber.js'; |
| 4 | +import * as NearAPI from 'near-api-js'; |
| 5 | + |
| 6 | +import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 7 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 8 | + |
| 9 | +import { FT_TRANSFER, STORAGE_DEPOSIT } from './constants'; |
| 10 | +import { ContractCallWrapper } from './contractCallWrapper'; |
| 11 | +import { StorageDepositInput } from './iface'; |
| 12 | +import { Transaction } from './transaction'; |
| 13 | +import { TransactionBuilder } from './transactionBuilder'; |
| 14 | +import utils from './utils'; |
| 15 | + |
| 16 | +export class FungibleTokenTransferBuilder extends TransactionBuilder { |
| 17 | + private contractCallWrapper: ContractCallWrapper; |
| 18 | + |
| 19 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 20 | + super(_coinConfig); |
| 21 | + this.contractCallWrapper = new ContractCallWrapper(); |
| 22 | + this.contractCallWrapper.methodName = FT_TRANSFER; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Check if a transaction is a fungible token transfer |
| 27 | + * |
| 28 | + * @param {NearAPI.transactions.Action[]} actions near transaction actions |
| 29 | + * @returns {Boolean} true if more than 1 action present or first action method name is ft transfer |
| 30 | + */ |
| 31 | + public static isFungibleTokenTransferTransaction(actions: NearAPI.transactions.Action[]): boolean { |
| 32 | + return actions.length > 1 || actions[0].functionCall?.methodName === FT_TRANSFER; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Initialize the transaction builder fields using the decoded transaction data |
| 37 | + * |
| 38 | + * @param {Transaction} tx the transaction data |
| 39 | + */ |
| 40 | + initBuilder(tx: Transaction): void { |
| 41 | + super.initBuilder(tx); |
| 42 | + for (const action of tx.nearTransaction.actions) { |
| 43 | + if (action.functionCall && action.functionCall.methodName === FT_TRANSFER) { |
| 44 | + this.contractCallWrapper.deposit = action.functionCall.deposit.toString(); |
| 45 | + this.contractCallWrapper.gas = action.functionCall.gas.toString(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the gas of this transaction. |
| 52 | + * |
| 53 | + * @param {String} gas the gas of this transaction |
| 54 | + * @returns {TransactionBuilder} This transaction builder |
| 55 | + */ |
| 56 | + public gas(gas: string): this { |
| 57 | + this.validateValue(new BigNumber(gas)); |
| 58 | + this.contractCallWrapper.gas = gas; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Sets the deposit of at-least 1 yoctoNear |
| 64 | + * |
| 65 | + * @param {string} deposit the deposit in the minimum unit (1 Near = 1e24 yoctoNear) of this transaction |
| 66 | + * @returns {TransactionBuilder} This transaction builder |
| 67 | + */ |
| 68 | + public deposit(deposit: string): this { |
| 69 | + this.validateValue(new BigNumber(deposit)); |
| 70 | + this.contractCallWrapper.deposit = deposit; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sets the actual receiver account id inside args |
| 76 | + * |
| 77 | + * @param accountId the receiver account id |
| 78 | + */ |
| 79 | + public ftReceiverId(accountId: string): this { |
| 80 | + utils.isValidAddress(accountId); |
| 81 | + this.contractCallWrapper.args = { receiver_id: accountId }; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets the ft amount to be transferred |
| 87 | + * |
| 88 | + * @param amount the amount of fungible token to be transferred |
| 89 | + */ |
| 90 | + public amount(amount: string): this { |
| 91 | + this.validateValue(new BigNumber(amount)); |
| 92 | + this.contractCallWrapper.args = { amount }; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the optional memo for the transfer |
| 98 | + * |
| 99 | + * @param memo |
| 100 | + */ |
| 101 | + public memo(memo: string): this { |
| 102 | + this.contractCallWrapper.args = { memo }; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets the storage deposit action |
| 108 | + * |
| 109 | + * @param {StorageDepositInput} input contains the deposit value, gas and optional account id |
| 110 | + * if account id is not provided then it is self transfer |
| 111 | + */ |
| 112 | + public addStorageDeposit(input: StorageDepositInput): void { |
| 113 | + const methodName = STORAGE_DEPOSIT; |
| 114 | + assert(input.deposit, new BuildTransactionError('deposit is required before building storage deposit transfer')); |
| 115 | + assert(input.gas, new BuildTransactionError('gas is required before building fungible token transfer')); |
| 116 | + const args = input.accountId ? { account_id: input.accountId } : {}; |
| 117 | + const action = NearAPI.transactions.functionCall(methodName, args, input.gas, input.deposit); |
| 118 | + super.action(action); |
| 119 | + } |
| 120 | + |
| 121 | + /** @inheritdoc */ |
| 122 | + protected async buildImplementation(): Promise<Transaction> { |
| 123 | + const { methodName, args, gas, deposit } = this.contractCallWrapper.getParams(); |
| 124 | + assert(gas, new BuildTransactionError('gas is required before building fungible token transfer')); |
| 125 | + assert(deposit, new BuildTransactionError('deposit is required before building fungible token transfer')); |
| 126 | + |
| 127 | + if (!this._actions || this._actions.length === 0 || this._actions[0].functionCall?.methodName !== methodName) { |
| 128 | + super.action(NearAPI.transactions.functionCall(methodName, args, BigInt(gas), BigInt(deposit))); |
| 129 | + } |
| 130 | + const tx = await super.buildImplementation(); |
| 131 | + tx.setTransactionType(TransactionType.Send); |
| 132 | + return tx; |
| 133 | + } |
| 134 | +} |
0 commit comments