gpt4 book ai didi

solidity - Web3 无法从契约(Contract)中获取公共(public)变量的值

转载 作者:行者123 更新时间:2023-12-05 07:14:58 25 4
gpt4 key购买 nike

实体代码:

pragma solidity ^0.5.8;


contract remainNew{

address public owner = msg.sender;
uint public cubeCount = 0;

modifier onlyBy(address _account){
require(
msg.sender == _account,
"Sender not authorized"
);
_;
}

struct aCube{
uint cubeID;
string owner;
string ipfsHash;
string fileName;
string fileSize;
string description;
bool isEncrypted;
string time;
}

mapping(uint => aCube) public cubes;


function generateCube(string memory _o,string memory _h, string memory _n, string memory _s,
string memory _d, bool _i, string memory _t) public {
cubeCount++;
cubes[cubeCount] = aCube(cubeCount,_o,_h,_n,_s,_d,_i,_t);
}

function setHash(string memory _newHash, uint _ID) public onlyBy(owner){
cubes[_ID].ipfsHash = _newHash;
}

}

阿比:

"abi": [
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "cubes",
"outputs": [
{
"name": "cubeID",
"type": "uint256"
},
{
"name": "owner",
"type": "string"
},
{
"name": "ipfsHash",
"type": "string"
},
{
"name": "fileName",
"type": "string"
},
{
"name": "fileSize",
"type": "string"
},
{
"name": "description",
"type": "string"
},
{
"name": "isEncrypted",
"type": "bool"
},
{
"name": "time",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "cubeCount",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_o",
"type": "string"
},
{
"name": "_h",
"type": "string"
},
{
"name": "_n",
"type": "string"
},
{
"name": "_s",
"type": "string"
},
{
"name": "_d",
"type": "string"
},
{
"name": "_i",
"type": "bool"
},
{
"name": "_t",
"type": "string"
}
],
"name": "generateCube",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_newHash",
"type": "string"
},
{
"name": "_ID",
"type": "uint256"
}
],
"name": "setHash",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]

web3:

 try{

const fileCount = await contract.methods.cubeCount().call();

console.log(fileCount);

}catch(error){
console.log(error);
return;
}

错误:

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. at ABICoder.push../node_modules/web3-eth-abi/src/index.js.ABICoder.decodeParameters (index.js:226) at Contract.push../node_modules/web3-eth-contract/src/index.js.Contract._decodeMethodReturn (index.js:471)

web3 版本是:1.2.4

从公共(public)变量中检索值时出现问题。虽然我在这里使用 await,但它似乎没有返回 promise 。我可以毫无错误地使用 send() 函数。需要帮助,谢谢。

如果我改用 const fileCount = await contract.methods.cubeCount.call();我会得到一个对象:

{arguments: Array(0), call: ƒ, send: ƒ, encodeABI: ƒ, estimateGas: ƒ, …} arguments: [] call: ƒ () encodeABI: ƒ () estimateGas: ƒ () send: ƒ () _ethAccounts: Accounts {_requestManager: RequestManager, givenProvider: Proxy, providers: {…}, _provider: Proxy, …} _method: {constant: true, inputs: Array(0), name: "cubeCount", outputs: Array(1), payable: false, …} _parent: Contract {_requestManager: RequestManager, givenProvider: Proxy, providers: {…}, _provider: Proxy, …} proto: Object

最佳答案

确保你部署了最新的联系人

就我而言,我需要这个。

truffle migrate --reset

关于solidity - Web3 无法从契约(Contract)中获取公共(public)变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59757051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com