admin管理员组文章数量:1432620
I was developing a front end for a smart contract using React JS and ethers.js. The smart contract has the following function.
function buyPremiumSpin(uint256 spins) external {
require(spins > 0, "You can't get 0 premium spins!");
uint256 cost = premiumSpinPrize * spins;
require(wblock.balanceOf(msg.sender) >= cost, "Not enough WBLOCKS!");
wblock.transferFrom(msg.sender, address(teamWallet), cost);
premiumSpins[msg.sender] += spins;
}
I tried to call the function using
const signer = provider.getSigner();
const contract = new ethers.Contract(CONTRACT_ADDRESS, abi, signer);
const transaction = await contract.buyPremiumSpin(
spins,
{gasLimit: gasLimit}
);
But the transaction failed with message
Fail with error 'ERC20: insufficient allowance'
After some research I found the approve function in erc20. That user has to approve an address to let them transfer tokens. But I couldn't implement it using JavaScript.
I tried
contract.approve(account, premiumSpinPrize, {gasLimit:gasLimit})
But this failed due to the JavaScript error that approve
wasn't a function.
What is the correct way to execute the smart contract function? Also it would be great if the user could approve maximum amount so they don't have to approve it every time.
I was developing a front end for a smart contract using React JS and ethers.js. The smart contract has the following function.
function buyPremiumSpin(uint256 spins) external {
require(spins > 0, "You can't get 0 premium spins!");
uint256 cost = premiumSpinPrize * spins;
require(wblock.balanceOf(msg.sender) >= cost, "Not enough WBLOCKS!");
wblock.transferFrom(msg.sender, address(teamWallet), cost);
premiumSpins[msg.sender] += spins;
}
I tried to call the function using
const signer = provider.getSigner();
const contract = new ethers.Contract(CONTRACT_ADDRESS, abi, signer);
const transaction = await contract.buyPremiumSpin(
spins,
{gasLimit: gasLimit}
);
But the transaction failed with message
Fail with error 'ERC20: insufficient allowance'
After some research I found the approve function in erc20. That user has to approve an address to let them transfer tokens. But I couldn't implement it using JavaScript.
I tried
contract.approve(account, premiumSpinPrize, {gasLimit:gasLimit})
But this failed due to the JavaScript error that approve
wasn't a function.
What is the correct way to execute the smart contract function? Also it would be great if the user could approve maximum amount so they don't have to approve it every time.
Share Improve this question edited Sep 5, 2022 at 15:30 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Sep 3, 2022 at 7:46 Shameel KadannamannaShameel Kadannamanna 3864 silver badges14 bronze badges1 Answer
Reset to default 4The approval happens on the ERC20 smart contract to give approval to your smart contract to spend the tokens on your behalf. You just need to grab the Token ABI and instantiate a contract instance of the Token like you did above with your contract ;)
var tokenContract = new ethers.Contract(tokenAddress, tokenABI, provider);
tokenContract.approve(<Your_Contract_Address>, amount, {gasLimit: gasLimit})
本文标签: javascriptError when calling an erc20 approve function using ethersjsStack Overflow
版权声明:本文标题:javascript - Error when calling an erc20 approve function using ethers.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745605558a2665817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论