Skip to content

Commit b3445b2

Browse files
authored
Merge pull request #20 from dev-protocol/feat-add-test
feat: add test cases for SBTProxy, SBTFactory, SBTFactoryProxy
2 parents 005e3fe + 8b2e119 commit b3445b2

File tree

4 files changed

+2794
-0
lines changed

4 files changed

+2794
-0
lines changed

test/SBTFactory.test.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { ethers } from 'hardhat'
2+
import { expect, use } from 'chai'
3+
import { type Contract } from 'ethers'
4+
import { solidity } from 'ethereum-waffle'
5+
6+
import { deploy, getSigners, ZERO_ADDRESS } from './utils'
7+
8+
use(solidity)
9+
10+
describe('SBTFactory', () => {
11+
const init = async (shouldInitialize = true): Promise<Contract> => {
12+
const sbtFactory = await deploy('SBTFactory')
13+
if (shouldInitialize) {
14+
await sbtFactory.initialize()
15+
}
16+
17+
return sbtFactory
18+
}
19+
20+
describe('initialize', () => {
21+
it('The initialize function should execute correctly', async () => {
22+
const signers = await getSigners()
23+
const sbtFactory = await init(false)
24+
expect(await sbtFactory.owner()).to.eq(ZERO_ADDRESS)
25+
await expect(sbtFactory.connect(signers.userA).initialize()).to.not
26+
.reverted
27+
expect(await sbtFactory.owner()).to.eq(signers.userA.address)
28+
})
29+
30+
it('The initialize function can only be executed once', async () => {
31+
const signers = await getSigners()
32+
const sbtFactory = await init()
33+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
34+
await expect(sbtFactory.initialize()).to.be.revertedWith(
35+
'Initializable: contract is already initialized'
36+
)
37+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
38+
})
39+
})
40+
41+
describe('makeNewSBT', () => {
42+
it('The makeNewSBT function should execute correctly', async () => {
43+
const signers = await getSigners()
44+
const sbtFactory = await init()
45+
46+
const identifier = ethers.utils.formatBytes32String('SBT')
47+
expect(await sbtFactory.sbtProxyMapping(identifier)).to.eq(ZERO_ADDRESS)
48+
await expect(
49+
sbtFactory.makeNewSBT(
50+
signers.proxyAdmin.address,
51+
ethers.utils.arrayify('0x'),
52+
signers.minterUpdater.address,
53+
[signers.minterA.address, signers.minterB.address],
54+
identifier
55+
)
56+
)
57+
.to.emit(sbtFactory, 'SBTImplementationCreated')
58+
.emit(sbtFactory, 'SBTProxyCreated')
59+
60+
expect(await sbtFactory.sbtProxyMapping(identifier)).to.not.eq(
61+
ZERO_ADDRESS
62+
)
63+
})
64+
65+
it('The makeNewSBT function should execute correctly if called by owner', async () => {
66+
const signers = await getSigners()
67+
const sbtFactory = await init()
68+
69+
const identifier = ethers.utils.formatBytes32String('SBT')
70+
expect(await sbtFactory.sbtProxyMapping(identifier)).to.eq(ZERO_ADDRESS)
71+
await expect(
72+
sbtFactory
73+
.connect(signers.deployer)
74+
.makeNewSBT(
75+
signers.proxyAdmin.address,
76+
ethers.utils.arrayify('0x'),
77+
signers.minterUpdater.address,
78+
[signers.minterA.address, signers.minterB.address],
79+
identifier
80+
)
81+
)
82+
.to.emit(sbtFactory, 'SBTImplementationCreated')
83+
.emit(sbtFactory, 'SBTProxyCreated')
84+
85+
expect(await sbtFactory.sbtProxyMapping(identifier)).to.not.eq(
86+
ZERO_ADDRESS
87+
)
88+
})
89+
90+
it('The makeNewSBT function should not execute if called by non-owner', async () => {
91+
const signers = await getSigners()
92+
const sbtFactory = await init()
93+
94+
const identifier = ethers.utils.formatBytes32String('SBT')
95+
expect(await sbtFactory.sbtProxyMapping(identifier)).to.eq(ZERO_ADDRESS)
96+
await expect(
97+
sbtFactory
98+
.connect(signers.minterA)
99+
.makeNewSBT(
100+
signers.proxyAdmin.address,
101+
ethers.utils.arrayify('0x'),
102+
signers.minterUpdater.address,
103+
[signers.minterA.address, signers.minterB.address],
104+
identifier
105+
)
106+
).to.revertedWith('Ownable: caller is not the owner')
107+
108+
expect(await sbtFactory.sbtProxyMapping(identifier)).to.eq(ZERO_ADDRESS)
109+
})
110+
})
111+
112+
describe('renounceOwnership', () => {
113+
it('The renounceOwnership function should execute correctly if owner', async () => {
114+
const signers = await getSigners()
115+
const sbtFactory = await init()
116+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
117+
118+
await expect(sbtFactory.renounceOwnership())
119+
.to.emit(sbtFactory, 'OwnershipTransferred')
120+
.withArgs(signers.deployer.address, ZERO_ADDRESS)
121+
122+
expect(await sbtFactory.owner()).to.eq(ZERO_ADDRESS)
123+
})
124+
125+
it('The renounceOwnership function should not execute correctly if not owner', async () => {
126+
const signers = await getSigners()
127+
const sbtFactory = await init()
128+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
129+
130+
await expect(
131+
sbtFactory.connect(signers.userA).renounceOwnership()
132+
).to.revertedWith('Ownable: caller is not the owner')
133+
134+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
135+
})
136+
})
137+
138+
describe('transferOwnership', () => {
139+
it('The transferOwnership function should execute correctly if owner', async () => {
140+
const signers = await getSigners()
141+
const sbtFactory = await init()
142+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
143+
144+
await expect(sbtFactory.transferOwnership(signers.userA.address))
145+
.to.emit(sbtFactory, 'OwnershipTransferred')
146+
.withArgs(signers.deployer.address, signers.userA.address)
147+
148+
expect(await sbtFactory.owner()).to.eq(signers.userA.address)
149+
})
150+
151+
it('The renounceOwnership function should not execute correctly if not owner', async () => {
152+
const signers = await getSigners()
153+
const sbtFactory = await init()
154+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
155+
156+
await expect(
157+
sbtFactory
158+
.connect(signers.userA)
159+
.transferOwnership(signers.userA.address)
160+
).to.revertedWith('Ownable: caller is not the owner')
161+
162+
expect(await sbtFactory.owner()).to.eq(signers.deployer.address)
163+
})
164+
})
165+
})

0 commit comments

Comments
 (0)