/
config.go
1344 lines (1181 loc) · 41.4 KB
/
config.go
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
package params
import (
"encoding/json"
"errors"
"math/big"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/deltaswapio/swaprouter/v3/common"
"github.com/deltaswapio/swaprouter/v3/log"
)
// router swap constants
const (
RouterSwapPrefixID = "routerswap"
)
// CustomizeConfigFunc customize config items
var CustomizeConfigFunc func(*RouterConfig)
// IsTestMode used for testing
var IsTestMode bool
// IsReload is reloading config
var IsReload bool
var (
routerConfig = NewRouterConfig()
routerConfigFile string
locDataDir string
// IsSwapServer is swap server
IsSwapServer bool
chainIDBlacklistMap = make(map[string]struct{})
tokenIDBlacklistMap = make(map[string]struct{})
tokenIDBlacklistOnChainMap = make(map[string]map[string]struct{})
accountBlacklistMap = make(map[string]struct{})
fixedGasPriceMap = make(map[string]*big.Int) // key is chainID
maxGasPriceMap = make(map[string]*big.Int) // key is chainID
callByContractWhitelist = make(map[string]map[string]struct{}) // chainID -> caller
callByContractCodeHashWhitelist = make(map[string]map[string]struct{}) // chainID -> codehash
bigValueWhitelist = make(map[string]map[string]struct{}) // tokenID -> caller
autoSwapNonceEnabledChains = make(map[string]struct{})
dynamicFeeTxEnabledChains = make(map[string]struct{})
enableCheckTxBlockHashChains = make(map[string]struct{})
enableCheckTxBlockIndexChains = make(map[string]struct{})
disableUseFromChainIDInReceiptChains = make(map[string]struct{})
useFastMPCChains = make(map[string]struct{})
increaseNonceWhenSendTxChains = make(map[string]struct{})
dontCheckReceivedTokenIDs = make(map[string]struct{})
dontCheckBalanceTokenIDs = make(map[string]struct{})
dontCheckTotalSupplyTokenIDs = make(map[string]struct{})
checkTokenBalanceEnabledChains = make(map[string]struct{})
ignoreAnycallFallbackAppIDs = make(map[string]struct{})
isDebugMode *bool
isNFTSwapWithData *bool
isAllowCallByContract *bool
isCheckEIP1167Master *bool
)
// exported variables
var (
GetBalanceBlockNumberOpt = "latest" // latest or pending
GatewayConfigFile string
)
func NewRouterConfig() *RouterConfig {
return &RouterConfig{
MPC: &MPCConfig{},
Blacklists: &Blacklists{},
Extra: &ExtraConfig{},
}
}
// RouterServerConfig only for server
type RouterServerConfig struct {
Admins []string
Assistants []string
MongoDB *MongoDBConfig
APIServer *APIServerConfig
AutoSwapNonceEnabledChains []string `toml:",omitempty" json:",omitempty"`
// extras
EnableReplaceSwap bool
EnablePassBigValueSwap bool
ReplacePlusGasPricePercent uint64 `toml:",omitempty" json:",omitempty"`
WaitTimeToReplace int64 `toml:",omitempty" json:",omitempty"` // seconds
MaxReplaceCount int `toml:",omitempty" json:",omitempty"`
MaxReplaceDistance uint64 `toml:",omitempty" json:",omitempty"`
PlusGasPricePercentage uint64 `toml:",omitempty" json:",omitempty"`
MaxPlusGasPricePercentage uint64 `toml:",omitempty" json:",omitempty"`
MaxGasPriceFluctPercent uint64 `toml:",omitempty" json:",omitempty"`
FixedGasPrice map[string]string `toml:",omitempty" json:",omitempty"` // key is chain ID
MaxGasPrice map[string]string `toml:",omitempty" json:",omitempty"` // key is chain ID
NoncePassedConfirmInterval map[string]int64 `toml:",omitempty" json:",omitempty"` // key is chain ID
CalcGasPriceMethod map[string]string `toml:",omitempty" json:",omitempty"` // key is chain ID
RetrySendTxLoopCount map[string]int `toml:",omitempty" json:",omitempty"` // key is chain ID
SendTxLoopCount map[string]int `toml:",omitempty" json:",omitempty"` // key is chain ID
SendTxLoopInterval map[string]int `toml:",omitempty" json:",omitempty"` // key is chain ID
DefaultFee map[string]string `toml:",omitempty" json:",omitempty"` // key is chain ID
DefaultGasLimit map[string]uint64 `toml:",omitempty" json:",omitempty"` // key is chain ID
MaxGasLimit map[string]uint64 `toml:",omitempty" json:",omitempty"` // key is chain ID
MaxTokenGasLimit map[string]map[string]uint64 `toml:",omitempty" json:",omitempty"` // key is tokenID,chainID
DynamicFeeTx map[string]*DynamicFeeTxConfig `toml:",omitempty" json:",omitempty"` // key is chain ID
}
// RouterOracleConfig only for oracle
type RouterOracleConfig struct {
ServerAPIAddress string
NoCheckServerConnection bool `toml:",omitempty" json:",omitempty"`
CheckGasTokenBalance bool `toml:",omitempty" json:",omitempty"`
}
// RouterConfig config
type RouterConfig struct {
Server *RouterServerConfig `toml:",omitempty" json:",omitempty"`
Oracle *RouterOracleConfig `toml:",omitempty" json:",omitempty"`
Identifier string
SwapType string
SwapSubType string
Onchain *OnchainConfig
*GatewayConfigs
MPC *MPCConfig
FastMPC *MPCConfig `toml:",omitempty" json:",omitempty"`
Extra *ExtraConfig `toml:",omitempty" json:",omitempty"`
*Blacklists
}
// GatewayConfigs gateway config
type GatewayConfigs struct {
Gateways map[string][]string // key is chain ID
GatewaysExt map[string][]string `toml:",omitempty" json:",omitempty"` // key is chain ID
EVMGatewaysExt map[string][]string `toml:",omitempty" json:",omitempty"` // key is chain ID
FinalizeGateways map[string][]string `toml:",omitempty" json:",omitempty"` // key is chain ID
GRPCGateways map[string][]string `toml:",omitempty" json:",omitempty"` // key is chain ID
}
// Blacklists black lists
type Blacklists struct {
ChainIDBlackList []string `toml:",omitempty" json:",omitempty"`
TokenIDBlackList []string `toml:",omitempty" json:",omitempty"`
TokenIDBlackListOnChain map[string][]string `toml:",omitempty" json:",omitempty"`
AccountBlackList []string `toml:",omitempty" json:",omitempty"`
}
// ExtraConfig extra config
type ExtraConfig struct {
IsDebugMode bool `toml:",omitempty" json:",omitempty"`
EnableSwapWithPermit bool `toml:",omitempty" json:",omitempty"`
ForceAnySwapInAuto bool `toml:",omitempty" json:",omitempty"`
IsNFTSwapWithData bool `toml:",omitempty" json:",omitempty"`
EnableParallelSwap bool `toml:",omitempty" json:",omitempty"`
UsePendingBalance bool `toml:",omitempty" json:",omitempty"`
DontPanicInInitRouter bool `toml:",omitempty" json:",omitempty"`
DontCheckInInitRouter bool `toml:",omitempty" json:",omitempty"`
MinReserveFee map[string]uint64 `toml:",omitempty" json:",omitempty"`
BaseFeePercent map[string]int64 `toml:",omitempty" json:",omitempty"` // key is chain ID
MinReserveBudget map[string]uint64 `toml:",omitempty" json:",omitempty"`
AllowCallByConstructor bool `toml:",omitempty" json:",omitempty"`
AllowCallByContract bool `toml:",omitempty" json:",omitempty"`
CheckEIP1167Master bool `toml:",omitempty" json:",omitempty"`
CallByContractWhitelist map[string][]string `toml:",omitempty" json:",omitempty"` // chainID -> whitelist
CallByContractCodeHashWhitelist map[string][]string `toml:",omitempty" json:",omitempty"` // chainID -> whitelist
BigValueWhitelist map[string][]string `toml:",omitempty" json:",omitempty"` // tokenID -> whitelist
DynamicFeeTxEnabledChains []string `toml:",omitempty" json:",omitempty"`
EnableCheckTxBlockHashChains []string `toml:",omitempty" json:",omitempty"`
EnableCheckTxBlockIndexChains []string `toml:",omitempty" json:",omitempty"`
DisableUseFromChainIDInReceiptChains []string `toml:",omitempty" json:",omitempty"`
UseFastMPCChains []string `toml:",omitempty" json:",omitempty"`
IncreaseNonceWhenSendTxChains []string `toml:",omitempty" json:",omitempty"`
DontCheckReceivedTokenIDs []string `toml:",omitempty" json:",omitempty"`
DontCheckBalanceTokenIDs []string `toml:",omitempty" json:",omitempty"`
DontCheckTotalSupplyTokenIDs []string `toml:",omitempty" json:",omitempty"`
CheckTokenBalanceEnabledChains []string `toml:",omitempty" json:",omitempty"`
IgnoreAnycallFallbackAppIDs []string `toml:",omitempty" json:",omitempty"`
RPCClientTimeout map[string]int `toml:",omitempty" json:",omitempty"` // key is chainID
// chainID,customKey => customValue
Customs map[string]map[string]string `toml:",omitempty" json:",omitempty"`
LocalChainConfig map[string]*LocalChainConfig `toml:",omitempty" json:",omitempty"` // key is chain ID
SpecialFlags map[string]string `toml:",omitempty" json:",omitempty"`
AttestationServer string `toml:",omitempty" json:",omitempty"`
}
// LocalChainConfig local chain config
type LocalChainConfig struct {
ForbidParallelLoading bool `toml:",omitempty" json:",omitempty"`
EstimatedGasMustBePositive bool `toml:",omitempty" json:",omitempty"`
IsReswapSupported bool `toml:",omitempty" json:",omitempty"`
DontCheckAddressMixedCase bool `toml:",omitempty" json:",omitempty"`
SmallestGasPriceUnit uint64 `toml:",omitempty" json:",omitempty"`
ForbidSwapoutTokenIDs []string `toml:",omitempty" json:",omitempty"`
BigValueDiscount uint64 `toml:",omitempty" json:",omitempty"`
// chainID -> tokenids
ChargeFeeOnDestChain map[string][]string `toml:",omitempty" json:",omitempty"`
FeeReceiverOnDestChain string `toml:",omitempty" json:",omitempty"`
forbidSwapoutTokenIDMap map[string]struct{}
lock *sync.Mutex
}
// OnchainConfig struct
type OnchainConfig struct {
Contract string
APIAddress []string
WSServers []string
ReloadCycle uint64 // seconds
IgnoreCheck bool
}
// MPCConfig mpc related config
type MPCConfig struct {
SignTypeEC256K1 string `toml:",omitempty" json:",omitempty"`
APIPrefix string
RPCTimeout uint64 `toml:",omitempty" json:",omitempty"`
SignTimeout uint64 `toml:",omitempty" json:",omitempty"`
MaxSignGroupFailures int `toml:",omitempty" json:",omitempty"`
MinIntervalToAddSignGroup int64 `toml:",omitempty" json:",omitempty"`
VerifySignatureInAccept bool `toml:",omitempty" json:",omitempty"`
GetAcceptListLoopInterval uint64 `toml:",omitempty" json:",omitempty"`
GetAcceptListRetryInterval uint64 `toml:",omitempty" json:",omitempty"`
MaxAcceptSignTimeInterval int64 `toml:",omitempty" json:",omitempty"`
PendingInvalidAccept bool `toml:",omitempty" json:",omitempty"`
GroupID *string
NeededOracles *uint32
TotalOracles *uint32
Mode uint32 // 0:managed 1:private (default 0)
Initiators []string
DefaultNode *MPCNodeConfig
OtherNodes []*MPCNodeConfig `toml:",omitempty" json:",omitempty"`
SignWithPrivateKey bool // use private key instead (use for testing)
SignerPrivateKeys map[string]string `json:"-"` // key is chain ID (use for testing)
}
// MPCNodeConfig mpc node config
type MPCNodeConfig struct {
RPCAddress *string
SignGroups []string `toml:",omitempty" json:",omitempty"`
KeystoreFile *string `json:"-"`
PasswordFile *string `json:"-"`
}
// APIServerConfig api service config
type APIServerConfig struct {
Port int
AllowedOrigins []string
MaxRequestsLimit int
}
// MongoDBConfig mongodb config
type MongoDBConfig struct {
DBURL string `toml:",omitempty" json:",omitempty"`
DBURLs []string `toml:",omitempty" json:",omitempty"`
DBName string
UserName string `json:"-"`
Password string `json:"-"`
}
// DynamicFeeTxConfig dynamic fee tx config
type DynamicFeeTxConfig struct {
PlusGasTipCapPercent uint64
PlusGasFeeCapPercent uint64
BlockCountFeeHistory int
MaxGasTipCap string
MaxGasFeeCap string
// cached values
maxGasTipCap *big.Int
maxGasFeeCap *big.Int
}
// GetMaxGasTipCap get max gas tip cap
func (c *DynamicFeeTxConfig) GetMaxGasTipCap() *big.Int {
return c.maxGasTipCap
}
// GetMaxGasFeeCap get max fee gas cap
func (c *DynamicFeeTxConfig) GetMaxGasFeeCap() *big.Int {
return c.maxGasFeeCap
}
// GetIdentifier get identifier (to distiguish in mpc accept)
func GetIdentifier() string {
return GetRouterConfig().Identifier
}
// GetSwapType get router swap type
func GetSwapType() string {
return GetRouterConfig().SwapType
}
// GetSwapSubType get router swap sub type
func GetSwapSubType() string {
return GetRouterConfig().SwapSubType
}
// IsSwapWithPermitEnabled is swap with permit enabled
func IsSwapWithPermitEnabled() bool {
return GetExtraConfig() != nil && GetExtraConfig().EnableSwapWithPermit
}
// IsForceAnySwapInAuto is forcely call anySwapinAuto
func IsForceAnySwapInAuto() bool {
return GetExtraConfig() != nil && GetExtraConfig().ForceAnySwapInAuto
}
// IsParallelSwapEnabled is parallel swap enabled
func IsParallelSwapEnabled() bool {
return GetExtraConfig() != nil && GetExtraConfig().EnableParallelSwap
}
// IsFixedGasPrice is fixed gas price of specified chain
func IsFixedGasPrice(chainID string) bool {
_, exist := fixedGasPriceMap[chainID]
return exist
}
// GetFixedGasPrice get fixed gas price of specified chain
func GetFixedGasPrice(chainID string) *big.Int {
if fixedGasPrice, ok := fixedGasPriceMap[chainID]; ok {
return new(big.Int).Set(fixedGasPrice)
}
return nil
}
// GetMaxGasPrice get max gas price of specified chain
func GetMaxGasPrice(chainID string) *big.Int {
if maxGasPrice, ok := maxGasPriceMap[chainID]; ok {
return new(big.Int).Set(maxGasPrice)
}
return nil
}
// GetMaxGasLimit get max gas limit of specified chain
func GetMaxGasLimit(chainID string) uint64 {
serverCfg := GetRouterServerConfig()
if serverCfg == nil {
return 0
}
return serverCfg.MaxGasLimit[chainID]
}
// GetMaxTokenGasLimit get max token gas limit of specified tokenID and chainID
func GetMaxTokenGasLimit(tokenID, chainID string) uint64 {
serverCfg := GetRouterServerConfig()
if serverCfg == nil {
return 0
}
return serverCfg.MaxTokenGasLimit[tokenID][chainID]
}
// GetNoncePassedConfirmInterval get nonce passed confirm interval
func GetNoncePassedConfirmInterval(chainID string) int64 {
serverCfg := GetRouterServerConfig()
if serverCfg == nil {
return 0
}
if interval, exist := serverCfg.NoncePassedConfirmInterval[chainID]; exist {
return interval
}
return 0
}
// GetCalcGasPriceMethod get calc gas price method eg. median (default), first, max, etc.
func GetCalcGasPriceMethod(chainID string) string {
serverCfg := GetRouterServerConfig()
if serverCfg != nil {
if method, exist := serverCfg.CalcGasPriceMethod[chainID]; exist {
return method
}
}
return "median" // default value
}
// GetMinReserveFee get min reserve fee
func GetMinReserveFee(chainID string) *big.Int {
if GetExtraConfig() == nil {
return nil
}
if minReserve, exist := GetExtraConfig().MinReserveFee[chainID]; exist {
return new(big.Int).SetUint64(minReserve)
}
return nil
}
// HasMinReserveBudgetConfig has min reserve budget config
func HasMinReserveBudgetConfig() bool {
return GetExtraConfig() != nil && len(GetExtraConfig().MinReserveBudget) > 0
}
// GetMinReserveBudget get min reserve budget
func GetMinReserveBudget(chainID string) *big.Int {
if GetExtraConfig() == nil {
return nil
}
if minReserve, exist := GetExtraConfig().MinReserveBudget[chainID]; exist {
return new(big.Int).SetUint64(minReserve)
}
return nil
}
// GetBaseFeePercent get base fee percent
func GetBaseFeePercent(chainID string) int64 {
extraCfg := GetExtraConfig()
if extraCfg == nil {
return 0
}
if baseFeePercent, exist := GetExtraConfig().BaseFeePercent[chainID]; exist {
return baseFeePercent
}
return 0
}
// GetRPCClientTimeout get rpc client timeout
func GetRPCClientTimeout(chainID string) int {
extraCfg := GetExtraConfig()
if extraCfg == nil {
return 0
}
return extraCfg.RPCClientTimeout[chainID]
}
// GetCustom get custom
func GetCustom(chainID, key string) string {
extraCfg := GetExtraConfig()
if extraCfg == nil {
return ""
}
mapping, exist := extraCfg.Customs[chainID]
if !exist || len(mapping) == 0 {
return ""
}
if value, exist := mapping[key]; exist {
return value
}
return ""
}
// GetSignerPrivateKey get signer private key (use for testing)
func (c *MPCConfig) GetSignerPrivateKey(chainID string) string {
if prikey, exist := c.SignerPrivateKeys[chainID]; exist {
return prikey
}
return ""
}
// SetSignerPrivateKey set signer private key (use for testing)
func (c *MPCConfig) SetSignerPrivateKey(chainID, prikey string) {
c.SignWithPrivateKey = true
if len(c.SignerPrivateKeys) == 0 {
c.SignerPrivateKeys = make(map[string]string)
}
c.SignerPrivateKeys[chainID] = prikey
}
// IsDebugMode is debug mode, add more debugging log infos
func IsDebugMode() bool {
if isDebugMode == nil {
flag := GetExtraConfig() != nil && GetExtraConfig().IsDebugMode
isDebugMode = &flag
}
return *isDebugMode
}
// SetDebugMode set debug mode
func SetDebugMode(flag bool) {
if isDebugMode == nil {
isDebugMode = &flag
} else {
*isDebugMode = flag
}
}
// IsNFTSwapWithData is nft swap with data, add data in swapout log and swapin argument
func IsNFTSwapWithData() bool {
if isNFTSwapWithData == nil {
flag := GetExtraConfig() != nil && GetExtraConfig().IsNFTSwapWithData
isNFTSwapWithData = &flag
}
return *isNFTSwapWithData
}
// AllowCallByConstructor allow call by constructor
func AllowCallByConstructor() bool {
return GetExtraConfig() != nil && GetExtraConfig().AllowCallByConstructor
}
// AllowCallByContract allow call into router from contract
func AllowCallByContract() bool {
if isAllowCallByContract == nil {
flag := GetExtraConfig() != nil && GetExtraConfig().AllowCallByContract
isAllowCallByContract = &flag
}
return *isAllowCallByContract
}
// SetAllowCallByContract set allow call by contract flag (used in testing)
func SetAllowCallByContract(allow bool) {
if isAllowCallByContract == nil {
isAllowCallByContract = &allow
} else {
*isAllowCallByContract = allow
}
}
// CheckEIP1167Master whether check eip1167 master call by contract
func CheckEIP1167Master() bool {
if isCheckEIP1167Master == nil {
flag := GetExtraConfig() != nil && GetExtraConfig().CheckEIP1167Master
isCheckEIP1167Master = &flag
}
return *isCheckEIP1167Master
}
func initCallByContractWhitelist() {
if GetExtraConfig() == nil || len(GetExtraConfig().CallByContractWhitelist) == 0 {
return
}
allWhitelist := make(map[string]map[string]struct{})
for cid, whitelist := range GetExtraConfig().CallByContractWhitelist {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initCallByContractWhitelist wrong chainID", "chainID", cid, "err", err)
}
whitelistMap := make(map[string]struct{}, len(whitelist))
for _, address := range whitelist {
if !common.IsHexAddress(address) {
log.Fatal("initCallByContractWhitelist wrong address", "chainID", cid, "address", address)
}
whitelistMap[strings.ToLower(address)] = struct{}{}
}
allWhitelist[cid] = whitelistMap
}
callByContractWhitelist = allWhitelist
log.Info("initCallByContractWhitelist success", "isReload", IsReload)
}
// IsInCallByContractWhitelist is in call by contract whitelist
func IsInCallByContractWhitelist(chainID, caller string) bool {
whitelist, exist := callByContractWhitelist[chainID]
if !exist {
return false
}
_, exist = whitelist[strings.ToLower(caller)]
return exist
}
// AddOrRemoveCallByContractWhitelist add or remove call by contract whitelist
//
//nolint:dupl // allow duplicate
func AddOrRemoveCallByContractWhitelist(chainID string, callers []string, isAdd bool) {
whitelist, exist := callByContractWhitelist[chainID]
if !exist {
if !isAdd {
return
}
callByContractWhitelist[chainID] = make(map[string]struct{})
whitelist = callByContractWhitelist[chainID]
}
for _, caller := range callers {
key := strings.ToLower(caller)
if isAdd {
whitelist[key] = struct{}{}
} else {
delete(whitelist, key)
}
}
if GetExtraConfig() != nil {
chainWhitelist := make([]string, 0, len(whitelist))
for caller := range whitelist {
chainWhitelist = append(chainWhitelist, caller)
}
if GetExtraConfig().CallByContractWhitelist == nil {
GetExtraConfig().CallByContractWhitelist = make(map[string][]string)
}
GetExtraConfig().CallByContractWhitelist[chainID] = chainWhitelist
}
}
func initCallByContractCodeHashWhitelist() {
if GetExtraConfig() == nil || len(GetExtraConfig().CallByContractCodeHashWhitelist) == 0 {
return
}
allWhitelist := make(map[string]map[string]struct{})
for cid, whitelist := range GetExtraConfig().CallByContractCodeHashWhitelist {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initCallByContractCodeHashWhitelist wrong chainID", "chainID", cid, "err", err)
}
whitelistMap := make(map[string]struct{}, len(whitelist))
for _, codehash := range whitelist {
if !common.IsHexHash(codehash) {
log.Fatal("initCallByContractCodeHashWhitelist wrong code hash", "chainID", cid, "codehash", codehash)
}
whitelistMap[codehash] = struct{}{}
}
allWhitelist[cid] = whitelistMap
}
callByContractCodeHashWhitelist = allWhitelist
log.Info("initCallByContractCodeHashWhitelist success", "isReload", IsReload)
}
// HasCallByContractCodeHashWhitelist has call by contract code hash whitelist
func HasCallByContractCodeHashWhitelist(chainID string) bool {
whitelist, exist := callByContractCodeHashWhitelist[chainID]
return exist && len(whitelist) > 0
}
// IsInCallByContractCodeHashWhitelist is in call by contract code hash whitelist
func IsInCallByContractCodeHashWhitelist(chainID, codehash string) bool {
whitelist, exist := callByContractCodeHashWhitelist[chainID]
if !exist {
return false
}
_, exist = whitelist[codehash]
return exist
}
// AddOrRemoveCallByContractCodeHashWhitelist add or remove call by contract code hash whitelist
func AddOrRemoveCallByContractCodeHashWhitelist(chainID string, codehashes []string, isAdd bool) {
whitelist, exist := callByContractCodeHashWhitelist[chainID]
if !exist {
if !isAdd {
return
}
callByContractCodeHashWhitelist[chainID] = make(map[string]struct{})
whitelist = callByContractCodeHashWhitelist[chainID]
}
for _, codehash := range codehashes {
key := codehash
if isAdd {
whitelist[key] = struct{}{}
} else {
delete(whitelist, key)
}
}
if GetExtraConfig() != nil {
chainWhitelist := make([]string, 0, len(whitelist))
for codehash := range whitelist {
chainWhitelist = append(chainWhitelist, codehash)
}
if GetExtraConfig().CallByContractCodeHashWhitelist == nil {
GetExtraConfig().CallByContractCodeHashWhitelist = make(map[string][]string)
}
GetExtraConfig().CallByContractCodeHashWhitelist[chainID] = chainWhitelist
}
}
func initBigValueWhitelist() {
if GetExtraConfig() == nil || len(GetExtraConfig().BigValueWhitelist) == 0 {
return
}
allWhitelist := make(map[string]map[string]struct{})
for tid, whitelist := range GetExtraConfig().BigValueWhitelist {
whitelistMap := make(map[string]struct{}, len(whitelist))
for _, address := range whitelist {
if address == "" {
log.Fatal("initBigValueWhitelist empty address", "tokenID", tid)
}
whitelistMap[strings.ToLower(address)] = struct{}{}
}
allWhitelist[tid] = whitelistMap
}
bigValueWhitelist = allWhitelist
log.Info("initBigValueWhitelist success", "isReload", IsReload)
}
// IsInBigValueWhitelist is in call by contract whitelist
func IsInBigValueWhitelist(tokenID, caller string) bool {
whitelist, exist := bigValueWhitelist[tokenID]
if !exist {
return false
}
_, exist = whitelist[strings.ToLower(caller)]
return exist
}
// AddOrRemoveBigValueWhitelist add or remove big value whitelist
//
//nolint:dupl // allow duplicate
func AddOrRemoveBigValueWhitelist(tokenID string, callers []string, isAdd bool) {
whitelist, exist := bigValueWhitelist[tokenID]
if !exist {
if !isAdd {
return
}
bigValueWhitelist[tokenID] = make(map[string]struct{})
whitelist = bigValueWhitelist[tokenID]
}
for _, caller := range callers {
key := strings.ToLower(caller)
if isAdd {
whitelist[key] = struct{}{}
} else {
delete(whitelist, key)
}
}
if GetExtraConfig() != nil {
tokenWhitelist := make([]string, 0, len(whitelist))
for caller := range whitelist {
tokenWhitelist = append(tokenWhitelist, caller)
}
if GetExtraConfig().BigValueWhitelist == nil {
GetExtraConfig().BigValueWhitelist = make(map[string][]string)
}
GetExtraConfig().BigValueWhitelist[tokenID] = tokenWhitelist
}
}
// GetRouterConfig get router config
func GetRouterConfig() *RouterConfig {
return routerConfig
}
// GetRouterServerConfig get router server config
func GetRouterServerConfig() *RouterServerConfig {
return routerConfig.Server
}
// GetRouterOracleConfig get router oracle config
func GetRouterOracleConfig() *RouterOracleConfig {
return routerConfig.Oracle
}
// GetMPCConfig get mpc config
func GetMPCConfig(isFastMPC bool) *MPCConfig {
if isFastMPC {
return routerConfig.FastMPC
}
return routerConfig.MPC
}
// GetLocalChainConfig get local chain config
func GetLocalChainConfig(chainID string) *LocalChainConfig {
if GetExtraConfig() != nil {
c := GetExtraConfig().LocalChainConfig[chainID]
if c != nil {
if c.lock == nil {
c.lock = new(sync.Mutex)
}
return c
}
}
return &LocalChainConfig{}
}
// GetSpecialFlag get special flag
func GetSpecialFlag(key string) string {
if GetExtraConfig() != nil {
key = strings.ToLower(key)
return GetExtraConfig().SpecialFlags[key]
}
return ""
}
// GetOnchainContract get onchain config contract address
func GetOnchainContract() string {
return routerConfig.Onchain.Contract
}
// GetExtraConfig get extra config
func GetExtraConfig() *ExtraConfig {
return routerConfig.Extra
}
// SetExtraConfig set extra config (used by testing)
func SetExtraConfig(extra *ExtraConfig) error {
if err := extra.CheckConfig(); err != nil {
return err
}
routerConfig.Extra = extra
return nil
}
// HasRouterAdmin has admin
func HasRouterAdmin() bool {
return len(routerConfig.Server.Admins) != 0
}
// IsRouterAdmin is admin
func IsRouterAdmin(account string) bool {
for _, admin := range routerConfig.Server.Admins {
if strings.EqualFold(account, admin) {
return true
}
}
return false
}
// IsRouterAssistant is router assistants
func IsRouterAssistant(account string) bool {
for _, assistant := range routerConfig.Server.Assistants {
if strings.EqualFold(account, assistant) {
return true
}
}
return false
}
// IsChainIDInBlackList is chain id in black list
func IsChainIDInBlackList(chainID string) bool {
_, exist := chainIDBlacklistMap[chainID]
return exist
}
// AddOrRemoveChainIDBlackList add or remove chainID blacklist
func AddOrRemoveChainIDBlackList(chainIDs []string, isAdd bool) {
for _, chainID := range chainIDs {
if isAdd {
chainIDBlacklistMap[chainID] = struct{}{}
} else {
delete(chainIDBlacklistMap, chainID)
}
}
blacklist := make([]string, 0, len(chainIDBlacklistMap))
for chainID := range chainIDBlacklistMap {
blacklist = append(blacklist, chainID)
}
GetRouterConfig().ChainIDBlackList = blacklist
}
// IsTokenIDInBlackListOnChain is token id in black list on chain
func IsTokenIDInBlackListOnChain(chainID, tokenID string) bool {
m, exist := tokenIDBlacklistOnChainMap[chainID]
if !exist {
return false
}
_, exist = m[strings.ToLower(tokenID)]
return exist
}
// IsTokenIDInBlackList is token id in black list
func IsTokenIDInBlackList(tokenID string) bool {
_, exist := tokenIDBlacklistMap[strings.ToLower(tokenID)]
return exist
}
// AddOrRemoveTokenIDBlackList add or remove tokenID blacklist
func AddOrRemoveTokenIDBlackList(tokenIDs []string, isAdd bool) {
for _, tokenID := range tokenIDs {
key := strings.ToLower(tokenID)
if isAdd {
tokenIDBlacklistMap[key] = struct{}{}
} else {
delete(tokenIDBlacklistMap, key)
}
}
blacklist := make([]string, 0, len(tokenIDBlacklistMap))
for tokenID := range tokenIDBlacklistMap {
blacklist = append(blacklist, tokenID)
}
GetRouterConfig().TokenIDBlackList = blacklist
}
// IsAccountInBlackList is account in black list
func IsAccountInBlackList(account string) bool {
_, exist := accountBlacklistMap[strings.ToLower(account)]
return exist
}
// AddOrRemoveAccountBlackList add or remove account blacklist
func AddOrRemoveAccountBlackList(accounts []string, isAdd bool) {
for _, account := range accounts {
key := strings.ToLower(account)
if isAdd {
accountBlacklistMap[key] = struct{}{}
} else {
delete(accountBlacklistMap, key)
}
}
blacklist := make([]string, 0, len(accountBlacklistMap))
for account := range accountBlacklistMap {
blacklist = append(blacklist, account)
}
GetRouterConfig().AccountBlackList = blacklist
}
func initAutoSwapNonceEnabledChains() {
serverCfg := GetRouterServerConfig()
if serverCfg == nil || len(serverCfg.AutoSwapNonceEnabledChains) == 0 {
return
}
tempMap := make(map[string]struct{})
for _, cid := range serverCfg.AutoSwapNonceEnabledChains {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initAutoSwapNonceEnabledChains wrong chainID", "chainID", cid, "err", err)
}
tempMap[cid] = struct{}{}
}
autoSwapNonceEnabledChains = tempMap
log.Info("initAutoSwapNonceEnabledChains success", "chains", serverCfg.AutoSwapNonceEnabledChains, "isReload", IsReload)
}
// IsAutoSwapNonceEnabled is auto swap nonce enabled
func IsAutoSwapNonceEnabled(chainID string) bool {
_, exist := autoSwapNonceEnabledChains[chainID]
return exist
}
func initDynamicFeeTxEnabledChains() {
if GetExtraConfig() == nil || len(GetExtraConfig().DynamicFeeTxEnabledChains) == 0 {
return
}
tempMap := make(map[string]struct{})
for _, cid := range GetExtraConfig().DynamicFeeTxEnabledChains {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initDynamicFeeTxEnabledChains wrong chainID", "chainID", cid, "err", err)
}
tempMap[cid] = struct{}{}
}
dynamicFeeTxEnabledChains = tempMap
log.Info("initDynamicFeeTxEnabledChains success", "isReload", IsReload)
}
// IsDynamicFeeTxEnabled is dynamic fee tx enabled (EIP-1559)
func IsDynamicFeeTxEnabled(chainID string) bool {
_, exist := dynamicFeeTxEnabledChains[chainID]
return exist
}
func initEnableCheckTxBlockHashChains() {
if GetExtraConfig() == nil || len(GetExtraConfig().EnableCheckTxBlockHashChains) == 0 {
return
}
tempMap := make(map[string]struct{})
for _, cid := range GetExtraConfig().EnableCheckTxBlockHashChains {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initEnableCheckTxBlockHashChains wrong chainID", "chainID", cid, "err", err)
}
tempMap[cid] = struct{}{}
}
enableCheckTxBlockHashChains = tempMap
log.Info("initEnableCheckTxBlockHashChains success", "isReload", IsReload)
}
// IsCheckTxBlockHashEnabled check tx block hash
func IsCheckTxBlockHashEnabled(chainID string) bool {
_, exist := enableCheckTxBlockHashChains[chainID]
return exist
}
func initEnableCheckTxBlockIndexChains() {
if GetExtraConfig() == nil || len(GetExtraConfig().EnableCheckTxBlockIndexChains) == 0 {
return
}
tempMap := make(map[string]struct{})
for _, cid := range GetExtraConfig().EnableCheckTxBlockIndexChains {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initEnableCheckTxBlockIndexChains wrong chainID", "chainID", cid, "err", err)
}
tempMap[cid] = struct{}{}
}
enableCheckTxBlockIndexChains = tempMap
log.Info("initEnableCheckTxBlockIndexChains success", "isReload", IsReload)
}
// IsCheckTxBlockIndexEnabled check tx block and index
func IsCheckTxBlockIndexEnabled(chainID string) bool {
_, exist := enableCheckTxBlockIndexChains[chainID]
return exist
}
func initDisableUseFromChainIDInReceiptChains() {
if GetExtraConfig() == nil || len(GetExtraConfig().DisableUseFromChainIDInReceiptChains) == 0 {
return
}
tempMap := make(map[string]struct{})
for _, cid := range GetExtraConfig().DisableUseFromChainIDInReceiptChains {
if _, err := common.GetBigIntFromStr(cid); err != nil {
log.Fatal("initDisableUseFromChainIDInReceiptChains wrong chainID", "chainID", cid, "err", err)
}
tempMap[cid] = struct{}{}
}
disableUseFromChainIDInReceiptChains = tempMap
log.Info("initDisableUseFromChainIDInReceiptChains success", "isReload", IsReload)