1

I am trying to call a ethereum smart contract function but it gives me error. It works perfectly fine with remix JVM environment but throws error with web3 provider. I am just trying to call a function but it is showing me different contract address , as if it has created new contract. Is there any problem with the contract ?

error :-

"Transaction has been reverted by the EVM:
{
  "blockHash": "0xac0800a862fb3f6864f2fadc354baa47b11279095ce1c1e130af5176c97140df",
  "blockNumber": 6369858,
  "contractAddress": "0xb7fcf989EDB79DAd87990DF85412DF116AC7406E",
  "cumulativeGasUsed": 2100000,
  "from": "0xf61343ef22bbccc7221dcda85c5a69219ea00c2b",
  "gasUsed": 2100000,
  "logs": [],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "status": false,
  "to": null,
  "transactionHash": "0xab70d64929785cb3e718c78238575c3d21e9f4d4f5e481d26ca4069f269ae9d9",
  "transactionIndex": 0
}"

web3 code :-

const NODE_ADDRESS = config.web3Provider;
    const PRIVATE_KEY = reqdata.signerPriKey;

    const sourceCode = fs.readFileSync('path_to_contract', 'utf8').toString();
    const compiledCode = compiler.compile(sourceCode, 1).contracts[':PropertyContract']
    const abi = JSON.parse(compiledCode.interface);

    async function send(web3, account, transaction) {
        while (true) {
            try {
                const options = {
                    data: transaction.encodeABI(),
                    //gas: await transaction.estimateGas({ from: account.address }),
                    gas: 2100000,
                    gasPrice: 10000000000,
                };
                const signed = await web3.eth.accounts.signTransaction(options, account.privateKey);
                const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
                return receipt;
            }
            catch (error) {
                return error
            }
        }
    }

    async function run() {
        try {
            const web3 = new Web3(NODE_ADDRESS);
            const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
            const contract = new web3.eth.Contract(abi, reqdata.contractAddr);
            const transaction = contract.methods.signAgreement(reqdata.signerPubKey);
            const receipt = await send(web3, account, transaction);
            console.log(JSON.stringify(receipt, null, 4));
            if (web3.currentProvider.constructor.name == "WebsocketProvider")
                web3.currentProvider.connection.close();
            if (receipt) {
                next(null, receipt)
            }
        } catch (error) {
            next(error, null)
        }  
    }
    run();
1

In the options object passed to the signTransaction function, add this:

to: transaction._parent._address

Note that if this (which you have masked out) fails:

await transaction.estimateGas({ from: account.address })

Then the actual transaction will fail too.

So there is no point masking it out; you should investigate why your function reverts.

| improve this answer | |
  • thanks a lot , it worked after adding "to". 1 more query is that should i use .call() or .sendTransaction() to call view only functions ? They needed to be signed ? Again thanks a lot i really appreciate your help. I do know much about ethereum , i recently started on ethereum. – Puneet Kumar Apr 24 at 5:46
  • 1
    @PuneetKumar: You're welcome. No, you do not need to sign them with a private key, because they do not change the state of the contract (i.e., the state of the blockchain). They are therefore not considered transactions, just RPCs - remote process calls, where the remote process is the Ethereum node that you're communicating with. You should therefore use .call(). – goodvibration Apr 24 at 5:49

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.