/
index.test.ts
250 lines (225 loc) · 8.83 KB
/
index.test.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* eslint-disable prefer-arrow-callback, func-names */
// tslint:disable-next-line no-implicit-dependencies
import { assert } from 'chai'
import type { Contract } from 'ethers'
import { networks } from '@unlock-protocol/networks'
import { useEnvironment } from './helpers'
// expectThrowsAsync
import type { CreateLockArgs } from '../src/createLock'
import {
UNLOCK_LATEST_VERSION,
PUBLIC_LOCK_LATEST_VERSION,
TASK_CREATE_LOCK,
} from '../src/constants'
import { locks } from './fixtures'
const defaultSignerAddress = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
const isIdenticalLock = async (lock: Contract, lockArgs: CreateLockArgs) => {
assert.equal(await lock.publicLockVersion(), PUBLIC_LOCK_LATEST_VERSION)
assert.equal(await lock.name(), lockArgs.name)
assert.equal((await lock.keyPrice()).toString(), lockArgs.keyPrice.toString())
assert.equal(await lock.maxNumberOfKeys(), lockArgs.maxNumberOfKeys)
assert.equal(await lock.expirationDuration(), lockArgs.expirationDuration)
}
describe('Unlock Hardhat plugin', function () {
describe('HRE extension', function () {
useEnvironment('hardhat-project')
it('Should add the unlock field', function () {
assert.instanceOf(this.hre.unlock, Object)
})
it('should store and extend existing networks info', function () {
assert.isTrue(Object.keys(this.hre.unlock.networks).includes('1'))
assert.equal(this.hre.unlock.networks['1'].name, networks['1'].name)
assert.equal(this.hre.unlock.networks['1'].id, networks['1'].id)
assert.equal(this.hre.unlock.networks['1'].unlockAddress, 'newAddress')
})
it('should store additional networks info', function () {
assert.isTrue(Object.keys(this.hre.unlock.networks).includes('31337'))
assert.equal(
this.hre.unlock.networks['31337'].name,
'Custom Localhost Name'
)
assert.equal(
this.hre.unlock.networks['31337'].subgraph?.endpoint,
'here goes a subgraph URI'
)
})
describe('functions', function () {
let protocol: {
unlock: Contract
publicLock: Contract
}
this.beforeEach(async function () {
protocol = await this.hre.unlock.deployProtocol()
})
describe('deployProtocol()', function () {
it('Should deploy the PublicLock contract', async function () {
const { publicLock } = protocol
assert.isTrue(publicLock.address.includes('0x'))
assert.equal(typeof publicLock.address, 'string')
})
it('Should deploy the Unlock contract', async function () {
const { unlock } = protocol
assert.isTrue(unlock.address.includes('0x'))
assert.equal(typeof unlock.address, 'string')
})
it('Should set the Unlock owner correctly', async function () {
const { unlock } = protocol
assert.equal(
(await unlock.owner()).toLowerCase(),
defaultSignerAddress.toLowerCase()
)
})
it('Should set the template correctly', async function () {
const { unlock, publicLock } = protocol
assert.equal(await unlock.publicLockAddress(), publicLock.address)
assert.equal(await unlock.unlockVersion(), UNLOCK_LATEST_VERSION)
assert.equal(
await unlock.publicLockImpls(PUBLIC_LOCK_LATEST_VERSION),
publicLock.address
)
assert.equal(
await unlock.publicLockVersions(publicLock.address),
PUBLIC_LOCK_LATEST_VERSION
)
assert.equal(
await unlock.publicLockLatestVersion(),
PUBLIC_LOCK_LATEST_VERSION
)
})
it('Should store deployed Unlock address in hre', async function () {
const { unlock } = protocol
assert.equal(
this.hre.unlock.networks['31337'].unlockAddress,
unlock.address
)
})
})
describe('getUnlockContract()', function () {
let protocol: {
unlock: Contract
publicLock: Contract
}
this.beforeEach(async function () {
protocol = await this.hre.unlock.deployProtocol()
})
it('Should retrieve the Unlock contract instance', async function () {
const { unlock } = protocol
const unlockGet = await this.hre.unlock.getUnlockContract(
unlock.address
)
assert.equal(typeof unlock, typeof unlockGet)
assert.equal(unlock.address, unlockGet.address)
assert.equal(await unlock.unlockVersion(), UNLOCK_LATEST_VERSION)
})
/*
it('Should throw if the Unlock contract is not defined', async function () {
await expectThrowsAsync(
this.hre.unlock.getUnlockContract(hre, '0x'),
[],
'Could not fetch the Unlock contract'
)
})
*/
})
describe('createLock()', function () {
it('Should create a new lock w correct params', async function () {
const { FIRST } = locks
const { lock, transactionHash, lockAddress } =
await this.hre.unlock.createLock(FIRST)
assert.equal(lockAddress, lock.address)
assert.equal(typeof transactionHash, 'string')
isIdenticalLock(lock, FIRST)
})
})
describe('deployAndSetTemplate()', function () {
it('Should deploy and set a new version', async function () {
const previousVersion = PUBLIC_LOCK_LATEST_VERSION - 1
const { FIRST } = locks
// deploy protocol w previous lock version
const { unlock, publicLock: publicLockBefore } =
await this.hre.unlock.deployProtocol(undefined, previousVersion)
// makes sure version is set correctly
assert.equal(await unlock.publicLockLatestVersion(), previousVersion)
assert.equal(
await unlock.publicLockAddress(),
publicLockBefore.address
)
// deploy and set the new template
const publicLock = await this.hre.unlock.deployAndSetTemplate(
PUBLIC_LOCK_LATEST_VERSION,
1
)
assert.equal(await unlock.publicLockAddress(), publicLock.address)
assert.notEqual(publicLockBefore.address, publicLock.address)
assert.equal(
await unlock.publicLockImpls(PUBLIC_LOCK_LATEST_VERSION),
publicLock.address
)
assert.equal(
await unlock.publicLockVersions(publicLock.address),
PUBLIC_LOCK_LATEST_VERSION
)
assert.equal(
await unlock.publicLockLatestVersion(),
PUBLIC_LOCK_LATEST_VERSION
)
// create a lock at correct version by default
const { lock } = await this.hre.unlock.createLock(FIRST)
assert.equal(
await lock.publicLockVersion(),
PUBLIC_LOCK_LATEST_VERSION
)
})
})
describe('getLock()', function () {
let lock: Contract
const { FIRST } = locks
this.beforeEach(async function () {
await this.hre.unlock.deployProtocol(undefined, undefined, 1)
;({ lock } = await this.hre.unlock.createLock(FIRST))
})
it('Should retrieve a lock w correct params', async function () {
const lockGet = await this.hre.unlock.getLockContract(lock.address)
assert.equal(
await lockGet.publicLockVersion(),
PUBLIC_LOCK_LATEST_VERSION
)
isIdenticalLock(lock, FIRST)
})
})
})
})
})
describe('HardhatConfig unlock extension', function () {
useEnvironment('hardhat-project')
it('Should add existing unlock networks to the config', function () {
assert.isTrue(Object.keys(this.hre.config).includes('unlock'))
assert.deepEqual(
Object.keys(this.hre.config.unlock).sort(),
[...Object.keys(networks), '12345', '31337'].sort()
)
})
it('Should allow user to configure exsiting network', function () {
assert.equal(this.hre.config.unlock['31337'].name, 'Custom Localhost Name')
})
it('Should allow user to pass a new network', function () {
assert.isTrue(Object.keys(this.hre.config.unlock).includes('12345'))
assert.equal(this.hre.config.unlock['12345'].name, 'New Network')
assert.equal(this.hre.config.unlock['12345'].id, 12345)
})
})
describe('Tasks', function () {
describe(TASK_CREATE_LOCK, function () {
useEnvironment('hardhat-project')
this.beforeEach(async function () {
await this.hre.unlock.deployProtocol(undefined, undefined, 1)
})
it('Should create a lock from command line args', async function () {
const { FIRST } = locks
const lockAddress: string = await this.hre.run(TASK_CREATE_LOCK, FIRST)
const lock = await this.hre.unlock.getLockContract(lockAddress)
assert.equal(lockAddress, lock.address)
isIdenticalLock(lock, FIRST)
})
})
})