Network
Launch Date
Consensus
Note
Sepolia
Oct 2021
PoW
Like-for-like representation of Ethereum
Görli
Jan 2019
PoA
Proof-of-Authority
Kiln
Mar 2022
PoS
Post-Merge (for ETH2), shadow fork of the mainnet
Kintsugi
Dec 2021
PoS
DEPRECATED, use Kiln; post-Merge (for ETH2)
Ropsten
Nov 2016
PoW
DEPRECATED, use Sepolia; the Merge to happen on Jun 8, 2022
Rinkeby
Apr 2017
PoA
DEPRECATED, use Görli and Görli Faucet
Kovan
Mar 2017
PoA
DEPRECATED, use Sepolia or Görli
List of active and deprecated Ethereum testnets, including Kintsugi.
Features
Optimistic rollup 
ZK-rollup 
Proof
Uses fraud proofs to prove transaction validity. 
Uses validity (zero-knowledge) proofs to prove transaction validity. 
Capital efficiency
Requires waiting through a 1-week delay (dispute period) before withdrawing funds. 
Users can withdraw funds immediately because validity proofs provide incontrovertible evidence of the authenticity of off-chain transactions. 
Data compression
Publishes full transaction data as calldata to Ethereum Mainnet, which increases rollup costs. 
Doesn't need to publish transaction data on Ethereum because ZK-SNARKs and ZK-STARKs already guarantee the accuracy of the rollup state. 
EVM compatibility
Uses a simulation of the Ethereum Virtual Machine (EVM), which allows it to run arbitrary logic and support smart contracts. 
Doesn't widely support EVM computation, although a few EVM-compatible ZK-rollups have appeared. 
Rollup costs
Reduces costs since it publishes minimal data on Ethereum and doesn't have to post proofs for transactions, except in special circumstances. 
Faces higher overhead from costs involved in generating and verifying proofs for every transaction block. ZK proofs require specialized, expensive hardware to create and have high on-chain verification costs. 
Trust assumptions
Doesn't require a trusted setup. 
Requires a trusted setup to work. 
Liveness requirements
Verifiers are needed to keep tabs on the actual rollup state and the one referenced in the state root to detect fraud. 
Users don't need someone to watch the L2 chain to detect fraud. 
Security properties 
Relies on cryptoeconomic incentives to assure users of rollup security. 
Relies on cryptographic guarantees for security. 
Start building
on Alchemy.
Sign up for free
Start building on Optimism.
Sign up for free
Start building on Arbitrum.
Sign up for free
Start building on Ethereum.
Sign up for free
Start building on Polygon.
Sign up for free
Start building on Starknet.
Sign up for free
Start building on Flow.
Sign up for free
kiln faucet
Get free Kiln ETH.
Start building today
Goerli faucet
Get free Goerli ETH.
Start building today
SEPOLIA FAUCET
Get free Sepolia ETH.
Start Building Today
mumbai faucet
Get free Mumbai Matic.
Start building today
rinkeby faucet
Get free Rinkeby
ETH.
Start building today
Start building on Ethereum.
Get started for free
Start building on Ethereum.
Get started for free
Start building on Flow.
Get started for free
Start building on Polygon.
Get started for free
Start building on Starknet.
Get started for free
Start building on Optimism.
Get started for free
Start building on Solana.
Get started for free
Start building on Solana.
Sign up for beta access
Start building on Solana.
Join the waitlist
Arbitrum logo
Start building on Arbitrum.
Get started for free
Build with Alchemy's
Gas Manager & Bundler APIs
Learn
Solidity at
Alchemy
University
Get started today
Build with Alchemy's
Gas Manager & Bundler APIs
curl 
https://release.solana.com/v1.10.32/solana-install-init-x86_64-pc-windows-msvc.exe 
--output 
C:\solana-install-tmp\solana-install-init.exe 
--create-dirs
Learn Solidity
YUL CONTRACT CALLS

How do Yul Contract Calls Work?

Learn how Contract Calls Work Within the Yul Smart Contract Programming Language
Last Updated:
Table of Contents
Table of Contents
Table of Contents

{{building-on-ethereum}}

Yul is an intermediate programming language that can be used to write a form of assembly language inside smart contracts. Now that you understand syntax, storage, and how memory works in Yul, it’s time to call contracts!

How do contract calls work in Yul?

In the final section of this series we will look at how contract calls work in Yul. Before we dive into some examples we need to learn some more Yul Operations first. Let’s take a look.

List of Yul operations and their explanations
Yul Operations

