Solidity
未處理的拒絕錯誤:無法從 ABI 解碼 uint256
我正在使用 NodeJS、Web3 和 Solc 使用範例契約,但我遇到了一些錯誤,即我創建的契約中的給定地址無法解碼。難道我做錯了什麼?這是我正在處理的 JS 程式碼
const Web3 = require('web3'); const fs = require('fs'); const solc = require('solc'); let web3 = new Web3(); web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); const source = fs.readFileSync(__dirname + '/solidity/Token.sol'); const output = solc.compile(source.toString(), 1); const bytecode = output.contracts[':Token'].bytecode; const abi = JSON.parse(output.contracts[':Token'].interface); const contract = new web3.eth.Contract(abi, '0x5bbf74f1e804bfe671d55cd6b9f3ada66568d5dd', { from: web3.eth.coinbase, gasPrice: 90000*2 }); contract.methods.totalSupply().call().then(function (err, result) { console.log('error', err); console.log('result', result); });
這是我正在使用的可靠程式碼。
pragma solidity ^0.4.4; contract Token { // stores the balances of the addresses mapping(address => uint) balances; mapping(address => mapping(address => uint)) approved; // number of tokens in circulation uint supply; event Transfer(address indexed _from, address indexed _to, uint256 _value); function Token() { // constructor // balances[tx.origin] = 10000; } function totalSupply() constant returns (uint) { return supply; } function balanceOf(address _owner) constant returns (uint balance) { return balances[_owner]; } function transfer(address _to, uint _value) returns (bool success) { if (balances[msg.sender] >= _value && _value > 0 ) { balances[msg.sender] -= _value; balances[_to] += _value; // trigger an event Transfer(msg.sender, _to, _value); return true; } return false; } // sets how many can a spender spend from a certain address function approve(address _spender, uint _value) returns (bool success) { if (balances[msg.sender] > _value) { approved[msg.sender][_spender] = _value; return true; } return false; } // check the value of the spender can spend function allowance(address _owner, address _spender) constant returns (uint remaining) { return approved[_owner][_spender]; } // transfer the approved value to spend function transferFrom(address _from, address _to, uint _value) constant returns (bool success) { if (balances[_from] >= _value && approved[_from][msg.sender] >= _value && _value > 0 ) { balances[_from] -= _value; approved[_from][msg.sender] -= _value; balances[_to] += _value; return true; } return false; } }
PS:我正在
testrpc
用作我的乙太坊提供者。
我自己完全陷入了這個問題,直到我意識到我將元遮罩設置為錯誤的網路(主網而不是 rinkeby)。值得檢查該設置…在花了很多時間試圖找到問題後,我意識到自己確實很傻。
web3
由於您對版本的評論,我不確定這是您問題的解決方案。你正在編譯一個新版本的智能合約,
solc
但你從未部署它並更新合約地址,如果你有一個過時的 ABI,這可能是個問題