-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupgradeablity.js
170 lines (147 loc) · 5.74 KB
/
upgradeablity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const { expect } = require("chai");
const { ethers, upgrades } = require("hardhat");
const { expectRevert } = require("@openzeppelin/test-helpers");
let admin, newAdmin, otherAccount;
let proxyContract;
let deployProxy;
let Box;
let BoxV2;
let BoxV3;
describe("Upgrade-able Box", function (accounts) {
beforeEach(async () => {
[admin, newAdmin, otherAccount] = await ethers.getSigners();
Box = await ethers.getContractFactory("Box");
BoxV2 = await ethers.getContractFactory("BoxV2");
});
it("should deploy the upgrade-able smart contract", async () => {
// Deploy
deployProxy = async () => {
const _proxy = await upgrades.deployProxy(Box, [200], {
initializer: "set",
kind: "uups",
});
await _proxy.deployed();
proxyContract = _proxy;
};
});
it("should should initialize correctly", async () => {
await deployProxy();
expect(await proxyContract.get()).be.equal(200);
expect(await proxyContract.owner()).be.equal(admin.address);
await expectRevert(
proxyContract.set(200),
"Initializable: contract is already initialized"
);
await expectRevert(
proxyContract.connect(newAdmin).set(200),
"Initializable: contract is already initialized"
);
});
it("should prevent un-authorized upgrade", async () => {
await expectRevert(
upgrades.upgradeProxy(proxyContract.address, BoxV2.connect(newAdmin)),
"Unauthorized access"
);
});
it("should limit pause-ability functionality only to admin or proxy admin", async () => {
await expectRevert(
proxyContract.connect(newAdmin).pause(),
"Ownable: caller is not the owner"
);
expect(await proxyContract.get()).be.equal(200);
await proxyContract.pause();
await expectRevert(
proxyContract.connect(otherAccount).get(),
"Pausable: paused"
);
await proxyContract.connect(admin).unpause();
});
it("should be unpaused by admin", async () => {
expect(await proxyContract.get()).to.be.equal(200);
await proxyContract.increment();
expect(await proxyContract.get()).to.be.equal(199); // Buggy value; the correct one is 201; so time to fix the bug!
});
});
describe("Fixing a discovered bug", () => {
it("should produce buggy result", async () => {
expect(await proxyContract.get()).to.be.equal(199);
await proxyContract.increment();
expect(await proxyContract.get()).to.be.equal(198); // Buggy value; the correct one is 201; so time to fix the bug!
});
it("should upgrade potential fix", async function () {
proxyContract = await upgrades.upgradeProxy(proxyContract.address, BoxV2);
});
it("should emit Upgraded event", async () => {
//! TODO check event emitting
// await upgradeResult.deployTransaction.wait();
// let { hash, receipt } = upgradeResult.deployTransaction
// let implementation_address = await upgrades.erc1967.getImplementationAddress(proxyContract.address)
// let _box = await Box.attach(implementation_address)
// await expectEvent.inTransaction(hash, _box, "Upgraded");
});
it("should keep the storage intact", async () => {
expect(await proxyContract.get()).be.equal(198);
});
it("should fix the bug", async () => {
await proxyContract.increment();
await proxyContract.increment();
expect(await proxyContract.get()).to.be.equal(200);
});
it("should initialize the upgraded implementation", async () => {
//! TODO how to add an initializer for new implementation?
// await expectRevert(
// proxyContract.newInitializer(),
// "Initializable: contract is already initialized"
// );
});
it("should change the proxy admin", async function () {
await expect(proxyContract.transferOwnership(newAdmin.address))
.to.emit(proxyContract, "OwnershipTransferred")
.withArgs(admin.address, newAdmin.address);
expect(await proxyContract.owner()).not.be.equal(admin.address);
await expectRevert(
proxyContract.connect(admin).pause(),
"Ownable: caller is not the owner"
);
expect(await proxyContract.owner()).be.equal(newAdmin.address);
});
it("should transfer ownership correctly", async () => {
await proxyContract.connect(newAdmin).pause();
await expectRevert(proxyContract.get(), "Pausable: paused");
await expectRevert(
proxyContract.connect(newAdmin).get(),
"Pausable: paused"
);
await proxyContract.connect(newAdmin).unpause();
expect(await proxyContract.get()).to.be.equal(200);
});
});
describe("Add new variable", () => {
it("should upgrade", async () => {
BoxV3 = await ethers.getContractFactory("BoxV3", newAdmin); // Upgrade by newAdmin
proxyContract = await upgrades.upgradeProxy(proxyContract.address, BoxV3);
});
it("should keep the storage intact", async () => {
expect(await proxyContract.get()).to.be.equal(200);
});
it("should keep the old functionalities intact", async () => {
await proxyContract.increment();
expect(await proxyContract.get()).be.equal(201);
});
it("should test new functionality", async () => {
// Deposit and listen for the Event
const otherAccountProxy = proxyContract.connect(otherAccount);
expect(await otherAccountProxy.readDepositedAmount()).be.equal(0);
await expectRevert(otherAccountProxy.deposit(0), "_amount > 0");
await expect(otherAccountProxy.deposit(1)).to.emit(
proxyContract,
"Deposited"
);
expect(await otherAccountProxy.readDepositedAmount()).be.equal(1);
await proxyContract.connect(newAdmin).pause();
expect(await otherAccountProxy.readDepositedAmount()).be.equal(1);
await expectRevert(otherAccountProxy.deposit(0), "Pausable: paused");
await proxyContract.connect(newAdmin).unpause();
await expectRevert(otherAccountProxy.deposit(0), "_amount > 0");
});
});