[英]erc20 claim token transfer function doesn't work
I have this contract and trying to call the claimFreeToken function.我有这份合同并试图调用 claimFreeToken 函数。 Contract already doesn't have enough tokens but the function doesn't return an error and also token doesn't receive.
合约已经没有足够的代币,但函数没有返回错误,也没有收到代币。 Where did I overlook it?
我在哪里忽略了它?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TestToken is ERC20 {
constructor(uint256 initialSupply) ERC20("Test Token", "TET") {
_mint(msg.sender, initialSupply * (10**decimals()));
}
function claimFreeToken() public payable {
transfer(msg.sender, 1000 * (10**decimals()));
}
}
Your implementation makes token transferred from your wallet to your wallet on claiming, not from the contract.您的实施使代币在声明时从您的钱包转移到您的钱包,而不是从合同中转移。
The transfer
function in ERC20 will send from the msg.sender
to the specified address. ERC20 中的
transfer
函数将从msg.sender
发送到指定地址。 So, in this case you are transferring from msg.sender
to msg.sender
.因此,在这种情况下,您将从
msg.sender
转移到msg.sender
。
The correct implementation should be like this,正确的实现应该是这样的,
function claimFreeToken() public payable {
_transfer(address(this), msg.sender, 1000 * (10**decimals()));
}
Read more: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L113阅读更多: https ://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L113
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.