iI read a lot of documentation in here and another web pages, iI have a question and iI so tried to solved but nothing works for me.
iI am using this example : https://forum.openzeppelin.com/t/example-on-how-to-use-erc20-token-in-another-contract/1682this example.
WorksIt works fine with truffle, but iI don't know what iI need to do to make this workswork fine using metamask.
The problem is the next :
- Create a ERC20 Token // works
- Import the IRC20 Token Contract to Another Contract Named "TokenReceiver" // iI think iI do correctly, iI only copy and paste to test in remix ethereum.
- Deploy , Use doStuff function into Thethe new Contract "TokenReceiver" // Error here
Error Says The error says:
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? gas required exceeds allowance (10000000) or always failing transaction
The code is the same :
pragma solidity ^0.5.0;
import "./IERC20.sol";
/**
* @title TokenReceiver
* @dev Very simple example of a contract receiving ERC20 tokens.
*/
contract TokenReceiver {
IERC20 private _token;
event DoneStuff(address from);
/**
* @dev Constructor sets token that can be received
*/
constructor (IERC20 token) public {
_token = token;
}
/**
* @dev Do stuff, requires tokens
*/
function doStuff() external {
address from = msg.sender;
_token.transferFrom(from, address(this), 1000);
emit DoneStuff(from);
}
}
iI read a lot but iI really know iI am doing something wrong but iI don't know what it is , reading I see that I need to approve the function before using transfer, but nothing works for me, iI am trying do this and the error is the same and i dontI don't see the approve window with metamask :
function doStuff() external {
address from = msg.sender;
_token.approve(from,1000); // i trying adding this line
_token.transferFrom(from, address(this), 1000);
emit DoneStuff(from);
}
Somebody Helphelp me pleasee please, it is hard tofor me to understand whatswhat's happening but iI really wannawant to know what iI am doing wrong :(.