Send Signed Transaction for Contract Interaction on Quorum using web3js

867 views Asked by At

I am currently running the 7 nodes example given in quorum-examples github repo. I have deployed a very simple storage contract which gets and sets the value. As per the example, I am able to interact with the smart contract inside the geth node cmd line. However I would like to interact with it using another address outside the node and so I wrote the following code:

const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:22000'))
const { Transaction } = require('@ethereumjs/tx')
const { default: Common } = require('@ethereumjs/common')

const run = async () => {
    const contractInstance = await new web3.eth.Contract(
        [
            {
                constant: true,
                inputs: [],
                name: 'storedData',
                outputs: [{ name: '', type: 'uint256' }],
                payable: false,
                type: 'function',
            },
            {
                constant: false,
                inputs: [{ name: 'x', type: 'uint256' }],
                name: 'set',
                outputs: [],
                payable: false,
                type: 'function',
            },
            {
                constant: true,
                inputs: [],
                name: 'get',
                outputs: [{ name: 'retVal', type: 'uint256' }],
                payable: false,
                type: 'function',
            },
            {
                inputs: [{ name: 'initVal', type: 'uint256' }],
                payable: false,
                type: 'constructor',
            },
        ],
        '0xd9d64b7dc034fafdba5dc2902875a67b5d586420'
    )

    const customCommon = Common.forCustomChain('mainnet', {
        chainId: 10,
    })

    const txCount = await web3.eth.getTransactionCount(
        'ed9d02e382b34818e88b88a309c7fe71e65f419d'
    )

    const txData = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: '0x47b760',
        gasPrice: '0x00',
        value: '0x0',
        chainId: 10,
        to: '0xd9d64b7dc034fafdba5dc2902875a67b5d586420',
        data: contractInstance.methods.set(10).encodeABI(),
    }

    const tx = Transaction.fromTxData(txData, { common: customCommon })

    const signedTx = tx.sign(
        Buffer.from(
            'e6181caaffff94a09d7e332fc8da9884d99902c7874eb74354bdcadf411929f1',
            'hex'
        )
    )
    const serializedTx = signedTx.serialize()

    const result = await web3.eth.sendSignedTransaction(
        `0x${serializedTx.toString('hex')}`
    )

    return result
}

run().then(console.log).catch(console.log)


However whenever I try to send the transtion, it always errors out to

"Transaction has been reverted by the EVM:\n{\n  \"blockHash\": \"0xd6b06321882912185f5e1d3401a012f58b6bbf7eee1e1d2c6c2cd80a0e13bbdc\",\n  \"blockNumber\": 5,\n  \"contractAddress\": null,\n  \"cumulativeGasUsed\": 23751,\n  \"from\": \"0x0fbdc686b912d7722dc86510934589e0aaf3b55a\",\n  \"gasUsed\": 23751,\n  \"logs\": [],\n  \"logsBloom\": \"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\n  \"status\": false,\n  \"to\": \"0x9d13c6d3afe1721beef56b55d303b09e021e27ab\",\n  \"transactionHash\": \"0x8c7fd175ab037e24e531804774e8b89bf5aea25de8d99aa9bc2c034229603299\",\n  \"transactionIndex\": 0\n}",

Do let me know if more info is required and I will update the post.

The smartcontract code is as follows:

pragma solidity ^0.5.0;

contract simplestorage {
  uint public storedData;

  constructor(uint initVal) public {
    storedData = initVal;
  }

  function set(uint x) public {
    storedData = x;
  }

  function get() view public returns (uint retVal) {
    return storedData;
  }
}
1

There are 1 answers

1
AtomXV On

I think i met the similar issue as well. My issue was invalid chain id signer. I checked many docs and tried to set the custom chain id which was what you did about "common". But nothing changed until i changed the version of ethereumjs-tx to ^1.3.7. btw you dont need common if you try my solution.