我在Ganache上部署了以下智能合约(通过Truffle迁移):
pragma solidity ^0.4.24;
contract Logistics{
address public owner = msg.sender;
mapping(address => string) public notes;
function sign(string note) public {
require(msg.sender == owner);
notes[owner] = note;
}
function transferOwnership(address newOwner) public {
require(msg.sender == owner);
owner = newOwner;
}
}
并一直在为其编写Web UI。但是,在我的UI的javascript代码中,当我从称为notes的公共映射中调用getter时,始终收到错误“无效地址”。
我的JS代码:
$(document).ready(function() {///////////////////////////
let setUp = new Promise(function(resolve, reject){
if (typeof web3 !== 'undefined') {
console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
window.web3 = new Web3(web3.currentProvider);
console.log("Web3 initialized!");
resolve('done');
}
else {
console.log('No Web3 Detected... using HTTP Provider')
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
console.log("Web3 initialized!");
resolve('done');
}
});
setUp.then(function(){ //After setup above^
web3.eth.defaultAccount = web3.eth.accounts[0]; //current metamask account
console.log("The defaultAccount is: " + web3.eth.defaultAccount);
var contractABI = web3.eth.contract([
{
"constant": false,
"inputs": [
{
"name": "note",
"type": "string"
}
],
"name": "sign",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "notes",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]);
var Note = contractABI.at(0xea449D80E775607612Cc6d5ae9232EA10e417Ec1);
$('#viewButton').click(function(){
if ($('#viewInput').val()){ //if text input is populated
Note.notes("0xf35f06208aCcaCF3FaF678df88A76142b923408e", function(err, res){
if(!err){
alert(res);
} else{
console.log("Error fetching information from given address");
}
});
}
});
}); //initial load promises 'then'
});/////////////////////////////////////////////////////
我确保已将默认帐户分配给web3.eth.accounts [0],那么可能是此问题的原因?
最佳答案
我能看到完整的JS代码吗?我认为,在您显示的代码之前多行出错。
我检查了Ropsten测试网络-一切正常
我的代码:
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<script>
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("https://api.myetherwallet.com/rop"));
}
const abi = [{"constant":false,"inputs":[{"name":"note","type":"string"}],"name":"sign","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"notes","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
const contract_address = '0x***'
var myContract = web3.eth.contract(abi).at(contract_address);
myContract.notes('0x***', function(error, result){
if(!error)
console.log(result);
else
console.error(error);
});
</script>
如果该行中的地址不正确,则会出现错误“无效地址”:
const contract_address ='0x ***'
如果地址错误
myContract.notes('0x ***',function(error,result){
您将看到一个错误“未捕获的错误:新的BigNumber()不是基数为16的数字:”
上
您在行中丢失了引号:
var Note = contractABI.at(0xea449D80E775607612Cc6d5ae9232EA10e417Ec1);
它将是正确的:var Note = contractABI.at('0xea449D80E775607612Cc6d5ae9232EA10e417Ec1');