|
| 1 | +import { BaseCoin as CoinConfig, NetworkType } from '@bitgo/statics'; |
| 2 | +import { InvalidParameterValueError, TransactionType } from '@bitgo/sdk-core'; |
| 3 | +import { |
| 4 | + AddressHashMode, |
| 5 | + AddressVersion, |
| 6 | + ClarityValue, |
| 7 | + ContractCallPayload, |
| 8 | + FungibleConditionCode, |
| 9 | + makeStandardFungiblePostCondition, |
| 10 | + PostCondition, |
| 11 | + PostConditionMode, |
| 12 | +} from '@stacks/transactions'; |
| 13 | +import BigNum from 'bn.js'; |
| 14 | + |
| 15 | +import { AbstractContractBuilder } from './abstractContractBuilder'; |
| 16 | +import { Transaction } from './transaction'; |
| 17 | +import { |
| 18 | + functionArgsToTokenTransferParams, |
| 19 | + getSTXAddressFromPubKeys, |
| 20 | + isValidAddress, |
| 21 | + isValidContractFunctionName, |
| 22 | +} from './utils'; |
| 23 | +import { TokenTransferParams } from './iface'; |
| 24 | +import { FUNCTION_NAME_TRANSFER } from './constants'; |
| 25 | + |
| 26 | +export class FungibleTokenTransferBuilder extends AbstractContractBuilder { |
| 27 | + private _fungibleTokenTransferParams: TokenTransferParams; |
| 28 | + private _tokenName: string; |
| 29 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 30 | + super(_coinConfig); |
| 31 | + } |
| 32 | + |
| 33 | + initBuilder(tx: Transaction): void { |
| 34 | + super.initBuilder(tx); |
| 35 | + this._fungibleTokenTransferParams = functionArgsToTokenTransferParams( |
| 36 | + (tx.stxTransaction.payload as ContractCallPayload).functionArgs |
| 37 | + ); |
| 38 | + this.contractAddress(this._contractAddress); |
| 39 | + this.contractName(this._contractName); |
| 40 | + this.functionName(this._functionName); |
| 41 | + this.functionArgs(this._functionArgs); |
| 42 | + this._postConditionMode = PostConditionMode.Deny; |
| 43 | + this._postConditions = this.tokenTransferParamsToPostCondition(this._fungibleTokenTransferParams); |
| 44 | + } |
| 45 | + |
| 46 | + /** @inheritdoc */ |
| 47 | + protected async buildImplementation(): Promise<Transaction> { |
| 48 | + await super.buildImplementation(); |
| 49 | + this.transaction.setTransactionType(TransactionType.Send); |
| 50 | + return this.transaction; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Function to check if a transaction is a fungible token contract call |
| 55 | + * |
| 56 | + * @param {ContractCallPayload} payload |
| 57 | + * @returns {Boolean} |
| 58 | + */ |
| 59 | + public static isFungibleTokenTransferContractCall(payload: ContractCallPayload): boolean { |
| 60 | + return FUNCTION_NAME_TRANSFER === payload.functionName.content; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set the token name |
| 65 | + * |
| 66 | + * @param {String} tokenName name of the token (@define-fungible-token value) |
| 67 | + * @returns {FungibleTokenTransferBuilder} This token transfer builder |
| 68 | + */ |
| 69 | + tokenName(tokenName: string): this { |
| 70 | + this._tokenName = tokenName; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Validate contract address |
| 76 | + * |
| 77 | + * @param {String} address contract address |
| 78 | + * @returns {FungibleTokenTransferBuilder} This token transfer builder |
| 79 | + */ |
| 80 | + contractAddress(address: string): this { |
| 81 | + if (!isValidAddress(address)) { |
| 82 | + throw new InvalidParameterValueError('Invalid address'); |
| 83 | + } |
| 84 | + this._contractAddress = address; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Validate contract name |
| 90 | + * |
| 91 | + * @param {String} name contract name |
| 92 | + * @returns {FungibleTokenTransferBuilder} This token transfer builder |
| 93 | + */ |
| 94 | + contractName(name: string): this { |
| 95 | + if (name.length === 0) { |
| 96 | + throw new InvalidParameterValueError('Invalid name'); |
| 97 | + } |
| 98 | + this._contractName = name; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Validate function name |
| 104 | + * |
| 105 | + * @param {String} name function name |
| 106 | + * @returns {FungibleTokenTransferBuilder} This token transfer builder |
| 107 | + */ |
| 108 | + functionName(name: string): this { |
| 109 | + if (name.length === 0) { |
| 110 | + throw new InvalidParameterValueError('Invalid name'); |
| 111 | + } |
| 112 | + if (!isValidContractFunctionName(name)) { |
| 113 | + throw new InvalidParameterValueError(`${name} is not supported contract function name`); |
| 114 | + } |
| 115 | + this._functionName = name; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Validate function arguments |
| 121 | + * |
| 122 | + * @param {ClarityValue[]} args array of clarity value as arguments |
| 123 | + * @returns {FungibleTokenTransferBuilder} This token transfer builder |
| 124 | + */ |
| 125 | + functionArgs(args: ClarityValue[]): this { |
| 126 | + if (args.length < 3) { |
| 127 | + throw new InvalidParameterValueError('Invalid number of arguments'); |
| 128 | + } |
| 129 | + this._functionArgs = args; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Function to convert token transfer params to post condition |
| 135 | + * |
| 136 | + * @param {TokenTransferParams} tokenTransferParams |
| 137 | + * @returns {PostCondition[]} returns stx fungible post condition |
| 138 | + */ |
| 139 | + private tokenTransferParamsToPostCondition(tokenTransferParams: TokenTransferParams): PostCondition[] { |
| 140 | + const amount: BigNum = new BigNum(tokenTransferParams.amount); |
| 141 | + return [ |
| 142 | + makeStandardFungiblePostCondition( |
| 143 | + getSTXAddressFromPubKeys( |
| 144 | + this._fromPubKeys, |
| 145 | + this._coinConfig.network.type === NetworkType.MAINNET |
| 146 | + ? AddressVersion.MainnetMultiSig |
| 147 | + : AddressVersion.TestnetMultiSig, |
| 148 | + this._fromPubKeys.length > 1 ? AddressHashMode.SerializeP2SH : AddressHashMode.SerializeP2PKH, |
| 149 | + this._numberSignatures |
| 150 | + ).address, |
| 151 | + FungibleConditionCode.Equal, |
| 152 | + amount, |
| 153 | + `${this._contractAddress}.${this._contractName}::${this._tokenName}` |
| 154 | + ), |
| 155 | + ]; |
| 156 | + } |
| 157 | +} |
0 commit comments