Remix

如何在混音中檢查 transferfrom 和 burnfrom 功能?

  • January 8, 2022

我正在創建簡單的 ERC20 令牌,其中涉及以下功能

transfer burn totalsupply transferFrom burnFrom 我如何檢查功能transferFromburnFrom從混音瀏覽器,因為它涉及批准和允許,每當我執行這些功能時,我都會收到錯誤。這是我的契約

pragma solidity ^ 0.4.21;

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 MyFirsttoken is ERC20Interface {

address public owner; uint256 totalsupply; string tokenname; string tokensymbol; uint8 decimal;

event Burn(address,uint256 amount);

mapping(address=>uint256)public balances; mapping(address =>mapping(address=>uint256)) internal allowed;

function MyFirsttoken(uint256 tokensupply, string symbol, string name){

owner = msg.sender; totalsupply = tokensupply; tokenname = name;
tokensymbol = symbol;
balances[owner] = totalsupply; }

function totalSupply() public constant returns(uint){ return totalsupply; }

function balanceOf(address tokenholder)public constant returns(uint256 balance){ return balances[tokenholder]; }

function transfer(address tos,uint amount) public returns(bool) { require(amount <= balances[msg.sender]); balances[msg.sender]-= amount; balances[tos] +=amount; emit Transfer(msg.sender,tos,amount);
}

function transferFrom(address from, address to ,uint256 tokens)public returns (bool success){ uint256 allowedbalance = allowance(from ,to); require(tokens <= allowedbalance); balances[from] -= tokens; balances[to] += tokens; emit Transfer(from,to,tokens); }

function allowance(address tokenOwner, address spender) public constant returns(uint){ return allowed[tokenOwner][spender]; }

function approve(address spender,uint256 amount) public returns(bool success){ allowed[msg.sender][spender]=amount; emit Approval(msg.sender,spender,amount); }

function burnabletoken(uint256 burnableamount)returns (bool){ require(msg.sender == owner); require(balances[msg.sender]>=burnableamount); balances[msg.sender] -= burnableamount ; totalsupply -= burnableamount; emit Burn(msg.sender,burnableamount);

}

function burnfrom(address from , uint256 burnableamount){

   require(balances[from] &gt;= burnableamount);
   require(burnableamount &lt;= allowed[from][msg.sender]);
   allowed[from][msg.sender] -= burnableamount;
   balances[from] -= burnableamount;
   totalsupply -= burnableamount;
   emit Burn(from,burnableamount);

}

}

為了打電話transferFrom(),你首先必須打電話approve()

假設您有使用者 A 和 B

  • 使用者 A 批准使用者 B
token.approve(B, 1000, { from: A })
  • 使用者 B 現在可以從 A 訪問多達 1000 個令牌並將它們轉移到 C
token.transferFrom(A, C, 500, { from: B })

問題是您的函式transfer, transferFrom,沒有返回任何值approveburnabletoken但這些函式應該返回bool。還要考慮上面答案中@ismael 提到的執行順序。

希望它能解決問題。

引用自:https://ethereum.stackexchange.com/questions/55457