5

as seen in the following transaction reciept: https://www.etherchain.org/tx/0xf6769d28b35ac24e2e89b6cebc835c16c4e616bd6e2f96d0eea337c7bd02605f

what is the payload field and what is the use? how to put/get data in it? is there web3 function to manipulate that?

3 Answers 3

3

The payload you mentioned is the data field in a transaction. You can use it via web3. See here:

data: String - (optional) Either a byte string containing the associated data of the message, or in the case of a contract-creation transaction, the initialisation code.

3

The payload tells the contract which function you want to call, and the respective argument values to execute that call.

Structure of a payload

A payload comprises of 2 main parts - the function selector and its argument values. Let's say you want to call a particular function in a specific contract:

transfer(address,uint256)

This is how the payload is formed (and take note that the following are hex-serialized):

  1. The function selector - Take the first 4 bytes of the Keccak-256 hash of a string of the function along with the argument types. (e.g. the first 4 bytes of transfer(address,uint256) gives you 0xa9059cbb)
  2. Argument values - Say you want to transfer to the address 0x337c67618968370907da31dAEf3020238D01c9de, and send 10000 tokens. You will need to pad these two values separately to 32 bytes, which will give you 0x000000000000000000000000337c67618968370907da31dAEf3020238D01c9de and 0x0000000000000000000000000000000000000000000000008ac7230489e80000 respectively. Make sure that the tokens are in the correct units though (such as 1 Ether being equal to 10^18 wei).

Finally, remove all the 0x at the first 2 indices of each of the hex-encoded data above and concatenate them to form a giant string which gives you the following: a9059cbb (from the hashed function signature) + 000000000000000000000000337c67618968370907da31dAEf3020238D01c9de (from the address argument) + 0000000000000000000000000000000000000000000000008ac7230489e80000 (from the uint256 amount of tokens sent argument).

Therefore, the data payload will be as follows:

a9059cbb000000000000000000000000337c67618968370907da31dAEf3020238D01c9de0000000000000000000000000000000000000000000000008ac7230489e80000

This in itself is sufficient to tell a specific contract which function you're calling, and the values that are supplied to it as arguments.

Source

2

Transaction is a message that is sent from one account to another account (which might be the same or empty, see below). It can include binary data (which is called “payload”) and Ether.

If the target account contains code, that code is executed and the payload is provided as input data.

If the target account is not set (the transaction does not have a recipient or the recipient is set to null), the transaction creates a new contract. As already mentioned, the address of that contract is not the zero address but an address derived from the sender and its number of transactions sent (the “nonce”). The payload of such a contract creation transaction is taken to be EVM bytecode and executed. The output data of this execution is permanently stored as the code of the contract. This means that in order to create a contract, you do not send the actual code of the contract, but in fact, code that returns that code when executed.

see this link:

https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#index-8

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.