Ok, now let’s look at some new contracts for these examples. First, let’s look at the contract we will be calling.

pragma solidity^0.8.17;
 
contract CallMe {
 
    uint256 public var1 = 1;
    uint256 public var2 = 2;
 
 
    function a(uint256 _var1, uint256 _var2) external payable returns(uint256, uint256) {
 
        // requires 1 ether was sent to contract
        require(msg.value >= 1 ether);
 
        // updates var1 & var2
        var1 = _var1;
        var2 = _var2;
 
        // returns var1 & var2
        return (var1, var2);
       
    }
 
 
    function b() external view returns(uint256, uint256) {
        return (var1, var2);
    }
 
}

This contract has two storage variables var1 and var2 that are stored in storage slots 1 and 2 respectively.

Function a() requires the user to send at least 1 ether to the contract, otherwise it reverts.

Next, function a() updates var1 and var2 and returns them.

Function b() simply reads var1 and var2 and returns them.

Before we move onto our contract that calls contract CallMe, we need to take a minute to understand function selectors.

Let’s look at the following call data for a transaction 0x773d45e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002.

The first 4 bytes of the calldata is what's called the function selector (0x773d45e0). This is how the EVM knows what function you want to call. We derive the function selector by getting the first 4 bytes of the hash of a string of the function signature.

So function a()’s signature would be a(uint256,uint256).

Taking the hash of this string gives us: 0x773d45e097aa76a22159880d254a5f1db8365bc2d0f0987a82bda7dfd3b9c8aa.

Looking at the first 4 bytes we see it equals 0x773d45e0.

Notice the lack of spaces in the signature. This is important because adding spaces will give us a completely different hash. You do not have to worry about getting the selectors for our code examples, I will provide them.

Let’s start by looking at the storage layout.

uint256 public var1;
uint256 public var2;
bytes4 selectorA = 0x773d45e0;
bytes4 selectorB = 0x4df7e3d0;

Notice how var1 & var2 have the same layout as contract CallMe. You may remember me saying that the layout has to be the same as our other contract for delegatecall() to work properly.

We satisfy those needs and are able to have other variables (selectorA & selectorB) as long as our new variables are appended to the end. This prevents any storage collisions.

We are now ready to make our first contract call.

How to Use staticcall() in Yul

Let’s start with something simple, staticcall(). Here is our function:

function getVars(address _callMe) external view returns(uint256, uint256) {
 
    assembly {
 
        // load slot 2 from memory
        let slot2 := sload(2)
       
        // shift selectorA off
        let funcSelector := shr( 32, slot2)
 
        // store selectorB to memory location 0x80
        mstore(0x00, funcSelector)
 
        // static call CallMe
        let result := staticcall(gas(), _callMe, 0x1c, 0x20, 0x80, 0xc0)
 
        // check if call was succesfull, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x80, 0xc0)
 
    }
 
}

Here’s what is happening:

  • Retrieve b()’s function selector from storage by loading slot 2 (both selectors are packed into one slot)
  • Shift right by 4 bytes (32 bits) to isolate selectorB
  • Store the function selector in the scratch space of memory
  • Make our static call
  • Pass in gas() (you can also specify the amount of gas)
  • Pass in the parameter _callMe for the contract address*
  • Check if the function call was successful, otherwise we return with no data. 
  • Return our data from memory and see the values 1 and 2

Note: *0x1c and 0x20 say we want to pass the last 4 bytes of what we stored to the scratch space. The last two staticcall() parameters specify we want to store the return data in memory locations 0x80 - 0xc0.

How to Use call() in Yul

Next let’s look at call(). We are going to call function a() from CallMe. Remember to send at least 1 ether to the contract! I will be passing in 3 and 4 as _var1 & _var2 for this example. 

Here is the code:

function callA(address _callMe, uint256 _var1, uint256 _var2) external payable returns (bytes memory) {
 
    assembly {
 
        // load slot 2
        let slot2 := sload(2)
 
        // isolate selectorA
        let mask := 0x000000000000000000000000000000000000000000000000000000000ffffffff
        let funcSelector := and(mask, slot2)
 
        // store function selectorA
        mstore(0x80, funcSelector)
 
        // copies calldata to memory location 0xa0
        // leaves out function selector and _callMe
        calldatacopy(0xa0, 0x24, sub( calldatasize(), 0x20 ) )
 
        // call contract
        let result := call(gas(), _callMe, callvalue(), 0x9c, 0xe0, 0x100, 0x120 )
 
        // check if call was succesfull, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x100, 0x120)
 
    }
 
 
}

