|
| 1 | +const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules"); |
| 2 | + |
| 3 | +/** |
| 4 | + * This is the first module that will be run. It deploys the proxy and the |
| 5 | + * proxy admin, and returns them so that they can be used by other modules. |
| 6 | + */ |
| 7 | +const proxyModule = buildModule("ProxyModule", (m) => { |
| 8 | + // This address is the owner of the ProxyAdmin contract, |
| 9 | + // so it will be the only account that can upgrade the proxy when needed. |
| 10 | + const proxyAdminOwner = m.getAccount(0); |
| 11 | + |
| 12 | + // This is our contract that will be proxied. |
| 13 | + // We will upgrade this contract with a new version later. |
| 14 | + const demo = m.contract("Demo"); |
| 15 | + |
| 16 | + // The TransparentUpgradeableProxy contract creates the ProxyAdmin within its constructor. |
| 17 | + // To read more about how this proxy is implemented, you can view the source code and comments here: |
| 18 | + // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v5.0.1/contracts/proxy/transparent/TransparentUpgradeableProxy.sol |
| 19 | + const proxy = m.contract("TransparentUpgradeableProxy", [ |
| 20 | + demo, |
| 21 | + proxyAdminOwner, |
| 22 | + "0x", |
| 23 | + ]); |
| 24 | + |
| 25 | + // We need to get the address of the ProxyAdmin contract that was created by the TransparentUpgradeableProxy |
| 26 | + // so that we can use it to upgrade the proxy later. |
| 27 | + const proxyAdminAddress = m.readEventArgument( |
| 28 | + proxy, |
| 29 | + "AdminChanged", |
| 30 | + "newAdmin" |
| 31 | + ); |
| 32 | + |
| 33 | + // Here we use m.contractAt(...) to create a contract instance for the ProxyAdmin that we can interact with later to upgrade the proxy. |
| 34 | + const proxyAdmin = m.contractAt("ProxyAdmin", proxyAdminAddress); |
| 35 | + |
| 36 | + // Return the proxy and proxy admin so that they can be used by other modules. |
| 37 | + return { proxyAdmin, proxy }; |
| 38 | +}); |
| 39 | + |
| 40 | +/** |
| 41 | + * This is the second module that will be run, and it is also the only module exported from this file. |
| 42 | + * It creates a contract instance for the Demo contract using the proxy from the previous module. |
| 43 | + */ |
| 44 | +const demoModule = buildModule("DemoModule", (m) => { |
| 45 | + // Get the proxy and proxy admin from the previous module. |
| 46 | + const { proxy, proxyAdmin } = m.useModule(proxyModule); |
| 47 | + |
| 48 | + // Here we're using m.contractAt(...) a bit differently than we did above. |
| 49 | + // While we're still using it to create a contract instance, we're now telling Hardhat Ignition |
| 50 | + // to treat the contract at the proxy address as an instance of the Demo contract. |
| 51 | + // This allows us to interact with the underlying Demo contract via the proxy from within tests and scripts. |
| 52 | + const demo = m.contractAt("Demo", proxy); |
| 53 | + |
| 54 | + // Return the contract instance, along with the original proxy and proxyAdmin contracts |
| 55 | + // so that they can be used by other modules, or in tests and scripts. |
| 56 | + return { demo, proxy, proxyAdmin }; |
| 57 | +}); |
| 58 | + |
| 59 | +module.exports = demoModule; |
0 commit comments