1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

fixed approve methods

This commit is contained in:
Bogdan Fazakas 2022-09-07 16:37:57 +03:00
parent 197a75a2f9
commit 63a17caa84

View File

@ -14,12 +14,15 @@ import { ReceiptOrEstimate } from '../@types'
/** /**
* Approve spender to spent amount tokens * Approve spender to spent amount tokens
* @param {Web3} web3
* @param {Config} config
* @param {String} account * @param {String} account
* @param {String} tokenAddress * @param {String} tokenAddress
* @param {String} spender * @param {String} spender
* @param {String} amount amount of ERC20 Datatokens (always expressed as wei) * @param {String} amount amount of ERC20 Datatokens (always expressed as wei)
* @param {boolean} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed * @param {boolean} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed
* @param {number} tokenDecimals optional number of decimals of the token * @param {number} tokenDecimals optional number of decimals of the token
* @param {boolean} estimateGas if true, returns the estimate gas cost for calling the method
*/ */
export async function approve<G extends boolean = false>( export async function approve<G extends boolean = false>(
web3: Web3, web3: Web3,
@ -36,7 +39,7 @@ export async function approve<G extends boolean = false>(
if (!force) { if (!force) {
const currentAllowence = await allowance(web3, tokenAddress, account, spender) const currentAllowence = await allowance(web3, tokenAddress, account, spender)
if (new Decimal(currentAllowence).greaterThanOrEqualTo(new Decimal(amount))) { if (new Decimal(currentAllowence).greaterThanOrEqualTo(new Decimal(amount))) {
return null return <ReceiptOrEstimate<G>>new Decimal(currentAllowence).toNumber()
} }
} }
const amountFormatted = await amountToUnits(web3, tokenAddress, amount, tokenDecimals) const amountFormatted = await amountToUnits(web3, tokenAddress, amount, tokenDecimals)
@ -62,11 +65,14 @@ export async function approve<G extends boolean = false>(
/** /**
* Approve spender to spent amount tokens * Approve spender to spent amount tokens
* @param {Web3} web3
* @param {Config} config
* @param {String} account * @param {String} account
* @param {String} tokenAddress * @param {String} tokenAddress
* @param {String} spender * @param {String} spender
* @param {String} amount amount of ERC20 tokens (always expressed as wei) * @param {String} amount amount of ERC20 tokens (always expressed as wei)
* @param {boolean} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed * @param {boolean} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed
* @param {boolean} estimateGas if true, returns the estimate gas cost for calling the method
*/ */
export async function approveWei<G extends boolean = false>( export async function approveWei<G extends boolean = false>(
web3: Web3, web3: Web3,
@ -82,7 +88,7 @@ export async function approveWei<G extends boolean = false>(
if (!force) { if (!force) {
const currentAllowence = await allowanceWei(web3, tokenAddress, account, spender) const currentAllowence = await allowanceWei(web3, tokenAddress, account, spender)
if (new BigNumber(currentAllowence).gt(new BigNumber(amount))) { if (new BigNumber(currentAllowence).gt(new BigNumber(amount))) {
return null return <ReceiptOrEstimate<G>>new Decimal(currentAllowence).toNumber()
} }
} }
let result = null let result = null
@ -205,8 +211,7 @@ export async function allowanceWei(
web3: Web3, web3: Web3,
tokenAddress: string, tokenAddress: string,
account: string, account: string,
spender: string, spender: string
tokenDecimals?: number
): Promise<string> { ): Promise<string> {
const tokenContract = new web3.eth.Contract(minAbi, tokenAddress) const tokenContract = new web3.eth.Contract(minAbi, tokenAddress)
return await tokenContract.methods.allowance(account, spender).call() return await tokenContract.methods.allowance(account, spender).call()