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