-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathtaskHelper.ts
More file actions
979 lines (866 loc) · 39.4 KB
/
Copy pathtaskHelper.ts
File metadata and controls
979 lines (866 loc) · 39.4 KB
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
import chalk from 'chalk'
import { Contract, Signer, ethers, utils } from 'ethers'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { OmniAddress, OmniPoint, OmniTransaction, flattenTransactions } from '@layerzerolabs/devtools'
import { createGetHreByEid } from '@layerzerolabs/devtools-evm-hardhat'
import { createModuleLogger } from '@layerzerolabs/io-devtools'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { addressToBytes32 } from '@layerzerolabs/lz-v2-utilities'
import { Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-devtools'
import { LzAppOmniGraph, OAppEdgeConfig } from '@layerzerolabs/ua-devtools'
export const COLORS = {
default: chalk.white,
error: chalk.red,
warning: chalk.yellow,
success: chalk.green,
palette: [
chalk.magenta,
chalk.cyan,
chalk.yellow,
chalk.blue,
chalk.magentaBright,
chalk.cyanBright,
chalk.blueBright,
],
}
export const SUCCESS_SYMBOL = COLORS.success`✓`
// Define constants for config types
const CONFIG_TYPE_EXECUTOR = 1
const CONFIG_TYPE_ULN = 2
// Define the zero address using ethers.js constant
export const zeroAddress = ethers.constants.AddressZero
/**
* Get the executor config for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Uln302ExecutorConfig or undefined
*/
export async function getEpv1ExecutorConfig(
hre: HardhatRuntimeEnvironment,
eid: EndpointId,
address: OmniAddress
): Promise<Uln302ExecutorConfig | undefined> {
try {
const sendUlnDeployment = await hre.deployments.get('SendUln301')
const signer: Signer = hre.ethers.provider.getSigner()
const sendUlnContract = new Contract(sendUlnDeployment.address, sendUlnDeployment.abi, signer)
const configBytes: string = await sendUlnContract.getConfig(eid, address, CONFIG_TYPE_EXECUTOR)
const [executorConfigRaw] = ethers.utils.defaultAbiCoder.decode(
['tuple(uint32 maxMessageSize, address executorAddress)'],
configBytes
)
const executorConfig: Uln302ExecutorConfig = {
executor: executorConfigRaw.executorAddress,
maxMessageSize: executorConfigRaw.maxMessageSize,
}
return executorConfig
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 executor config: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the ULN config for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Uln302UlnConfig or undefined
*/
export async function getEpv1SendUlnConfig(
hre: HardhatRuntimeEnvironment,
eid: EndpointId,
address: OmniAddress
): Promise<Uln302UlnConfig | undefined> {
try {
const sendUlnDeployment = await hre.deployments.get('SendUln301')
const signer: Signer = hre.ethers.provider.getSigner()
const sendUlnContract = new Contract(sendUlnDeployment.address, sendUlnDeployment.abi, signer)
const configBytes: string = await sendUlnContract.getConfig(eid, address, CONFIG_TYPE_ULN)
const [ulnConfigRaw] = ethers.utils.defaultAbiCoder.decode(
[
'tuple(uint64 confirmations, uint8 requiredDVNCount, uint8 optionalDVNCount, uint8 optionalDVNThreshold, address[] requiredDVNs, address[] optionalDVNs)',
],
configBytes
)
const ulnConfig: Uln302UlnConfig = {
confirmations: ulnConfigRaw.confirmations.toNumber(),
requiredDVNs: ulnConfigRaw.requiredDVNs,
requiredDVNCount: ulnConfigRaw.requiredDVNCount,
optionalDVNs: ulnConfigRaw.optionalDVNs,
optionalDVNCount: ulnConfigRaw.optionalDVNCount,
optionalDVNThreshold: ulnConfigRaw.optionalDVNThreshold,
}
return ulnConfig
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 ULN config: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the ULN config for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Uln302UlnConfig or undefined
*/
export async function getEpv1ReceiveUlnConfig(
hre: HardhatRuntimeEnvironment,
eid: EndpointId,
address: OmniAddress
): Promise<Uln302UlnConfig | undefined> {
try {
const sendUlnDeployment = await hre.deployments.get('ReceiveUln301')
const signer: Signer = hre.ethers.provider.getSigner()
const sendUlnContract = new Contract(sendUlnDeployment.address, sendUlnDeployment.abi, signer)
const configBytes: string = await sendUlnContract.getConfig(eid, address, CONFIG_TYPE_ULN)
const [ulnConfigRaw] = ethers.utils.defaultAbiCoder.decode(
[
'tuple(uint64 confirmations, uint8 requiredDVNCount, uint8 optionalDVNCount, uint8 optionalDVNThreshold, address[] requiredDVNs, address[] optionalDVNs)',
],
configBytes
)
const ulnConfig: Uln302UlnConfig = {
confirmations: ulnConfigRaw.confirmations.toNumber(),
requiredDVNs: ulnConfigRaw.requiredDVNs,
requiredDVNCount: ulnConfigRaw.requiredDVNCount,
optionalDVNs: ulnConfigRaw.optionalDVNs,
optionalDVNCount: ulnConfigRaw.optionalDVNCount,
optionalDVNThreshold: ulnConfigRaw.optionalDVNThreshold,
}
return ulnConfig
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 ULN config: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the send library address for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Send library address or undefined
*/
export async function getEpv1SendLibraryAddress(
hre: HardhatRuntimeEnvironment,
address: OmniAddress
): Promise<string | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const sendLibraryAddress: string = await endpointContract.getSendLibraryAddress(address)
return sendLibraryAddress
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 send library: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the receive library address for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Receive library address or undefined
*/
export async function getEpv1ReceiveLibraryAddress(
hre: HardhatRuntimeEnvironment,
eid: EndpointId,
address: OmniAddress
): Promise<string | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const receiveLibraryAddress: string = await endpointContract.getReceiveLibraryAddress(address)
return receiveLibraryAddress
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 receive library: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the send library address for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Send library address or undefined
*/
export async function getEpv1DefaultSendLibraryAddress(hre: HardhatRuntimeEnvironment): Promise<string | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const DEFAULT_SEND_VERSION = await endpointContract.getSendVersion(zeroAddress)
const sendLibraryAddress = await endpointContract.libraryLookup(DEFAULT_SEND_VERSION)
return sendLibraryAddress
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 send library address: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the default Receive Library address for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} - Hardhat runtime environment
* @returns {Promise<string | undefined>}
*/
export async function getEpv1DefaultReceiveLibraryAddress(hre: HardhatRuntimeEnvironment): Promise<string | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
// 1. get default versions
const DEFAULT_RECEIVE_VERSION = await endpointContract.defaultReceiveVersion()
// 2. lookup library
const defaultReceiveLibraryAddress: string = await endpointContract.libraryLookup(DEFAULT_RECEIVE_VERSION)
return defaultReceiveLibraryAddress
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 receive library address: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the executor config for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @param address {OmniAddress} Address of the OApp
* @returns Uln302ExecutorConfig or undefined
*/
export async function getEpv1DefaultExecutorConfig(
hre: HardhatRuntimeEnvironment,
eid: EndpointId
): Promise<Uln302ExecutorConfig | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const DEFAULT_SEND_VERSION = await endpointContract.defaultSendVersion()
const configBytes: string = await endpointContract.getConfig(
DEFAULT_SEND_VERSION,
eid,
zeroAddress,
CONFIG_TYPE_EXECUTOR
)
const emptyExecutorConfig: Uln302ExecutorConfig = {
executor: zeroAddress,
maxMessageSize: 0,
}
if (configBytes === '0x0000000000000000000000000000000000000000000000000000000000000000') {
return emptyExecutorConfig
}
const [executorConfigRaw] = ethers.utils.defaultAbiCoder.decode(
['tuple(uint32 maxMessageSize, address executorAddress)'],
configBytes
)
const executorConfig: Uln302ExecutorConfig = {
executor: executorConfigRaw.executorAddress,
maxMessageSize: executorConfigRaw.maxMessageSize,
}
return executorConfig
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 default Executor config: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the executor config for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @returns Uln302ExecutorConfig or undefined
*/
export async function getEpv1DefaultSendConfig(
hre: HardhatRuntimeEnvironment,
eid: EndpointId
): Promise<Uln302UlnConfig | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const DEFAULT_SEND_VERSION = await endpointContract.defaultSendVersion()
const configBytes: string = await endpointContract.getConfig(
DEFAULT_SEND_VERSION,
eid,
zeroAddress,
CONFIG_TYPE_ULN
)
const emptyUlnConfig: Uln302UlnConfig = {
confirmations: BigInt(0),
requiredDVNs: [zeroAddress],
requiredDVNCount: 0,
optionalDVNs: [],
optionalDVNCount: 0,
optionalDVNThreshold: 0,
}
if (configBytes === '0x0000000000000000000000000000000000000000000000000000000000000000') {
return emptyUlnConfig
}
const [ulnConfigRaw] = ethers.utils.defaultAbiCoder.decode(
[
'tuple(uint64 confirmations, uint8 requiredDVNCount, uint8 optionalDVNCount, uint8 optionalDVNThreshold, address[] requiredDVNs, address[] optionalDVNs)',
],
configBytes
)
const ulnConfig: Uln302UlnConfig = {
confirmations: ulnConfigRaw.confirmations.toNumber(),
requiredDVNs: ulnConfigRaw.requiredDVNs,
requiredDVNCount: ulnConfigRaw.requiredDVNCount,
optionalDVNs: ulnConfigRaw.optionalDVNs,
optionalDVNCount: ulnConfigRaw.optionalDVNCount,
optionalDVNThreshold: ulnConfigRaw.optionalDVNThreshold,
}
return ulnConfig
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 default ULN config: ${(error as Error).message}`)
return undefined
}
}
/**
* Get the executor config for EPV1 (EVM-based) OApp
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param eid {EndpointId} Remote Endpoint ID
* @returns Uln302ExecutorConfig or undefined
*/
export async function getEpv1DefaultReceiveConfig(
hre: HardhatRuntimeEnvironment,
eid: EndpointId
): Promise<Uln302UlnConfig | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const DEFAULT_RECEIVE_VERSION = await endpointContract.defaultReceiveVersion()
const configBytes: string = await endpointContract.getConfig(
DEFAULT_RECEIVE_VERSION,
eid,
zeroAddress,
CONFIG_TYPE_ULN
)
const emptyUlnConfig: Uln302UlnConfig = {
confirmations: BigInt(0),
requiredDVNs: [zeroAddress],
requiredDVNCount: 0,
optionalDVNs: [],
optionalDVNCount: 0,
optionalDVNThreshold: 0,
}
if (configBytes === '0x0000000000000000000000000000000000000000000000000000000000000000') {
return emptyUlnConfig
}
const [ulnConfigRaw] = ethers.utils.defaultAbiCoder.decode(
[
'tuple(uint64 confirmations, uint8 requiredDVNCount, uint8 optionalDVNCount, uint8 optionalDVNThreshold, address[] requiredDVNs, address[] optionalDVNs)',
],
configBytes
)
const ulnConfig: Uln302UlnConfig = {
confirmations: ulnConfigRaw.confirmations.toNumber(),
requiredDVNs: ulnConfigRaw.requiredDVNs,
requiredDVNCount: ulnConfigRaw.requiredDVNCount,
optionalDVNs: ulnConfigRaw.optionalDVNs,
optionalDVNCount: ulnConfigRaw.optionalDVNCount,
optionalDVNThreshold: ulnConfigRaw.optionalDVNThreshold,
}
return ulnConfig
} catch (error) {
const moduleLogger = createModuleLogger('LzApp')
moduleLogger.error(`Error fetching EPV1 default ULN receive config: ${(error as Error).message}`)
return undefined
}
}
export interface LzAppConfig {
sendLibrary: string
receiveLibrary: string
sendExecutorConfig: Uln302ExecutorConfig
sendUlnConfig: Uln302UlnConfig
receiveUlnConfig: Uln302UlnConfig
}
/**
* Retrieves the index of a library address from the Endpoint's libraryLookup mapping.
* @param hre {HardhatRuntimeEnvironment} Hardhat runtime environment
* @param libraryAddress {string} Address of the library to find
* @returns The index of the library or undefined if not found
*/
export async function getLibraryIndex(
hre: HardhatRuntimeEnvironment,
libraryAddress: string
): Promise<number | undefined> {
try {
const endpointDeployment = await hre.deployments.get('Endpoint')
const signer: Signer = hre.ethers.provider.getSigner()
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi, signer)
const MAX_ITERATIONS = await endpointContract.latestVersion()
for (let i = 0; i <= MAX_ITERATIONS; i++) {
const currentLibraryAddress = await endpointContract.libraryLookup(i)
if (currentLibraryAddress === libraryAddress) {
return i
}
}
console.error(`[LzApp] Library address ${libraryAddress} not found in libraryLookup mapping.`)
return undefined
} catch (error) {
console.error(`[LzApp] Error searching for library index: ${(error as Error).message}`)
return undefined
}
}
/**
* Encodes the ExecutorConfig into ABI-encoded bytes.
* @param config Uln302ExecutorConfig object
* @returns ABI-encoded string
*/
function encodeExecutorConfig(config: Uln302ExecutorConfig): string {
return ethers.utils.defaultAbiCoder.encode(
['tuple(uint32 maxMessageSize, address executorAddress)'],
[[config.maxMessageSize, config.executor]]
)
}
/**
* Encodes the UlnConfig into ABI-encoded bytes.
*
* The DVN counts are derived from the array lengths rather than read from the config's
* `requiredDVNCount`/`optionalDVNCount`. That is only valid because the callers here feed
* RESOLVED configs (read via `getConfig` -> `getUlnConfig`, which collapses any stored NIL
* sentinel to 0 before returning). Uln301 inherits `UlnBase`, so its stored config CAN hold a
* NIL sentinel — if this were ever pointed at a raw/stored config (`getAppUlnConfig`), the
* length derivation would silently drop that sentinel. Keep it on the resolved-config path.
*
* @param config Uln302UlnConfig object (resolved, not raw/stored)
* @returns ABI-encoded string
*/
function encodeUlnConfig(config: Uln302UlnConfig): string {
return ethers.utils.defaultAbiCoder.encode(
[
'tuple(uint64 confirmations, uint8 requiredDVNCount, uint8 optionalDVNCount, uint8 optionalDVNThreshold, address[] requiredDVNs, address[] optionalDVNs)',
],
[
[
config.confirmations ?? 0,
(config.requiredDVNs || []).length,
(config.optionalDVNs || []).length,
config.optionalDVNThreshold ?? 0,
config.requiredDVNs || [],
config.optionalDVNs || [],
],
]
)
}
/**
* Retrieves the current Send Library version index from the LzApp contract.
*/
export async function getSendLibrary(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint
): Promise<[string | undefined, number | undefined]> {
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider)
const endpointDeployment = await hre.deployments.get('Endpoint')
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi)
try {
const sendLibraryIndex = await lzAppContract.sendVersion()
const currentLibraryAddress = await endpointContract.libraryLookup(sendLibraryIndex)
return [currentLibraryAddress, sendLibraryIndex]
} catch (error) {
console.error(`[LzApp] Failed to retrieve Send Library version:`, error)
return [undefined, undefined]
}
}
/**
* Retrieves the current Receive Library version index from the LzApp contract.
*/
export async function getReceiveLibrary(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint
): Promise<[string | undefined, number | undefined]> {
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider)
const endpointDeployment = await hre.deployments.get('Endpoint')
const endpointContract = new Contract(endpointDeployment.address, endpointDeployment.abi)
try {
const receiveLibraryIndex = await lzAppContract.receiveVersion()
const currentLibraryAddress = await endpointContract.libraryLookup(receiveLibraryIndex)
return [currentLibraryAddress, receiveLibraryIndex]
} catch (error) {
console.error(`[LzApp] Failed to retrieve Receive Library version:`, error)
return [undefined, undefined]
}
}
// function getConfig(
// uint16 _version,
// uint16 _chainId,
// address,
// uint _configType
/**
* Retrieves the current Executor Config for a specific Endpoint ID from the LzApp contract.
*/
export async function getExecutorConfig(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
currentLibVersion: number,
eid: EndpointId
): Promise<Uln302ExecutorConfig | undefined> {
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider)
try {
const executorConfig = await lzAppContract.getConfig(currentLibVersion, eid, CONFIG_TYPE_EXECUTOR)
console.log(`[LzApp] Current Executor Config for Endpoint ID ${eid}:`, executorConfig)
return executorConfig
} catch (error) {
console.error(`[LzApp] Failed to retrieve Executor Config for Endpoint ID ${eid}:`, error)
return undefined
}
}
/**
* Retrieves the current ULN Config for a specific Endpoint ID from the LzApp contract.
*/
export async function getUlnConfig(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
sendLibVersion: number,
receiveLibVersion: number,
eid: EndpointId
): Promise<{ sendConfig: Uln302UlnConfig; receiveConfig: Uln302UlnConfig } | undefined> {
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider)
try {
const sendUlnConfig = await lzAppContract.getConfig(sendLibVersion, eid, CONFIG_TYPE_ULN)
const receiveUlnConfig = await lzAppContract.getConfig(receiveLibVersion, eid, CONFIG_TYPE_ULN)
console.log(`[LzApp] Current ULN Config for Endpoint ID ${eid}:`, {
sendConfig: sendUlnConfig,
receiveConfig: receiveUlnConfig,
})
return { sendConfig: sendUlnConfig, receiveConfig: receiveUlnConfig }
} catch (error) {
console.error(`[LzApp] Failed to retrieve ULN Config for Endpoint ID ${eid}:`, error)
return undefined
}
}
/**
* Sets the Send Library address on the LzApp contract and returns the transaction.
*/
export async function setSendLibrary(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
sendLibraryAddress: string
): Promise<OmniTransaction | undefined> {
if (!sendLibraryAddress || sendLibraryAddress === zeroAddress) {
console.log(`[LzApp] Invalid Send Library address: ${sendLibraryAddress}. Skipping.`)
return undefined
}
const sendLibraryIndex = await getLibraryIndex(hre, sendLibraryAddress)
if (sendLibraryIndex === undefined) {
console.error(`[LzApp] Send Library address not found: ${sendLibraryAddress}`)
return undefined
}
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider.getSigner())
const data = lzAppContract.interface.encodeFunctionData('setSendVersion', [sendLibraryIndex])
return {
point: lzApp,
data,
description: `Set Send Library version to index ${sendLibraryIndex}`,
}
}
/**
* Sets the Receive Library address on the LzApp contract and returns the transaction.
*/
export async function setReceiveLibrary(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
receiveLibraryAddress: string
): Promise<OmniTransaction | undefined> {
if (!receiveLibraryAddress || receiveLibraryAddress === zeroAddress) {
console.log(`[LzApp] Invalid Receive Library address: ${receiveLibraryAddress}. Skipping.`)
return undefined
}
const receiveLibraryIndex = await getLibraryIndex(hre, receiveLibraryAddress)
if (receiveLibraryIndex === undefined) {
console.error(`[LzApp] Receive Library address not found: ${receiveLibraryAddress}`)
return undefined
}
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider.getSigner())
const data = lzAppContract.interface.encodeFunctionData('setReceiveVersion', [receiveLibraryIndex])
return {
point: lzApp,
data,
description: `Set Receive Library version to index ${receiveLibraryIndex}`,
}
}
/**
* Sets the Executor Config on the LzApp contract and returns the transaction.
*/
export async function setExecutorConfig(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
lzAppConfig: OAppEdgeConfig,
eid: EndpointId
): Promise<OmniTransaction | undefined> {
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider.getSigner())
const sendUlnIndex = await getLibraryIndex(hre, lzAppConfig.sendLibrary!)
const encodedConfig = encodeExecutorConfig(lzAppConfig.sendConfig?.executorConfig as Uln302ExecutorConfig)
const data = lzAppContract.interface.encodeFunctionData('setConfig', [
sendUlnIndex,
eid,
CONFIG_TYPE_EXECUTOR,
encodedConfig,
])
return {
point: lzApp,
data,
description: `Set Executor Config for Endpoint ID ${eid}`,
}
}
/**
* Sets the ULN Config on the LzApp contract and returns the transaction.
*/
export async function setUlnConfig(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
lzAppConfig: OAppEdgeConfig,
eid: EndpointId
): Promise<OmniTransaction[]> {
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, hre.ethers.provider.getSigner())
const sendUlnIndex = await getLibraryIndex(hre, lzAppConfig.sendLibrary!)
const receiveUlnIndex = await getLibraryIndex(hre, lzAppConfig.receiveLibraryConfig?.receiveLibrary!)
const encodedSendUlnConfig = encodeUlnConfig(lzAppConfig.sendConfig?.ulnConfig as Uln302UlnConfig)
const encodedReceiveUlnConfig = encodeUlnConfig(lzAppConfig.receiveConfig?.ulnConfig as Uln302UlnConfig)
const sendTx = {
point: lzApp,
data: lzAppContract.interface.encodeFunctionData('setConfig', [
sendUlnIndex,
eid,
CONFIG_TYPE_ULN,
encodedSendUlnConfig,
]),
description: `Set Send ULN Config for Endpoint ID ${eid}`,
}
const receiveTx = {
point: lzApp,
data: lzAppContract.interface.encodeFunctionData('setConfig', [
receiveUlnIndex,
eid,
CONFIG_TYPE_ULN,
encodedReceiveUlnConfig,
]),
description: `Set Receive ULN Config for Endpoint ID ${eid}`,
}
return [sendTx, receiveTx]
}
/**
* Configures the LzApp contract with executor, ULN configs, and library addresses.
* @param hre {HardhatRuntimeEnvironment} - Hardhat runtime environment
* @param lzApp {OmniPoint} - The LzApp identifier containing contract name or address.
* @param config {LzAppConfig} - The LzApp configuration object.
* @param to {eid: EndpointId; address: OmniAddress} - Remote endpoint information.
* @param from {eid: EndpointId; address: OmniAddress} - Local endpoint information.
* @returns {Promise<void>}
*/
export async function configureLzApp(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
config: LzAppConfig,
to: { eid: EndpointId; address: OmniAddress }
): Promise<void> {
await Promise.all([
setSendLibrary(hre, lzApp, config.sendLibrary),
setReceiveLibrary(hre, lzApp, config.receiveLibrary),
setExecutorConfig(hre, lzApp, config, to.eid),
setUlnConfig(hre, lzApp, config, to.eid),
])
}
/**
* Sets the trusted remote address for the LzApp contract.
* @param hre {HardhatRuntimeEnvironment} - Hardhat runtime environment.
* @param lzApp {OmniPoint} - The LzApp identifier containing contract name or address.
* @param to {eid: EndpointId; address: OmniAddress} - Remote endpoint information.
* @param from {eid: EndpointId; address: OmniAddress} - Local endpoint information.
* @returns {Promise<void>}
*/
export async function setTrustedRemote(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
to: { eid: EndpointId; address: OmniAddress }
): Promise<OmniTransaction | undefined> {
try {
// Define the LzApp contract
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const signer: Signer = hre.ethers.provider.getSigner()
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, signer)
// Validate `from` and `to` details
if (!lzApp.address || !to.address) {
console.error(`Error: Missing local or remote LzApp address.`)
return undefined
}
// Encode the path (remote address followed by local address)
const path = utils.solidityPack(
['address', 'address'],
[addressToBytes32(to.address), ethers.utils.getAddress(lzApp.address)]
)
const setTrustedRemoteTx = {
point: lzApp,
data: lzAppContract.interface.encodeFunctionData('setTrustedRemote', [to.eid, path]),
description: `Set trusted remote for Endpoint ID ${to.eid}`,
}
return setTrustedRemoteTx
} catch (error) {
console.error(`Error setting trusted remote: ${(error as Error).message}`)
}
}
/**
* Retrieves the trusted remote address for a given chain ID.
* @param hre {HardhatRuntimeEnvironment} - Hardhat runtime environment.
* @param lzApp {OmniPoint} - The LzApp identifier containing contract name or address.
* @param remoteChainId {number} - The remote chain ID for which to fetch the trusted remote address.
* @returns {Promise<string | null>} - The trusted remote address or null if not found.
*/
export async function getTrustedRemote(
hre: HardhatRuntimeEnvironment,
lzApp: OmniPoint,
dstEid: number
): Promise<string | null> {
try {
// Define the LzApp contract
const lzAppDeployment = await hre.deployments.get(lzApp.contractName || lzApp.address)
const signer: Signer = hre.ethers.provider.getSigner()
const lzAppContract = new Contract(lzAppDeployment.address, lzAppDeployment.abi, signer)
// Call the contract's getter function
const path: string = await lzAppContract.trustedRemoteLookup(dstEid)
if (path === '0x') {
return path
}
// Decode the path to retrieve the remote address
const remoteAddress = path.slice(0, path.length - 40) // Remove the last 20 bytes (local address)
return remoteAddress
} catch (error) {
console.error(`Error getting trusted remote: ${(error as Error).message}`)
return null
}
}
/**
* Configures the LzApp graph by generating transactions for each connection.
*/
export const configureLzAppGraph = async (
graph: LzAppOmniGraph,
hre: HardhatRuntimeEnvironment
): Promise<OmniTransaction[]> => {
const logger = createModuleLogger('LzApp')
const getHreByEid = createGetHreByEid(hre)
return flattenTransactions(
(
await Promise.all(
graph.connections.map(async ({ vector: { from, to }, config }) => {
logger.verbose(`Configuring connection: ${from.eid} → ${to.eid}`)
const hreForEid = await getHreByEid(from.eid)
if (!hreForEid) {
logger.error(`Failed to get hre for EID: ${from.eid}`)
return []
}
// Ensure properties exist on `config`
const typedConfig = config as OAppEdgeConfig
if (
!typedConfig.sendLibrary ||
!typedConfig.receiveLibraryConfig?.receiveLibrary ||
!typedConfig.sendConfig?.executorConfig ||
!typedConfig.sendConfig?.ulnConfig ||
!typedConfig.receiveConfig?.ulnConfig
) {
logger.error(`Missing configuration properties for connection from ${from.eid} to ${to.eid}`)
return []
}
const transactions: OmniTransaction[] = []
try {
logger.info('Checking LzApp trusted remotes configuration')
const currentTrustedRemote = await getTrustedRemote(hreForEid, from, to.eid)
if (currentTrustedRemote === null) {
throw new Error(`Failed to retrieve trusted remote for ${from.eid} → ${to.eid}`)
}
const newRemote = addressToBytes32(to.address).toString()
const currentRemote = addressToBytes32(currentTrustedRemote).toString()
if (currentRemote !== newRemote) {
const setTrustedRemoteTx = await setTrustedRemote(hreForEid, from, to)
if (setTrustedRemoteTx) transactions.push(setTrustedRemoteTx)
}
logger.info(`${SUCCESS_SYMBOL} Checked LzApp trusted remotes configuration`)
// Generate transactions using the retrieved hre
logger.info(`Checking LzApp send libraries configuration`)
if (!from.contractName) {
throw new Error(`Failed to retrieve contract name for ${from.eid}`)
}
const LzApp = await hreForEid.deployments.get(from.contractName)
if (!LzApp) {
throw new Error(`Failed to retrieve LzApp deployment for ${from.contractName}`)
}
const getSendLibrary = await getEpv1SendLibraryAddress(hreForEid, LzApp.address)
if (!getSendLibrary) {
throw new Error(`Failed to retrieve Send Library address for ${LzApp.address}`)
}
if (getSendLibrary !== typedConfig.sendLibrary) {
const sendLibraryTx = await setSendLibrary(hreForEid, from, typedConfig.sendLibrary)
if (sendLibraryTx) transactions.push(sendLibraryTx)
}
logger.info(`${SUCCESS_SYMBOL} Checked LzApp send libraries configuration`)
logger.info(`Checking LzApp receive libraries configuration`)
const getReceiveLibrary = await getEpv1ReceiveLibraryAddress(hreForEid, to.eid, LzApp.address)
if (!getReceiveLibrary) {
throw new Error(`Failed to retrieve Receive Library address for ${LzApp.address}`)
}
if (getReceiveLibrary !== typedConfig.receiveLibraryConfig.receiveLibrary) {
const receiveLibraryTx = await setReceiveLibrary(
hreForEid,
from,
typedConfig.receiveLibraryConfig.receiveLibrary
)
if (receiveLibraryTx) transactions.push(receiveLibraryTx)
}
logger.info(`${SUCCESS_SYMBOL} Checked LzApp receive libraries configuration`)
logger.info(`Checking LzApp uln configuration`)
const getExecutorConfig = await getEpv1ExecutorConfig(hreForEid, to.eid, LzApp.address)
if (!getExecutorConfig) {
throw new Error(`Failed to retrieve Executor Config for ${LzApp.address}`)
}
if (
getExecutorConfig.executor.toLowerCase() !==
typedConfig.sendConfig.executorConfig.executor.toLowerCase() ||
BigInt(getExecutorConfig.maxMessageSize) !==
BigInt(typedConfig.sendConfig.executorConfig.maxMessageSize)
) {
console.log(getExecutorConfig)
const executorTx = await setExecutorConfig(hreForEid, from, typedConfig, to.eid)
if (executorTx) transactions.push(executorTx)
}
const getSendUlnConfig = await getEpv1SendUlnConfig(hreForEid, to.eid, LzApp.address)
const getReceiveUlnConfig = await getEpv1ReceiveUlnConfig(hreForEid, to.eid, LzApp.address)
if (!getSendUlnConfig || !getReceiveUlnConfig) {
throw new Error(`Failed to retrieve ULN Config for ${LzApp.address}`)
}
encodeUlnConfig(getSendUlnConfig as Uln302UlnConfig) !==
encodeUlnConfig(typedConfig!.sendConfig!.ulnConfig as Uln302UlnConfig) ||
encodeUlnConfig(getReceiveUlnConfig as Uln302UlnConfig) !==
encodeUlnConfig(typedConfig!.receiveConfig!.ulnConfig as Uln302UlnConfig)
? transactions.push(...(await setUlnConfig(hreForEid, from, typedConfig, to.eid)))
: null
logger.info(`${SUCCESS_SYMBOL} Checked LzApp uln configuration`)
logger.info(`${SUCCESS_SYMBOL} Checked LzApp configuration`)
} catch (error) {
logger.error(`Error configuring connection from ${from.eid} to ${to.eid}: ${error}`)
throw error
}
// Combine all transactions for this connection
return transactions
})
)
)
.flat()
.filter((tx): tx is OmniTransaction => tx !== undefined)
)
}