From cab4d5b1a73e41bbf6eea74a2af9d7f52c56e447 Mon Sep 17 00:00:00 2001 From: "Miquel A. Cabot" Date: Mon, 16 May 2022 15:48:40 +0200 Subject: [PATCH] restore estApprove() function and add estTransfer() function --- src/utils/TokenUtils.ts | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index 22d20141..c8008c3f 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,4 +1,5 @@ import Decimal from 'decimal.js' +import { Contract } from 'web3-eth-contract' import { amountToUnits, estimateGas, @@ -10,6 +11,29 @@ import LoggerInstance from './Logger' import { TransactionReceipt } from 'web3-core' import Web3 from 'web3' +/** + * Estimate gas cost for approval function + * @param {String} account + * @param {String} tokenAddress + * @param {String} spender + * @param {String} amount + * @param {String} force + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ +export async function estApprove( + web3: Web3, + account: string, + tokenAddress: string, + spender: string, + amount: string, + contractInstance?: Contract +): Promise { + const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) + + return estimateGas(account, tokenContract.methods.approve, spender, amount) +} + /** * Approve spender to spent amount tokens * @param {String} account @@ -58,6 +82,29 @@ export async function approve( return result } +/** + * Estimate gas cost for transfer function + * @param {String} account + * @param {String} tokenAddress + * @param {String} recipient + * @param {String} amount + * @param {String} force + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ +export async function estTransfer( + web3: Web3, + account: string, + tokenAddress: string, + recipient: string, + amount: string, + contractInstance?: Contract +): Promise { + const tokenContract = contractInstance || new web3.eth.Contract(minAbi, tokenAddress) + + return estimateGas(account, tokenContract.methods.transfer, recipient, amount) +} + /** * Moves amount tokens from the caller’s account to recipient. * @param {String} account