0
0
Blockchain / Solidityprogramming~30 mins

Proxy pattern (upgradeable contracts) in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Proxy Pattern for Upgradeable Contracts
📖 Scenario: You are building a simple blockchain smart contract system where you want to keep your contract upgradeable without changing its address. This is useful because users interact with the same contract address, but the logic can be updated later.
🎯 Goal: Build a proxy contract that delegates calls to a logic contract. You will create a simple logic contract with a counter, a proxy contract that forwards calls, and then show how to call the proxy to interact with the logic.
📋 What You'll Learn
Create a logic contract with a counter variable and functions to increment and get the counter
Create a proxy contract that stores the logic contract address and delegates calls to it
Add a function in the proxy to update the logic contract address
Demonstrate calling the proxy contract to increment and get the counter value
💡 Why This Matters
🌍 Real World
Upgradeable contracts let blockchain projects fix bugs or add features without changing the contract address users interact with.
💼 Career
Understanding proxy patterns is important for blockchain developers working on smart contract upgrades and maintaining decentralized applications.
Progress0 / 4 steps
1
Create the Logic Contract
Write a Solidity contract called LogicContract with a public uint256 variable called counter. Add a function increment() that increases counter by 1, and a function getCounter() that returns the current counter value.
Blockchain / Solidity
Need a hint?

Think of counter as a box that holds a number. The increment() function adds 1 to that number. The getCounter() function lets you see the number.

2
Create the Proxy Contract with Logic Address
Write a Solidity contract called ProxyContract that has an address variable called logicContract. Add a constructor that sets logicContract to an address passed as a parameter. This will store the address of the logic contract.
Blockchain / Solidity
Need a hint?

The proxy needs to remember where the logic contract lives. The constructor sets this address when the proxy is created.

3
Add Delegatecall and Upgrade Function
In ProxyContract, add a function upgrade(address _newLogic) that updates logicContract to _newLogic. Also add a fallback() function that uses delegatecall to forward all calls to logicContract. This will let the proxy run the logic contract's code.
Blockchain / Solidity
Need a hint?

The fallback() function catches all calls and forwards them to the logic contract using delegatecall. The upgrade() function lets you change the logic contract address later.

4
Interact with Proxy to Increment and Get Counter
Write a script or code snippet that calls increment() on the ProxyContract and then calls getCounter() on the ProxyContract. Print the returned counter value. This shows the proxy forwarding calls to the logic contract.
Blockchain / Solidity
Need a hint?

This test contract creates the logic and proxy contracts, calls increment() through the proxy, then calls getCounter() through the proxy and returns the counter value. The expected output is 1.