Web3 Невозможно получить значение переменной publi c из контракта - PullRequest
0 голосов
/ 15 января 2020

Код солидности:

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:

"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;
}

ошибка:

Ошибка: Возвращенные значения не действительны, закончились ли они? Эта ошибка также может появиться, если вы не используете правильный ABI для контракта, из которого извлекаете данные, запрашиваете данные из несуществующего номера блока или запрашивает узел, который не полностью синхронизирован. в ABICoder.pu sh .. / node_modules / web3-eth-abi / src / index. js .ABICoder.decodeParameters (index. js: 226) в Contract.pu sh .. / node_modules / web3-eth-contract / src / index. js .Contract._decodeMethodReturn (index. js: 471)

версия web3: 1.2.4

Возникла проблема. получение значения из переменной publi c. Хотя я использую await здесь, но кажется, что он не возвращает обещание. Я могу использовать функцию send () без ошибок. Нужна помощь с этим, спасибо.

если я использую const fileCount = await contract.methods.cubeCount.call();, вместо этого я получу объект:

{аргументы: Array (0), вызов: ƒ, отправка: ƒ, кодированиеABI: estimate, оценкаGas: ƒ,…} аргументы: [] вызов: ƒ () encodeABI: ƒ () оценкаGas: ƒ () отправка: ƒ () _ethAccounts: Accounts {_requestManager: RequestManager, GivenProvider: прокси, поставщики: {…}, _provider: прокси, …} _Method: {constant: true, входные данные: Array (0), имя: «cubeCount», выходные данные: Array (1), кредиторская задолженность: false,…} _parent: Contract {_requestManager: RequestManager, GivenProvider: Proxy, provider: { …}, _Provider: Proxy,…} proto : Объект

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...