Get to know Ethereum Calldata

Jean Tanadol
Laika Labs
Published in
4 min readJan 13, 2022

What’s in Ethereum Calldata?
And how to interpret it with Laika
(Ref: https://ethereum.stackexchange.com/questions/52989/what-is-calldata)

Have you ever wondered what lies under the hood of the Ethereum network? How the transaction is made. How the data was passing around. And what is it made of?

This blog is intended to be a very basic introduction to how to look at Ethereum Calldata (Or any other EVM-based Calldata) we’re going to look at how it structures, what it keeps, and how it can be categorized into what ways.

So WTF is Calldata?

It’s basically what “Data” is passing around on an EVM-based chain. For example, this is the data in an attempt to swap UNI to ether on UNIv2 (It’s a quite long string of hexes)

When you want to do something like quickly glance at the smart contracts state. Execute some functions on the contract. You would have to pass data around. And that data is called “Calldata” (Hehe No pun intended!)

How is it structured then?

Let’s say that we have this contract here. It has one simple function call baz which receives 2 parameters of x and y

contract Foo {function baz(uint32 x, bool y) returns (bool r) { r = x > 32 || y; }}

So what is the Calldata going to be structured if we want to call this function?

The Calldata basically consists of these parts

1. Function Signature (aka. Sighash, Method ID, …)

It is something working similar to the id of that function. An identifier of each function in the contract.

How to get the value of the function signature is really simple. You just have to pass the function signature to keccak-256 then get the first 4 bytes of it.

For Example. our function is baz(uint32,bool) then we pass to keccak-256 and we get cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2

then take first 4 bytes which is cdcd77c0

2. Parameters

This part is simple. You just have to convert your parameters into byte format. But the thing to note is each parameter in the Calldata will have to be 32 bytes long. So if your data is less than that just pad it with zeros

Let say that I want to send a boolean of true into the parameters the boolean is only 0 or 1 So to comply with this form I would have to pad it with 31 bytes of zeros.

That’s it! Let’s structure it together (By hand! 🤏).

First, is putting the function signature in the most front.

cdcd77c0

Secondly, The parameters here we have 2 parameters of x and y let say we want x to be 99 and y to be true

x: 99 to hex is 63 it’s only 1 bytes long so we have to padded it to 64 bytes we get 0x0000000000000000000000000000000000000000000000000000000000000063

y: true is only 0x1 so we also have to pad it.

0x0000000000000000000000000000000000000000000000000000000000000001

And now we have all our ingredients let’s merge them together our final Calldata would be.

0xcdcd77c000000000000000000000000000000000000000000000000000000000000000630000000000000000000000000000000000000000000000000000000000000001

With a length of 68 bytes in total. And this is how the data that passes around the Ethereum network looks like!

The Laika’s way

We’ve been looking at how we could structure the Calldata by hand already. Let’s look at how to do it the easy way.

If you don’t know what Laika is, Laika is a Blockchain development tool that helps you request smart contracts without having the hassle of writing a single line of code — a postman for web3!

When you head to Laika and finish off your request you’ll see the Call Data tab below the contract address input form.

And, This is what you’re going to see! Laika is automatically converting your request to the format of the byte and you can conveniently take a look at it deeply byte by byte! (Even better you can look at it word by word 👀)

Don’t forget to put the value of the parameter or Laika wouldn’t be able to generate the call data for you.

From right here you can see the function signature, parameters, And the entire call data and that’s happening with just a few clicks on the UIs!

And this is it! Thank you so much for staying until the very end here I hope you liked this article 😁

Actually, there are a lot more features of Laika! Don’t forget to try it at https://getlaika.app/

See ya next time! 🤗

#BUIDL #getlaikaapp

--

--