Use create2 to upgrade contracts
This example shows how to use create2 to upgrade contract as described in the wisp article.
There are 3 contracts in the contracts folder:
Springboard.sol
- This is a contract factory. It uses create2 to create new contracts. The intend of this exercise is to use this contract to create the Wallet contract and upgrade to WalletV2 by first selfdestructing Wallet and create WalletV2 at the same address as Wallet.Wallet.sol
- Wallet version 1.0WalletV2.sol
- Wallet version 2.0
How does Springboard work?
The Springboard contract passes a special initcode to create2 to create new contract. As a reminder, the create2 syntax is create2(msg.value, initcode, initcode_length, salt). The initcode logic is to return the runtime byte code for the new contract. It gets the runtime byte code by calling Springboard.getPendingRuntimeCode(). See initcode vs runtime code for a refresher of what initcode and runtime bytecode are.
There are 2 scripts in the scripts folder used to generate the initcode:
bootstrap
- contains the list of opcodes to call Springboard.getPendingRuntimeCode()assemble.js
- assemble the opcodes in bootstrap to generate the bytecode
How to complete this exercise
- fork and clone this project
- install packages: npm install
- update the test/test-springboard.js file to add logic to upgrade the Wallet contract to WalletV2
- after the upgrade, verify that the wallet version is 2.0
- commit your changes to github and submit your github url
Special notes
The contracts created by the Springboard contract do not initialize the state variable in the constructor. You will need to create a regular function (not constructor) to initialize the variables instead