Lesson 3 of 4 Advanced Solidity

Calling Other Contracts

Contracts call each other to build larger systems, which developers call composability. This lesson explains high-level calls through interfaces, low-level call, staticcall and delegatecall, and deploying contracts from contracts with CREATE and CREATE2.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish high-level calls from low-level calls.
  • Compare call, staticcall and delegatecall.
  • Explain the risks of delegatecall.
  • Compare CREATE and CREATE2 for deploying contracts from contracts.

High-level calls

The usual way to call another contract is through its interface or contract type: IERC20(token).transferFrom(user, address(this), amount). The compiler ABI-encodes the call, checks that the target has code, decodes the return value and reverts the caller if the callee reverts. Ether is attached with {value: x} and gas can be limited with {gas: g}.

Low-level calls and delegatecall

Low-level calls operate on raw calldata and return (bool success, bytes memory data) without reverting on failure. The following comparison shows the three variants.

call
Code executedTarget's
Storage usedTarget's
msg.sender insideThe calling contract
State changes allowedYes
Typical useSending ether, calling unknown functions
staticcall
Code executedTarget's
Storage usedTarget's (read-only)
msg.sender insideThe calling contract
State changes allowedNo
Typical useSafe reads
delegatecall
Code executedTarget's
Storage usedCaller's
msg.sender insideThe original caller (unchanged)
State changes allowedYes, to the caller's storage
Typical useProxies and external libraries

With delegatecall, the target's code reads and writes the caller's storage slots by position, so the two contracts must agree on storage layout. Calling delegatecall on an address supplied by a user is extremely dangerous: the target can overwrite any of the caller's state, including its owner.

The following diagram contrasts the context of call and delegatecall.

call compared with delegatecall. With call, contract A runs contract B's code against B's own storage, and inside B msg.sender is A. With delegatecall, B's code runs against A's storage, by slot position, and msg.sender is still the original user. This is how proxies and libraries work, and why a delegatecall to an address a user supplies can overwrite any of the caller's state, including its owner.
delegatecall borrows code, not storage.
Sort into groups

call or delegatecall?

Exam skills 2.2.3

Decide which low-level call each behaviour describes when contract A calls contract B.

Log in to try this activity Interactive activities are free with an account.

Creating contracts from contracts

A contract can deploy another with new: Child c = new Child(arg); uses the CREATE opcode, so the child's address depends on the factory's address and nonce. Adding a salt, new Child{salt: s}(arg), uses CREATE2, whose address is computed as follows.

address = last 20 bytes of keccak256(0xff ++ factoryAddress ++ salt ++ keccak256(creationCode))

Because the address depends only on the factory, salt and code, it can be computed before deployment, shared with users, and reproduced on other chains. Wallet factories and decentralised exchanges use this for counterfactual deployment: funds can be sent to an address before the contract exists.

Select all that apply

CREATE2 addresses

Exam skills 2.2.3 · 4.2.1

Identify the inputs that determine a contract address created with CREATE2.

Log in to try this activity Interactive activities are free with an account.
Worked example

An ignored failure

A batch payout contract contains recipient.call{value: amount}(""); without using the returned values, then records the payout as done. Some recipients report that they were never paid. What is wrong?

Low-level call returns false when the recipient reverts or runs out of gas; it does not revert the payer. The contract ignored the flag and recorded failed payments as successful. It must check success and either revert or record the amount as still owed so that the recipient can withdraw it later.

Worked example

Predicting an address

A team wants each customer to have a deposit contract whose address can be shown to the customer before the contract is deployed, and wants the same address on mainnet and a layer 2 network. Which deployment method fits?

CREATE2 from a factory deployed at the same address on both networks, with a salt derived from the customer ID. The address depends only on the factory, salt and creation code, so it can be computed off chain in advance and will match on both networks. CREATE addresses depend on the factory's nonce, which changes with every deployment and differs between chains.

Check yourself

In a delegatecall from contract A to contract B, whose storage is modified and what is msg.sender?

Show answer

A's storage is modified, because B's code runs in A's context, and msg.sender stays the account that called A.

Exam Tips

Remember for the exam

  • High-level interface calls revert on failure; low-level calls return false.
  • delegatecall runs the target's code in the caller's storage and context.
  • Never delegatecall an untrusted or user-supplied address.
  • CREATE2 addresses depend on factory, salt and code, so they can be predicted.