查看功能错误:返回的值ern' t有效,它用完了气体吗?
我正在调用Ganache中部署的智能合约中的方法。其他方法工作正常,但是视图功能返回错误。
这是固体中的视图函数:
mapping (address => Count) private counts;
function getCounts (address user)
public
view
returns(uint a, uint b, uint total){
return(counts[user].a, counts[user].b, counts[user].total);
}
我称之为方法:
web3.eth.getAccounts(function(error, accounts){
if(error){
console.log(error);
}
var account = accounts[0];
contracts.SampleContract.deployed().then(function(instance){
credsInstance = instance;
return credsInstance.getCounts.call(account, {from: account});
}).then(function(creds){
console.log(creds[0]);
}).catch(function(error){
console.log(error.message);
});
});
这是getCounts
的ABI,
{
"inputs": [
{
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "getCounts",
"outputs": [
{
"internalType": "uint256",
"name": "academic",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "workExp",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "total",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
},
我会遇到此错误:
返回的值无效,它用完了气体吗?如果您不使用正确的合同数据,请从不存在的块号中请求数据或查询未完全同步的节点的数据,也可能会看到此错误。
我也有最新的Web3。我该如何解决?
编辑:
要初始化合同,我使用了松露的官方文件。这是代码:
var contract = require('@truffle/contract');
$.getJSON('../build/contracts/SampleContract.json', function(data) {
// Get the necessary contract artifact file and instantiate it with @truffle/contract
var CredsArtifact = data;
contracts.SampleContract = TruffleContract(CredsArtifact);
// Set the provider for our contract
contracts.SampleContract.setProvider(web3Provider);
});
I'm calling the methods in the deployed smart contracts in Ganache. Other methods are working fine, but the view functions returns an error.
Here's the view function in Solidity:
mapping (address => Count) private counts;
function getCounts (address user)
public
view
returns(uint a, uint b, uint total){
return(counts[user].a, counts[user].b, counts[user].total);
}
Here's how I call the method:
web3.eth.getAccounts(function(error, accounts){
if(error){
console.log(error);
}
var account = accounts[0];
contracts.SampleContract.deployed().then(function(instance){
credsInstance = instance;
return credsInstance.getCounts.call(account, {from: account});
}).then(function(creds){
console.log(creds[0]);
}).catch(function(error){
console.log(error.message);
});
});
Here's the ABI of getCounts
{
"inputs": [
{
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "getCounts",
"outputs": [
{
"internalType": "uint256",
"name": "academic",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "workExp",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "total",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
},
I'm getting this error:
Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
I also have the latest web3. How can I fix this?
Edit:
to initialize the contract I used Truffle's official documentation. Here's the code:
var contract = require('@truffle/contract');
$.getJSON('../build/contracts/SampleContract.json', function(data) {
// Get the necessary contract artifact file and instantiate it with @truffle/contract
var CredsArtifact = data;
contracts.SampleContract = TruffleContract(CredsArtifact);
// Set the provider for our contract
contracts.SampleContract.setProvider(web3Provider);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,我正在使用硬汉。
花了几个小时并无法解决后,我切换了Rinkeby测试网络,一切顺利。
请参阅: https://ethereum.stackexchange.com/a/129721/30431
I had the same problem, and I was using Hardhat .
after spending hours and not resolved, I switched Rinkeby test network, and everything goes well.
refer to: https://ethereum.stackexchange.com/a/129721/30431