Ok, so similar to our last example we have to load slot2. This time, however, we are going to mask selectorB to isolate selectorA. 

Now we will store the selector at 0x80. 

Since we need parameters from the calldata, we are going to use calldatacopy(). We are telling calldatacopy() to:

  • store our calldata at memory location 0xa0
  • skip the first 36 bytes*
  • store the size of the calldata minus 36 bytes

Note: *the first 4 bytes we skip are the function selector for callA() and the next 32 bytes is the address of callMe.

Now we are ready to make our contract call! 

Like last time, we pass in gas() and _callMe. However, this time we pass in our call data from 0x9c (last 4 bytes of 0x80 memory series) - 0xe0, and store our data in memory location 0x100 - 0x120. 

Again, we check if the call was successful and return our output. If we check contract CallMe we see the values were successfully updated to 3 and 4.

For extra clarification for what’s happening, here is the memory layout right before we return:

Table of memory locations and their current stored values
Current Memory

How to Use Delegate Call in Yul

In our last section we will look at delegatecall(). The code will look almost identical to call() with only one change.

function delgatecallA(address _callMe, uint256 _var1, uint256 _var2) external payable returns (bytes memory) {
 
    assembly {
 
        // load slot 2
        let slot2 := sload(2)
 
        // isolate selectorA
        let mask := 0x000000000000000000000000000000000000000000000000000000000ffffffff
        let funcSelector := and(mask, slot2)
 
        // store function selectorA
        mstore(0x80, funcSelector)
 
        // copies calldata to memory location 0xa0
        // leaves out function selector and _callMe
        calldatacopy(0xa0, 0x24, sub( calldatasize(), 0x20 ) )
 
        // call contract
        let result := delegatecall(gas(), _callMe, 0x9c, 0xe0, 0x100, 0x120 )
 
        // check if call was successful, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x100, 0x120)
 
    }
 
 
}

The only change we made was changing call() to delegatecall() and removing callvalue(). 

We do not need a callvalue(), because delegate call executes the code from CallMe inside its own state. Therefore the require() statement in a() is checking if ether was sent to our Caller contract. If we check var1 and var2 in CallMe, we see no changes. However, var1 and var2 in our Caller contract was updated successfully.

This wraps up our section on contract calls! Next, learn how Memory, Storage, and Smart Contract Calls work in Yul.

ALCHEMY SUPERNODE - ETHEREUM NODE API

Scale to any size, without any errors

Alchemy Supernode finally makes it possible to scale blockchain applications without all the headaches. Plus, our legendary support will guide you every step of the way.

Get started for free
Supernode footer
Learn Solidity
YUL CONTRACT CALLS

How do Yul Contract Calls Work?

Learn how Contract Calls Work Within the Yul Smart Contract Programming Language
Last Updated:
Last Updated:
March 14, 2023
Don't miss an update
Sign up for our newsletter to get alpha, key insights, and killer resources.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Table of Contents

Talk to an Expert

Learn how Alchemy's blockchain developer tools can help your business succeed in web3!
Valid number
Thank you! An Alchemy expert will be in touch with you shortly!
Oops! Something went wrong while submitting the form.

{{building-on-ethereum}}

Yul is an intermediate programming language that can be used to write a form of assembly language inside smart contracts. Now that you understand syntax, storage, and how memory works in Yul, it’s time to call contracts!

How do contract calls work in Yul?

In the final section of this series we will look at how contract calls work in Yul. Before we dive into some examples we need to learn some more Yul Operations first. Let’s take a look.

List of Yul operations and their explanations
Yul Operations

Ok, now let’s look at some new contracts for these examples. First, let’s look at the contract we will be calling.

pragma solidity^0.8.17;
 
contract CallMe {
 
    uint256 public var1 = 1;
    uint256 public var2 = 2;
 
 
    function a(uint256 _var1, uint256 _var2) external payable returns(uint256, uint256) {
 
        // requires 1 ether was sent to contract
        require(msg.value >= 1 ether);
 
        // updates var1 & var2
        var1 = _var1;
        var2 = _var2;
 
        // returns var1 & var2
        return (var1, var2);
       
    }
 
 
    function b() external view returns(uint256, uint256) {
        return (var1, var2);
    }
 
}

This contract has two storage variables var1 and var2 that are stored in storage slots 1 and 2 respectively.

Function a() requires the user to send at least 1 ether to the contract, otherwise it reverts.

Next, function a() updates var1 and var2 and returns them.

