Proxy Contract

Aman Agarwal
Geek Culture
Published in
7 min readApr 15, 2023

A contract acting as a middleman between a client and another contract, also known as the target contract, is referred to as a proxy in the context of smart contracts.

By separating the target contract's implementation logic from its address, using a proxy contract makes it possible to maintain and upgrade the target contract without affecting its interfaces or losing any state information.

A proxy contract can be thought of as an additional layer of indirection that maintains the interface and operation of the original contract while rerouting calls to the target contract. A function to upgrade the target contract's implementation by changing its address is typically included in the code of the proxy contract. This function can be helpful to fix bugs, adding new features, or enhancing the contract's performance.

It can also be used to control access to the target contract, ensuring that only legitimate users or contracts can interact with its features. This can be done by including access control logic in the code of the proxy contract, which is updated separately from the target contract's implementation.

This contract is a common design pattern in the blockchain industry because it can increase a smart contract system's flexibility, security, and maintainability.

Why do we need it?

In order to provide upgradeability and access control to the contract logic, proxy contracts are a design pattern used in smart contract development. With proxy contracts, the interface of the contract is kept constant, but the way the contract logic is implemented can be updated or modified without changing the contract's address or state.

Due to the immutability of smart contracts once they are deployed to the blockchain, there is a need for upgradeability. A deployed contract cannot be changed, and any bugs or weaknesses in the logic of the contract cannot be fixed without deploying a new contract. This can be problematic for contracts that require frequent updates or modifications, such as contracts used for managing token sales or decentralized applications (dApps).

By separating the interface of the contract from its implementation, proxy contracts are able to address this issue by allowing the implementation to be upgraded without affecting the contract's address or state. The proxy contract is updated to point to the new implementation while keeping the same interface and address whenever a new version of the contract logic is deployed.

Proxy contracts may also offer access control to the contract logic in addition to upgradeability. It is possible to limit access to the contract's features or state information to only authorized users or applications by using a proxy contract as an intermediary. This can be useful for contracts that demand permission access, such as those used to manage confidential information or business transactions.

It offers a sophisticated and adaptable method of putting into practice access-controlled and upgradeable smart contracts. Proxy contracts allow for quick updates and modifications to the contract logic while maintaining the same contract address and interface. This is accomplished by separating the contract's interface from its implementation.

What are its benefits?

In a smart contract system, using proxy contracts has a number of benefits.

  1. Upgradability: One of the main benefits of using a proxy contract is that it enables the target contract's logic to be upgraded without causing the contract's interfaces to malfunction or losing any state information. This is so that the interface and behavior of the original contract are preserved. The proxy contract serves as an intermediary, rerouting calls to the target contract.
  2. Security: By enabling quick and safe upgrades to the target contract's logic, using a proxy contract can also increase the security of the smart contract system. This can be especially helpful when bugs or vulnerabilities are found in the contract's code because the contract can be upgraded right away to fix the problems.
  3. Maintenance: By allowing for independent upgrades of the contract's logic and access control, proxy contracts can also make smart contract maintenance simpler. This can make it simpler to upgrade access control policies, optimize the performance of the contract, or add new features while maintaining the contract's current functionality.
  4. Access Control: Proxy contracts can also be used to control access to the target contract, ensuring that only approved users or contracts can interact with its features. This can be done by including access control logic in the code of the proxy contract, which can be modified without affecting the implementation of the target contract.
  5. Cost-Effectiveness: Finally, since the contract's logic can be updated without necessitating a full redeployment, proxy contracts can also save money on gas expenses. This is especially useful when the contract contains a lot of data or state, as redeploying the contract in those cases might be prohibitively expensive.

A proxy contract is a common design pattern in the blockchain industry because it can increase flexibility, security, and maintainability in a smart contract system.

Scrubbing hands

Here is a straightforward example of a proxy contract that demonstrates how it can be used to update a target contract's logic without causing it to lose its interface:

In this example, we have a target contract called MyContract that has a single function incrementCounter() that increments a public variablecounter. We also have a proxy contract calledMyContractProxy that is responsible for forwarding calls to the target contract, and a usage contract called MyUsageContract that interacts with the proxy contract.

The MyUsageContract the constructor takes an instance ofMyContract as a parameter and creates a new instance ofMyContractProxy, passing the address of the target contract as a parameter to the proxy constructor.

