How to add transfer function on a contract to a user using openZeppelin?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TokenSample is ERC20 {
constructor() ERC20("TokenSample", "SMPL") {
_mint(msg.sender, 21000000 * 10 ** decimals());
}
}
When I deploy the above contract, I get a transfer form with 2 parameter (recipient, amount). It's okay.
But because of one reason, I need to implement my own transfer function inside the contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TokenSample is ERC20 {
constructor() ERC20("TokenSample", "SMPL") {
_mint(msg.sender, 21000000 * 10 ** decimals());
}
function getToken(uint256 _amount) external returns(bool) {
// my internal logic here
transfer(msg.sender, _amount);
return true;
}
}
From the code I got error : ERC20: transfer amount exceeds balance
I am not really sure but I guest it's because the token is belong to the owner not belong to the contract. What is the right way to implement that getToken method?