forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
1906 lines (1669 loc) · 54.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 config
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/connchecker"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
)
// errExchangeConfigIsNil defines an error when the config is nil
var errExchangeConfigIsNil = errors.New("exchange config is nil")
// GetCurrencyConfig returns currency configurations
func (c *Config) GetCurrencyConfig() CurrencyConfig {
return c.Currency
}
// GetExchangeBankAccounts returns banking details associated with an exchange
// for depositing funds
func (c *Config) GetExchangeBankAccounts(exchangeName, id, depositingCurrency string) (*banking.Account, error) {
m.Lock()
defer m.Unlock()
for x := range c.Exchanges {
if strings.EqualFold(c.Exchanges[x].Name, exchangeName) {
for y := range c.Exchanges[x].BankAccounts {
if strings.EqualFold(c.Exchanges[x].BankAccounts[y].ID, id) {
if common.StringDataCompareInsensitive(
strings.Split(c.Exchanges[x].BankAccounts[y].SupportedCurrencies, ","),
depositingCurrency) {
return &c.Exchanges[x].BankAccounts[y], nil
}
}
}
}
}
return nil, fmt.Errorf("exchange %s bank details not found for %s",
exchangeName,
depositingCurrency)
}
// UpdateExchangeBankAccounts updates the configuration for the associated
// exchange bank
func (c *Config) UpdateExchangeBankAccounts(exchangeName string, bankCfg []banking.Account) error {
m.Lock()
defer m.Unlock()
for i := range c.Exchanges {
if strings.EqualFold(c.Exchanges[i].Name, exchangeName) {
c.Exchanges[i].BankAccounts = bankCfg
return nil
}
}
return fmt.Errorf("exchange %s not found",
exchangeName)
}
// GetClientBankAccounts returns banking details used for a given exchange
// and currency
func (c *Config) GetClientBankAccounts(exchangeName, targetCurrency string) (*banking.Account, error) {
m.Lock()
defer m.Unlock()
for x := range c.BankAccounts {
if (strings.Contains(c.BankAccounts[x].SupportedExchanges, exchangeName) ||
c.BankAccounts[x].SupportedExchanges == "ALL") &&
strings.Contains(c.BankAccounts[x].SupportedCurrencies, targetCurrency) {
return &c.BankAccounts[x], nil
}
}
return nil, fmt.Errorf("client banking details not found for %s and currency %s",
exchangeName,
targetCurrency)
}
// UpdateClientBankAccounts updates the configuration for a bank
func (c *Config) UpdateClientBankAccounts(bankCfg *banking.Account) error {
m.Lock()
defer m.Unlock()
for i := range c.BankAccounts {
if c.BankAccounts[i].BankName == bankCfg.BankName && c.BankAccounts[i].AccountNumber == bankCfg.AccountNumber {
c.BankAccounts[i] = *bankCfg
return nil
}
}
return fmt.Errorf("client banking details for %s not found, update not applied",
bankCfg.BankName)
}
// CheckClientBankAccounts checks client bank details
func (c *Config) CheckClientBankAccounts() {
m.Lock()
defer m.Unlock()
if len(c.BankAccounts) == 0 {
c.BankAccounts = append(c.BankAccounts,
banking.Account{
ID: "test-bank-01",
BankName: "Test Bank",
BankAddress: "42 Bank Street",
BankPostalCode: "13337",
BankPostalCity: "Satoshiville",
BankCountry: "Japan",
AccountName: "Satoshi Nakamoto",
AccountNumber: "0234",
SWIFTCode: "91272837",
IBAN: "98218738671897",
SupportedCurrencies: "USD",
SupportedExchanges: "Kraken,Bitstamp",
},
)
return
}
for i := range c.BankAccounts {
if c.BankAccounts[i].Enabled {
err := c.BankAccounts[i].Validate()
if err != nil {
c.BankAccounts[i].Enabled = false
log.Warn(log.ConfigMgr, err.Error())
}
}
}
}
// PurgeExchangeAPICredentials purges the stored API credentials
func (c *Config) PurgeExchangeAPICredentials() {
m.Lock()
defer m.Unlock()
for x := range c.Exchanges {
if !c.Exchanges[x].API.AuthenticatedSupport && !c.Exchanges[x].API.AuthenticatedWebsocketSupport {
continue
}
c.Exchanges[x].API.AuthenticatedSupport = false
c.Exchanges[x].API.AuthenticatedWebsocketSupport = false
if c.Exchanges[x].API.CredentialsValidator.RequiresKey {
c.Exchanges[x].API.Credentials.Key = DefaultAPIKey
}
if c.Exchanges[x].API.CredentialsValidator.RequiresSecret {
c.Exchanges[x].API.Credentials.Secret = DefaultAPISecret
}
if c.Exchanges[x].API.CredentialsValidator.RequiresClientID {
c.Exchanges[x].API.Credentials.ClientID = DefaultAPIClientID
}
c.Exchanges[x].API.Credentials.PEMKey = ""
c.Exchanges[x].API.Credentials.OTPSecret = ""
}
}
// GetCommunicationsConfig returns the communications configuration
func (c *Config) GetCommunicationsConfig() base.CommunicationsConfig {
m.Lock()
comms := c.Communications
m.Unlock()
return comms
}
// UpdateCommunicationsConfig sets a new updated version of a Communications
// configuration
func (c *Config) UpdateCommunicationsConfig(config *base.CommunicationsConfig) {
m.Lock()
c.Communications = *config
m.Unlock()
}
// GetCryptocurrencyProviderConfig returns the communications configuration
func (c *Config) GetCryptocurrencyProviderConfig() CryptocurrencyProvider {
m.Lock()
provider := c.Currency.CryptocurrencyProvider
m.Unlock()
return provider
}
// UpdateCryptocurrencyProviderConfig returns the communications configuration
func (c *Config) UpdateCryptocurrencyProviderConfig(config CryptocurrencyProvider) {
m.Lock()
c.Currency.CryptocurrencyProvider = config
m.Unlock()
}
// CheckCommunicationsConfig checks to see if the variables are set correctly
// from config.json
func (c *Config) CheckCommunicationsConfig() {
m.Lock()
defer m.Unlock()
// If the communications config hasn't been populated, populate
// with example settings
if c.Communications.SlackConfig.Name == "" {
c.Communications.SlackConfig = base.SlackConfig{
Name: "Slack",
TargetChannel: "general",
VerificationToken: "testtest",
}
}
if c.Communications.SMSGlobalConfig.Name == "" {
if c.SMS != nil {
if c.SMS.Contacts != nil {
c.Communications.SMSGlobalConfig = base.SMSGlobalConfig{
Name: "SMSGlobal",
Enabled: c.SMS.Enabled,
Verbose: c.SMS.Verbose,
Username: c.SMS.Username,
Password: c.SMS.Password,
Contacts: c.SMS.Contacts,
}
// flush old SMS config
c.SMS = nil
} else {
c.Communications.SMSGlobalConfig = base.SMSGlobalConfig{
Name: "SMSGlobal",
From: c.Name,
Username: "main",
Password: "test",
Contacts: []base.SMSContact{
{
Name: "bob",
Number: "1234",
Enabled: false,
},
},
}
}
} else {
c.Communications.SMSGlobalConfig = base.SMSGlobalConfig{
Name: "SMSGlobal",
Username: "main",
Password: "test",
Contacts: []base.SMSContact{
{
Name: "bob",
Number: "1234",
Enabled: false,
},
},
}
}
} else {
if c.Communications.SMSGlobalConfig.From == "" {
c.Communications.SMSGlobalConfig.From = c.Name
}
if len(c.Communications.SMSGlobalConfig.From) > 11 {
log.Warnf(log.ConfigMgr, "SMSGlobal config supplied from name exceeds 11 characters, trimming.\n")
c.Communications.SMSGlobalConfig.From = c.Communications.SMSGlobalConfig.From[:11]
}
if c.SMS != nil {
// flush old SMS config
c.SMS = nil
}
}
if c.Communications.SMTPConfig.Name == "" {
c.Communications.SMTPConfig = base.SMTPConfig{
Name: "SMTP",
Host: "smtp.google.com",
Port: "537",
AccountName: "some",
AccountPassword: "password",
RecipientList: "[email protected]",
}
}
if c.Communications.TelegramConfig.Name == "" {
c.Communications.TelegramConfig = base.TelegramConfig{
Name: "Telegram",
VerificationToken: "testest",
}
}
if c.Communications.SlackConfig.Name != "Slack" ||
c.Communications.SMSGlobalConfig.Name != "SMSGlobal" ||
c.Communications.SMTPConfig.Name != "SMTP" ||
c.Communications.TelegramConfig.Name != "Telegram" {
log.Warnln(log.ConfigMgr, "Communications config name/s not set correctly")
}
if c.Communications.SlackConfig.Enabled {
if c.Communications.SlackConfig.TargetChannel == "" ||
c.Communications.SlackConfig.VerificationToken == "" ||
c.Communications.SlackConfig.VerificationToken == "testtest" {
c.Communications.SlackConfig.Enabled = false
log.Warnln(log.ConfigMgr, "Slack enabled in config but variable data not set, disabling.")
}
}
if c.Communications.SMSGlobalConfig.Enabled {
if c.Communications.SMSGlobalConfig.Username == "" ||
c.Communications.SMSGlobalConfig.Password == "" ||
len(c.Communications.SMSGlobalConfig.Contacts) == 0 {
c.Communications.SMSGlobalConfig.Enabled = false
log.Warnln(log.ConfigMgr, "SMSGlobal enabled in config but variable data not set, disabling.")
}
}
if c.Communications.SMTPConfig.Enabled {
if c.Communications.SMTPConfig.Host == "" ||
c.Communications.SMTPConfig.Port == "" ||
c.Communications.SMTPConfig.AccountName == "" ||
c.Communications.SMTPConfig.AccountPassword == "" {
c.Communications.SMTPConfig.Enabled = false
log.Warnln(log.ConfigMgr, "SMTP enabled in config but variable data not set, disabling.")
}
}
if c.Communications.TelegramConfig.Enabled {
if c.Communications.TelegramConfig.VerificationToken == "" {
c.Communications.TelegramConfig.Enabled = false
log.Warnln(log.ConfigMgr, "Telegram enabled in config but variable data not set, disabling.")
}
}
}
// GetExchangeAssetTypes returns the exchanges supported asset types
func (c *Config) GetExchangeAssetTypes(exchName string) (asset.Items, error) {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return nil, err
}
if exchCfg.CurrencyPairs == nil {
return nil, fmt.Errorf("exchange %s currency pairs is nil", exchName)
}
return exchCfg.CurrencyPairs.GetAssetTypes(false), nil
}
// SupportsExchangeAssetType returns whether or not the exchange supports the supplied asset type
func (c *Config) SupportsExchangeAssetType(exchName string, assetType asset.Item) error {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return err
}
if exchCfg.CurrencyPairs == nil {
return fmt.Errorf("exchange %s currency pairs is nil", exchName)
}
if !assetType.IsValid() {
return fmt.Errorf("exchange %s invalid asset type %s",
exchName,
assetType)
}
if !exchCfg.CurrencyPairs.GetAssetTypes(false).Contains(assetType) {
return fmt.Errorf("exchange %s unsupported asset type %s",
exchName,
assetType)
}
return nil
}
// SetPairs sets the exchanges currency pairs
func (c *Config) SetPairs(exchName string, assetType asset.Item, enabled bool, pairs currency.Pairs) error {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return err
}
err = c.SupportsExchangeAssetType(exchName, assetType)
if err != nil {
return err
}
exchCfg.CurrencyPairs.StorePairs(assetType, pairs, enabled)
return nil
}
// GetCurrencyPairConfig returns currency pair config for the desired exchange and asset type
func (c *Config) GetCurrencyPairConfig(exchName string, assetType asset.Item) (*currency.PairStore, error) {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return nil, err
}
err = c.SupportsExchangeAssetType(exchName, assetType)
if err != nil {
return nil, err
}
return exchCfg.CurrencyPairs.Get(assetType)
}
// CheckPairConfigFormats checks to see if the pair config format is valid
func (c *Config) CheckPairConfigFormats(exchName string) error {
assetTypes, err := c.GetExchangeAssetTypes(exchName)
if err != nil {
return err
}
for x := range assetTypes {
assetType := assetTypes[x]
pairFmt, err := c.GetPairFormat(exchName, assetType)
if err != nil {
return err
}
// No err checking is required as the above checks the same
// conditions
pairs, _ := c.GetCurrencyPairConfig(exchName, assetType)
if len(pairs.Available) == 0 || len(pairs.Enabled) == 0 {
continue
}
checker := func(enabled bool) error {
pairsType := "enabled"
loadedPairs := pairs.Enabled
if !enabled {
pairsType = "available"
loadedPairs = pairs.Available
}
for y := range loadedPairs {
if pairFmt.Delimiter != "" && pairFmt.Index != "" {
return fmt.Errorf(
"exchange %s %s %s cannot have an index and delimiter set at the same time",
exchName, pairsType, assetType)
}
if pairFmt.Delimiter != "" {
if !strings.Contains(loadedPairs[y].String(), pairFmt.Delimiter) {
return fmt.Errorf(
"exchange %s %s %s pairs does not contain delimiter",
exchName, pairsType, assetType)
}
}
if pairFmt.Index != "" {
if !strings.Contains(loadedPairs[y].String(), pairFmt.Index) {
return fmt.Errorf("exchange %s %s %s pairs does not contain an index",
exchName, pairsType, assetType)
}
}
}
return nil
}
err = checker(true)
if err != nil {
return err
}
err = checker(false)
if err != nil {
return err
}
}
return nil
}
// CheckPairConsistency checks to see if the enabled pair exists in the
// available pairs list
func (c *Config) CheckPairConsistency(exchName string) error {
assetTypes, err := c.GetExchangeAssetTypes(exchName)
if err != nil {
return err
}
var atLeastOneEnabled bool
for x := range assetTypes {
enabledPairs, err := c.GetEnabledPairs(exchName, assetTypes[x])
if err == nil {
if len(enabledPairs) != 0 {
atLeastOneEnabled = true
continue
}
var enabled bool
enabled, err = c.AssetTypeEnabled(assetTypes[x], exchName)
if err != nil {
return err
}
if !enabled {
continue
}
var availPairs currency.Pairs
availPairs, err = c.GetAvailablePairs(exchName, assetTypes[x])
if err != nil {
return err
}
err = c.SetPairs(exchName,
assetTypes[x],
true,
currency.Pairs{availPairs.GetRandomPair()})
if err != nil {
return err
}
atLeastOneEnabled = true
continue
}
// On error an enabled pair is not found in the available pairs list
// so remove and report
availPairs, err := c.GetAvailablePairs(exchName, assetTypes[x])
if err != nil {
return err
}
var pairs, pairsRemoved currency.Pairs
for x := range enabledPairs {
if !availPairs.Contains(enabledPairs[x], true) {
pairsRemoved = append(pairsRemoved, enabledPairs[x])
continue
}
pairs = append(pairs, enabledPairs[x])
}
if len(pairsRemoved) == 0 {
return fmt.Errorf("check pair consistency fault for asset %s, conflict found but no pairs removed",
assetTypes[x])
}
// Flush corrupted/misspelled enabled pairs in config
err = c.SetPairs(exchName, assetTypes[x], true, pairs)
if err != nil {
return err
}
log.Warnf(log.ConfigMgr,
"Exchange %s: [%v] Removing enabled pair(s) %v from enabled pairs list, as it isn't located in the available pairs list.\n",
exchName,
assetTypes[x],
pairsRemoved.Strings())
if len(pairs) != 0 {
atLeastOneEnabled = true
continue
}
enabled, err := c.AssetTypeEnabled(assetTypes[x], exchName)
if err != nil {
return err
}
if !enabled {
continue
}
err = c.SetPairs(exchName,
assetTypes[x],
true,
currency.Pairs{availPairs.GetRandomPair()})
if err != nil {
return err
}
atLeastOneEnabled = true
}
// If no pair is enabled across the entire range of assets, then atleast
// enable one and turn on the asset type
if !atLeastOneEnabled {
avail, err := c.GetAvailablePairs(exchName, assetTypes[0])
if err != nil {
return err
}
newPair := avail.GetRandomPair()
err = c.SetPairs(exchName, assetTypes[0], true, currency.Pairs{newPair})
if err != nil {
return err
}
log.Warnf(log.ConfigMgr,
"Exchange %s: [%v] No enabled pairs found in available pairs list, randomly added %v pair.\n",
exchName,
assetTypes[0],
newPair)
}
return nil
}
// SupportsPair returns true or not whether the exchange supports the supplied
// pair
func (c *Config) SupportsPair(exchName string, p currency.Pair, assetType asset.Item) bool {
pairs, err := c.GetAvailablePairs(exchName, assetType)
if err != nil {
return false
}
return pairs.Contains(p, false)
}
// GetPairFormat returns the exchanges pair config storage format
func (c *Config) GetPairFormat(exchName string, assetType asset.Item) (currency.PairFormat, error) {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return currency.PairFormat{}, err
}
err = c.SupportsExchangeAssetType(exchName, assetType)
if err != nil {
return currency.PairFormat{}, err
}
if exchCfg.CurrencyPairs.UseGlobalFormat {
return *exchCfg.CurrencyPairs.ConfigFormat, nil
}
p, err := exchCfg.CurrencyPairs.Get(assetType)
if err != nil {
return currency.PairFormat{}, err
}
if p == nil {
return currency.PairFormat{},
fmt.Errorf("exchange %s pair store for asset type %s is nil",
exchName,
assetType)
}
if p.ConfigFormat == nil {
return currency.PairFormat{},
fmt.Errorf("exchange %s pair config format for asset type %s is nil",
exchName,
assetType)
}
return *p.ConfigFormat, nil
}
// GetAvailablePairs returns a list of currency pairs for a specifc exchange
func (c *Config) GetAvailablePairs(exchName string, assetType asset.Item) (currency.Pairs, error) {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return nil, err
}
pairFormat, err := c.GetPairFormat(exchName, assetType)
if err != nil {
return nil, err
}
pairs, err := exchCfg.CurrencyPairs.GetPairs(assetType, false)
if err != nil {
return nil, err
}
if pairs == nil {
return nil, nil
}
return pairs.Format(pairFormat.Delimiter, pairFormat.Index,
pairFormat.Uppercase), nil
}
// GetEnabledPairs returns a list of currency pairs for a specifc exchange
func (c *Config) GetEnabledPairs(exchName string, assetType asset.Item) (currency.Pairs, error) {
exchCfg, err := c.GetExchangeConfig(exchName)
if err != nil {
return nil, err
}
pairFormat, err := c.GetPairFormat(exchName, assetType)
if err != nil {
return nil, err
}
pairs, err := exchCfg.CurrencyPairs.GetPairs(assetType, true)
if err != nil {
return pairs, err
}
if pairs == nil {
return nil, nil
}
return pairs.Format(pairFormat.Delimiter,
pairFormat.Index,
pairFormat.Uppercase),
nil
}
// GetEnabledExchanges returns a list of enabled exchanges
func (c *Config) GetEnabledExchanges() []string {
var enabledExchs []string
for i := range c.Exchanges {
if c.Exchanges[i].Enabled {
enabledExchs = append(enabledExchs, c.Exchanges[i].Name)
}
}
return enabledExchs
}
// GetDisabledExchanges returns a list of disabled exchanges
func (c *Config) GetDisabledExchanges() []string {
var disabledExchs []string
for i := range c.Exchanges {
if !c.Exchanges[i].Enabled {
disabledExchs = append(disabledExchs, c.Exchanges[i].Name)
}
}
return disabledExchs
}
// CountEnabledExchanges returns the number of exchanges that are enabled.
func (c *Config) CountEnabledExchanges() int {
counter := 0
for i := range c.Exchanges {
if c.Exchanges[i].Enabled {
counter++
}
}
return counter
}
// GetCurrencyPairDisplayConfig retrieves the currency pair display preference
func (c *Config) GetCurrencyPairDisplayConfig() *CurrencyPairFormatConfig {
return c.Currency.CurrencyPairFormat
}
// GetAllExchangeConfigs returns all exchange configurations
func (c *Config) GetAllExchangeConfigs() []Exchange {
m.Lock()
configs := c.Exchanges
m.Unlock()
return configs
}
// GetExchangeConfig returns exchange configurations by its indivdual name
func (c *Config) GetExchangeConfig(name string) (*Exchange, error) {
m.Lock()
defer m.Unlock()
for i := range c.Exchanges {
if strings.EqualFold(c.Exchanges[i].Name, name) {
return &c.Exchanges[i], nil
}
}
return nil, fmt.Errorf("%s %w", name, ErrExchangeNotFound)
}
// GetForexProvider returns a forex provider configuration by its name
func (c *Config) GetForexProvider(name string) (currency.FXSettings, error) {
m.Lock()
defer m.Unlock()
for i := range c.Currency.ForexProviders {
if strings.EqualFold(c.Currency.ForexProviders[i].Name, name) {
return c.Currency.ForexProviders[i], nil
}
}
return currency.FXSettings{}, errors.New("provider not found")
}
// GetForexProviders returns a list of available forex providers
func (c *Config) GetForexProviders() []currency.FXSettings {
m.Lock()
fxProviders := c.Currency.ForexProviders
m.Unlock()
return fxProviders
}
// GetPrimaryForexProvider returns the primary forex provider
func (c *Config) GetPrimaryForexProvider() string {
m.Lock()
defer m.Unlock()
for i := range c.Currency.ForexProviders {
if c.Currency.ForexProviders[i].PrimaryProvider {
return c.Currency.ForexProviders[i].Name
}
}
return ""
}
// UpdateExchangeConfig updates exchange configurations
func (c *Config) UpdateExchangeConfig(e *Exchange) error {
m.Lock()
defer m.Unlock()
for i := range c.Exchanges {
if strings.EqualFold(c.Exchanges[i].Name, e.Name) {
c.Exchanges[i] = *e
return nil
}
}
return fmt.Errorf("%s %w", e.Name, ErrExchangeNotFound)
}
// CheckExchangeConfigValues returns configuation values for all enabled
// exchanges
func (c *Config) CheckExchangeConfigValues() error {
if len(c.Exchanges) == 0 {
return errors.New("no exchange configs found")
}
exchanges := 0
for i := range c.Exchanges {
if strings.EqualFold(c.Exchanges[i].Name, "GDAX") {
c.Exchanges[i].Name = "CoinbasePro"
}
// Check to see if the old API storage format is used
if c.Exchanges[i].APIKey != nil {
// It is, migrate settings to new format
c.Exchanges[i].API.AuthenticatedSupport = *c.Exchanges[i].AuthenticatedAPISupport
if c.Exchanges[i].AuthenticatedWebsocketAPISupport != nil {
c.Exchanges[i].API.AuthenticatedWebsocketSupport = *c.Exchanges[i].AuthenticatedWebsocketAPISupport
}
c.Exchanges[i].API.Credentials.Key = *c.Exchanges[i].APIKey
c.Exchanges[i].API.Credentials.Secret = *c.Exchanges[i].APISecret
if c.Exchanges[i].APIAuthPEMKey != nil {
c.Exchanges[i].API.Credentials.PEMKey = *c.Exchanges[i].APIAuthPEMKey
}
if c.Exchanges[i].APIAuthPEMKeySupport != nil {
c.Exchanges[i].API.PEMKeySupport = *c.Exchanges[i].APIAuthPEMKeySupport
}
if c.Exchanges[i].ClientID != nil {
c.Exchanges[i].API.Credentials.ClientID = *c.Exchanges[i].ClientID
}
// Flush settings
c.Exchanges[i].AuthenticatedAPISupport = nil
c.Exchanges[i].AuthenticatedWebsocketAPISupport = nil
c.Exchanges[i].APIKey = nil
c.Exchanges[i].APISecret = nil
c.Exchanges[i].ClientID = nil
c.Exchanges[i].APIAuthPEMKeySupport = nil
c.Exchanges[i].APIAuthPEMKey = nil
c.Exchanges[i].APIURL = nil
c.Exchanges[i].APIURLSecondary = nil
c.Exchanges[i].WebsocketURL = nil
}
if c.Exchanges[i].Features == nil {
c.Exchanges[i].Features = &FeaturesConfig{}
}
if c.Exchanges[i].SupportsAutoPairUpdates != nil {
c.Exchanges[i].Features.Supports.RESTCapabilities.AutoPairUpdates = *c.Exchanges[i].SupportsAutoPairUpdates
c.Exchanges[i].Features.Enabled.AutoPairUpdates = *c.Exchanges[i].SupportsAutoPairUpdates
c.Exchanges[i].SupportsAutoPairUpdates = nil
}
if c.Exchanges[i].Websocket != nil {
c.Exchanges[i].Features.Enabled.Websocket = *c.Exchanges[i].Websocket
c.Exchanges[i].Websocket = nil
}
// Check if see if the new currency pairs format is empty and flesh it out if so
if c.Exchanges[i].CurrencyPairs == nil {
c.Exchanges[i].CurrencyPairs = new(currency.PairsManager)
c.Exchanges[i].CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore)
if c.Exchanges[i].PairsLastUpdated != nil {
c.Exchanges[i].CurrencyPairs.LastUpdated = *c.Exchanges[i].PairsLastUpdated
}
c.Exchanges[i].CurrencyPairs.ConfigFormat = c.Exchanges[i].ConfigCurrencyPairFormat
c.Exchanges[i].CurrencyPairs.RequestFormat = c.Exchanges[i].RequestCurrencyPairFormat
var availPairs, enabledPairs currency.Pairs
if c.Exchanges[i].AvailablePairs != nil {
availPairs = *c.Exchanges[i].AvailablePairs
}
if c.Exchanges[i].EnabledPairs != nil {
enabledPairs = *c.Exchanges[i].EnabledPairs
}
c.Exchanges[i].CurrencyPairs.UseGlobalFormat = true
c.Exchanges[i].CurrencyPairs.Store(asset.Spot,
currency.PairStore{
AssetEnabled: convert.BoolPtr(true),
Available: availPairs,
Enabled: enabledPairs,
},
)
// flush old values
c.Exchanges[i].PairsLastUpdated = nil
c.Exchanges[i].ConfigCurrencyPairFormat = nil
c.Exchanges[i].RequestCurrencyPairFormat = nil
c.Exchanges[i].AssetTypes = nil
c.Exchanges[i].AvailablePairs = nil
c.Exchanges[i].EnabledPairs = nil
} else {
assets := c.Exchanges[i].CurrencyPairs.GetAssetTypes(false)
var atLeastOne bool
for index := range assets {
err := c.Exchanges[i].CurrencyPairs.IsAssetEnabled(assets[index])
if err != nil {
// Checks if we have an old config without the ability to
// enable disable the entire asset
if err.Error() == "cannot ascertain if asset is enabled, variable is nil" {
log.Warnf(log.ConfigMgr,
"Exchange %s: upgrading config for asset type %s and setting enabled.\n",
c.Exchanges[i].Name,
assets[index])
err = c.Exchanges[i].CurrencyPairs.SetAssetEnabled(assets[index], true)
if err != nil {
return err
}
atLeastOne = true
}
continue
}
atLeastOne = true
}
if !atLeastOne {
if len(assets) == 0 {
c.Exchanges[i].Enabled = false
log.Warnf(log.ConfigMgr,
"%s no assets found, disabling...",
c.Exchanges[i].Name)
continue
}
// turn on an asset if all disabled
log.Warnf(log.ConfigMgr,
"%s assets disabled, turning on asset %s",
c.Exchanges[i].Name,
assets[0])
err := c.Exchanges[i].CurrencyPairs.SetAssetEnabled(assets[0], true)
if err != nil {
return err
}
}
}
if c.Exchanges[i].Enabled {
if c.Exchanges[i].Name == "" {
log.Errorf(log.ConfigMgr, ErrExchangeNameEmpty, i)
c.Exchanges[i].Enabled = false
continue
}
if (c.Exchanges[i].API.AuthenticatedSupport || c.Exchanges[i].API.AuthenticatedWebsocketSupport) &&
c.Exchanges[i].API.CredentialsValidator != nil {
var failed bool
if c.Exchanges[i].API.CredentialsValidator.RequiresKey &&
(c.Exchanges[i].API.Credentials.Key == "" || c.Exchanges[i].API.Credentials.Key == DefaultAPIKey) {
failed = true
}
if c.Exchanges[i].API.CredentialsValidator.RequiresSecret &&
(c.Exchanges[i].API.Credentials.Secret == "" || c.Exchanges[i].API.Credentials.Secret == DefaultAPISecret) {
failed = true
}
if c.Exchanges[i].API.CredentialsValidator.RequiresClientID &&
(c.Exchanges[i].API.Credentials.ClientID == DefaultAPIClientID || c.Exchanges[i].API.Credentials.ClientID == "") {
failed = true
}
if failed {
c.Exchanges[i].API.AuthenticatedSupport = false
c.Exchanges[i].API.AuthenticatedWebsocketSupport = false
log.Warnf(log.ConfigMgr, WarningExchangeAuthAPIDefaultOrEmptyValues, c.Exchanges[i].Name)
}
}
if !c.Exchanges[i].Features.Supports.RESTCapabilities.AutoPairUpdates &&
!c.Exchanges[i].Features.Supports.WebsocketCapabilities.AutoPairUpdates {
lastUpdated := convert.UnixTimestampToTime(c.Exchanges[i].CurrencyPairs.LastUpdated)
lastUpdated = lastUpdated.AddDate(0, 0, pairsLastUpdatedWarningThreshold)
if lastUpdated.Unix() <= time.Now().Unix() {
log.Warnf(log.ConfigMgr,
WarningPairsLastUpdatedThresholdExceeded,
c.Exchanges[i].Name,
pairsLastUpdatedWarningThreshold)
}
}
if c.Exchanges[i].HTTPTimeout <= 0 {
log.Warnf(log.ConfigMgr,
"Exchange %s HTTP Timeout value not set, defaulting to %v.\n",
c.Exchanges[i].Name,
defaultHTTPTimeout)
c.Exchanges[i].HTTPTimeout = defaultHTTPTimeout
}
if c.Exchanges[i].WebsocketResponseCheckTimeout <= 0 {
log.Warnf(log.ConfigMgr,