0

I've seen similar questions but can't figure out why my code isn't working. I'm trying to make a function within a smart contract that allows the user to transfer their Goerli Chainlink token to this smart contract. The approveLink function seems to work fine. But, the transferToContract function returns a warning about "Gas estimation error" and fails if sent. My understanding is that first, the user will need to call the approveLink function and specify the amount of the Chainlink token they're approving to transfer to the smart contract. Then, the user can call the approveLink function, specify an amount up to what's approved, and the chainlink token will be sent to the smart contract.

The user's wallet does have adequate Link on the Goerli network.

Does anyone know what I'm doing wrong?

pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol";

contract fundLink {
    LinkTokenInterface LINKTOKEN;

    address link_token_contract = 0x326C977E6efc84E512bB9C30f76E30c160eD06FB;

    constructor() {
        LINKTOKEN = LinkTokenInterface(link_token_contract);
    }

    // 1000000000000000000 = 1 LINK
    function transferToContract(uint256 amount) external {
        LINKTOKEN.transferFrom(
            msg.sender,
            address(this),
            amount);
    }

    function approveLink(uint256 amount) external {
        LINKTOKEN.approve(address(this), amount);
    }

1 Answer 1

0

It looks like your transferToContract function is trying to transfer Link tokens from the message sender to the contract, but it is not checking whether the sender has sufficient Link tokens or whether the contract has been approved to receive the specified amount.

To fix this, you can add a require statement to check whether the contract has been approved to receive the specified amount of Link tokens from the message sender, and whether the message sender has sufficient Link tokens to transfer. You can use the allowance and balanceOf functions from the LinkTokenInterface to check this.

Here is an example of how your transferToContract function could be updated to include these checks:

function transferToContract(uint256 amount) external {
require(LINKTOKEN.allowance(msg.sender, address(this)) >= amount, "Contract not approved to receive specified amount of Link tokens");
require(LINKTOKEN.balanceOf(msg.sender) >= amount, "Sender does not have sufficient Link tokens to transfer");

LINKTOKEN.transferFrom(
    msg.sender,
    address(this),
    amount);
}

This will ensure that the transfer only occurs if the contract has been approved to receive the specified amount of Link tokens from the message sender, and the message sender has sufficient Link tokens to transfer.

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.