0

I'm creating a smart contract where users can create NFT raffles. I will be using Chainlink VRF for getting provably fair results. For this, the user who creates the raffle needs to supply the contract with LINK tokens. I'm attempting to transfer these tokens using an allowance.

  function initRaffle(address _tokenContract, uint256 _tokenId, uint256 _ticketPrice) external {
      require(_ticketPrice > 0, "Ticket price must be bigger than 0");
      require(LINKToken.balanceOf(msg.sender) >= ChainlinkFee, "Insufficient LINK supplied");
      require(LINKToken.allowance(msg.sender, address(this)) >= ChainlinkFee, "Allowance failed");

Running initRaffle results in Allowance failed. I've checked and the LINKToken.balanceOf(msg.sender) is bigger than the fee, so that shouldn't be the problem. The LINKToken.balanceOf(address(this)) is 0.

What's going wrong? And how do I create a working function for having the user transfer (fee amount) link tokens to the contract.

1
  • Did you set the allowance for how many LINK tokens the contract can take from you? By the way you should just transfer the tokens. The token's transferFrom already checks these things and you are wasting gas by checking them twice.
    – user253751
    Dec 12, 2022 at 13:16

1 Answer 1

1

Did you call approve() function of the LINK token from the msg.sender address?

Allowance is increased by calling approve() on the LINK contract to increase the allowance. If your check fails there, it means the allowance is < than ChainLink fee. So the address of the user need to first call the approve() function of the link contract.

Hope this helps!

3
  • How do I call this approve function from the smart contract / remix? I've tried LINKToken.approve(msg.sender, (0.1 * 10 ** 18)); Before the require statements, but this didn't work?
    – TRS
    Dec 12, 2022 at 13:14
  • @TRS Your account (the one giving the tokens) has to call it to say you are okay with the contract taking a certain number of tokens from you.
    – user253751
    Dec 12, 2022 at 13:17
  • Either copy the code from the etherscan page of the deployed link erc20 or use any erc20 compliant contract and use "load contract from address". But you need to call the approve() function from the user address Dec 12, 2022 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.