Ethereum: Web3.js does not include extra data with the function?
Understanding of Ethereum Web3.js and Hex -Data
As a developer who uses web3.js, you may have problems extracting hex data from transactions. In this article, we will deal with the concept of Web3.js ‘TX’ object and hex data and examine how common problems can overcome.
What is web3.js?
Web3.js is a JavaScript library for interaction with the Ethereum blockchain. It offers a number of APIs and supply companies for the establishment of decentralized applications (DAPPS) in the Ethereum network. When working with transactions, web3.js uses the “TXobject", which represents a transaction sent to the blockchain.
HEX data in Web3.js transactions
HEX data is used to display the ROW transaction data stored on the blockchain. It is a number of hexadecimal signs that contain information about the transaction, such as: B. hash, payload and other details. In web3.js, these hex data are embedded in the "TX object”.
The tx
object and the hex data
If you create a new transaction with web3.js, the tx
object is initialized with several properties:
From ': The address of the sender
- To
: The address of the recipient
- Chainid
: The ID of the Ethereum Blockchain network (in your case 44445)
- Value
: The amount to be transmitted
However, if you print or log the hex data assigned with this transaction using methods such asconsole.log (tx.rawtransaction.hex). However, if we dig deeper, we can see that this is actually the Roh -Hex data that represent the transaction itself.
The problem: hidden hex data
Now let’s assume that you are trying to extract specific information from the hex data using methods such as json.Parse () or
hex2abiaddr () ‘. If you do this, you can encounter problems because these functions cannot analyze the embedded hex data within the “TXobject".
Why does that happen?
The reason why your hex data appears hidden is that web3.js uses a specific coding scheme to save transaction data on the blockchain. This coding scheme contains the embedded HEX data as part of the transaction load and not as a separate bytes.
In other words, if you print or logtx.rawtransaction.hex, print the RAW hex data from the Ethereum network that is stored in the transaction load. However, your program has no direct access to this payload, so it appears as a hidden string of hexadecimal signs.
solve the problem
To overcome this problem, you can use some problem bypass:
- Usetx.rawtransaction.hex
direct
: Instead of printing or logging the hex data separately, you can hand over to your functions that need it that you need.
- Extract hex data with integrated web3.js methods : You can use the integrated methods of web3.js liketx.rawtransaction.hex ()
to extract the hex data from the transaction load load.
- Use another coding scheme : If you create a custom application, you may need to implement your own coding scheme for saving and transmitted transaction data.
Example code
Here is an example code snippet that shows how the integrated methods of web3.js are used to extract hex data:
JavaScript
Const Web3 = demands (‘Web3’);
// Create a new web3 instance
Const Web3 = New Web3 ();
// Get the current instance of Ethereum Provider
Const Provider = Waits Web3.eth.GetProvider ();
Const tx = {
From: ‘0x1234567890abcdef’, ‘,
to: ‘0x9876543210fedcba’, ‘,’
Chainid: 44445,
Value: ‘1.2 ether’
};
// extract hex data with integrated web3.js-integrated methods
Const rawtransaction = provider is waiting.
const txhex = waiting web3.eth.abitohex (rawtransaction.rawtransaction);
console.log (txhex); // prints the extracted hex data
// Alternatively you can use:
Const txdata = rawtransaction.rawtransaction;
Const hex = web3.utils.