You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/content/ignition/docs/guides/upgradeable-proxies.md
+33-5
Original file line number
Diff line number
Diff line change
@@ -38,13 +38,19 @@ pragma solidity ^0.8.9;
38
38
39
39
// A contrived example of a contract that can be upgraded
40
40
contract DemoV2 {
41
+
string public name;
42
+
41
43
function version() public pure returns (string memory) {
42
44
return "2.0.0";
43
45
}
46
+
47
+
function setName(string memory _name) public {
48
+
name = _name;
49
+
}
44
50
}
45
51
```
46
52
47
-
This contract is identical to the first one, except that it returns an updated version string.
53
+
In addition to updating the version string, this contract also adds a `name` state variable and a `setName` function that allows us to set the value of `name`. We'll use this function later when we upgrade our proxy.
48
54
49
55
Finally, we'll create a file called `Proxies.sol` to import our proxy contracts. This file will look a little different than the others:
50
56
@@ -184,7 +190,7 @@ module.exports = demoModule;
184
190
185
191
::::
186
192
187
-
### Part 3: Upgrading our proxy
193
+
### Part 3: Upgrading our proxy with an initialization function
188
194
189
195
Next it's time to upgrade our proxy to a new version. To do this, we'll create a new file within our `ignition/modules` directory called `UpgradeModule.js` (or `UpgradeModule.ts` if you're using TypeScript). Inside this file, we'll again break up our module into two parts. To start, we'll write our `UpgradeModule`:
@@ -210,7 +220,9 @@ Next, we use the `m.useModule(...)` method to get the `ProxyAdmin` and proxy con
210
220
211
221
Then, we deploy our `DemoV2` contract. This will be the contract that we'll upgrade our proxy to.
212
222
213
-
Finally, we call the `upgradeAndCall` method on the `ProxyAdmin` contract. This method takes three arguments: the proxy contract, the new implementation contract, and a data parameter that can be used to call an additional function. We don't need it right now, so we'll leave it blank by setting it to an empty hex string (`"0x"`). We also provide the `from` option to ensure that the upgrade is called from the owner of the `ProxyAdmin` contract.
223
+
Next, we encode a call to the `setName` function in the `DemoV2` contract. This function takes a single argument, a string, which we'll set to `"Example Name"`. This encoded function call will be used to call the `setName` function on the `DemoV2` contract when we upgrade the proxy.
224
+
225
+
Finally, we call the `upgradeAndCall` method on the `ProxyAdmin` contract. This method takes three arguments: the proxy contract, the new implementation contract, and a data parameter that can be used to call an additional function on the target contract. In this case, we're calling the `setName` function on the `DemoV2` contract with the encoded function call we created earlier. We also provide the `from` option to ensure that the upgrade is called from the owner of the `ProxyAdmin` contract.
214
226
215
227
Lastly, we again return the `ProxyAdmin` and proxy contracts so that we can use them in our next module.
216
228
@@ -289,6 +301,14 @@ describe("Demo Proxy", function () {
@@ -330,7 +358,7 @@ describe("Demo Proxy", function () {
330
358
331
359
::::
332
360
333
-
Here we use Hardhat Ignition to deploy our imported modules. First, we deploy our base `ProxyModule` that returns the first version of our `Demo` contract and test it to ensure the proxy worked and retrieves the appropriate version string. Then, we deploy our `UpgradeModule` that returns the second version of our `Demo` contract and test it to ensure the proxy now returns the updated version string.
361
+
Here we use Hardhat Ignition to deploy our imported modules. First, we deploy our base `ProxyModule` that returns the first version of our `Demo` contract and test it to ensure the proxy worked and retrieves the appropriate version string. Then, we deploy our `UpgradeModule` that returns the second version of our `Demo` contract and test it to ensure the proxy now returns the updated version string. We also test that our initialization function was called and set the `name` state variable to `"Example Name"`.
0 commit comments