Search code examples
soliditychainlink

How can i get my token funded with LINK from user through solidity?


My contract needs LINK token in order to function.

I want to let users fund LINK tokens to the contract via a function on the contract, and then do some logic for the user based on their funding.

How can i make this possible within the contract? I've tried to do calls like this.

LINK.balanceOf(walletaddress) does work, (It gets the link amount that's in the wallet). However, this function below does not work for some reason. It goes through and all, but with like empty data.

Metamask shows differently when i do the same call from their front-end button. (I assume it does the same as remix does)

https://testnet.bscscan.com/token/0x84b9B910527Ad5C03A9Ca831909E21e236EA7b06#readContract

Here is how i try to get my contracts approval.

function approveTransfer(uint256 amount) public returns(string memory) {
        uint256 approveAmt = amount * 10**18;
        LINK.approve(_msgSender(),approveAmt);
        approvedAmount = approveAmt;
    }

Solution

  • Not possible from within your contract, unless the external contract explicitly allows it or contains a security flaw using deprecated tx.origin instead of msg.sender.

    See the last paragraph and code snippet in this answer to see how it could be misused if it were possible.


    When your contract executes LINK's function, msg.sender in the LINK contract is now your contract - not the user.

    Both transfer() and approve() (in order to call transferFrom() later) functions rely on msg.sender, which you need to be the user. But it's not - it's your contract.


    When your contract delegates a call to LINK's function, the state changes are stored in your contract - not in the LINK.

    But you'd need to store the state changes in the LINK contract, so this is not an option either.