TheupgradeMyContract() function in the MyUsageContract allows for upgrading the target contract by calling the upgrade() function on the proxy contract and passing the address of a new instance of MyContract as a parameter.

TheincrementMyCounter() function in the MyUsageContract forwards theincrementCounter() call to the proxy contract, which then forwards it to the target contract using the fallback function defined in the proxy contract.

ThegetMyCounter() function in the MyUsageContract retrieves the current value of thecounter variable from the target contract by accessing it through the proxy contract.

Overall, this example shows how a proxy contract may be used to separate a target contract’s implementation logic from the rest of the contract, enabling the target contract to be upgraded without affecting the contract’s interface or losing any state information.

Disadvantages

While proxy contracts provide several advantages, they also have some disadvantages that should be taken into consideration when designing a smart contract system:

  1. Complexity: Proxy contracts can add complexity to the smart contract system, as they introduce an additional layer of abstraction that must be carefully designed and tested to ensure correct behavior. This can make the contract code more difficult to understand, debug, and maintain, especially for less experienced developers.
  2. Gas Costs: Proxy contracts can also incur additional gas costs, as each function call must be forwarded through the proxy contract, which can add extra computation and storage costs. While the cost impact is typically small, it can add up over time, especially for contracts with high transaction volumes or frequent upgrades.
  3. Security Risks: If not properly designed and implemented, proxy contracts can introduce additional security risks to the smart contract system, such as potential vulnerabilities or attack vectors that could be exploited by malicious actors. For example, a poorly designed proxy contract could allow unauthorized access to the target contract’s functions or state data.
  4. Versioning Complexity: Proxy contracts can also introduce versioning complexity, especially if multiple versions of the contract logic are deployed concurrently. This can make it difficult to manage and maintain different versions of the contract, especially if backward compatibility is not carefully considered.
  5. Limited Upgradability: While proxy contracts provide a mechanism for upgrading the contract logic, there are still some limitations on upgradability. For example, some changes to the contract’s state or storage structure may require redeploying the entire contract, which can be costly and time-consuming. In addition, upgrading the contract logic may require updating any dependent contracts or applications, which can add additional complexity and coordination challenges.

Alternates of this implementation to make contracts upgradeable

There are several alternative approaches to implementing upgradeable smart contracts besides using proxy contracts:

  1. Eternal Storage: The everlasting storage pattern involves decoupling the logic of the contract from its state data and storing the state data in a separate contract that can be readily upgraded without affecting the logic. This makes it possible to easily upgrade the contract’s state data while retaining the original contract’s logic and interface. Nevertheless, this strategy can be difficult to apply and, in some situations, may need redeploying the entire contract.
  2. Library Contracts: Library contracts provide a way to share common functionality between contracts, allowing for code reuse and reducing duplication. By separating the contract’s logic into multiple library contracts, it may be possible to upgrade individual libraries without affecting the main contract’s logic or state. However, this approach may not be suitable for all types of contracts and can add additional complexity.
  3. Delegation: Delegation involves creating a separate contract that delegates calls to the main contract’s logic, similar to how proxy contracts work. However, instead of forwarding calls through a proxy contract, the delegation contract forwards calls to the main contract using the delegateCall opcode, which preserves the context of the calling contract. This approach can provide some of the benefits of proxy contracts, such as upgradability and access control, while avoiding some of the complexities and security risks associated with proxy contracts.
  4. Upgradeable Libraries: Another approach is to use upgradeable libraries, which allow for easy upgrades of the contract’s logic without affecting the contract’s state or interfaces. This approach can be useful for contracts that require frequent updates or modifications to their logic, as it allows for easy and fast updates without the need for a full contract redeployment. However, this approach can be complex to implement and may require additional gas costs.

Conclusion

The creation of upgradeable and access-controlled smart contracts is made possible via smart proxy contracts, which are a powerful tool. Proxy contracts preserve the same contract address and interface while making it simple to update and modify the contract logic by separating the interface from the implementation. Proxy contracts also have the ability to control access to the contract logic, limiting access to only authorized users or applications.

You can support me and my content by buying a coffee ☕ here.

Follow me on Twitter and LinkedIn.

--

--

Aman Agarwal
Geek Culture

Engineer | Explorer | Blockchain | Golang | JavaScript developer