Skip to content

Commit

Permalink
add encodeFunctionCall to upgrade example
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Jun 17, 2024
1 parent 3f5fd18 commit 548e0b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions examples/upgradeable/contracts/DemoV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ pragma solidity ^0.8.9;

// A contrived example of a contract that can be upgraded
contract DemoV2 {
string public name;

function version() public pure returns (string memory) {
return "2.0.0";
}

function setName(string memory _name) public {
name = _name;
}
}
15 changes: 12 additions & 3 deletions examples/upgradeable/ignition/modules/UpgradeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ const upgradeModule = buildModule("UpgradeModule", (m) => {
// This is the new version of the Demo contract that we want to upgrade to.
const demoV2 = m.contract("DemoV2");

// The `upgradeAndCall` function on the ProxyAdmin contract allows us to upgrade the proxy
// and call a function on the new implementation contract in a single transaction.
// To do this, we need to encode the function call data for the function we want to call.
// We'll then pass this encoded data to the `upgradeAndCall` function.
const encodedFunctionCall = m.encodeFunctionCall(demoV2, "setName", [
"Example Name",
]);

// Upgrade the proxy to the new version of the Demo contract.
// This function also accepts a data parameter, which can be used to call a function,
// but we don't need it here so we pass an empty hex string ("0x").
m.call(proxyAdmin, "upgradeAndCall", [proxy, demoV2, "0x"], {
// This function also accepts a data parameter, which accepts encoded function call data.
// We pass the encoded function call data we created above to the `upgradeAndCall` function
// so that the `setName` function is called on the new implementation contract after the upgrade.
m.call(proxyAdmin, "upgradeAndCall", [proxy, demoV2, encodedFunctionCall], {
from: proxyAdminOwner,
});

Expand Down
8 changes: 8 additions & 0 deletions examples/upgradeable/test/ProxyDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@ describe("Demo Proxy", function () {

expect(await demo.connect(otherAccount).version()).to.equal("2.0.0");
});

it("Should have set the name during upgrade", async function () {
const [owner, otherAccount] = await ethers.getSigners();

const { demo } = await ignition.deploy(UpgradeModule);

expect(await demo.connect(otherAccount).name()).to.equal("Example Name");
});
});
});

0 comments on commit 548e0b3

Please sign in to comment.