Function b() simply reads var1 and var2 and returns them.

Before we move onto our contract that calls contract CallMe, we need to take a minute to understand function selectors.

Let’s look at the following call data for a transaction 0x773d45e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002.

The first 4 bytes of the calldata is what's called the function selector (0x773d45e0). This is how the EVM knows what function you want to call. We derive the function selector by getting the first 4 bytes of the hash of a string of the function signature.

So function a()’s signature would be a(uint256,uint256).

Taking the hash of this string gives us: 0x773d45e097aa76a22159880d254a5f1db8365bc2d0f0987a82bda7dfd3b9c8aa.

Looking at the first 4 bytes we see it equals 0x773d45e0.

Notice the lack of spaces in the signature. This is important because adding spaces will give us a completely different hash. You do not have to worry about getting the selectors for our code examples, I will provide them.

Let’s start by looking at the storage layout.

uint256 public var1;
uint256 public var2;
bytes4 selectorA = 0x773d45e0;
bytes4 selectorB = 0x4df7e3d0;

Notice how var1 & var2 have the same layout as contract CallMe. You may remember me saying that the layout has to be the same as our other contract for delegatecall() to work properly.

We satisfy those needs and are able to have other variables (selectorA & selectorB) as long as our new variables are appended to the end. This prevents any storage collisions.

We are now ready to make our first contract call.

How to Use staticcall() in Yul

Let’s start with something simple, staticcall(). Here is our function:

function getVars(address _callMe) external view returns(uint256, uint256) {
 
    assembly {
 
        // load slot 2 from memory
        let slot2 := sload(2)
       
        // shift selectorA off
        let funcSelector := shr( 32, slot2)
 
        // store selectorB to memory location 0x80
        mstore(0x00, funcSelector)
 
        // static call CallMe
        let result := staticcall(gas(), _callMe, 0x1c, 0x20, 0x80, 0xc0)
 
        // check if call was succesfull, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x80, 0xc0)
 
    }
 
}

Here’s what is happening:

  • Retrieve b()’s function selector from storage by loading slot 2 (both selectors are packed into one slot)
  • Shift right by 4 bytes (32 bits) to isolate selectorB
  • Store the function selector in the scratch space of memory
  • Make our static call
  • Pass in gas() (you can also specify the amount of gas)
  • Pass in the parameter _callMe for the contract address*
  • Check if the function call was successful, otherwise we return with no data. 
  • Return our data from memory and see the values 1 and 2

Note: *0x1c and 0x20 say we want to pass the last 4 bytes of what we stored to the scratch space. The last two staticcall() parameters specify we want to store the return data in memory locations 0x80 - 0xc0.

How to Use call() in Yul

Next let’s look at call(). We are going to call function a() from CallMe. Remember to send at least 1 ether to the contract! I will be passing in 3 and 4 as _var1 & _var2 for this example. 

Here is the code:

function callA(address _callMe, uint256 _var1, uint256 _var2) external payable returns (bytes memory) {
 
    assembly {
 
        // load slot 2
        let slot2 := sload(2)
 
        // isolate selectorA
        let mask := 0x000000000000000000000000000000000000000000000000000000000ffffffff
        let funcSelector := and(mask, slot2)
 
        // store function selectorA
        mstore(0x80, funcSelector)
 
        // copies calldata to memory location 0xa0
        // leaves out function selector and _callMe
        calldatacopy(0xa0, 0x24, sub( calldatasize(), 0x20 ) )
 
        // call contract
        let result := call(gas(), _callMe, callvalue(), 0x9c, 0xe0, 0x100, 0x120 )
 
        // check if call was succesfull, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x100, 0x120)
 
    }
 
 
}

Ok, so similar to our last example we have to load slot2. This time, however, we are going to mask selectorB to isolate selectorA. 

Now we will store the selector at 0x80. 

Since we need parameters from the calldata, we are going to use calldatacopy(). We are telling calldatacopy() to:

  • store our calldata at memory location 0xa0
  • skip the first 36 bytes*
  • store the size of the calldata minus 36 bytes

Note: *the first 4 bytes we skip are the function selector for callA() and the next 32 bytes is the address of callMe.

Now we are ready to make our contract call! 

Like last time, we pass in gas() and _callMe. However, this time we pass in our call data from 0x9c (last 4 bytes of 0x80 memory series) - 0xe0, and store our data in memory location 0x100 - 0x120. 

Again, we check if the call was successful and return our output. If we check contract CallMe we see the values were successfully updated to 3 and 4.

