Skip to content

Commit 9a737d1

Browse files
committed
organised the test and wrote a seperate upgradable test file
1 parent 28c5a5d commit 9a737d1

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

contracts/XXXTokenV2.sol renamed to contracts/upgrades/XXXTokenV2.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.24;
33

4-
import "./XXXToken.sol";
4+
import "../XXXToken.sol";
55

66
/**
77
* @title XXXTokenV2

test/XXXToken.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,30 +156,4 @@ describe("XXXToken", function () {
156156
).to.be.revertedWithCustomError(token, "ERC20InsufficientBalance");
157157
});
158158
});
159-
160-
describe("Upgrading", function () {
161-
it("Should allow upgrader to upgrade the contract", async function () {
162-
const XXXTokenV2Factory = await ethers.getContractFactory("XXXTokenV2");
163-
const upgraded = await upgrades.upgradeProxy(await token.getAddress(), XXXTokenV2Factory) as unknown as XXXTokenV2;
164-
await upgraded.initializeV2();
165-
expect(await upgraded.version()).to.equal(2);
166-
});
167-
168-
it("Should not allow initializing V2 twice", async function () {
169-
const XXXTokenV2Factory = await ethers.getContractFactory("XXXTokenV2");
170-
const upgraded = await upgrades.upgradeProxy(await token.getAddress(), XXXTokenV2Factory) as unknown as XXXTokenV2;
171-
await upgraded.initializeV2();
172-
173-
await expect(
174-
upgraded.initializeV2()
175-
).to.be.revertedWithCustomError(upgraded, "InvalidInitialization");
176-
});
177-
178-
it("Should not allow non-upgrader to upgrade the contract", async function () {
179-
const XXXTokenV2Factory = await ethers.getContractFactory("XXXTokenV2");
180-
await expect(
181-
upgrades.upgradeProxy(await token.getAddress(), XXXTokenV2Factory.connect(user1))
182-
).to.be.revertedWithCustomError(token, "AccessControlUnauthorizedAccount");
183-
});
184-
});
185159
});

test/upgrades/XXXToken.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { expect } from "chai";
2+
import { ethers, upgrades } from "hardhat";
3+
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
4+
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
5+
import { XXXToken, XXXTokenV2 } from "../../typechain-types";
6+
7+
describe("XXXToken Upgrades", function () {
8+
// Test roles
9+
const UPGRADER_ROLE = ethers.keccak256(ethers.toUtf8Bytes("UPGRADER_ROLE"));
10+
11+
// Test accounts
12+
let owner: SignerWithAddress;
13+
let upgrader: SignerWithAddress;
14+
let user1: SignerWithAddress;
15+
16+
// Contract instance
17+
let token: XXXToken;
18+
19+
async function deployTokenFixture() {
20+
[owner, upgrader, user1] = await ethers.getSigners();
21+
22+
const XXXToken = await ethers.getContractFactory("XXXToken");
23+
const token = await upgrades.deployProxy(XXXToken, [], { initializer: 'initialize' });
24+
await token.waitForDeployment();
25+
26+
// Grant roles to test accounts
27+
await token.grantRole(UPGRADER_ROLE, upgrader.address);
28+
29+
return { token, owner, upgrader, user1 };
30+
}
31+
32+
beforeEach(async function () {
33+
({ token, owner, upgrader, user1 } = await loadFixture(deployTokenFixture));
34+
});
35+
36+
describe("Upgrading", function () {
37+
it("Should allow upgrader to upgrade the contract", async function () {
38+
const XXXTokenV2Factory = await ethers.getContractFactory("XXXTokenV2");
39+
const upgraded = await upgrades.upgradeProxy(await token.getAddress(), XXXTokenV2Factory) as unknown as XXXTokenV2;
40+
await upgraded.initializeV2();
41+
expect(await upgraded.version()).to.equal(2);
42+
});
43+
44+
it("Should not allow initializing V2 twice", async function () {
45+
const XXXTokenV2Factory = await ethers.getContractFactory("XXXTokenV2");
46+
const upgraded = await upgrades.upgradeProxy(await token.getAddress(), XXXTokenV2Factory) as unknown as XXXTokenV2;
47+
await upgraded.initializeV2();
48+
49+
await expect(
50+
upgraded.initializeV2()
51+
).to.be.revertedWithCustomError(upgraded, "InvalidInitialization");
52+
});
53+
54+
it("Should not allow non-upgrader to upgrade the contract", async function () {
55+
const XXXTokenV2Factory = await ethers.getContractFactory("XXXTokenV2");
56+
await expect(
57+
upgrades.upgradeProxy(await token.getAddress(), XXXTokenV2Factory.connect(user1))
58+
).to.be.revertedWithCustomError(token, "AccessControlUnauthorizedAccount");
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)