0
0
Blockchain / Solidityprogramming~20 mins

Gas optimization for L2 in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
L2 Gas Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Gas cost difference between storage and memory

Consider the following Solidity function on an L2 chain. What is the main difference in gas cost when using storage vs memory for the array?

Blockchain / Solidity
pragma solidity ^0.8.19;

contract GasTest {
    uint[] public data;

    function storeData(uint[] calldata input) external {
        for (uint i = 0; i < input.length; i++) {
            data.push(input[i]);
        }
    }

    function processData() external view returns (uint sum) {
        uint[] storage arr = data;
        for (uint i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
    }

    function processDataMemory() external view returns (uint sum) {
        uint[] memory arr = data;
        for (uint i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
    }
}
ABoth <code>storage</code> and <code>memory</code> have the same gas cost on L2 because storage reads are free.
BUsing <code>memory</code> copies the entire array from storage to memory, increasing gas cost due to data copying on L2.
CUsing <code>storage</code> for the array reads directly from blockchain storage, which is cheaper on L2 than copying to <code>memory</code>.
DUsing <code>memory</code> is cheaper because it avoids storage reads, which are very expensive on L2.
Attempts:
2 left
💡 Hint

Think about what happens when you copy data from storage to memory on L2.

Predict Output
intermediate
2:00remaining
Effect of calldata vs memory on gas usage

What is the gas cost difference when using calldata vs memory for function parameters on an L2 chain?

Blockchain / Solidity
pragma solidity ^0.8.19;

contract CalldataTest {
    function useCalldata(uint[] calldata input) external pure returns (uint) {
        uint sum = 0;
        for (uint i = 0; i < input.length; i++) {
            sum += input[i];
        }
        return sum;
    }

    function useMemory(uint[] memory input) external pure returns (uint) {
        uint sum = 0;
        for (uint i = 0; i < input.length; i++) {
            sum += input[i];
        }
        return sum;
    }
}
AUsing <code>calldata</code> is more expensive because it requires decoding data on each access.
BUsing <code>memory</code> is cheaper because it allows faster access to data on L2.
CBoth <code>calldata</code> and <code>memory</code> cost the same gas on L2.
DUsing <code>calldata</code> is cheaper because it avoids copying data into memory on L2.
Attempts:
2 left
💡 Hint

Consider how calldata is handled differently from memory in Solidity.

🔧 Debug
advanced
2:30remaining
Unexpected high gas cost in loop with storage writes

Why does the following Solidity function consume unexpectedly high gas on an L2 chain?

Blockchain / Solidity
pragma solidity ^0.8.19;

contract LoopGas {
    mapping(uint => uint) public data;

    function updateData(uint n) external {
        for (uint i = 0; i < n; i++) {
            data[i] = i * 2;
        }
    }
}
ABecause each storage write inside the loop costs gas, and writing to storage is expensive even on L2.
BBecause the loop variable <code>i</code> is not declared as <code>uint256</code>, causing overflow errors.
CBecause the mapping <code>data</code> is not initialized, causing runtime errors.
DBecause the function lacks the <code>payable</code> modifier, causing gas to be refunded.
Attempts:
2 left
💡 Hint

Think about the cost of writing to storage repeatedly in a loop on L2.

📝 Syntax
advanced
2:00remaining
Correct syntax for unchecked arithmetic to save gas

Which option correctly uses unchecked to save gas on L2 by skipping overflow checks?

Blockchain / Solidity
pragma solidity ^0.8.19;

contract UncheckedExample {
    function sum(uint a, uint b) external pure returns (uint) {
        uint c;
        // Insert unchecked block here
        return c;
    }
}
A{ unchecked c = a + b; }
Bunchecked c = a + b;
Cunchecked { c = a + b; }
Dc = unchecked(a + b);
Attempts:
2 left
💡 Hint

Recall the correct syntax for the unchecked block in Solidity.

🚀 Application
expert
3:00remaining
Optimizing batch token transfers on L2

You want to optimize a batch token transfer function on an L2 chain to minimize gas. Which approach will reduce gas the most?

AUse a single <code>transferFrom</code> call for the total amount, then update balances in memory and write once to storage.
BUse <code>delegatecall</code> to an external contract for each transfer to save gas.
CUse multiple <code>transferFrom</code> calls but mark the function <code>payable</code> to get gas refunds.
DUse a single <code>transferFrom</code> call per recipient inside a loop, writing to storage each time.
Attempts:
2 left
💡 Hint

Think about minimizing storage writes and external calls on L2.