简体   繁体   English

erc20 认领令牌转移功能不起作用

[英]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.

相关问题 Mint功能的传输事件和ERC20智能合约的传输功能有什么区别? - what is difference between transfer event of Mint function and Transfer function of ERC20 Smart Contract? 当我明确给出要从中进行转移的地址时,为什么我的ERC20令牌从0x000000进行转移? - Why is my ERC20 token transferring from 0x000000 when Im explicitly giving an address to transfer from? .allowance 方法用于将 erc20 令牌导入元掩码 - .allowance method for importing erc20 token to metamask 了解以太坊 ERC20 代币创建代码 - Understanding Ethereum ERC20 token creation code 如何使用 web3js 发送 ERC20 令牌 - How to send an ERC20 token with web3js 如何将 ERC20 代币发送到智能合约余额? - How to send ERC20 token to smart contract balance? 使用 Moralis 获取 ERC20 代币价格 - Getting ERC20 token prices using Moralis 当我从其他合约调用 ERC20 合约中的转账 function 时出错,但是,可以使用 mint/burn - Error when I call transfer function in ERC20 contract from other contract, But, can use mint/burn 在 MetaMask 中显示自定义 ERC20 function 的交易值 - Displaying the transaction value of a custom ERC20 function in MetaMask 从 nodejs 后端的所有者帐户发送令牌(自定义 ERC20 令牌) - Send token from owner account from nodejs backend (Custom ERC20 token)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM