Ethereum: How to get revert message in Web3js?

Here is an article on how to recover the “return message” from a call from the sending method to web3.js:

Decodifies the rest message on web3.js

On web3.js, when using the `send 'method to perform a transaction, it returns an receipt object that contains various information on the transaction. One of the values ​​you can access are theLogs [0] .Ta, which is a string containing the hexadecimal value of the data stored in the transaction.

The "return message" is codified in this hexadecimal value. To decode the return message, you will have to use some methods and web3.js.

Step 1: Access to the object received

Ethereum: How to get revert message in Web3js?

First of all, it is necessary to access the object received by the return value of the methodSed.

`Javascript

Const receipt = EXPECT ETERS.GETTRECEIPT ();

`

Step 2: extraction of the hexadecimal value

Subsequently, extract the hexadecimal value from the registers [0] You can do it using theTostring (16) method to convert the hexadecimal string into a number.

Javascript

Const Revertmessage = Receipt.logs [0] .tata.tostring (‘Hex’);

`

Step 3: Decoding of the rest message

Now, you have to decode the message restored by the hexadecimal value. On web3.js, this can be done using thegives a” optional “decoding” method.

`Javascript

Const Revert = wait eters.utils.fromhex (revertmessage, {decode: true});

`

Thedecodeis a callback function that requires two topics: the message codified with a debt collector and an object with various properties. In this case, we are using it to decode the restored message.

Put everything together

Here is the complete example:

Javascript

GetrevertMessage asynchric function () {

Attempt {

Const receipt = EXPECT ETERS.GETTRECEIPT ();

Const Revertmessage = Receipt.logs [0] .tata.tostring (‘Hex’);

Const Revert = wait eters.utils.fromhex (revertmessage, {decode: true});

return the restoration;

} Catch (error) {

console.error (error);

}

}

`

Example of use of the case

In this example, we are using theGetrevertMessage ‘function to recover and print the recovery message. The restored message is archived in the `reverts variable.

Note that the function is decoding ‘can launch an error if it is not able to analyze the hexadecimal value. You should manage this error accordingly.

I hope this helps! Let me know if you have questions or you need further assistance.

ethereum winner different everytime

Artigos relacionados

Deixe o primeiro comentário