Solidity
如何從我的智能合約中轉移 Solidity 中的任何 ERC20 代幣?
我無法在我的智能合約中轉移 ERC20 代幣。程式碼如下。看一下函式 hodl,我想將任何代幣存入這個智能合約,但是雖然我進行了交易,但沒有代幣被轉移,我不知道為什麼,因為我呼叫了 transferFrom 函式。在絕望中也嘗試過轉移,但這可能僅適用於代幣智能合約轉移。
我在這裡嘗試開發的是一個智能合約,它將接受代幣存款並持有它直到使用者在進行交易時定義的時間限製到期。
pragma solidity ^0.4.19; contract ERC20Interface { function totalSupply() public constant returns (uint); function balanceOf(address tokenOwner) public constant returns (uint balance); function allowance(address tokenOwner, address spender) public constant returns (uint remaining); function transfer(address to, uint tokens) public returns (bool success); function approve(address spender, uint tokens) public returns (bool success); function transferFrom(address from, address to, uint tokens) public returns (bool success); event Transfer(address indexed from, address indexed to, uint tokens); event Approval(address indexed tokenOwner, address indexed spender, uint tokens); } contract hodlForYouContractV3 { event Hodl(address indexed hodler, address token, uint amount, uint timeLimit); event PanicSell(address indexed hodler, address token, uint amount, uint timediff); event Withdrawal(address indexed hodler, address token, uint amount); struct Hodler { uint etherBalance; address hodler; mapping(address => Token) tokens; } struct Token { bytes32 symbol; uint tokenBalance; address tokenAddress; uint timeLimit; } mapping(address => Hodler) public hodlers; function hodl(address token, byte tokenSymbol, uint256 amount, uint256 timeLimit) { hodlers[msg.sender] = Hodler(0, msg.sender); Hodler hodler = hodlers[msg.sender]; hodler.tokens[token] = Token(tokenSymbol, amount, token, timeLimit); // hodler.tokens[token].timeLimit = timeLimit; // hodler.tokens[token].tokenBalance = amount; ERC20Interface(token).approve(msg.sender, amount); ERC20Interface(token).transfer(this, amount); ERC20Interface(token).transferFrom(msg.sender, this, amount); Hodl(msg.sender, token, amount, timeLimit); } function withdraw(address token) { Hodler hodler = hodlers[msg.sender]; require(block.timestamp > hodler.tokens[token].timeLimit); uint amount = hodler.tokens[token].tokenBalance; hodler.tokens[token].tokenBalance = 0; ERC20Interface(token).approve(msg.sender, amount); ERC20Interface(token).transferFrom(this, msg.sender, amount); Withdrawal(msg.sender, token, amount); } function panicSell(address token) { //This function should have a fee for quicker withdrawing without waiting Hodler hodler = hodlers[msg.sender]; uint amount = hodler.tokens[token].tokenBalance; hodler.tokens[token].tokenBalance = 0; ERC20Interface(token).approve(msg.sender, amount); ERC20Interface(token).transferFrom(this, msg.sender, amount); PanicSell(msg.sender, token, amount, hodler.tokens[token].timeLimit - block.timestamp); } }
這行沒有意義:
ERC20Interface(token).approve(msg.sender, amount);
通話中有兩個參與者
approve
:代幣所有者和代幣消費者。代幣所有者呼叫
approve(spender, amount)
,以便花費者可以花費這些代幣。在這裡,合約是代幣所有者,呼叫函式的使用者是支出者。
我認為你想要的是讓使用者在呼叫
approve(contract, amount)
之前呼叫hodl
,然後你想要token.transferFrom(msg.sender, this, amount)
將這些令牌從使用者轉移到合約。