0

I don't know why but i'm not able to perform transaction with this code. please help me out i'm stuck. i just have to call setter method of my contract with some arguments.

router.post("/set", async function (request, response) {

let fromAddress = request.body.from_address;
let privateKey = request.body.from_private_key;
let contractAddress = request.body.contract_address;
let caseId = request.body.case_id;
let userIdentityAddress = request.body.user_identity_address;
let complaintType = request.body.complaint_type;
let complaintNature = request.body.complaint_nature;
let complaintDescription = request.body.complaint_description;
let document = request.body.document;

console.log(fromAddress)
// const sleep = (milliseconds) => {
//     return new Promise(resolve => setTimeout(resolve, milliseconds))
// }

const contract = await new web3.eth.Contract((abi), contractAddress);
let count = web3.eth.getTransactionCount(fromAddress);
web3.eth.defaultAccount = fromAddress;

const tx_builder = contract.methods.createCase(
    caseId,
    userIdentityAddress,
    complaintType,
    complaintNature,
    complaintDescription,
    document)

let encoded_tx = tx_builder.encodeABI();

let gasPrice = await web3.eth.getGasPrice();
let gasLimit = 500000;
let transactionObject = {
    "nonce": web3.utils.toHex(count),
    "from": fromAddress,
    "gasPrice": web3.utils.toHex(gasPrice),
    "gasLimit": web3.utils.toHex(gasLimit),
    "to": contractAddress,
    "data": encoded_tx,
    "chainId": 0x03
};

web3.eth.accounts.signTransaction(transactionObject, privateKey, function (error, signedTx) {
    if (error) {
        console.log('thiss', error);
        // handle error
    } else {
        web3.eth.sendSignedTransaction(signedTx.rawTransaction)
            .on('receipt', function (receipt) {
                console.log('heloo', receipt)
            });
    }
})

Error Response: { messageHash: '0x7c02b35df7d5c6eea7dba25fe38fe29b224e4e38eff2dda76ca70629ac330f44', v: '0x2a', r: '0x5675ff3d56b6b57c18dcc35fc3396a12bb1c3bb338537047ff10bea687ae9e21', s: '0xb38d8a3b19ba34aea031b9e286fa0e4733dfb6640313d594321d626f5c87110',

If you see here 's' is missing one hex character

(node:26007) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Returned error: invalid argument 0: json: cannot unmarshal hex string of odd length into Go value of type hexutil.Bytes (node:26007) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

After logging transactionObject and signedTx

enter image description here

1

My guess is that the (or at least an?) issue is here:

let count = web3.eth.getTransactionCount(fromAddress);

You're missing an await, so you're not getting a proper nonce. Perhaps that's causing an encoding problem of some sort later when signing the transaction object.

If that doesn't resolve the issue, I recommend logging transactionObject and then signedTx to see what's in those variables.

  • 1
    There's also a missing await on call to contract.methods.createCase. An attempt to use the returned value from this call is made before an attempt to use the returned value which you have mentioned - count. – goodvibration Jun 14 at 21:29
  • 1
    Addressed in a comment on your (now deleted) answer, but contract.methods.createCase does not return a Promise. – smarx Jun 14 at 21:33
  • Thanks for your answer but its not a issue of await i think. i have checked it again with await but still the same error. – Hamza Yasin Jun 14 at 21:59
  • I have added Image of transactionObject after logging. Please see question again. Thanks – Hamza Yasin Jun 14 at 22:06
  • I'm not 100% sure what you're logging, but it looks like maybe signedTx is a Promise? If so, make sure to await it first. – smarx Jun 14 at 22:12

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.