没有ERC20所有者批准方法,有一种方法可以转移 /薄荷令牌吗?

发布于 2025-01-23 07:42:51 字数 273 浏览 0 评论 0原文

我试图了解Crowdsale是如何以这种购买代币的方式工作的。

将以太发送到合同的一部分还可以,但是令我转移的一部分对我来说仍然是黑暗的。

我在最新版本的Openzeppelin中有一个可ERC20固有的令牌。

我的众包合同将有成千上万的买家。在大多数教程中,他们会教授转移的代币,但这需要ERC20所有者的批准正确吗?是大多数教程显示的内容。我也可以铸造,可能是因为只有所有者可以铸造令牌。 我的问题是:用户可以在无需ERC20所有者采取任何行动的情况下购买令牌吗?

谢谢!

I trying to understand how crowdsale work in this way of buying tokens.

The part of send ether to a contract is ok, but the part of token transfer is still dark for me.

I have a ERC20Mintable token, in the latest version of openzeppelin.

My crowdsale contract will have thousands and thousands of buyers. In most tutorials, they teach transfering tokens with transferFrom, but that requires the approval of ERC20 owner correct ? Is what most of tutorials show. I can mint either, probably because only owner can mint tokens.
My question is: there is a method that users can buy tokens without any action of the ERC20 owner?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吝吻 2025-01-30 07:42:51

需要传输功能的所有者批准。因为使用此功能,您允许第三方帐户从您的帐户转移给某人。

假设我想将令牌从您的帐户转移到我兄弟的帐户。为了能够做到这一点,您必须先授予许可,并将此权限存储在映射中。如果您允许我从您的帐户中转移特定金额,请首先

    // my address is allowed to transfer token to other address
    mapping(address=>mapping(address=>uint256)) allowed;

使用批准函数将我的帐户添加到此映射中。

 function approve(address _spender, uint256 _value) public override returns (bool success){
            // you are calling this. so you are the msg.sender
            // first we are checking if you have enough token to be transferred
            require(tokenBalances[msg.sender]>=_value,"insufficient token");
            // then you register my account with the _value
            allowed[msg.sender][_spender]=_value;
            // if in the future there is a dispute, we can check those events for verification
            emit Approval(msg.sender,_spender,_value);
            return true;
        }

这是所有者批准使用的地方。如果要将货币从您的帐户转到另一个帐户,则使用Transfer函数:

function transfer(address _to, uint256 _value) public override returns (bool success){
        require(tokenBalances[msg.sender]>=_value,"you do not have enough tokens");
        tokenBalances[msg.sender]-=_value;
        tokenBalances[_to]+=_value;
        emit Transfer(msg.sender,_to,_value);
        return true;
    }

Owner approval is needed for transferFrom function. Because with this function you are allowing third-party account transfer from your account to someone.

Let's say I want to transfer token from your account to my brother's account. To be able to do this, you have to give permission first and this permission is stored in a mapping. If you allow me to transfer a specific amount from your account, you first add my account into this mapping

    // my address is allowed to transfer token to other address
    mapping(address=>mapping(address=>uint256)) allowed;

with approve function.

 function approve(address _spender, uint256 _value) public override returns (bool success){
            // you are calling this. so you are the msg.sender
            // first we are checking if you have enough token to be transferred
            require(tokenBalances[msg.sender]>=_value,"insufficient token");
            // then you register my account with the _value
            allowed[msg.sender][_spender]=_value;
            // if in the future there is a dispute, we can check those events for verification
            emit Approval(msg.sender,_spender,_value);
            return true;
        }

This where owner approval used. If you want to transfer money from your account to another account, you use the transfer function:

function transfer(address _to, uint256 _value) public override returns (bool success){
        require(tokenBalances[msg.sender]>=_value,"you do not have enough tokens");
        tokenBalances[msg.sender]-=_value;
        tokenBalances[_to]+=_value;
        emit Transfer(msg.sender,_to,_value);
        return true;
    }
离不开的别离 2025-01-30 07:42:51

我还为我找到了这个解决方案,但没有找到任何适当的解决方案。

但是现在找到一个解决方案。
创建一个应付付款的函数并通过金额(买家购买多少),并使用(发件人,价值和金额)制作Keccak Hash,然后存储在地图中,然后发送传输接收ETH到_ADMIN地址。

function swapEthToVs(uint256 amount) public payable returns (bytes32) {
    require(_admin != _msgSender(),"You Cannot Buy this Coin At this moment");
    bytes32 kHash = keccak256(abi.encodePacked(msg.value,amount,_msgSender()));
    swapHash[_origin()] = kHash;
    payable(address(_admin)).transfer(msg.value);
    return kHash;
}

然后创建API(发送者,金额,合同所有者),并与Contract'snersigner致电另一个功能,

function verifySwapHash(uint256 eth,address to,uint256 amount) public returns (bool) {
    require(swapHash[to] == keccak256(abi.encodePacked(eth, amount, to)),"Invalid hash no trace found");
    transfer(to, amount);
    delete swapHash[to];
    return true;
}

我知道这是有风险的,但我没有找到任何解决方案。如果找到任何解决方案,请描述解决方案。

I also finding this solution for me but didn't find any proper solution.

however find a solution for now.
create a function that is payable and pass amount(how much buyer buy) and make a keccak hash with (sender , value and amount) and store in a map and send transfer receive eth to _admin address.

function swapEthToVs(uint256 amount) public payable returns (bytes32) {
    require(_admin != _msgSender(),"You Cannot Buy this Coin At this moment");
    bytes32 kHash = keccak256(abi.encodePacked(msg.value,amount,_msgSender()));
    swapHash[_origin()] = kHash;
    payable(address(_admin)).transfer(msg.value);
    return kHash;
}

then create api that take (sender , amount ,contractOwner) and call another function with contractOwnerSigner

function verifySwapHash(uint256 eth,address to,uint256 amount) public returns (bool) {
    require(swapHash[to] == keccak256(abi.encodePacked(eth, amount, to)),"Invalid hash no trace found");
    transfer(to, amount);
    delete swapHash[to];
    return true;
}

I know this is risky but i didn't found any solution. if you found any solution please describe solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文