forked from holographxyz/holograph-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_holograph_registry_tests.ts
372 lines (343 loc) · 16.5 KB
/
15_holograph_registry_tests.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { ethers } from 'hardhat';
import web3 from 'web3';
import { generateInitCode, zeroAddress } from '../scripts/utils/helpers';
import { MockExternalCall, MockExternalCall__factory } from '../typechain-types';
import setup, { PreTest } from './utils';
import {
ALREADY_INITIALIZED_ERROR_MSG,
CONTRACT_ALREADY_SET_ERROR_MSG,
EMPTY_CONTRACT_ERROR_MSG,
FACTORY_ONLY_ERROR_MSG,
ONLY_ADMIN_ERROR_MSG,
} from './utils/error_constants';
describe('Holograph Registry Contract', async function () {
let HolographRegistry: any;
let holographRegistry: any;
let accounts: SignerWithAddress[];
let deployer: SignerWithAddress;
let owner: SignerWithAddress;
let randUser: SignerWithAddress;
let mockAddress: string;
let hTokenAddress: string;
let utilityTokenAddress: string;
let chain1: PreTest;
let mockExternalCall: MockExternalCall;
const validChainId = 5;
const invalidChainId = 0;
before(async function () {
chain1 = await setup();
accounts = await ethers.getSigners();
deployer = accounts[0];
owner = accounts[2];
randUser = accounts[10];
mockAddress = '0xeB721f3E4C45a41fBdF701c8143E52665e67c76b'; // NOTE: sample Address
hTokenAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F'; // NOTE: sample Address
utilityTokenAddress = '0x4b02422DC46bb21D657A701D02794cD3Caeb17d0'; // NOTE: sample Address
HolographRegistry = await ethers.getContractFactory('HolographRegistry');
holographRegistry = await HolographRegistry.deploy();
await holographRegistry.deployed();
const mockExternalCallFactory = await ethers.getContractFactory<MockExternalCall__factory>('MockExternalCall');
mockExternalCall = await mockExternalCallFactory.deploy();
await mockExternalCall.deployed();
});
function createRandomAddress() {
return ethers.Wallet.createRandom().address;
}
function getContractType(contractName = 'HolographERC721') {
return '0x' + web3.utils.asciiToHex(contractName).substring(2).padStart(64, '0');
}
async function testExternalCallToFunction(fnAbi: string, fnName: string, args: any[] = []) {
const ABI = [fnAbi];
const iface = new ethers.utils.Interface(ABI);
const encodedFunctionData = iface.encodeFunctionData(fnName, args);
await expect(mockExternalCall.connect(deployer).callExternalFn(holographRegistry.address, encodedFunctionData)).to
.not.be.reverted;
}
describe('constructor', async function () {
it('should successfully deploy', async function () {
expect(holographRegistry.address).to.not.equal(zeroAddress);
});
});
describe('init()', async function () {
it('should successfully be initialized once', async function () {
const initCode = generateInitCode(['address', 'bytes32[]'], [deployer.address, []]);
await expect(holographRegistry.connect(deployer).init(initCode)).to.not.be.reverted;
});
it('should fail be initialized twice', async function () {
const initCode = generateInitCode(['address', 'bytes32[]'], [deployer.address, []]);
await expect(holographRegistry.connect(deployer).init(initCode)).to.be.revertedWith(
ALREADY_INITIALIZED_ERROR_MSG
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('setHolographedHashAddress', async function () {
it('Should return fail to add contract because it does not have a factory', async function () {
const contractHash = getContractType();
await expect(
chain1.registry.connect(chain1.deployer).setHolographedHashAddress(contractHash, chain1.holographErc721.address)
).to.be.revertedWith(FACTORY_ONLY_ERROR_MSG);
});
it('Should allow external contract to call fn');
// it('should fail to allow inherited contract to call fn');
});
describe('getHolographableContracts', async function () {
it('Should return valid contracts', async function () {
const expectedHolographableContractsCount = 5;
const contracts = await chain1.registry.getHolographableContracts(0, expectedHolographableContractsCount);
expect(contracts.length).to.equal(expectedHolographableContractsCount);
expect(contracts).include(chain1.sampleErc721Holographer.address);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function getHolographableContracts(uint256 index, uint256 length) external view returns (address[] memory contracts)',
'getHolographableContracts',
[0, 1]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('getHolographableContractsLength', async function () {
it('Should return valid _holographableContracts length', async function () {
const expectedHolographableContractsCount = 5;
const length = await chain1.registry.getHolographableContractsLength();
expect(length).to.equal(expectedHolographableContractsCount);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function getHolographableContractsLength() external view returns (uint256)',
'getHolographableContractsLength'
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('isHolographedContract', async function () {
it('Should return true if smartContract is valid', async function () {
const isHolographed = await chain1.registry.isHolographedContract(chain1.sampleErc721Holographer.address);
expect(isHolographed).to.equal(true);
});
it('Should return false if smartContract is INVALID', async function () {
const isHolographed = await chain1.registry.connect(chain1.deployer).isHolographedContract(mockAddress);
expect(isHolographed).to.equal(false);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function isHolographedContract(address smartContract) external view returns (bool)',
'isHolographedContract',
[mockAddress]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('isHolographedHashDeployed', async function () {
it('Should return true if hash is valid', async function () {
const isHolographed = await chain1.registry.isHolographedHashDeployed(chain1.sampleErc721Hash.erc721ConfigHash);
expect(isHolographed).to.equal(true);
});
it('should return false if hash is INVALID', async function () {
const contractHash = getContractType();
const isHolographed = await chain1.registry.isHolographedHashDeployed(contractHash);
expect(isHolographed).to.equal(false);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function isHolographedHashDeployed(bytes32 hash) external view returns (bool)',
'isHolographedHashDeployed',
[chain1.sampleErc721Hash.erc721ConfigHash]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('getHolographedHashAddress', async function () {
it('Should return valid _holographedContractsHashMap', async function () {
const address = await chain1.registry.getHolographedHashAddress(chain1.sampleErc721Hash.erc721ConfigHash);
expect(address).to.equal(chain1.sampleErc721Holographer.address);
});
it('should return 0x0 for invalid hash', async function () {
const contractHash = getContractType();
const address = await chain1.registry.getHolographedHashAddress(contractHash);
expect(address).to.equal(zeroAddress);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function getHolographedHashAddress(bytes32 hash) external view returns (address)',
'getHolographedHashAddress',
[chain1.sampleErc721Hash.erc721ConfigHash]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('setReservedContractTypeAddress()', async function () {
it('should allow admin to set contract type address', async function () {
const contractTypeHash = getContractType();
await expect(chain1.registry.connect(chain1.deployer).setReservedContractTypeAddress(contractTypeHash, true)).to
.not.be.reverted;
});
it('should fail to allow rand user to alter contract type address', async function () {
const contractTypeHash = getContractType();
await expect(chain1.registry.connect(randUser).setReservedContractTypeAddress(contractTypeHash, true)).to.be
.reverted;
});
});
describe('getReservedContractTypeAddress()', async function () {
it('should return expected contract type address', async function () {
const contractTypeHash = getContractType();
const contractAddress = await chain1.registry.getReservedContractTypeAddress(contractTypeHash);
expect(contractAddress).to.be.equal(chain1.holographErc721.address);
});
});
describe('setContractTypeAddress', async function () {
it('should allow admin to alter setContractTypeAddress', async function () {
const contractTypeHash = getContractType();
const contractAddress = createRandomAddress();
await chain1.registry.connect(chain1.deployer).setReservedContractTypeAddress(contractTypeHash, true);
await expect(chain1.registry.connect(chain1.deployer).setContractTypeAddress(contractTypeHash, contractAddress))
.to.not.be.reverted;
const tmp = await chain1.registry.getReservedContractTypeAddress(contractTypeHash);
expect(tmp).to.equal(contractAddress);
});
it('should fail to allow rand user to alter setContractTypeAddress', async function () {
const contractTypeHash = getContractType();
const contractAddress = createRandomAddress();
await chain1.registry.connect(chain1.deployer).setReservedContractTypeAddress(contractTypeHash, true);
await expect(chain1.registry.connect(randUser).setContractTypeAddress(contractTypeHash, contractAddress)).to.be
.reverted;
const tmp = await chain1.registry.getReservedContractTypeAddress(contractTypeHash);
expect(tmp).to.not.equal(contractAddress);
});
it('should allow external contract to call fn');
// it('should fail to allow inherited contract to call fn');
});
describe('getContractTypeAddress()', async function () {
it('Should return valid _contractTypeAddresses', async function () {
const contractTypeHash = getContractType();
const contractAddress = createRandomAddress();
await chain1.registry.connect(chain1.deployer).setReservedContractTypeAddress(contractTypeHash, true);
await expect(chain1.registry.connect(chain1.deployer).setContractTypeAddress(contractTypeHash, contractAddress))
.to.not.be.reverted;
const tmp = await chain1.registry.getContractTypeAddress(contractTypeHash);
expect(tmp).to.equal(contractAddress);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function getContractTypeAddress(bytes32 contractType) external view returns (address)',
'getContractTypeAddress',
[getContractType()]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('referenceContractTypeAddress', async function () {
it('should return valid address', async function () {
await expect(chain1.registry.referenceContractTypeAddress(chain1.holographErc20.address)).to.not.be.reverted;
});
it('should fail if contract is empty', async function () {
const contractAddress = createRandomAddress();
await expect(chain1.registry.referenceContractTypeAddress(contractAddress)).to.be.revertedWith(
EMPTY_CONTRACT_ERROR_MSG
);
});
it('should fail if contract is already set', async function () {
await expect(chain1.registry.referenceContractTypeAddress(chain1.holographErc20.address)).to.revertedWith(
CONTRACT_ALREADY_SET_ERROR_MSG
);
});
it('should fail if the address type is reserved already');
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function referenceContractTypeAddress(address contractAddress) external returns (bytes32)',
'referenceContractTypeAddress',
[chain1.holographErc20.address]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('setHolograph()', async function () {
it('should allow admin to alter _holographSlot', async function () {
await holographRegistry.connect(deployer).setHolograph(mockAddress);
const holographAddr = await holographRegistry.connect(deployer).getHolograph();
expect(holographAddr).to.equal(mockAddress);
});
it('should fail to allow owner to alter _holographSlot', async function () {
await expect(holographRegistry.connect(owner).setHolograph(mockAddress)).to.be.revertedWith(ONLY_ADMIN_ERROR_MSG);
});
it('should fail to allow non-owner to alter _holographSlot', async function () {
await expect(holographRegistry.connect(randUser).setHolograph(mockAddress)).to.be.revertedWith(
ONLY_ADMIN_ERROR_MSG
);
});
});
describe('getHolograph()', async function () {
it('Should return valid _holographSlot', async function () {
const holographAddr = await holographRegistry.connect(deployer).getHolograph();
expect(holographAddr).to.equal(mockAddress);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function getHolograph() external view returns (address holograph)',
'getHolograph'
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('setHToken()', async function () {
it('should allow admin to alter _hTokens', async function () {
await holographRegistry.connect(deployer).setHToken(validChainId, hTokenAddress);
const hTokenAddr = await holographRegistry.connect(deployer).getHToken(validChainId);
expect(hTokenAddr).to.equal(hTokenAddress);
});
it('should fail to allow owner to alter _hTokens', async function () {
await expect(holographRegistry.connect(owner).setHToken(validChainId, hTokenAddress)).to.be.revertedWith(
ONLY_ADMIN_ERROR_MSG
);
});
it('should fail to allow non-owner to alter _hTokens', async function () {
await expect(holographRegistry.connect(randUser).setHToken(validChainId, hTokenAddress)).to.be.revertedWith(
ONLY_ADMIN_ERROR_MSG
);
});
});
describe('getHToken', async function () {
it('Should return valid _hTokens', async function () {
const hTokenAddr = await holographRegistry.connect(deployer).getHToken(validChainId);
expect(hTokenAddr).to.equal(hTokenAddress);
});
it('should return 0x0 for invalid chainId', async function () {
const hTokenAddr = await holographRegistry.connect(deployer).getHToken(invalidChainId);
expect(hTokenAddr).to.equal(zeroAddress);
});
it('Should allow external contract to call fn', async function () {
await testExternalCallToFunction(
'function getHToken(uint32 chainId) external view returns (address)',
'getHToken',
[validChainId]
);
});
// it('should fail to allow inherited contract to call fn');
});
describe('setUtilityToken()', async function () {
it('should allow admin to alter _utilityTokenSlot', async function () {
await holographRegistry.connect(deployer).setUtilityToken(utilityTokenAddress);
const utilityTokenAddr = await holographRegistry.connect(deployer).getUtilityToken();
expect(utilityTokenAddr).to.equal(utilityTokenAddress);
});
it('should fail to allow owner to alter _utilityTokenSlot', async function () {
await expect(holographRegistry.connect(owner).setUtilityToken(utilityTokenAddress)).to.be.revertedWith(
ONLY_ADMIN_ERROR_MSG
);
});
it('should fail to allow non-owner to alter _utilityTokenSlot', async function () {
await expect(holographRegistry.connect(randUser).setUtilityToken(utilityTokenAddress)).to.be.revertedWith(
ONLY_ADMIN_ERROR_MSG
);
});
});
describe('getUtilityToken', async function () {
it('Should return valid _hTokens', async function () {
const utilityToken = await holographRegistry.connect(deployer).getUtilityToken();
expect(utilityToken).to.equal(utilityTokenAddress);
});
});
});