1

I want to deploy a smart contract with Node-js. I followed this guide but an error occurs where i effort instantiate my smart contract.

app.js:

var Web3=require('web3');
var fs=require('fs');
var solc=require('solc');

var web3=new Web3('ws://127.0.0.1:8545');
var address="0x7c028611F45a40a0ef035416B6bFc405F190990c";
var contract_sol=fs.readFileSync('E:/Deploy/contracts/Deploy.sol','utf8');
var contract_compiled=solc.compile(contract_sol);
for (let contractName in contract_compiled.contracts) {
var contract_byteCode=contract_compiled.contracts[contractName].bytecode;
var contract_abi=JSON.parse(contract_compiled.contracts[contractName].interface);
}
var gasEstimate=web3.eth.estimateGas({data:contract_byteCode});
var gasResult;
var contract=new web3.eth.Contract(contract_abi);

In the last line the error occurs:

Error: You must provide the json interface of the contract when instantiating a contract object.
0

Probably contract_compiled.contracts[contractName] does not have a field named interface.

This means that contract_compiled.contracts[contractName].interface == undefined.

I believe that the field you're looking for is called _jsonInterface.

But in either case, do yourself a favor:

console.log(JSON.stringify(contract_compiled.contracts[contractName], null, 4));

Find out the real name of this field and use it instead.


Also note that web3.eth.estimateGas returns a Promise objcet, which you need to resolve in order to get the actual value (for example, using return await gasEstimate=web3.eth.estimateGas(...) from inside an async function).

| improve this answer | |
0

There are several errors I see in your code:

  1. Do not use absolute paths such as fs.readFileSync('E:/Deploy/contracts/Deploy.sol','utf8'); but start using relative paths, because the moment you change the project path or run it from other device the project won't work.
  2. You are looping thru all contracts in contract_compiled.contracts how do you after the loop is finished which contract abi will be saved in variable contract_abi? You need to take the abi definition only for the desired contract like this contract_compiled.contracts[<NAME>].
  3. Also you might wanna pass the address parameter when creating the contract instance new web3.eth.Contract(contract_abi, <CONTRACT_ADDRESS>).
| improve this answer | |
  • 3. you might wanna pass the address parameter <CONTRACT_ADDRESS> - to my understanding, this user wants to deploy a new contract, not to interact with an already deployed contract. – goodvibration Mar 8 at 9:56
  • Correct, he didn't attach the code where he is deploying the contract so I added this just in case if he already have it deployed. – Miroslav Nedelchev Mar 8 at 10:08
0

I used ABI and bytecode of my contract as a value and the mentioned error never occured.

var contractAbi=[
    {
        "constant": true,
        "inputs": [],
        "name": "notation",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "pure",
        "type": "function"
    }
];

Now, my whole code is here without the mentioned error:

var Web3=require('web3');
var fs=require('fs');
var solc=require('solc');
var web3=new Web3('ws://127.0.0.1:8545');
var address="0x7c028611F45a40a0ef035416B6bFc405F190990c";
var contractAbi=[
    {
        "constant": true,
        "inputs": [],
        "name": "notation",
        "outputs": [
            {
                "internalType": "string",
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "pure",
        "type": "function"
    }
];
var contract_sol=fs.readFileSync('E:/Deploy/contracts/Deploy.sol','utf8');
var contract_compiled=solc.compile(contract_sol);
for (let contractName in contract_compiled.contracts) {
var contract_byteCode=contract_compiled.contracts[contractName].bytecode;
}
var gasEstimate=web3.eth.estimateGas({data:contract_byteCode});
var contract_object=new web3.eth.Contract(contractAbi);

There may be a better way, but this way eliminates the problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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