Metamorphic Contracts: An Advanced Proxy Upgrade Pattern for Smart Contracts
Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. They are stored on a blockchain, which acts as a decentralized ledger, making them transparent, trustworthy, and highly secure.
People assume that smart contract code is immutable and cannot be changed once deployed but with a little bit of “magic” you can create metamorphic smart contracts.
Metamorphic contracts are an exotic proxy upgrade pattern for smart contracts where the code behind a smart contract address changes, but the address stays the same. In this article, we will explore the concept of metamorphic contracts, how to deploy them, and their potential dangers.
What Are Metamorphic Contracts?
The proxy pattern is a well-known design pattern used in smart contracts to substitute for the actual implementation of a smart contract. It can be used for several reasons, including security, upgradability, and extensibility. The proxy contract is the actual contract deployed on the blockchain, while the implementation contract is the code that the proxy contract points to. The implementation contract can be changed without affecting the proxy contract's address.
Metamorphic contracts take the proxy pattern to the next level by allowing the implementation contract's code to change while keeping the same address. This can be a powerful tool for smart contract developers, as it allows them to update their contract's logic without changing the contract's address.
Metamorphic contracts use the CREATE2 assembly op-code for Solidity introduced to Ethereum in February 2019. The original CREATE opcode makes it difficult for developers to control the destination address for a to-be-deployed smart contract. By using CREATE2 it’s possible to know the address that you're going to deploy the smart contract to in advance.
A Smart Contract’s address is usually created by taking the deployersAddress and the nonce. But the address of a contract that has been deployed by CREATE2 depends on the caller’s address, a supplied salt parameter, and the initialization code of the contract that will be created.
How to Deploy Metamorphic Contracts
To deploy a metamorphic contract, you need a metamorphic contract factory. A metamorphic contract factory is a smart contract that deploys other smart contracts. To deploy a metamorphic contract, you need to provide the factory with the implementation contract's code and a salt value. The salt value is used to generate the same address every time you deploy a new metamorphic contract with the same implementation code and salt value.
Recommended by LinkedIn
Once you have the factory, you can deploy the implementation contract, which is the contract whose code you want to change. After you have deployed the implementation contract, you can use the factory to deploy a metamorphic contract that points to the implementation contract's address. The metamorphic contract will copy the implementation contract's code and use it as its own code. From this point on, you can update the implementation contract's code, and the metamorphic contract will automatically use the updated code.
Potential Dangers of Metamorphic Contracts
As we mentioned earlier, metamorphic contracts can be dangerous if you are not careful. If you interact with a metamorphic contract, the code can change underneath you, and the address will stay the same. This can lead to several problems, including loss of funds and compromised security.
One way to mitigate these risks is to check if a contract is a metamorphic contract before interacting with it. You can do this by checking if the contract has a self-destruct function. If it does, it might be a metamorphic contract. You can also check if the contract was deployed from a factory that can deploy metamorphic contracts.
Conclusion
In conclusion, metamorphic contracts are an exotic proxy upgrade pattern for smart contracts that can be a powerful tool for smart contract developers. They allow developers to update their contract's logic without changing the contract's address. However, they also come with potential dangers that need to be taken into account. As always, when interacting with smart contracts, you need to be careful and do your research beforehand.
Smart contracts are revolutionizing the way we do business, and metamorphic contracts are just one example of the innovative ways developers are using them. It's important to stay informed and up-to-date on the latest trends and developments in the smart contract space, as they have the potential to change the world as we know it.
Very informative Asante Help understand what the downside of a contract's address changes would be. Why would I need to keep the same address but change the Logic?