Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Proxy pattern (upgradeable contracts) in Blockchain / Solidity - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the proxy contract's storage variable for the implementation address.

Blockchain / Solidity
address public [1];
Drag options to blanks, or click blank then click option'
Aowner
Bimplementation
Cproxy
Dadmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using owner or admin instead of implementation.
Forgetting to declare the variable as public.
2fill in blank
medium

Complete the fallback function to delegate calls to the implementation contract.

Blockchain / Solidity
fallback() external payable {
    (bool success, ) = [1].delegatecall(msg.data);
    require(success, "Delegatecall failed");
}
Drag options to blanks, or click blank then click option'
Aimplementation
Bowner
Cmsg.sender
Daddress(this)
Attempts:
3 left
💡 Hint
Common Mistakes
Using owner or msg.sender instead of implementation.
Using call instead of delegatecall.
3fill in blank
hard

Fix the error in the upgrade function to correctly update the implementation address.

Blockchain / Solidity
function upgradeTo(address newImplementation) external {
    require(newImplementation != address(0), "Invalid address");
    [1] = newImplementation;
}
Drag options to blanks, or click blank then click option'
Aowner
Badmin
Cproxy
Dimplementation
Attempts:
3 left
💡 Hint
Common Mistakes
Updating owner or admin instead of implementation.
Not checking for zero address before upgrade.
4fill in blank
hard

Fill both blanks to create a mapping for admin addresses and a modifier to restrict access.

Blockchain / Solidity
mapping(address => bool) public [1];

modifier only[2]() {
    require(admins[msg.sender], "Not admin");
    _;
}
Drag options to blanks, or click blank then click option'
Aadmins
Bowners
CAdmins
DOwners
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names between mapping and modifier.
Not capitalizing the modifier name correctly.
5fill in blank
hard

Fill all three blanks to write a dictionary comprehension that maps function selectors to implementation addresses if the selector is valid.

Blockchain / Solidity
mapping(bytes4 => address) public [1] = {
    [2]: [3] for selector in selectors if isValid(selector)
};
Drag options to blanks, or click blank then click option'
AselectorToImpl
Bselector
Cimplementation
Dselectors
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable names for keys or values.
Confusing the iterable selectors with the key variable selector.

Practice

(1/5)
1.

What is the main purpose of using the Proxy pattern in smart contracts?

easy
A. To upgrade contract logic without changing the contract address
B. To reduce gas fees by optimizing code
C. To create multiple copies of the same contract
D. To prevent any changes to the contract after deployment

Solution

  1. Step 1: Understand the Proxy pattern role

    The Proxy pattern allows a contract to forward calls to another contract, enabling upgrades.
  2. Step 2: Identify the main benefit

    This forwarding lets you change the logic contract without changing the proxy's address.
  3. Final Answer:

    To upgrade contract logic without changing the contract address -> Option A
  4. Quick Check:

    Proxy pattern = Upgrade logic without address change [OK]
Hint: Proxy pattern upgrades logic, keeps address same [OK]
Common Mistakes:
  • Thinking proxy reduces gas fees
  • Believing proxy creates contract copies
  • Assuming proxy prevents all changes
2.

Which Solidity keyword is used inside a proxy contract to forward calls to the implementation contract?

easy
A. delegatecall
B. call
C. transfer
D. send

Solution

  1. Step 1: Recall Solidity call types

    Solidity has several low-level calls: call, delegatecall, send, transfer.
  2. Step 2: Identify forwarding call for proxy

    Proxy contracts use delegatecall to run implementation code in proxy's context.
  3. Final Answer:

    delegatecall -> Option A
  4. Quick Check:

    Proxy forwarding uses delegatecall [OK]
Hint: Proxy uses delegatecall to keep storage context [OK]
Common Mistakes:
  • Confusing call with delegatecall
  • Using transfer or send which are for Ether
  • Not knowing delegatecall preserves storage
3.

Consider this simplified proxy contract snippet in Solidity:

contract Proxy {
    address implementation;
    
    fallback() external payable {
        (bool success, ) = implementation.delegatecall(msg.data);
        require(success);
    }
}

What happens if implementation address is zero?

medium
A. The contract will self-destruct
B. The call will succeed but do nothing
C. The fallback function will be ignored
D. The call will fail and revert the transaction

Solution

  1. Step 1: Understand delegatecall to zero address

    Calling delegatecall on address zero means no code to execute.
  2. Step 2: Effect of delegatecall failure

    delegatecall returns false on failure; require(success) then reverts transaction.
  3. Final Answer:

    The call will fail and revert the transaction -> Option D
  4. Quick Check:

    delegatecall to zero address = revert [OK]
Hint: delegatecall to zero address always fails [OK]
Common Mistakes:
  • Assuming call succeeds silently
  • Thinking fallback is skipped
  • Believing contract self-destructs
4.

Identify the bug in this proxy upgrade function:

function upgradeTo(address newImplementation) public {
    implementation = newImplementation;
}

What is the main issue?

medium
A. Implementation address is not validated
B. Missing event emission after upgrade
C. No access control, anyone can upgrade implementation
D. Function should be external, not public

Solution

  1. Step 1: Check function access control

    The function is public, so anyone can call it and change implementation.
  2. Step 2: Understand security risk

    Without restricting access, attackers can hijack the contract logic.
  3. Final Answer:

    No access control, anyone can upgrade implementation -> Option C
  4. Quick Check:

    Upgrade function needs access control [OK]
Hint: Always restrict upgrade function access [OK]
Common Mistakes:
  • Ignoring access control importance
  • Focusing only on event emission
  • Thinking public vs external affects security
5.

You want to upgrade a proxy contract to a new implementation that adds a new state variable. What must you ensure to avoid breaking storage layout?

hard
A. Rearrange all variables in the new implementation for optimization
B. Add new variables only at the end of existing storage variables
C. Remove unused variables from the old implementation
D. Change variable types to reduce storage size

Solution

  1. Step 1: Understand storage layout importance

    Proxy pattern requires storage layout consistency between implementations.
  2. Step 2: Correct way to add variables

    New variables must be appended to avoid overwriting existing storage slots.
  3. Final Answer:

    Add new variables only at the end of existing storage variables -> Option B
  4. Quick Check:

    Storage layout consistency = append variables [OK]
Hint: Append new variables to preserve storage layout [OK]
Common Mistakes:
  • Rearranging variables breaks storage
  • Removing old variables causes data loss
  • Changing types shifts storage slots