For extra clarification for what’s happening, here is the memory layout right before we return:

Table of memory locations and their current stored values
Current Memory

How to Use Delegate Call in Yul

In our last section we will look at delegatecall(). The code will look almost identical to call() with only one change.

function delgatecallA(address _callMe, uint256 _var1, uint256 _var2) external payable returns (bytes memory) {
 
    assembly {
 
        // load slot 2
        let slot2 := sload(2)
 
        // isolate selectorA
        let mask := 0x000000000000000000000000000000000000000000000000000000000ffffffff
        let funcSelector := and(mask, slot2)
 
        // store function selectorA
        mstore(0x80, funcSelector)
 
        // copies calldata to memory location 0xa0
        // leaves out function selector and _callMe
        calldatacopy(0xa0, 0x24, sub( calldatasize(), 0x20 ) )
 
        // call contract
        let result := delegatecall(gas(), _callMe, 0x9c, 0xe0, 0x100, 0x120 )
 
        // check if call was successful, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x100, 0x120)
 
    }
 
 
}

The only change we made was changing call() to delegatecall() and removing callvalue(). 

We do not need a callvalue(), because delegate call executes the code from CallMe inside its own state. Therefore the require() statement in a() is checking if ether was sent to our Caller contract. If we check var1 and var2 in CallMe, we see no changes. However, var1 and var2 in our Caller contract was updated successfully.

This wraps up our section on contract calls! Next, learn how Memory, Storage, and Smart Contract Calls work in Yul.

Yul is an intermediate programming language that can be used to write a form of assembly language inside smart contracts. Now that you understand syntax, storage, and how memory works in Yul, it’s time to call contracts!

How do contract calls work in Yul?

In the final section of this series we will look at how contract calls work in Yul. Before we dive into some examples we need to learn some more Yul Operations first. Let’s take a look.

List of Yul operations and their explanations
Yul Operations

Ok, now let’s look at some new contracts for these examples. First, let’s look at the contract we will be calling.

pragma solidity^0.8.17;
 
contract CallMe {
 
    uint256 public var1 = 1;
    uint256 public var2 = 2;
 
 
    function a(uint256 _var1, uint256 _var2) external payable returns(uint256, uint256) {
 
        // requires 1 ether was sent to contract
        require(msg.value >= 1 ether);
 
        // updates var1 & var2
        var1 = _var1;
        var2 = _var2;
 
        // returns var1 & var2
        return (var1, var2);
       
    }
 
 
    function b() external view returns(uint256, uint256) {
        return (var1, var2);
    }
 
}

This contract has two storage variables var1 and var2 that are stored in storage slots 1 and 2 respectively.

Function a() requires the user to send at least 1 ether to the contract, otherwise it reverts.

Next, function a() updates var1 and var2 and returns them.

Function b() simply reads var1 and var2 and returns them.

Before we move onto our contract that calls contract CallMe, we need to take a minute to understand function selectors.

Let’s look at the following call data for a transaction 0x773d45e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002.

The first 4 bytes of the calldata is what's called the function selector (0x773d45e0). This is how the EVM knows what function you want to call. We derive the function selector by getting the first 4 bytes of the hash of a string of the function signature.

So function a()’s signature would be a(uint256,uint256).

Taking the hash of this string gives us: 0x773d45e097aa76a22159880d254a5f1db8365bc2d0f0987a82bda7dfd3b9c8aa.

Looking at the first 4 bytes we see it equals 0x773d45e0.

Notice the lack of spaces in the signature. This is important because adding spaces will give us a completely different hash. You do not have to worry about getting the selectors for our code examples, I will provide them.

Let’s start by looking at the storage layout.

uint256 public var1;
uint256 public var2;
bytes4 selectorA = 0x773d45e0;
bytes4 selectorB = 0x4df7e3d0;

Notice how var1 & var2 have the same layout as contract CallMe. You may remember me saying that the layout has to be the same as our other contract for delegatecall() to work properly.

We satisfy those needs and are able to have other variables (selectorA & selectorB) as long as our new variables are appended to the end. This prevents any storage collisions.

We are now ready to make our first contract call.

How to Use staticcall() in Yul

Let’s start with something simple, staticcall(). Here is our function:

function getVars(address _callMe) external view returns(uint256, uint256) {
 
    assembly {
 
        // load slot 2 from memory
        let slot2 := sload(2)
       
        // shift selectorA off
        let funcSelector := shr( 32, slot2)
 
        // store selectorB to memory location 0x80
        mstore(0x00, funcSelector)
 
        // static call CallMe
        let result := staticcall(gas(), _callMe, 0x1c, 0x20, 0x80, 0xc0)
 
        // check if call was succesfull, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x80, 0xc0)
 
    }
 
}

