Truffle
使用 Metamask 的 Ganche 和 Truffle 問題。無法載入代幣餘額
我正在測試在 Truffle 中創建一個令牌;它編譯得很好,但它不會將令牌的餘額載入到 Metamask 中。我已正確設置連接:它確實正確載入了 Ganache 鏈的 ETH 餘額。
我不確定發生了什麼。這是契約:
pragma solidity ^0.5.0; contract TestToken{ // Track how many tokens are owned by each address. mapping (address => uint256) public balanceOf; string public name = "TestToken1"; string public symbol = "TTO"; uint8 public decimals = 18; uint256 public totalSupply = 1000000 * (uint256(10) ** decimals); event Transfer(address indexed from, address indexed to, uint256 value); constructor() public { balanceOf[msg.sender] = totalSupply; } function isContract(address _addr) private view returns (bool is_contract) { uint length; assembly { //retrieve the size of the code on target address, this needs assembly length := extcodesize(_addr) } return length > 0; } function transfer(address to, uint256 value) public returns (bool success) { bytes memory empty; return transfer(to, value, empty); } function transfer(address to, uint256 value, bytes memory data) public returns (bool) { require(balanceOf[msg.sender] >= value); balanceOf[msg.sender] -= value; balanceOf[to] += value; emit Transfer(msg.sender, to, value); // if (isContract(to)) { // ITokenReceiver(to).tokenFallback(msg.sender, value, data); // } return true; } event Approval(address indexed owner, address indexed spender, uint256 value); mapping(address => mapping(address => uint256)) public allowance; function approve(address spender, uint256 value) public returns (bool success) { allowance[msg.sender][spender] = value; emit Approval(msg.sender, spender, value); return true; } function transferFrom(address from, address to, uint256 value) public returns (bool success) { require(value <= balanceOf[from]); require(value <= allowance[from][msg.sender]); balanceOf[from] -= value; balanceOf[to] += value; allowance[from][msg.sender] -= value; emit Transfer(from, to, value); return true; } }
正如你所看到的,它是最基本的。但在 Metamask 控制台中,我看到了這個:
ui.js:1 未能在 0xaec97fa2a3bd75ac77a431e06e5fa0aeb7e3083e(匿名)@……..
令牌更新失敗錯誤:
$$ ethjs-query $$格式化 RPC 的輸出時 ‘{“value”:{“code”:-32603,“message”:“Internal JSON-RPC error.”,“data”:{“originalError”:{}},“stack”:“錯誤:"message" 必須是非空字元串。\n at new……
是什麼導致了 RPC 錯誤?是我在 ganache 和 Metamask 之間犯了錯誤還是某種類型的問題?
這可能有效:MetaMask 設置 -> 高級 -> 重置帳戶。
我不確定這是否會完全解決它,但是您的類型轉換
totalSupply
不正確。您應該轉換decimals
為uint256
,而不是10
。它應該是:
uint256 public totalSupply = 1000000 * (10 ** uint256(decimals));