javascript - Error when calling an erc20 approve function using ethers.js - Stack Overflow

I was developing a front end for a smart contract using React JS and ethers.js. The smart contract has

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

The 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})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745605559a4635652.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信