I am trying to execute a function from the truffle console and getting the error without much information.
contract LPTokenWrapper {
IERC20 public lp_tkn = IERC20(0xf7a35Eef60dC35fa2D3188Dfb22e635E4308fc8b);
function stake(uint256 amount) public {
_totalSupply = _totalSupply.add(amount);
_balances[msg.sender] = _balances[msg.sender].add(amount);
tkn.safeTransferFrom(msg.sender, address(this), amount);
}
}
from truffle console doing(with the same account which has lp_tkn) lp_wrp.stake(web3.utils.toWie("1"));
https://testnet.bscscan.com/tx/0x2eba99c94ba96513794da6e10e1c88d80e3ca619b13fc994428abc0e6fa2fd4c
Best Answer
It is possible that
address(this)
is not authorised totransferFrom()
funds that belong tomsg.sender
. It can't simply help itself to someone else's tokens.In the ERC20 standard, the user is required to
approve()
- allow the receiver, which is the contract, to take a certain amount of tokens before the receiver can usetransferFrom()
. This is usually coordinated by the UI so the user just signs the authorizing transaction.Importantly, the transaction must be from the user directly, not via a smart contract, sent directly to the ERC20 token contract.
The Open Zeppelin implementation:
So, the user will send a transaction to the ERC20 contract to
approve
a certain address, your contract, totransferFrom()
a certain amount of tokens from the user - the allowance. Then, your contract will be able totransferFrom()
.Hope it helps.