0

I can not transfer ERC20 tokens from my address to the contract. But I can transfer from the contract to other addresses.

my account(msg.sender) has 10 link(ERC20) tokens . and i want to send these tokens to contract

i am using remix and rinkeby network - metamask this is my code

// SPDX-License-Identifier: GPL-3.
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; contract TransferTokens { function transferToMe(address _token, uint _amount) public { //token is chainlink_ERC20 address //i have links in mo account IERC20(_token).transferFrom(msg.sender, address(this), _amount); }

}

the error is The transaction execution will likely fail

  • If you're calling transferFrom function, you're doing so on behalf of the msg.sender. This action needs to have prior approval using approve or increaseAllowance function as defined in: https://docs.openzeppelin.com/contracts/4.x/api/token/erc20#ERC20-approve-address-uint256- – Stanislav Svědiroh Feb 18 '22 at 18:38
  • @StanislavSvědiroh Hello thanks for your answer. i used approve before transferFrom but it doesn't work still – srs.aslani Feb 18 '22 at 18:54
  • There is something wrong with your logic. Msg.sender will be address of the smart contract (transferTokens), and address(this) is also the address of the smart contract (transferTokens), so you are transfering tokens from an address to the same address. – João Paulo Morais Feb 19 '22 at 01:38
  • 1
    @JoãoPauloMorais You're mistaken. transferToMe function is called externally (wallet or a different contract) hence msg.sender is not TransferTokens. As far as I understood the question, this function works. The thing OP needs to accomplish is to successfully send tokens TO this smart contract. – Stanislav Svědiroh Feb 19 '22 at 09:55
  • TransferToMe is called externally, but then the Smart contract calls another contract, so the msg.sender will be the address of the contract. Please look at https://ethereum.stackexchange.com/questions/1891/whats-the-difference-between-msg-sender-and-tx-origin – João Paulo Morais Feb 19 '22 at 12:50
  • Stanislav Svědiroh My answer solved . thank you create an answer i want to rate you my friend – srs.aslani Feb 24 '22 at 07:31

0 Answers0