|
| 1 | +import { ethers, upgrades } from 'hardhat'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { getImplementationAddress } from '@openzeppelin/upgrades-core'; |
| 4 | + |
| 5 | +import { Greeter, GreeterV2, GreeterV2__factory, Greeter__factory } from '../typechain-types'; |
| 6 | + |
| 7 | +describe('UpgradeableGreeter contract', () => { |
| 8 | + let greeterFactory: Greeter__factory; |
| 9 | + let greeterV2Factory: GreeterV2__factory; |
| 10 | + let instance: Greeter; |
| 11 | + let instanceV2: GreeterV2; |
| 12 | + let proxyAddr: string; |
| 13 | + let implAddrV1: string; |
| 14 | + |
| 15 | + const msg0 = 'Hello, Goku!'; |
| 16 | + const msg1 = 'Hello, Kakarot!'; |
| 17 | + const msg2 = 'Hello, Vegeta!'; |
| 18 | + |
| 19 | + before(async () => { |
| 20 | + console.log('deploying greater V1 and proxy ..'); |
| 21 | + greeterFactory = await ethers.getContractFactory('Greeter'); |
| 22 | + instance = await upgrades.deployProxy(greeterFactory, [msg0]) as unknown as Greeter; |
| 23 | + await instance.waitForDeployment(); |
| 24 | + |
| 25 | + proxyAddr = await instance.getAddress(); |
| 26 | + implAddrV1 = await getImplementationAddress(ethers.provider, proxyAddr); |
| 27 | + console.log('proxy with greeter v1 deployed at', proxyAddr); |
| 28 | + console.log('implementation address', implAddrV1); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('Deployment', () => { |
| 32 | + it('should return the greeting set at deployment', async () => { |
| 33 | + expect(await instance.greet()).to.equal(msg0); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return a new greeting when one is set', async () => { |
| 37 | + await (await instance.setGreeting(msg1)).wait(); |
| 38 | + expect(await instance.greet()).to.equal(msg1); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('Upgrade', () => { |
| 43 | + before(async () => { |
| 44 | + console.log('upgrading to greeter V2 ...'); |
| 45 | + greeterV2Factory = await ethers.getContractFactory('GreeterV2'); |
| 46 | + instanceV2 = await upgrades.upgradeProxy(await instance.getAddress(), greeterV2Factory) as unknown as GreeterV2; |
| 47 | + |
| 48 | + let implAddrV2 = await getImplementationAddress(ethers.provider, proxyAddr); |
| 49 | + while (implAddrV2 === implAddrV1) { |
| 50 | + // there is no better way to check if the upgrade is done? |
| 51 | + console.log('still waiting for upgrade confirmation ...'); |
| 52 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 53 | + implAddrV2 = await getImplementationAddress(ethers.provider, proxyAddr); |
| 54 | + } |
| 55 | + console.log('proxy with greeter V2 upgraded at', await instanceV2.getAddress()); |
| 56 | + console.log('implementation address V2: ', implAddrV2); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should maintain the greeting after the upgrade', async () => { |
| 60 | + expect(await instanceV2.greet()).to.equal(msg1); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should maintain the original method', async () => { |
| 64 | + await (await instanceV2.setGreeting(msg2)).wait(); |
| 65 | + expect(await instanceV2.greet()).to.equal(msg2); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should add a new method', async () => { |
| 69 | + await (await instanceV2.setGreetingV2(msg2)).wait(); |
| 70 | + expect(await instanceV2.greet()).to.equal(`${msg2} - V2`); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments