0

I have a smart contract. I want to execute one of its state modifying functions.

This is the code:

const result = await smartContractObject.methods
    .addSomething(_valueToAdd)
    .send( { gas: '5000000', from:masterAccount });

How can I manually assign a nonce to this transaction?

Do I need to create a raw_tx?

New contributor
Pave is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

I am not sure about modifying the nonce with send methods. You can use instead classic web3.eth.sendTransaction method (https://web3js.readthedocs.io/en/v1.2.1/web3-eth.html#sendtransaction). However, you'll need to provide the data field for this transaction.

const nonceThatIWant = x;
const txObject = smartContractObject.methods.addSomething(_valueToAdd);
const txData = txObject.encodeABI();
await web3.eth.sendTransaction({
   from:masterAccount,
   nonce: nonceThatIWant,
   data: txData,
   to: smartContractObject._address, //or smartContractObject.address depending on web3 version,

   //rest parameters like gas, value, gasPrice, etc.
});
  • Ok thanks for your help. However I need further assistance. The smartContract emits a event. How do I receive this value now? Originally it worked this way: result.events.myEvent.returnValues['myreturnvalue'] – Pave Aug 14 at 10:23
  • use the resolved promise object's logs property. For example const result = await web3.eth.sendTransaction .... Then result.logs will contain all the events of transaction. The problem might be they have not very convenient format. – Oleg Kondrakhanov Aug 14 at 14:52

Your Answer

Pave is a new contributor. Be nice, and check out our Code of Conduct.

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.