0

I have a Hyperledger Besu blockchain working on my VM with 4 nodes, built with Qorum Quickstart, using NPX. This blockchain is configured for 0 fees. (doc) I can develop, build, deploy, use my contract on Remix, but when i want to deploy the same smart contract on Visual Studio Code i have some mistakes with web3.js and ethers.js. On this post i just explain ethers.js issue but i can create another post for web3.js.

  • Ubuntu : 22.04.1 LTS
  • Docker : 20.10.12
  • Besu : image: hyperledger/besu:${BESU_VERSION:-latest}
  • solc : 0.8.21

This is my genesis file content :

{
    "config" : {
      "chainId" : 1337,
      "homesteadBlock": 0,
      "eip150Block": 0,
      "eip155Block": 0,
      "eip158Block": 0,
      "byzantiumBlock": 0,
      "constantinoplefixblock" : 0,
      "qbft": {
        "epochlength": 30000,
        "blockperiodseconds": 5,
        "requesttimeoutseconds": 10
      }
    },
    "nonce" : "0x0",
    "timestamp" : "0x58ee40ba",
    "gasLimit" : "0x1fffffffffffff",
    "difficulty" : "0x1",
    "number": "0x0",
    "gasUsed": "0x0",
(...)

This is my contract :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Test {
    function retriveString() public pure returns (string memory) {
        return "string returned";
    }
}

This is my JS code for deploy the smart contract :

// Import the ethers.js library
const { ethers, JsonRpcProvider, ContractFactory } = require("ethers");
const fs = require("fs");
const solFile = fs.readFileSync("../Test.sol", "utf8");
const solc = require("solc");

// 11/09/23 : back from 0 with ethers.js

const provider = new JsonRpcProvider("http://localhost:8545");
const privateKey =
  "0x0ba970ff5e9b9c4ce2435fdf94a998af0b5892c4f6f7cb9c3fcb0772ae98ab54";
const wallet = new ethers.Wallet(privateKey, provider);

async function buildContract() {
  const input = {
    language: "Solidity",
    sources: {
      "Test.sol": {
        content: solFile,
      },
    },
    settings: {
      outputSelection: {
        "*": {
          "*": ["*"],
        },
      },
    },
  };
  const output = JSON.parse(solc.compile(JSON.stringify(input)));
  const bytecode = output.contracts["Test.sol"]["Test"].evm.bytecode.object;
  const abi = output.contracts["Test.sol"]["Test"].abi;
  console.log('output => ' ,output)
  console.log('bytecode => ' ,bytecode)
  console.log('abi => ' ,abi)

  const factory3 = new ethers.ContractFactory(abi, bytecode, wallet);
  const nonce = await provider.getTransactionCount(wallet)
  const contractDeployed = await factory3.deploy({
    gasLimit: "0x4e85c",
    gasPrice: "0x0",
    nonce: nonce
  });

  // ERROR IF I USE THE LINE BELLOW
  // await contractDeployed.waitForDeployment();
  // -----------------------

  console.log("contractDeployed ==> ", contractDeployed);

  const contractDeployedAddress = await contractDeployed.getAddress();
  console.log("contractDeployedAddress ==> ", contractDeployedAddress);

  const myContract = new ethers.Contract(
    contractDeployedAddress,
    abi,
    wallet
  );
  
  const deployedTx = contractDeployed.deploymentTransaction()
  console.log('deployedTx ==> ' ,deployedTx)

  myContract['retriveString'].staticCall().then(result => {
    console.log('result in staticCall ==> ' ,result)
  }).catch(error => {
    console.log("error ==> " ,error)
  })

}

buildContract();

In case where i don't use the line await contractDeployed.waitForDeployment() i can display these console.log, and work with "contractDeployed" but i have error with my smart contract function :

output :

output =>  {
  contracts: { 'Test.sol': { Test: [Object] } },
  sources: { 'Test.sol': { id: 0 } }
}

abi :

[
  {
    inputs: [],
    name: 'retriveString',
    outputs: [ [Object] ],
    stateMutability: 'pure',
    type: 'function'
  }
]

bytecode :

608060405234801561000f575f80fd5b506101688061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610029575f3560e01c806319ea3c781461002d575b5f80fd5b61003561004b565b6040516100429190610112565b60405180910390f35b60606040518060400160405280600f81526020017f737472696e672072657475726e65640000000000000000000000000000000000815250905090565b5f81519050919050565b5f82825260208201905092915050565b5f5b838110156100bf5780820151818401526020810190506100a4565b5f8484015250505050565b5f601f19601f8301169050919050565b5f6100e482610088565b6100ee8185610092565b93506100fe8185602086016100a2565b610107816100ca565b840191505092915050565b5f6020820190508181035f83015261012a81846100da565b90509291505056fea26469706673582212203f6d93c42f307337aa85385803ee2204170f4e2f9a0996c6e4e505037f51708064736f6c63430008150033

contractDeployed :

BaseContract {
  target: '0xae4C40737AEC8D59721d90efCF8c95ADCB348A3D',
  interface: Interface {
    fragments: [ [FunctionFragment] ],
    deploy: ConstructorFragment {
      type: 'constructor',
      inputs: [],
      payable: false,
      gas: null
    },
    fallback: null,
    receive: false
  },
  runner: Wallet {
    provider: JsonRpcProvider {},
    address: '0xd48ab4450681d37CbfbDcf21e0b1eE9405045A9F'
  },
  filters: {},
  fallback: null,
  [Symbol(_ethersInternal_contract)]: {}
}

contractDeployedAddress : 0xae4C40737AEC8D59721d90efCF8c95ADCB348A3D

deployedTx :

ContractTransactionResponse {
  provider: JsonRpcProvider {},
  blockNumber: null,
  blockHash: null,
  index: undefined,
  hash: '0xb202de7c28e98e142a4f9e636a36b737dcfc91df3fb33d75629e525386779bc2',
  type: 0,
  to: null,
  from: '0xd48ab4450681d37CbfbDcf21e0b1eE9405045A9F',
  nonce: 352,
  gasLimit: 321628n,
  gasPrice: 0n,
  maxPriorityFeePerGas: null,
  maxFeePerGas: null,
  data: '0x608060405234801561000f575f80fd5b506101688061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610029575f3560e01c806319ea3c781461002d575b5f80fd5b61003561004b565b6040516100429190610112565b60405180910390f35b60606040518060400160405280600f81526020017f737472696e672072657475726e65640000000000000000000000000000000000815250905090565b5f81519050919050565b5f82825260208201905092915050565b5f5b838110156100bf5780820151818401526020810190506100a4565b5f8484015250505050565b5f601f19601f8301169050919050565b5f6100e482610088565b6100ee8185610092565b93506100fe8185602086016100a2565b610107816100ca565b840191505092915050565b5f6020820190508181035f83015261012a81846100da565b90509291505056fea26469706673582212203f6d93c42f307337aa85385803ee2204170f4e2f9a0996c6e4e505037f51708064736f6c63430008150033',
  value: 0n,
  chainId: 1337n,
  signature: Signature { r: "0xf6c49a0fb65d2570856a3046b1ef943f13d6e5cb271dfc1e5c3957f70350ddf4", s: "0x10a39114359a0bd86994381f0b1f6cd2765eb7cbf1d2b45426f9cce82fc7292d", yParity: 1, networkV: 2710 },
  accessList: null
}

error (in myContract staticCall) :

Error: could not decode result data (value="0x", info={ "method": "retriveString", "signature": "retriveString()" }, code=BAD_DATA, version=6.7.1)
    at makeError (/home/adminlogin/BESU_NETWORK/v5/contracts/EOBLOCK v2/contracts/node_modules/ethers/lib.commonjs/utils/errors.js:125:21)
    at assert (/home/adminlogin/BESU_NETWORK/v5/contracts/EOBLOCK v2/contracts/node_modules/ethers/lib.commonjs/utils/errors.js:142:15)
    at Interface.decodeFunctionResult (/home/adminlogin/BESU_NETWORK/v5/contracts/EOBLOCK v2/contracts/node_modules/ethers/lib.commonjs/abi/interface.js:780:31)
    at staticCallResult (/home/adminlogin/BESU_NETWORK/v5/contracts/EOBLOCK v2/contracts/node_modules/ethers/lib.commonjs/contract/contract.js:237:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async AsyncFunction.staticCall (/home/adminlogin/BESU_NETWORK/v5/contracts/EOBLOCK v2/contracts/node_modules/ethers/lib.commonjs/contract/contract.js:202:24) {
  code: 'BAD_DATA',
  value: '0x',
  info: { method: 'retriveString', signature: 'retriveString()' }
}

But if i use the line await contractDeployed.waitForDeployment() i have this error :

Error: transaction execution reverted (action="sendTransaction", data=null, reason=null, invocation=null, revert=null, transaction={ "data": "", "from": "0xd48ab4450681d37CbfbDcf21e0b1eE9405045A9F", "to": null }, receipt={ "_type": "TransactionReceipt", "blockHash": "0xf0431aeffef9aa6c4054079280c018b4eab1fe118f6188b674c428bf29c3c26a", "blockNumber": 174615, "contractAddress": "0xC363580f4F9890Da64b47b5f396A170720905233", "cumulativeGasUsed": "321628", "from": "0xd48ab4450681d37CbfbDcf21e0b1eE9405045A9F", "gasPrice": "0", "gasUsed": "321628", "hash": "0xcbb51dcbd275a7fd616a351d9c8531403a06ed427b47d431d28ab7cb7c3c3d7d", "index": 0, "logs": [  ], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "root": null, "status": 0, "to": null }

If anyone have ideas for these differents issues ...

Thanks !

1 Answer 1

0

i think the reason you get an error if you use

await contractDeployed.waitForDeployment();

is because the contract isn't actually being deployed

you're getting BAD_DATA 'cause no contract actually exists there

did you ever figure out how to get it deployed and read the data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.