mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #1387 from oceanprotocol/add-transfer-function-to-tokenutils
Add transfer() function to TokenUtils
This commit is contained in:
commit
fda1203068
@ -39,7 +39,7 @@ export async function estApprove(
|
||||
* @param {String} account
|
||||
* @param {String} tokenAddress
|
||||
* @param {String} spender
|
||||
* @param {String} amount (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 {number} tokenDecimals optional number of decimals of the token
|
||||
*/
|
||||
@ -76,12 +76,73 @@ export async function approve(
|
||||
})
|
||||
} catch (e) {
|
||||
LoggerInstance.error(
|
||||
`ERRPR: Failed to approve spender to spend tokens : ${e.message}`
|
||||
`ERROR: Failed to approve spender to spend tokens : ${e.message}`
|
||||
)
|
||||
}
|
||||
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
|
||||
* @param {String} tokenAddress
|
||||
* @param {String} recipient
|
||||
* @param {String} amount amount of ERC20 tokens (not as wei)
|
||||
* @param {String} 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
|
||||
*/
|
||||
export async function transfer(
|
||||
web3: Web3,
|
||||
account: string,
|
||||
tokenAddress: string,
|
||||
recipient: string,
|
||||
amount: string
|
||||
): Promise<TransactionReceipt | string> {
|
||||
const tokenContract = new web3.eth.Contract(minAbi, tokenAddress)
|
||||
|
||||
let result = null
|
||||
const amountFormatted = await amountToUnits(web3, tokenAddress, amount)
|
||||
const estGas = await estimateGas(
|
||||
account,
|
||||
tokenContract.methods.transfer,
|
||||
recipient,
|
||||
amountFormatted
|
||||
)
|
||||
|
||||
try {
|
||||
result = await tokenContract.methods.transfer(recipient, amountFormatted).send({
|
||||
from: account,
|
||||
gas: estGas + 1,
|
||||
gasPrice: await getFairGasPrice(web3, null)
|
||||
})
|
||||
} catch (e) {
|
||||
LoggerInstance.error(`ERROR: Failed to transfer tokens : ${e.message}`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Allowance for any erc20
|
||||
* @param {Web3} web3
|
||||
|
Loading…
x
Reference in New Issue
Block a user