0

I am using remix and ganache and metamask and I encounter a confusing error. I compiled and deployed USDT at

0xdac17f958d2ee523a2206206994597c13d831ec7

on ganache with no problem. In order to do some test on it I wrote following contract in remix:

pragma solidity ^0.6.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"; import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/SafeERC20.sol";

contract testTether {

using SafeERC20 for IERC20;
IERC20 public token;

constructor(IERC20 _token) public {
    token = _token;
}

function transfer(address to, uint256 amount) public {
    token.safeTransfer(to, amount);
}

}

when I try to call transfer function I get:

Gas estimation failed: Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Returned error: VM Exception while processing transaction: revert SafeERC20: low-level call failed

what is the problem?

Thanks in advance.

Kourosh
  • 3
  • 1
  • 3
  • You are probably specifying an amount larger than the balance of the from account. – goodvibration Sep 08 '20 at 17:13
  • @goodvibration Thanks for reply but no, it is not the problem and from account has sufficient balance. I am wondering if I should approve amount of token before transfer for owner? As I read here for token exchange two transaction is needed. – Kourosh Sep 08 '20 at 19:38
  • You should approve before transferFrom, not before transfer. – goodvibration Sep 08 '20 at 19:56
  • 1
    As implemented testTether.transfer will transfer tokens from testTether contract balance. It doesn't move tokens from msg.sender. – Ismael Sep 09 '20 at 00:29
  • @Ismael, Thanks, yes you are right. It was because of lack of enough balance in testTether contract. Is there any way to transfer token from one account to another directly using a smart contract function like my code above in a way that transaction fee be paid by sender account not smart contract? – Kourosh Sep 10 '20 at 14:21
  • @Kourosh It is possible the token's owner has to call approve with the contract address and amount to spend, then the contract can call transferFrom on behalf of the token's owner. See this question https://ethereum.stackexchange.com/questions/46457/send-tokens-using-approve-and-transferfrom-vs-only-transfer for more details. – Ismael Sep 10 '20 at 19:22

0 Answers0