Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 47 ยท Object-Oriented Solidity

ABI Encoding and Low-Level Calls

Learn how function selectors and abi.encode work, why encodePacked can collide, and how call, staticcall and delegatecall differ, with runnable stand-ins.

When you write token.transfer(bob, 5) in Solidity, the EVM never sees a function name. It sees a list of bytes, called calldata, and it is the contract's job to work out which function those bytes mean. The rules for turning a function call into bytes (and back) are called the ABI, the Application Binary Interface. Wallets, libraries like ethers.js and other contracts all use the same rules, which is why they can talk to any contract that follows them.

Most of the time the compiler encodes calls for you. But sooner or later you will meet abi.encode, abi.encodePacked, .call(...) and delegatecall, and each one has sharp edges. This lesson explains what the bytes look like and when each low-level tool is the right (or wrong) choice. If you have not seen contract-to-contract calls yet, read Interfaces and Libraries first.

A call is just bytes

Selector The first 4 bytes pick the function.
Arguments Each one is padded to 32-byte words.
Return data Comes back as bytes you must decode.

Calldata = selector + encoded arguments.

1 / 2

Function selectors

A function is identified by its signature: the name followed by the parameter types in parentheses, with no spaces and no parameter names. For example transfer(address,uint256). The selector is the first four bytes of the Keccak-256 hash of that text.

SOLIDITY
// Read-only: well-known selectors from the ERC-20 standard
bytes4 a = bytes4(keccak256("transfer(address,uint256)")); // 0xa9059cbb
bytes4 b = this.transfer.selector;                         // same value, no string typo possible
bytes4 c = IERC20.balanceOf.selector;                      // 0x70a08231

Two details matter. The types must be written in their canonical form (uint256, never uint), and spelling mistakes silently give you a different selector. That is why Interface.fn.selector is safer than hashing a string yourself.

Only four bytes means collisions are possible in theory (two different signatures sharing a selector), and the compiler refuses to compile a contract where two of its own functions collide. It matters mostly for proxies, which you will see in Proxies and Upgradeable Contracts.

What encoded arguments look like

After the selector come the arguments. abi.encode pads every value to a full 32-byte word. A uint256 is the number itself, left-padded with zeros. An address (20 bytes) is also left-padded to 32. Dynamic types such as string and bytes use a head/tail layout: the head holds an offset (where the data starts), and the tail holds the length and the bytes, padded to a multiple of 32.

The stand-in below uses a simple 32-bit hash instead of Keccak-256 (the real hash is not built into JavaScript), but the layout of the calldata is the same idea: a short selector followed by 32-byte words.

JS Selector plus 32-byte words

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that a space or a uint instead of uint256 gives a completely different selector. Calling a contract with a wrong selector does not produce a helpful error: the contract just does not find a match, which is where the next section comes in.

How a contract dispatches

The compiler generates a small switch at the start of every contract. It reads the first four bytes of calldata, compares them with the selector of each external function, and jumps to the matching one. If none match, the contract runs its fallback function when it has one, and reverts when it does not. An empty calldata with ETH attached goes to receive instead (see Sending and Receiving Ether).

JS A selector-based dispatcher

Press Run (or Ctrl + Enter) to see the console output here.

No output.

abi.encode, encodePacked and collisions

Solidity gives you several encoders:

Function What it does
abi.encode(a, b) Standard ABI encoding: every value padded to 32 bytes, dynamic data length-prefixed
abi.encodePacked(a, b) Tightly packed: no padding, no lengths, values just glued together
abi.encodeWithSelector(sel, a, b) Selector followed by abi.encode of the arguments
abi.encodeWithSignature("f(uint256)", a) Same, but computes the selector from a string (typo-prone)
abi.encodeCall(Interface.f, (a)) Type-checked: the compiler verifies the arguments match the function

abi.encodeCall is the one to prefer, because a wrong argument type is a compile error rather than a silent bug.

encodePacked is smaller and cheaper, which makes it popular for hashing. The danger: when two or more values are dynamic (strings, bytes, arrays), different inputs can produce identical output. abi.encodePacked("aa", "bb") and abi.encodePacked("a", "abb") are the same bytes. If you hash that to build a key or check a signature, an attacker can shift the boundary between fields.

JS Why encodePacked collides

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The usual fixes are to use abi.encode when hashing several dynamic values, to add a fixed-size separator or length, or to make sure at most one value in the packed data is dynamic.

call, staticcall and delegatecall

Besides the normal token.transfer(...) syntax, an address has three low-level methods. They take raw bytes and return (bool success, bytes memory data).

SOLIDITY
// Read-only: the three low-level calls
(bool ok, bytes memory ret) = target.call{value: 1 ether}(
    abi.encodeCall(IReceiver.deposit, (42))
);
require(ok, "call failed");

(bool ok2, bytes memory ret2) = target.staticcall(
    abi.encodeCall(IReader.balanceOf, (msg.sender))
);
uint256 balance = abi.decode(ret2, (uint256));

(bool ok3, ) = library_.delegatecall(abi.encodeCall(ILib.step, ()));
  • call runs the target's code in the target's own context: its storage, and msg.sender is the calling contract.
  • staticcall is the same but forbids state changes. If the target tries to write, the call fails. It is how view functions are called on other contracts.
  • delegatecall borrows the target's code but runs it in the caller's context: the caller's storage, the caller's balance, and the original msg.sender.

Three rules keep low-level calls safe:

  1. Always check success. A low-level call does not revert when the target reverts; it just returns false.
  2. A call to an address with no code succeeds. The EVM treats it as an empty call. The normal IToken(addr).fn() syntax checks that code exists and reverts otherwise; .call does not.
  3. Never delegatecall into untrusted code. It runs with full power over your storage.
JS call vs delegatecall: whose storage changes?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the selector idea, 32-byte padding, the head/tail layout for strings, the encodePacked collision and the storage and msg.sender rules of call and delegatecall follow the EVM. What is the stand-in: the hash is FNV, not Keccak-256; the "contracts" are JavaScript objects and the dispatcher is a plain table lookup; there is no gas, no revert rollback and no real addresses.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a function selector?

    The selector is the first four bytes of keccak256 of the canonical signature, like transfer(address,uint256). The contract compares it with the selector of each external function.
  2. 2. Why is abi.encodePacked risky when hashing two strings?

    Packed encoding has no lengths or padding, so ("aa", "bb") and ("a", "abb") give identical bytes. Use abi.encode (or a separator) when more than one value is dynamic.
  3. 3. A low-level .call() to an address that has no contract code returns what?

    The EVM treats a call to a code-less address as a successful no-op. That is why you check extcodesize-style conditions yourself, or use the high-level interface syntax, which checks for code.
  4. 4. Which storage does delegatecall change?

    delegatecall runs the target's code against the caller's storage, balance and msg.sender. That is the basis of libraries and proxies, and why it is dangerous with untrusted code.

Summary

  • Calldata is a 4-byte selector followed by arguments padded to 32-byte words; dynamic types use a head/tail layout.
  • A selector is the first four bytes of keccak256 of the canonical signature (transfer(address,uint256)).
  • Prefer abi.encodeCall over encodeWithSignature: it is type-checked.
  • abi.encodePacked is compact but ambiguous with several dynamic values; do not hash it for keys or signatures.
  • call uses the target's storage, staticcall forbids writes, delegatecall uses the caller's storage with the target's code.
  • Always check the success flag, remember that code-less addresses succeed, and never delegatecall into untrusted code.

Next: Proxies and Upgradeable Contracts.