Here’s what is happening:

  • Retrieve b()’s function selector from storage by loading slot 2 (both selectors are packed into one slot)
  • Shift right by 4 bytes (32 bits) to isolate selectorB
  • Store the function selector in the scratch space of memory
  • Make our static call
  • Pass in gas() (you can also specify the amount of gas)
  • Pass in the parameter _callMe for the contract address*
  • Check if the function call was successful, otherwise we return with no data. 
  • Return our data from memory and see the values 1 and 2

Note: *0x1c and 0x20 say we want to pass the last 4 bytes of what we stored to the scratch space. The last two staticcall() parameters specify we want to store the return data in memory locations 0x80 - 0xc0.

How to Use call() in Yul

Next let’s look at call(). We are going to call function a() from CallMe. Remember to send at least 1 ether to the contract! I will be passing in 3 and 4 as _var1 & _var2 for this example. 

Here is the code:

function callA(address _callMe, uint256 _var1, uint256 _var2) external payable returns (bytes memory) {
 
    assembly {
 
        // load slot 2
        let slot2 := sload(2)
 
        // isolate selectorA
        let mask := 0x000000000000000000000000000000000000000000000000000000000ffffffff
        let funcSelector := and(mask, slot2)
 
        // store function selectorA
        mstore(0x80, funcSelector)
 
        // copies calldata to memory location 0xa0
        // leaves out function selector and _callMe
        calldatacopy(0xa0, 0x24, sub( calldatasize(), 0x20 ) )
 
        // call contract
        let result := call(gas(), _callMe, callvalue(), 0x9c, 0xe0, 0x100, 0x120 )
 
        // check if call was succesfull, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x100, 0x120)
 
    }
 
 
}

Ok, so similar to our last example we have to load slot2. This time, however, we are going to mask selectorB to isolate selectorA. 

Now we will store the selector at 0x80. 

Since we need parameters from the calldata, we are going to use calldatacopy(). We are telling calldatacopy() to:

  • store our calldata at memory location 0xa0
  • skip the first 36 bytes*
  • store the size of the calldata minus 36 bytes

Note: *the first 4 bytes we skip are the function selector for callA() and the next 32 bytes is the address of callMe.

Now we are ready to make our contract call! 

Like last time, we pass in gas() and _callMe. However, this time we pass in our call data from 0x9c (last 4 bytes of 0x80 memory series) - 0xe0, and store our data in memory location 0x100 - 0x120. 

Again, we check if the call was successful and return our output. If we check contract CallMe we see the values were successfully updated to 3 and 4.

For extra clarification for what’s happening, here is the memory layout right before we return:

Table of memory locations and their current stored values
Current Memory

How to Use Delegate Call in Yul

In our last section we will look at delegatecall(). The code will look almost identical to call() with only one change.

function delgatecallA(address _callMe, uint256 _var1, uint256 _var2) external payable returns (bytes memory) {
 
    assembly {
 
        // load slot 2
        let slot2 := sload(2)
 
        // isolate selectorA
        let mask := 0x000000000000000000000000000000000000000000000000000000000ffffffff
        let funcSelector := and(mask, slot2)
 
        // store function selectorA
        mstore(0x80, funcSelector)
 
        // copies calldata to memory location 0xa0
        // leaves out function selector and _callMe
        calldatacopy(0xa0, 0x24, sub( calldatasize(), 0x20 ) )
 
        // call contract
        let result := delegatecall(gas(), _callMe, 0x9c, 0xe0, 0x100, 0x120 )
 
        // check if call was successful, else revert
        if iszero(result) {
            revert(0,0)
        }
 
        // return values from memory
        return (0x100, 0x120)
 
    }
 
 
}

The only change we made was changing call() to delegatecall() and removing callvalue(). 

We do not need a callvalue(), because delegate call executes the code from CallMe inside its own state. Therefore the require() statement in a() is checking if ether was sent to our Caller contract. If we check var1 and var2 in CallMe, we see no changes. However, var1 and var2 in our Caller contract was updated successfully.

This wraps up our section on contract calls! Next, learn how Memory, Storage, and Smart Contract Calls work in Yul.

{{building-on-ethereum}}

Contact Us

Talk to an expert at Alchemy to answer all of your product questions.
Valid number
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Build blockchain magic with Alchemy

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get started for free