0
0
Blockchain / Solidityprogramming~10 mins

Proxy pattern (upgradeable contracts) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Proxy pattern (upgradeable contracts)
Client calls Proxy Contract
Proxy forwards call to Logic Contract
Logic Contract executes code
Logic Contract returns result
Proxy returns result to Client
Upgrade? Replace Logic Contract address in Proxy
Next calls use new Logic Contract
The proxy contract receives calls and forwards them to a logic contract. Upgrades happen by changing the logic contract address in the proxy.
Execution Sample
Blockchain / Solidity
contract Proxy {
  address logic;
  function call(bytes calldata data) external returns (bytes memory) {
    (bool success, bytes memory result) = logic.delegatecall(data);
    require(success, "Delegatecall failed");
    return result;
  }
  function upgrade(address newLogic) external {
    logic = newLogic;
  }
}
A proxy contract forwards calls to a logic contract and can upgrade by changing the logic address.
Execution Table
StepActionProxy logic addressCall dataDelegatecall targetResult
1Deploy Proxy with logic = LogicV1LogicV1---
2Client calls Proxy.call with data for function foo()LogicV1foo()LogicV1foo() result from LogicV1
3Upgrade Proxy logic to LogicV2LogicV2---
4Client calls Proxy.call with data for function foo()LogicV2foo()LogicV2foo() result from LogicV2
5Client calls Proxy.call with data for function bar()LogicV2bar()LogicV2bar() result from LogicV2
6No more upgrades, calls continue to LogicV2LogicV2foo()LogicV2foo() result from LogicV2
💡 Execution stops when no more calls or upgrades happen.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
logicnullLogicV1LogicV2LogicV2
call data-foo()foo()foo() or bar()
result-foo() result from LogicV1foo() result from LogicV2foo() or bar() result from LogicV2
Key Moments - 3 Insights
Why does the proxy not contain the logic code itself?
Because the proxy only forwards calls to the logic contract using delegatecall, so the logic code stays in the logic contract and can be upgraded by changing its address (see execution_table steps 2 and 4).
How does upgrading the logic contract affect existing state?
The proxy holds the state, and delegatecall runs logic in the proxy's context, so upgrading logic changes behavior but keeps the same state (see variable 'logic' changes in variable_tracker).
What happens if the logic contract address is not updated?
Calls keep forwarding to the old logic contract, so no new features or fixes apply (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the logic address after step 3?
ALogicV1
BLogicV2
Cnull
DProxy itself
💡 Hint
Check the 'Proxy logic address' column at step 3 in execution_table.
At which step does the proxy start forwarding calls to LogicV2?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Delegatecall target' column in execution_table for when it changes to LogicV2.
If the upgrade function is never called, what happens to the calls?
AThey forward to LogicV1
BThey fail with error
CThey forward to LogicV2
DThey execute in Proxy contract
💡 Hint
See variable_tracker 'logic' variable if it never changes from LogicV1.
Concept Snapshot
Proxy pattern lets a contract forward calls to a logic contract.
The proxy holds state; logic contract holds code.
Upgrade by changing logic contract address in proxy.
Calls use delegatecall to run logic in proxy context.
This enables upgradeable contracts without losing state.
Full Transcript
The proxy pattern in upgradeable contracts works by having a proxy contract that receives calls from clients. Instead of executing logic itself, the proxy forwards these calls to a separate logic contract using delegatecall. This means the logic contract's code runs in the proxy's storage context, so the proxy holds the state. When an upgrade is needed, the proxy's stored logic contract address is changed to a new logic contract with updated code. Calls then forward to the new logic contract, enabling upgrades without losing stored data. The execution table shows deploying the proxy with LogicV1, calling functions forwarded to LogicV1, upgrading to LogicV2, and subsequent calls forwarding to LogicV2. Variables like 'logic' track the current logic contract address. Key moments clarify why the proxy doesn't hold logic code, how state is preserved, and what happens if no upgrade occurs. The visual quiz tests understanding of when upgrades happen and how calls are forwarded. This pattern is essential for blockchain contracts that need to evolve after deployment.