Solidity Proxy Pattern: A Beginner’s Tutorial
The proxy pattern helps solve a key challenge with Ethereum smart contracts: their immutability after deployment. While we can’t modify deployed contracts, we can implement upgradeable systems using proxies. Understanding Proxy pattern is essential for understanding the diamond pattern (EIP-2535) which extends proxies to support multiple implementation contracts instead of just one.
A proxy contract stores the address of an implementation contract and delegates all calls to it. When users interact with the proxy, it forwards their calls to the implementation contract using delegatecall. The implementation executes the logic, but within the proxy's storage context - meaning all state is stored in and read from the proxy's storage.
To upgrade the system, we simply deploy a new implementation contract and update the proxy’s reference. The proxy’s storage remains unchanged, preserving all data while allowing us to modify the business logic.
This tutorial uses Remix IDE without external libraries to demonstrate the core mechanics of the proxy pattern. By implementing it from scratch, we’ll gain deeper insight into how proxies actually work under the hood. We shall begin with the Proxy contract and build a a custom library to aid it. Then we will try it with several contracts. Notice that I will not relate to Transparent Proxy or UUPS (Universal Upgradeable Proxy Standard) at this tutorial but I will say that our proxy will be a Transparent Proxy.
Proxy and supporting library
In a folder named libraries we shall create a file named StorageSlot.sol.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
library StorageSlot {
struct AddressSlot {
address value;
}
function getAddressSlot(bytes32 slot)
internal
pure
returns (AddressSlot storage addr)
{
assembly {
addr.slot := slot
}
}
}The library’s purpose is to provide a way to access arbitrary storage slots. The bytes32 parameter determines which storage slot to access, and we return an AddressSlot storage reference pointing to that slot. The assembly code addr.slot := slot tells the EVM "this AddressSlot struct should read/write from the storage slot specified by our bytes32 parameter”.
contract StorageExample {
uint256 first; // slot 0
address second; // slot 1
mapping(address => uint256) balances;
// declares slot 2, but doesn't store values there
// If we store balances[0x123...] = 100
// The value 100 would be stored at: keccak256(abi.encode(0x123..., 2))
}Assembly is needed here because Solidity doesn’t allow direct manipulation of arbitrary storage slots in regular code. The assembly block lets us explicitly set which storage slot the AddressSlot struct will use, allowing dynamic storage slot allocation — the ability to assign specific storage slots at runtime, ensuring separate logic and storage layers.
Create a folder named contracts and inside create a file named Proxy.sol.
contract Proxy {
bytes32 private constant IMPLEMENTATION_SLOT =
bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1);
function _getImplementation() private view returns (address) {
return StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value;
}
function _setImplementation(address _implementation) private {
require(
_implementation.code.length > 0,
"implementation is not contract"
);
StorageSlot.getAddressSlot(IMPLEMENTATION_SLOT).value = _implementation;
}
}The StorageSlot library provides a structured way to access storage slots using assembly. EIP-1967 specifies a standard pattern: take a descriptive string (like "eip1967.proxy.implementation"), hash it with keccak256, subtract 1, and use that as the storage slot. The library defines an AddressSlot struct and uses inline assembly to directly set the slot where the address value will be stored. When setting a new implementation, it writes the address to that slot, and when reading, it retrieves the value from that specific slot in the proxy contract storage
In _setImplementation, we store _implementation (address of the contract we wish to use) at IMPLEMENTATION_SLOT. Since this slot is derived from a keccak256 hash minus 1, it maps to a very large index in the storage array, making accidental collisions with other state variables virtually impossible.
Get Ido Zamir’s stories in your inbox
Join Medium for free to get updates from this writer.
Now that we have made sure there will be no collisions with the implementation slot, we need to write a function that will do the following:
- Call a specific function in the implementation contract.
- Execute it within the current contract (Proxy) context.
- All changes made to storage, will reflect only in this contract.
- If it fails, revert.
In order to achieve it, we will use once again in Assembly which will allow us to use the following functions:
calldatacopy: Specifically copies calldata from a given offset to memory.delegatecall: Delegates the call to the implementation contract and also preserves msg.sender.returndatacopy: Copies the return data from a given offset to memory
`calldatacopy(t, f, s)`:
Copy s bytes of calldata from position f to memory position t
`delegatecall(g, a, s, r, o)`:
Execute code at address a with gas g, returning success status
`returndatacopy(t, f, s)`:
Copy s bytes of return data from position f to memory position t
fallback() external payable {
_delegate();
}
receive() external payable {
_delegate();
}
function _delegate() internal virtual {
address _implementation = _getImplementation();
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(
gas(), // Forward all gas
_implementation, // Implementation contract address
0, // READ input FROM position 0
calldatasize(), // Length of input data
0, // This is just a placeholder - data isn't actually stored here yet
0 // We'll determine output size after the call (We don't know the size yet)
)
returndatacopy(0, 0, returndatasize())
switch result
// delegatecall returns 0 on error.
case 0 {
// revert(p, s) - end execution, revert state changes
revert(0, returndatasize())
}
default {
// return(p, s) - end execution
return(0, returndatasize())
}
}
}Counters Contracts
In create folder counters within contracts folder with two files: Counter_v1.sol and Counter_v2.sol.
contract CounterV1 {
uint256 public count;
function inc() external {
count += 1;
}
}contract CounterV2 {
uint256 public count;
function inc() external {
count += 1;
}
function dec() external {
count -= 1;
}
}The only difference between them is dec function which was added to CounterV2.
Show time
Compile all three files (Counter_v1.sol, Counter_v2.sol and Proxy.sol) and deploy Counter_v1.sol and Proxy.sol. Create a folder names abis, and copy CounterV1 ABI to Counter_v1.abi. Quick look at CounterV1 ABI:
[
{
"inputs": [],
"name": "count",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "inc",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]As we can see, it is an array which includes count variable and inc function. In Solidity the compiler auto-generates getter functions for every public state-variable in the contract.
Copy CounterV1 address to Proxy upgradeTo in order to connect the two
Open Counter_v1.abi, copy Proxy contract address and paste it in ‘At Address’ box. Then this popup should be displayed.
Notice that we now have a third contract, with the same functions as CounterV1 has.
Click on inc function a few times and then hit count. In my case, the count shows 3. Go ahead and click on count in CounterV1 contract, the result is 0 since we have not changed anything in its’ state.
Deploy CounterV2 contract, and use its’ address in the Proxy contract upgradeTo and At Address with CounterV2 ABI. Click on the new <At Address> contract count and you get the same result as you had before.
Hows does it look with ether?
Create a folder named safe within contracts with two files: Safe_v1.sol and Safe_v2.sol.
contract SafeV1 {
mapping(address => uint256) private balances;
address private owner;
event Deposit(address indexed user, uint256 amount);
event Withdrawal(address indexed user, uint256 amount);
event OwnerWithdrawal(address indexed owner, uint256 amount);
function initialize(address _owner) public {
require(owner == address(0), "Already initialized");
owner = _owner;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
function deposit() public payable {
require(msg.value > 0, "Must send some Ether");
balances[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}
function getBalance() public view returns (uint256) {
return balances[msg.sender];
}
}Avoid constructors in implementation contracts because they won’t be called when used via a proxy. Use an initializer function instead (initialize). Compile SafeV1 and Proxy, deploy them and interact as seen earlier. I deposited 1 ether.
Whoops, we forgot to include a withdraw function. Luckily we use a Proxy contract.
contract SafeV2 {
mapping(address => uint256) private balances;
address private owner;
event Deposit(address indexed user, uint256 amount);
event Withdrawal(address indexed user, uint256 amount);
function initialize(address _owner) public {
require(owner == address(0), "Already initialized");
owner = _owner;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
function deposit() public payable {
require(msg.value > 0, "Must send some Ether");
balances[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint256 amount) public {
require(amount > 0, "Amount must be greater than zero");
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
emit Withdrawal(msg.sender, amount);
}
function getBalance() public view returns (uint256) {
return balances[msg.sender];
}
}Compile, deploy and upgrade Proxy to use this one. Click on getBalance, Ta-Da, it should 1 ether. Withdraw and click on getBalance. See that has the current amount?
Conclusion
I hope this tutorial helped you to understand the basics of Proxy contract pattern. It is essential to understand how this works, even in theory if you wish to delve more into Solidity or even blockchain. You can find the full code in my github repository.









