简体   繁体   English

其他非eth erc-20代币转移操作的固定合同

[英]Solidity contract for other non eth erc-20 token transfer operations

I used the official code to create a token contract,and I created a new contract. 我使用官方代码创建了代币合同,然后又创建了新合同。 Now I hope to use this new contract to invoke token contract, transfer token from A account to B account, and encounter the problem of no transferable quota. 现在,我希望使用此新合同来调用令牌合同,将令牌从A帐户转移到B帐户,并遇到无法转让配额的问题。

pragma solidity ^0.4.17;

interface Token {
    function approve(address spender, uint256 value) public returns (bool);
    function transferFrom(address from, address to, uint256 value) public returns (bool);   
}


/**
 * The TrxCoin contract does this and that...
 */
contract TrxCoin {

    Token token = Token(0xAc08fe3C9F442990C52904dE997D9972499bD3E6);

    function getContractAddr() view public returns (address) {
        return this;
    }

    function approve(address spender, uint256 value) public {
        require(token.approve(spender, value));
    }

    function transfer(address _to, uint value) public payable {
        require(token.transferFrom(msg.sender, _to, value));
    }
}

When I use the token contract to call the approve method directly, I can transfer through the new contract, but I can't allocate the quota directly by calling the approve method with the new contract. 当我使用令牌合同直接调用approve方法时,可以通过新合同进行转移,但是不能通过使用新合同调用approve方法来直接分配配额。

Why is this? 为什么是这样? Thank you for the answer! 谢谢您的回答!

You're hitting this problem because you're attempting to approve the transfer of tokens from your contract, not the actual owner's address. 之所以遇到此问题,是因为您试图批准从合同(而不是实际所有者的地址)中转移令牌。

The ERC20 approve method writes to it's state that the requester is giving permission to let the spender perform the transaction. ERC20 approve方法将其写入状态,即请求者正在授予允许支出者执行交易的状态。 It does this with something like allowed[msg.sender][_spender] = _value; 它使用诸如allowed[msg.sender][_spender] = _value; .

When you are invoking the token contract (C) from your account (A), msg.sender is set to address(A) . 从帐户(A)调用令牌合约(C)时, msg.sender设置为address(A) However, when you're calling the token contract from TrxCoin, you've now introduced a new contract (B) as a middleman. 但是,当您从TrxCoin调用令牌合约时,您现在已经引入了一个新合约(B)作为中间人。 The chain is now A->B->C. 现在,链为A-> B-> C。 In this case, the msg.sender that C receives is now address(B). 在这种情况下,C收到的msg.sender现在是地址(B)。 At this point, the Token contract state is still set to not allow any tokens owned by A to be transferred to spender . 此时,代币合约状态仍设置为不允许A拥有的任何代币转让给spender

There's no reason to delegate through the TrxCoin contract for the approval. 没有理由通过TrxCoin合同委派批准。 Just invoke the Token contract directly. 只需直接调用令牌合约即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM