NEWTON
Asked
5 months ago
53
views
0
What is the proxy pattern and how it can help make my smart contracts upgradable in Cairo Language? Is there any standard lib for that?
Newton
asked
5 months ago
1
Accepted answer
Proxies allow to extend the functionality of a smart contract by making it essentialy modifiable. One of the key properties of smart contracts is their immutability, however there have been many examples where this has been a problem in the past.
For instance, a protocol identifying a serious bug while deployed on mainnet, and not being able to fix it. Or a hacker exploting a bug and no way to remediate the issue (btw, pausing functionality would be really useful here too). Sure, one could re-deploy the contract, but what about the contract storage?
Due to this, knowing how to safely implement the proxy pattern can be a great tool in a developer's arsenal.
Yet again, OpenZeppelin made everyone's life easier and implemented a standard Proxy pattern which anyone can use to make their contracts upgreadable.
Here is the default Proxy preset provided by OZ. This is very simple, has the __default__
function (which is like our fallback in Solidity), a L1 handler
, and a constructor. Before deploying this contract, developers should declare the contract class of their implementation contract. After, they can deploy the Proxy contract and pass the implementation_hash as parameter to the constructor.
The actual Proxy library contract contains a couple of storage_var
as seen below:
@storage_var func Proxy_implementation_hash() -> (class_hash: felt){ } @storage_var func Proxy_admin() -> (proxy_admin: felt){ } @storage_var func Proxy_initialized() -> (initialized: felt){ }
Always remember to not deploy your implementation contract (the one that contains the actual logic of your protocol). Instead this should be declared
as a contract_class
with:
starknet declare --contract contract_compiled.json
One other important piece of advice to keep in mind is to use the initializer "modifier" for the initialization function of an implementation contract. This will prevent your contract from being initialized twice (which could be a very big problem).
ctrlc03
answered
5 months ago
Has anyone implemented a minimal proxy in Cairo lang?
How to protect my contract from reentrancy in Cairo Language / StarkNet?
Is it possible to transpile contracts that use openzeppelin libraries?
What if my solidity contract contains Assembly or special EVM calls ?
Which lib can help me to make a multicall in JS for StarkNet?
What are events in Cairo Language?
Did you change the factory/create2 part (to compute pool addresses)? What's easier, adapt the create2 trick in Solidity then transpile or transpile first then adapt in Cairo?
How can I read contract memory with starknetjs?
Problem with transfering USDC via Starkgate. Can anyone help?
Can you explain about cancellation (L1 -> L2) in StarkNet (sequencer)?
How do you optimize gas in Cairo with Uint256/felt?
when starknet token?
Will StarkNet wallets work after Q4 2022 Final State Reset? Or do I need to create and merge a new wallet?
how I could read a felt* from ap in Cairo lang?