NEWTON

NEWTON


Popular tags

    What is the proxy pattern and how it can help make my smart contracts upgradable in Cairo Language?

    Asked

    3 months ago

    46

    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?

      cairosecurityproxy pattern

    Newton

    asked

    3 months ago


    1 answers

    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

    3 months ago

    Your answer

    NEWTON

    NEWTON