-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsmart-contract.test.ts
1588 lines (1352 loc) · 50.9 KB
/
smart-contract.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {Transaction, TxParams} from '@zilliqa-js/account'
import {BN, bytes, Long} from '@zilliqa-js/util'
import {toChecksumAddress} from '@zilliqa-js/crypto'
import {Zilliqa} from '@zilliqa-js/zilliqa'
import {Contract} from '@zilliqa-js/contract'
import {readFileSync} from 'fs'
import {contract_info as account_funder_contract_info} from './contract_info/account_funder.json'
import {contract_info as auction_registrar_contract_info} from './contract_info/auction_registrar.json'
import {contract_info as marketplace_contract_info} from './contract_info/marketplace.json'
import {contract_info as registry_contract_info} from './contract_info/registry.json'
import {contract_info as resolver_contract_info} from './contract_info/resolver.json'
import {contract_info as simple_registrar_contract_info} from './contract_info/simple_registrar.json'
import {generateMapperFromContractInfo} from './lib/params'
import Zns from './lib/Zns'
const accountFunderData = generateMapperFromContractInfo(
account_funder_contract_info,
)
const auctionRegistrarData = generateMapperFromContractInfo(
auction_registrar_contract_info,
)
const marketplaceData = generateMapperFromContractInfo(
marketplace_contract_info,
)
const registryData = generateMapperFromContractInfo(registry_contract_info)
const resolverData = generateMapperFromContractInfo(resolver_contract_info)
const simpleRegistrarData = generateMapperFromContractInfo(
simple_registrar_contract_info,
)
const getZilliqaNodeType = (): string => {
const environmentVariable = process.env.ZIL_NODE_TYPE // kaya, testnet
if (['testnet', 'standalone-node'].includes(environmentVariable)) {
return environmentVariable
}
console.warn(
"ZIL_NODE_TYPE environment variable should set as either 'kaya' or 'testnet'. 'kaya' is set by default",
)
return 'kaya'
}
const zilliqaNodeType = getZilliqaNodeType()
const testParams = {
testnet: {
jestTimeout: 15 * 60 * 1000,
},
'standalone-node': {
jestTimeout: 15 * 60 * 1000,
},
}[zilliqaNodeType]
const zilliqaTestnetNodeParams = {
chainId: 333,
msgVersion: 1,
url: 'https://dev-api.zilliqa.com',
}
const standaloneNode = {
chainId: 1,
msgVersion: 1,
url: 'http://127.0.0.1:5555',
}
const zilliqaNodeParams = {
testnet: zilliqaTestnetNodeParams,
'standalone-node': standaloneNode,
}[zilliqaNodeType]
const getZilliqa = () => new Zilliqa(zilliqaNodeParams.url)
const version = bytes.pack(
zilliqaNodeParams.chainId,
zilliqaNodeParams.msgVersion,
)
const defaultParams: TxParams = {
version,
toAddr: '0x' + '0'.repeat(40),
amount: new BN(0),
gasPrice: new BN(2000000000),
gasLimit: Long.fromNumber(25000),
}
function deployAccountFunder(zilliqa: Zilliqa, params: Partial<TxParams> = {}) {
return zilliqa.contracts
.new(
readFileSync('./scilla/account_funder.scilla', 'utf8'),
accountFunderData.init({}),
)
.deploy({...defaultParams, ...params})
}
function deployMarketplace(
zilliqa: Zilliqa,
{registry, seller, zone},
params: Partial<TxParams> = {},
) {
return zilliqa.contracts
.new(
readFileSync('./scilla/marketplace.scilla', 'utf8'),
marketplaceData.init({registry, seller, zone}),
)
.deploy({...defaultParams, ...params})
}
function deploySimpleRegistrar(
zilliqa: Zilliqa,
{
registry,
ownedNode,
owner,
initialDefaultPrice, // = 1,
initialQaPerUSD, // 0.017 * 10 ** 12,
},
params: Partial<TxParams> = {},
) {
return zilliqa.contracts
.new(
readFileSync('./scilla/simple_registrar.scilla', 'utf8'),
simpleRegistrarData.init({
registry,
ownedNode,
owner,
initialDefaultPrice,
initialQaPerUSD,
}),
)
.deploy({...defaultParams, ...params})
}
function deployAuctionRegistrar(
zilliqa: Zilliqa,
{
owner,
registry,
ownedNode,
initialAuctionLength,
minimumAuctionLength,
initialDefaultPrice,
bidIncrementNumerator,
bidIncrementDenominator,
initialPricePerQa,
initialMaxPriceUSD,
},
params: Partial<TxParams> = {},
) {
return zilliqa.contracts
.new(
readFileSync('./scilla/auction_registrar.scilla', 'utf8'),
auctionRegistrarData.init({
owner,
registry,
ownedNode,
initialAuctionLength,
minimumAuctionLength,
initialDefaultPrice,
bidIncrementNumerator,
bidIncrementDenominator,
initialPricePerQa,
initialMaxPriceUSD,
}),
)
.deploy({...defaultParams, ...params})
}
const address = '0xd90f2e538ce0df89c8273cad3b63ec44a3c4ed82'
const privateKey =
'e53d1c3edaffc7a7bab5418eb836cf75819a82872b4a1a0f1c7fcf5c3e020b89'
const address2 = '0x7bb3b0e8a59f3f61d9bff038f4aeb42cae2ecce8'
const privateKey2 =
'db11cfa086b92497c8ed5a4cc6edb3a5bfe3a640c43ffb9fc6aa0873c56f2ee3'
const defaultRootDomain = 'zil'
const defaultRootNode = Zns.namehash(defaultRootDomain)
const nullAddress = '0x' + '0'.repeat(40)
const asHash = (params) => {
return params.reduce((a, v) => ({...a, [v.vname]: v.value}), {})
}
const contractField = async (contract: Contract, name) => {
const value = (await contract.getState())[name]
if (!value) {
throw new Error(`Unknown contract field ${name}`)
}
return value
}
const expectUnchangedState = async (contract: Contract, block) => {
const oldState = await contract.getState()
const result = await block.call()
expect(await contract.getState()).toEqual(oldState)
return result
}
const contractMapValue = async (contract, field, key) => {
const map = await contractField(contract, field)
return map[key] || null
}
const transactionEvents = (tx: Transaction): Array<object> => {
const events = tx.txParams.receipt.event_logs || []
// Following the original reverse order of events
return events.map((event) => {
return {_eventname: event._eventname, ...asHash(event.params)}
})
}
describe('smart contracts', () => {
jest.setTimeout(testParams.jestTimeout)
beforeEach(() => {
jest.resetModules()
})
describe('resolver.scilla', () => {
it('should deploy', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const resolver = await new Zns(zilliqa, address, {
version,
}).deployResolver('test')
await resolver.reload()
expect(resolver.records).toEqual({})
})
it('should deploy non-blank initial state', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const resolver = await new Zns(zilliqa, address, {
version,
}).deployResolver('hello', {
crypto: {
ADA: {address: '0x1111'},
BTC: {address: '0x2222'},
EOS: {address: '0x3333'},
ETH: {address: '0x4444'},
XLM: {address: '0x5555'},
XRP: {address: '0x6666'},
ZIL: {address: '0x7777'},
},
})
let records = {
'crypto.ADA.address': '0x1111',
'crypto.BTC.address': '0x2222',
'crypto.EOS.address': '0x3333',
'crypto.ETH.address': '0x4444',
'crypto.XLM.address': '0x5555',
'crypto.XRP.address': '0x6666',
'crypto.ZIL.address': '0x7777',
}
expect(resolver.records).toEqual(records)
expect((await resolver.reload()).records).toEqual(records)
expect(await resolver.isLive()).toBeFalsy()
})
it('should set and unset records', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
const domain = 'tld'
const resolver = await zns.deployResolver(domain)
await zns.bestow(domain, address, resolver.address)
expect(await resolver.isLive()).toBeTruthy()
expect(await resolver.isDetached()).toBeFalsy()
await resolver.reload()
expect(resolver.records).toEqual({})
const keyForSetTx = 'crypto.ADA.address'
const valueForSetTx = '0x7357'
const setTx = await resolver.set(keyForSetTx, valueForSetTx)
const recordsSetEvent = {
_eventname: 'RecordsSet',
node: Zns.namehash(domain),
registry: zns.address.toLowerCase(),
}
const configuredEvent = {
_eventname: 'Configured',
node: Zns.namehash(domain),
owner: address,
resolver: resolver.address.toLowerCase(),
}
expect(resolver.records).toEqual({
[keyForSetTx]: valueForSetTx,
})
await resolver.reload()
expect(resolver.records).toEqual({
[keyForSetTx]: valueForSetTx,
})
expect(await transactionEvents(setTx)).toEqual([
resolver.getRecordsSetEvent(),
resolver.configuredEvent,
])
const unsetTx = await resolver.unset(keyForSetTx)
expect(resolver.records).toEqual({})
await resolver.reload()
expect(resolver.records).toEqual({})
expect(await transactionEvents(setTx)).toEqual([
resolver.getRecordsSetEvent(),
resolver.configuredEvent,
])
await resolver.set(keyForSetTx, valueForSetTx)
await resolver.set(keyForSetTx, '')
await resolver.reload()
expect(resolver.records).toEqual({})
})
it('should setMulti records and unset empty ones', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
const domain = 'tld'
const resolver = await zns.deployResolver(domain, {
crypto: {ETH: {address: '0x0000'}},
})
await zns.bestow(domain, address, resolver.address)
expect(await resolver.isLive()).toBeTruthy()
//////////////////////////////////////////////////////////////////////////
// setMulti records
//////////////////////////////////////////////////////////////////////////
const pair1 = ['crypto.ADA.address', '0x1111']
const pair2 = ['crypto.BTC.address', '0x2222']
const pair3 = ['crypto.ETH.address', '']
const resolverAddress = resolver.contract.address.toLowerCase();
const values = resolverData.f.setMulti({
newRecords: [
{constructor: `${resolverAddress}.RecordKeyValue`, argtypes: [], arguments: pair1},
{constructor: `${resolverAddress}.RecordKeyValue`, argtypes: [], arguments: pair2},
{constructor: `${resolverAddress}.RecordKeyValue`, argtypes: [], arguments: pair3},
],
});
values[0].type = `List (${resolverAddress}.RecordKeyValue)`;
const setMultiTx = await resolver.contract.call(
'setMulti',
values,
defaultParams,
)
const recordsSetEvent = {
_eventname: 'RecordsSet',
node: Zns.namehash(domain),
registry: zns.address.toLowerCase(),
}
const configuredEvent = {
_eventname: 'Configured',
node: Zns.namehash(domain),
owner: address,
resolver: resolver.address.toLowerCase(),
}
await resolver.reload()
expect(resolver.records).toEqual({
[pair1[0]]: pair1[1],
[pair2[0]]: pair2[1],
})
expect(await transactionEvents(setMultiTx)).toEqual([
resolver.getRecordsSetEvent(),
resolver.configuredEvent,
])
})
it('should fail to set, unset and setMulti records if sender not owner', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
let zns = new Zns(zilliqa, address, {version})
let resolver = await zns.deployResolver('hello.zil')
let {contract} = resolver
//////////////////////////////////////////////////////////////////////////
// fail to set record using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey2))
await expectUnchangedState(contract, async () => {
await expect(resolver.set('test', '0x7357')).rejects.toThrow(
/Sender not owner/,
)
})
//////////////////////////////////////////////////////////////////////////
// set record then fail to unset record using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(toChecksumAddress(address))
await resolver.set('test', '0x7357')
await resolver.reload()
expect(resolver.records).toEqual({test: '0x7357'})
zilliqa.wallet.setDefault(toChecksumAddress(address2))
await expectUnchangedState(contract, async () => {
await expect(resolver.unset('test')).rejects.toThrow(
/Sender not owner or key does not exist/,
)
})
//////////////////////////////////////////////////////////////////////////
// fail to call setMulti using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(toChecksumAddress(address2))
const resolverAddress = resolver.contract.address.toLowerCase();
const values = resolverData.f.setMulti({
newRecords: [
{
constructor: `${resolverAddress}.RecordKeyValue`,
argtypes: [],
arguments: ['test', '0x7357'],
},
],
});
values[0].type = `List (${resolverAddress}.RecordKeyValue)`;
await expectUnchangedState(resolver.contract, async () => {
await resolver.contract.call(
'setMulti',
values,
defaultParams,
)
})
})
it("should gracefully fail to unset records if they don't exist", async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
let zns = new Zns(zilliqa, address, {version})
const resolver = await zns.deployResolver('hello.zil')
await expectUnchangedState(resolver.contract, async () => {
await expect(resolver.unset('does_not_exist')).rejects.toThrow(
/Sender not owner or key does not exist/,
)
})
})
})
describe('registry.scilla', () => {
it('should deploy', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
expect(await zns.contract.getInit()).toHaveLength(5)
})
it('should disallow onResolverConfigured call from unauthorized resources', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
const registry = zns.contract
await zns.bestow('tld', address, address2)
const onResolverConfiguredTx = await registry.call(
'onResolverConfigured',
registryData.f.onResolverConfigured({
node: Zns.namehash('tld.zil'),
}),
defaultParams,
)
expect(onResolverConfiguredTx.isConfirmed()).toBeTruthy()
expect(await transactionEvents(onResolverConfiguredTx)).toEqual([])
const onResolverConfiguredTx2 = await registry.call(
'onResolverConfigured',
registryData.f.onResolverConfigured({
node: Zns.namehash('unknown'),
}),
defaultParams,
)
expect(onResolverConfiguredTx2.isConfirmed()).toBeTruthy()
expect(await transactionEvents(onResolverConfiguredTx2)).toEqual([])
})
it('should approve addresses and set and unset operators for addresses', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(
zilliqa,
undefined,
defaultRootNode,
{version},
)
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// approve normally
//////////////////////////////////////////////////////////////////////////
await zns.setApprovedAddress(defaultRootNode, address2)
expect(await zns.getApprovedAddress(defaultRootNode)).toEqual(address2)
//////////////////////////////////////////////////////////////////////////
// approve null address
//////////////////////////////////////////////////////////////////////////
await zns.setApprovedAddress(defaultRootNode, nullAddress)
expect(await zns.getApprovedAddress(defaultRootNode)).toEqual(nullAddress)
//////////////////////////////////////////////////////////////////////////
// fail to approve node owned by someone else
//////////////////////////////////////////////////////////////////////////
await expectUnchangedState(registry, async () => {
await expect(
zns.setApprovedAddress('node-owned-by-someone-else', address2),
).rejects.toThrow(/Sender not node owner/)
})
//////////////////////////////////////////////////////////////////////////
// add operator
//////////////////////////////////////////////////////////////////////////
await registry.call(
'approveFor',
registryData.f.approveFor({
address: address2,
isApproved: true,
}),
defaultParams,
)
expect(await contractMapValue(registry, 'operators', address)).toEqual([
'0x7bb3b0e8a59f3f61d9bff038f4aeb42cae2ecce8',
])
//////////////////////////////////////////////////////////////////////////
// remove operator
//////////////////////////////////////////////////////////////////////////
await registry.call(
'approveFor',
registryData.f.approveFor({
address: address2,
isApproved: false,
}),
defaultParams,
)
expect(await contractMapValue(registry, 'operators', address)).toEqual([])
})
it('should add and remove admins if currently admin', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// add admin
//////////////////////////////////////////////////////////////////////////
await zns.setAdmin(address2)
expect(await zns.getAdminAddresses()).toEqual([address2, address])
//////////////////////////////////////////////////////////////////////////
// remove admin
//////////////////////////////////////////////////////////////////////////
await zns.setAdmin(address2, false)
expect(await zns.getAdminAddresses()).toEqual([address])
//////////////////////////////////////////////////////////////////////////
// fail to set admin using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey2))
await expectUnchangedState(registry, async () => {
await expect(zns.setAdmin(address2, true)).rejects.toThrow(
/Sender not root node owner/,
)
})
})
it('rotates admin key', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
expect(await zns.getAdminAddresses()).toEqual([address])
const registry = zns.contract
await zns.rotateAdmin(privateKey2)
expect(await zns.getAdminAddresses()).toEqual([address2])
})
it('should freely configure names properly', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(
zilliqa,
undefined,
defaultRootNode,
{version},
)
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// configure resolver
//////////////////////////////////////////////////////////////////////////
const configureResolverTx = await registry.call(
'configureResolver',
registryData.f.configureResolver({
node: defaultRootNode,
resolver: address2,
}),
defaultParams,
)
expect(configureResolverTx.isConfirmed()).toBeTruthy()
expect(transactionEvents(configureResolverTx)).toEqual([
{
_eventname: 'Configured',
node: defaultRootNode,
owner: address,
resolver: address2,
},
])
expect(await zns.getResolverAddress(defaultRootNode)).toEqual(address2)
expect(await zns.getOwnerAddress(defaultRootNode)).toEqual(address)
//////////////////////////////////////////////////////////////////////////
// configure node
//////////////////////////////////////////////////////////////////////////
const configureNodeTx = await registry.call(
'configureNode',
registryData.f.configureNode({
node: defaultRootNode,
owner: address2,
resolver: address2,
}),
defaultParams,
)
expect(configureNodeTx.isConfirmed()).toBeTruthy()
expect(transactionEvents(configureNodeTx)).toEqual([
{
_eventname: 'Configured',
node: defaultRootNode,
owner: address2,
resolver: address2,
},
])
expect(await zns.getResolverAddress(defaultRootNode)).toEqual(address2)
expect(await zns.getOwnerAddress(defaultRootNode)).toEqual(address2)
//////////////////////////////////////////////////////////////////////////
// fail to configure resolver using bad address
//////////////////////////////////////////////////////////////////////////
await expectUnchangedState(registry, async () => {
await registry.call(
'configureResolver',
registryData.f.configureResolver({
node: defaultRootNode,
resolver: address,
}),
defaultParams,
)
})
//////////////////////////////////////////////////////////////////////////
// fail to configure node using bad address
//////////////////////////////////////////////////////////////////////////
await expectUnchangedState(registry, async () => {
await registry.call(
'configureNode',
registryData.f.configureNode({
node: defaultRootNode,
owner: address,
resolver: address,
}),
defaultParams,
)
})
})
it('should freely transfer names properly', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(
zilliqa,
undefined,
defaultRootNode,
{version},
)
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// approve address to check transfer
//////////////////////////////////////////////////////////////////////////
await zns.setApprovedAddress(defaultRootNode, address)
const transferTx = await registry.call(
'transfer',
registryData.f.transfer({
node: defaultRootNode,
owner: address2,
}),
defaultParams,
)
expect(transferTx.isConfirmed()).toBeTruthy
expect(await transactionEvents(transferTx)).toEqual([
{
_eventname: 'Configured',
node: defaultRootNode,
owner: address2,
resolver: nullAddress,
},
])
expect(await zns.getOwnerAddress(defaultRootNode)).toEqual(address2)
expect(await zns.getResolverAddress(defaultRootNode)).toEqual(nullAddress)
//////////////////////////////////////////////////////////////////////////
// fail to transfer using bad address
//////////////////////////////////////////////////////////////////////////
await expectUnchangedState(registry, async () => {
await registry.call(
'transfer',
registryData.f.transfer({
node: defaultRootNode,
owner: address,
}),
defaultParams,
)
})
})
it('should freely assign names properly', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(
zilliqa,
undefined,
defaultRootNode,
{version},
)
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// assign subdomain
//////////////////////////////////////////////////////////////////////////
await zns.setApprovedAddress(defaultRootNode, address)
const assignTx = await registry.call(
'assign',
registryData.f.assign({
parent: defaultRootNode,
label: 'tld',
owner: address,
}),
defaultParams,
)
expect(assignTx.isConfirmed()).toBeTruthy
expect(await transactionEvents(assignTx)).toEqual([
{
_eventname: 'Configured',
node: Zns.namehash('tld.zil'),
owner: address,
resolver: nullAddress,
},
{
_eventname: 'NewDomain',
parent: defaultRootNode,
label: 'tld',
},
])
expect(await zns.getOwnerAddress(defaultRootNode)).toEqual(address)
expect(await zns.getResolverAddress(defaultRootNode)).toEqual(nullAddress)
expect(await zns.getOwnerAddress('tld.zil')).toEqual(address)
expect(await zns.getResolverAddress('tld.zil')).toEqual(nullAddress)
//////////////////////////////////////////////////////////////////////////
// assign owned subdomain
//////////////////////////////////////////////////////////////////////////
await registry.call(
'assign',
registryData.f.assign({
parent: defaultRootNode,
label: 'tld',
owner: address2,
}),
defaultParams,
)
expect(await zns.getOwnerAddress(defaultRootNode)).toEqual(address)
expect(await zns.getResolverAddress(defaultRootNode)).toEqual(nullAddress)
expect(await zns.getOwnerAddress('tld.zil')).toEqual(address2)
expect(await zns.getResolverAddress('tld.zil')).toEqual(nullAddress)
//////////////////////////////////////////////////////////////////////////
// fail to assign subdomain using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey2))
await expectUnchangedState(registry, async () => {
await registry.call(
'assign',
registryData.f.assign({
parent: defaultRootNode,
label: 'tld',
owner: nullAddress,
}),
defaultParams,
)
})
})
it('should freely bestow names properly', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(
zilliqa,
undefined,
defaultRootNode,
{version},
)
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// bestow name
//////////////////////////////////////////////////////////////////////////
const bestowTx = await zns.bestow('tld', address, address)
expect(await transactionEvents(bestowTx)).toEqual([
{
_eventname: 'Configured',
node: Zns.namehash('tld.zil'),
owner: address,
resolver: address,
},
{
_eventname: 'NewDomain',
parent: defaultRootNode,
label: 'tld',
},
])
expect(await zns.getOwnerAddress(defaultRootNode)).toEqual(address)
expect(await zns.getOwnerAddress('tld.zil')).toEqual(address)
expect(await zns.getOwnerAddress('unknown')).toEqual(undefined)
expect(await zns.getResolverAddress(defaultRootNode)).toEqual(nullAddress)
expect(await zns.getResolverAddress('tld.zil')).toEqual(address)
expect(await zns.getResolverAddress('unknown')).toEqual(undefined)
//////////////////////////////////////////////////////////////////////////
// fail to bestow owned name
//////////////////////////////////////////////////////////////////////////
await expectUnchangedState(registry, async () => {
await expect(zns.bestow('tld', address2, address2)).rejects.toThrow(
/Sender admin/,
)
})
//////////////////////////////////////////////////////////////////////////
// fail to bestow owned using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey2))
await expectUnchangedState(registry, async () => {
await expect(
zns.bestow('other-tld', address2, address2),
).rejects.toThrow(/Sender admin/)
})
})
it('should allow admins to set registrar', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(zilliqa, undefined, undefined, {
version,
})
const registry = zns.contract
//////////////////////////////////////////////////////////////////////////
// set registrar address
//////////////////////////////////////////////////////////////////////////
await registry.call(
'setRegistrar',
registryData.f.setRegistrar({address: address2}),
defaultParams,
)
expect(await contractField(registry, 'registrar')).toEqual(address2)
//////////////////////////////////////////////////////////////////////////
// fail to set registrar address using bad address
//////////////////////////////////////////////////////////////////////////
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey2))
await expectUnchangedState(registry, async () => {
await registry.call(
'setRegistrar',
registryData.f.setRegistrar({address: address}),
defaultParams,
)
})
})
})
describe('simple_registrar.scilla', () => {
it('should deploy', async () => {
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const [registrarTx, registrar] = await deploySimpleRegistrar(
zilliqa,
{
registry: '0x' + '0'.repeat(40),
owner: '0x' + '0'.repeat(40),
ownedNode: defaultRootNode,
initialDefaultPrice: '1',
initialQaPerUSD: '1',
},
{gasLimit: Long.fromNumber(100000)},
)
expect(registrarTx.isConfirmed()).toBeTruthy()
expect(await registrar.getInit()).toHaveLength(8)
})
it('should register name', async () => {
const labelToRegister = 'name'
const domainToRegister = `${labelToRegister}.${defaultRootDomain}`
const nodeToRegister = Zns.namehash(domainToRegister)
const zilliqa = getZilliqa()
zilliqa.wallet.setDefault(zilliqa.wallet.addByPrivateKey(privateKey))
const zns = await Zns.deployRegistry(
zilliqa,
undefined,
defaultRootNode,
{version},
)
const registry = zns.contract
const [, registrar] = await deploySimpleRegistrar(
zilliqa,
{
registry: zns.address,
owner: address,
ownedNode: defaultRootNode,
initialDefaultPrice: '1',
initialQaPerUSD: '1',
},
{gasLimit: Long.fromNumber(100000)},
)
await registry.call(