Skip to content

Commit

Permalink
restore estApprove() function and add estTransfer() function
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelcabot committed May 16, 2022
1 parent 0a0d96a commit cab4d5b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/utils/TokenUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Decimal from 'decimal.js'
import { Contract } from 'web3-eth-contract'
import {
amountToUnits,
estimateGas,
Expand All @@ -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<number>}
*/
export async function estApprove(
web3: Web3,
account: string,
tokenAddress: string,
spender: string,
amount: string,
contractInstance?: Contract
): Promise<number> {
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
Expand Down Expand Up @@ -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<number>}
*/
export async function estTransfer(
web3: Web3,
account: string,
tokenAddress: string,
recipient: string,
amount: string,
contractInstance?: Contract
): Promise<number> {
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
Expand Down

0 comments on commit cab4d5b

Please sign in to comment.