-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.go
2184 lines (1945 loc) · 59.7 KB
/
cli.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 cli
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
rippledata "github.com/rubblelabs/ripple/data"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
"github.com/CoreumFoundation/coreum/v4/pkg/config"
"github.com/CoreumFoundation/coreum/v4/pkg/config/constant"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/buildinfo"
bridgeclient "github.com/CoreumFoundation/coreumbridge-xrpl/relayer/client"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/cmd/cli/cosmos/keys"
overridekeyring "github.com/CoreumFoundation/coreumbridge-xrpl/relayer/cmd/cli/cosmos/override/crypto/keyring"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/coreum"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/logger"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/runner"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/xrpl"
)
//go:generate mockgen -destination=cli_mocks_test.go -package=cli_test . BridgeClient,Runner
func init() {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
DefaultHomeDir = filepath.Join(userHomeDir, ".coreumbridge-xrpl-relayer")
}
// DefaultHomeDir is default home for the relayer.
var DefaultHomeDir string
const (
sampleAmount = "100ucore"
)
const (
// FlagAmount is the amount flag.
FlagAmount = "amount"
// FlagHome is home flag.
FlagHome = "home"
// FlagKeyName is key name flag.
FlagKeyName = "key-name"
// FlagCoreumKeyName is coreum key name flag.
FlagCoreumKeyName = "coreum-key-name"
// FlagXRPLKeyName is XRPL key name flag.
FlagXRPLKeyName = "xrpl-key-name"
// FlagCoreumChainID is chain-id flag.
FlagCoreumChainID = "coreum-chain-id"
// FlagCoreumGRPCURL is Coreum GRPC URL flag.
FlagCoreumGRPCURL = "coreum-grpc-url"
// FlagXRPLRPCURL is XRPL RPC URL flag.
FlagXRPLRPCURL = "xrpl-rpc-url"
// FlagInitOnly is init only flag.
FlagInitOnly = "init-only"
// FlagRelayersCount is relayers count flag.
FlagRelayersCount = "relayers-count"
// FlagTokenState is token state flag.
FlagTokenState = "token-state"
// FlagSendingPrecision is sending precision flag.
FlagSendingPrecision = "sending-precision"
// FlagBridgingFee is bridging fee flag.
FlagBridgingFee = "bridging-fee"
// FlagRefundID is id of a pending refund.
FlagRefundID = "refund-id"
// FlagMaxHoldingAmount is max holding amount flag.
FlagMaxHoldingAmount = "max-holding-amount"
// FlagDeliverAmount is deliver amount flag.
FlagDeliverAmount = "deliver-amount"
// FlagTicketsToAllocate is tickets to allocate flag.
FlagTicketsToAllocate = "tickets-to-allocate"
// FlagMetricsEnabled enables metrics server.
FlagMetricsEnabled = "metrics-enabled"
// FlagsMetricsListenAddr sets listen address for metrics server.
FlagsMetricsListenAddr = "metrics-listen-addr"
)
// BridgeClient is bridge client used to interact with the chains and contract.
//
//nolint:interfacebloat
type BridgeClient interface {
Bootstrap(ctx context.Context,
senderAddress sdk.AccAddress,
bridgeAccountKeyName string,
cfg bridgeclient.BootstrappingConfig,
) (sdk.AccAddress, error)
GetContractConfig(ctx context.Context) (coreum.ContractConfig, error)
RecoverTickets(
ctx context.Context,
ownerAddress sdk.AccAddress,
ticketsToAllocate *uint32,
) error
RegisterCoreumToken(
ctx context.Context,
ownerAddress sdk.AccAddress,
denom string,
decimals uint32,
sendingPrecision int32,
maxHoldingAmount sdkmath.Int,
bridgingFee sdkmath.Int,
) (coreum.CoreumToken, error)
RegisterXRPLToken(
ctx context.Context,
ownerAddress sdk.AccAddress,
issuer rippledata.Account, currency rippledata.Currency,
sendingPrecision int32,
maxHoldingAmount sdkmath.Int,
bridgingFee sdkmath.Int,
) (coreum.XRPLToken, error)
GetAllTokens(ctx context.Context) ([]coreum.CoreumToken, []coreum.XRPLToken, error)
SendFromCoreumToXRPL(
ctx context.Context,
sender sdk.AccAddress,
recipient rippledata.Account,
amount sdk.Coin,
deliverAmount *sdkmath.Int,
) error
SendFromXRPLToCoreum(
ctx context.Context,
senderKeyName string,
amount rippledata.Amount,
recipient sdk.AccAddress,
) error
SetXRPLTrustSet(
ctx context.Context,
senderKeyName string,
limitAmount rippledata.Amount,
) error
UpdateCoreumToken(
ctx context.Context,
sender sdk.AccAddress,
denom string,
state *coreum.TokenState,
sendingPrecision *int32,
maxHoldingAmount *sdkmath.Int,
bridgingFee *sdkmath.Int,
) error
UpdateXRPLToken(
ctx context.Context,
sender sdk.AccAddress,
issuer, currency string,
state *coreum.TokenState,
sendingPrecision *int32,
maxHoldingAmount *sdkmath.Int,
bridgingFee *sdkmath.Int,
) error
RotateKeys(
ctx context.Context,
sender sdk.AccAddress,
cfg bridgeclient.KeysRotationConfig,
) error
UpdateXRPLBaseFee(
ctx context.Context,
sender sdk.AccAddress,
xrplBaseFee uint32,
) error
GetCoreumBalances(ctx context.Context, address sdk.AccAddress) (sdk.Coins, error)
GetXRPLBalances(ctx context.Context, acc rippledata.Account) ([]rippledata.Amount, error)
GetPendingRefunds(ctx context.Context, address sdk.AccAddress) ([]coreum.PendingRefund, error)
ClaimRefund(ctx context.Context, address sdk.AccAddress, pendingRefundID string) error
GetFeesCollected(ctx context.Context, address sdk.Address) (sdk.Coins, error)
ClaimRelayerFees(
ctx context.Context,
sender sdk.AccAddress,
amounts sdk.Coins,
) error
RecoverXRPLTokenRegistration(
ctx context.Context,
sender sdk.AccAddress,
issuer, currency string,
) error
HaltBridge(
ctx context.Context,
sender sdk.AccAddress,
) error
ResumeBridge(
ctx context.Context,
sender sdk.AccAddress,
) error
CancelPendingOperation(
ctx context.Context,
sender sdk.AccAddress,
operationID uint32,
) error
GetPendingOperations(ctx context.Context) ([]coreum.Operation, error)
DeployContract(
ctx context.Context,
sender sdk.AccAddress,
contractByteCodePath string,
) (*sdk.TxResponse, uint64, error)
MigrateContract(
ctx context.Context,
sender sdk.AccAddress,
codeID uint64,
) error
}
// BridgeClientProvider is function which returns the BridgeClient from the input cmd.
type BridgeClientProvider func(cmd *cobra.Command) (BridgeClient, error)
// Runner is a runner interface.
type Runner interface {
Start(ctx context.Context) error
}
// RunnerProvider is function which returns the Runner from the input cmd.
type RunnerProvider func(cmd *cobra.Command) (Runner, error)
// NewRunnerFromHome returns runner from home.
func NewRunnerFromHome(cmd *cobra.Command) (*runner.Runner, error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return nil, errors.Wrap(err, "failed to get client context")
}
xrplClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), xrpl.KeyringSuffix)
if err != nil {
return nil, errors.Wrap(err, "failed to configure xrpl keyring")
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return nil, errors.Wrap(err, "failed to configure coreum keyring")
}
cfg, err := GetHomeRunnerConfig(cmd)
if err != nil {
return nil, err
}
zapLogger, err := logger.NewZapLogger(logger.ZapLoggerConfig(cfg.LoggingConfig))
if err != nil {
return nil, err
}
components, err := runner.NewComponents(cfg, xrplClientCtx.Keyring, coreumClientCtx.Keyring, zapLogger, true, true)
if err != nil {
return nil, err
}
rnr, err := runner.NewRunner(cmd.Context(), components, cfg)
if err != nil {
return nil, err
}
return rnr, nil
}
// InitCmd returns the init cmd.
func InitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initializes the relayer home with the default config.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
home, err := getRelayerHome(cmd)
if err != nil {
return err
}
log, err := GetCLILogger()
if err != nil {
return err
}
log.Info(ctx, "Generating settings", zap.String("home", home))
chainID, err := cmd.Flags().GetString(FlagCoreumChainID)
if err != nil {
return errors.Wrapf(err, "failed to read %s", FlagCoreumChainID)
}
coreumGRPCURL, err := cmd.Flags().GetString(FlagCoreumGRPCURL)
if err != nil {
return errors.Wrapf(err, "failed to read %s", FlagCoreumGRPCURL)
}
xrplRPCURL, err := cmd.Flags().GetString(FlagXRPLRPCURL)
if err != nil {
return errors.Wrapf(err, "failed to read %s", FlagXRPLRPCURL)
}
metricsEnabled, err := cmd.Flags().GetBool(FlagMetricsEnabled)
if err != nil {
return errors.Wrapf(err, "failed to read %s", FlagMetricsEnabled)
}
metricsListenAddr, err := cmd.Flags().GetString(FlagsMetricsListenAddr)
if err != nil {
return errors.Wrapf(err, "failed to read %s", FlagsMetricsListenAddr)
}
cfg := runner.DefaultConfig()
cfg.Coreum.Network.ChainID = chainID
cfg.Coreum.GRPC.URL = coreumGRPCURL
cfg.XRPL.RPC.URL = xrplRPCURL
cfg.Metrics.Enabled = metricsEnabled
cfg.Metrics.Server.ListenAddress = metricsListenAddr
if err = runner.InitConfig(home, cfg); err != nil {
return err
}
log.Info(ctx, "Settings are generated successfully")
return nil
},
}
addCoreumChainIDFlag(cmd)
cmd.PersistentFlags().String(FlagXRPLRPCURL, "", "XRPL RPC address.")
cmd.PersistentFlags().String(FlagCoreumGRPCURL, "", "Coreum GRPC address.")
cmd.PersistentFlags().Bool(FlagMetricsEnabled, false, "Start metric server in relayer.")
cmd.PersistentFlags().String(FlagsMetricsListenAddr, "localhost:9090", "Address metrics server listens on.")
addHomeFlag(cmd)
return cmd
}
// StartCmd returns the start cmd.
func StartCmd(pp RunnerProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start relayer.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// scan helps to wait for any input infinitely and just then call the relayer. That handles
// the relayer restart in the container. Because after the restart the container is detached, relayer
// requests the keyring password and fail immediately.
log, err := GetCLILogger()
if err != nil {
return err
}
log.Info(ctx, "Press any key to start the relayer.")
input := bufio.NewScanner(os.Stdin)
input.Scan()
runner, err := pp(cmd)
if err != nil {
return err
}
return runner.Start(ctx)
},
}
addHomeFlag(cmd)
addKeyringFlags(cmd)
return cmd
}
// WithKeyring adds suffix-specific keyring to the context.
func WithKeyring(clientCtx client.Context, flagSet *pflag.FlagSet, suffix string) (client.Context, error) {
if flagSet.Lookup(flags.FlagKeyringDir) == nil || flagSet.Lookup(flags.FlagKeyringBackend) == nil {
return clientCtx, nil
}
keyringDir, err := flagSet.GetString(flags.FlagKeyringDir)
if err != nil {
return client.Context{}, errors.WithStack(err)
}
if keyringDir == "" {
keyringDir = filepath.Join(clientCtx.HomeDir, "keyring")
}
keyringDir += "-" + suffix
clientCtx = clientCtx.WithKeyringDir(keyringDir)
keyringBackend, err := flagSet.GetString(flags.FlagKeyringBackend)
if err != nil {
return client.Context{}, errors.WithStack(err)
}
kr, err := client.NewKeyringFromBackend(clientCtx, keyringBackend)
if err != nil {
return client.Context{}, errors.WithStack(err)
}
return clientCtx.WithKeyring(kr), nil
}
// KeyringCmd returns cosmos keyring cmd inti with the correct keys home.
// Based on provided suffix and coinType it uses keyring dedicated to xrpl or coreum.
func KeyringCmd(
suffix string,
coinType uint32,
addressFormatter overridekeyring.AddressFormatter,
) (*cobra.Command, error) {
// We need to set CoinType before initializing keys commands because keys.Commands() sets default
// flag value from sdk config. See github.com/cosmos/cosmos-sdk@v0.47.5/client/keys/add.go:78
sdk.GetConfig().SetCoinType(coinType)
// we set it for the keyring manually since it doesn't use the runner which does it for other CLI commands
cmd := keys.Commands(DefaultHomeDir)
for _, childCmd := range cmd.Commands() {
childCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
overridekeyring.SelectedAddressFormatter = addressFormatter
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.WithStack(err)
}
clientCtx, err = WithKeyring(clientCtx, cmd.Flags(), suffix)
if err != nil {
return err
}
if err := client.SetCmdClientContext(cmd, clientCtx); err != nil {
return errors.WithStack(err)
}
return setCoreumConfigFromHomeFlag(cmd)
}
}
cmd.Use += "-" + suffix
return cmd, nil
}
// RelayerKeyInfoCmd prints the relayer keys info.
func RelayerKeyInfoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "relayer-keys-info",
Short: "Prints the coreum and XRPL relayer keys info.",
RunE: func(cmd *cobra.Command, args []string) error {
if err := setCoreumConfigFromHomeFlag(cmd); err != nil {
return err
}
ctx := cmd.Context()
log, err := GetCLILogger()
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
// XRPL
cfg, err := GetHomeRunnerConfig(cmd)
if err != nil {
return err
}
xrplClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), xrpl.KeyringSuffix)
if err != nil {
return err
}
xrplKeyringTxSigner := xrpl.NewKeyringTxSigner(xrplClientCtx.Keyring)
xrplAddress, err := xrplKeyringTxSigner.Account(cfg.XRPL.MultiSignerKeyName)
if err != nil {
return err
}
xrplPubKey, err := xrplKeyringTxSigner.PubKey(cfg.XRPL.MultiSignerKeyName)
if err != nil {
return err
}
// Coreum
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
coreumKeyRecord, err := coreumClientCtx.Keyring.Key(cfg.Coreum.RelayerKeyName)
if err != nil {
return errors.Wrapf(err, "failed to get coreum key, keyName:%s", cfg.Coreum.RelayerKeyName)
}
coreumAddress, err := coreumKeyRecord.GetAddress()
if err != nil {
return errors.Wrapf(err, "failed to get coreum address from key, keyName:%s", cfg.Coreum.RelayerKeyName)
}
log.Info(
ctx,
"Keys info",
zap.String("coreumAddress", coreumAddress.String()),
zap.String("xrplAddress", xrplAddress.String()),
zap.String("xrplPubKey", xrplPubKey.String()),
)
return nil
},
}
addKeyringFlags(cmd)
addKeyNameFlag(cmd)
addHomeFlag(cmd)
return cmd
}
// BootstrapBridgeCmd safely creates XRPL bridge account with all required settings and deploys the bridge contract.
func BootstrapBridgeCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "bootstrap-bridge [config-path]",
Args: cobra.ExactArgs(1),
Short: "Sets up the XRPL bridge account with all required settings and deploys the bridge contract.",
Long: strings.TrimSpace(fmt.Sprintf(
`Sets up the XRPL bridge account with all required settings and deploys the bridge contract.
Example:
$ bootstrap-bridge bootstrapping.yaml --%s bridge-account
`, FlagKeyName)),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
log, err := GetCLILogger()
if err != nil {
return err
}
xrplKeyName, err := cmd.Flags().GetString(FlagXRPLKeyName)
if err != nil {
return errors.Wrapf(err, "failed to get %s", FlagXRPLKeyName)
}
xrplClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), xrpl.KeyringSuffix)
if err != nil {
return err
}
xrplKeyringTxSigner := xrpl.NewKeyringTxSigner(xrplClientCtx.Keyring)
xrplBridgeAddress, err := xrplKeyringTxSigner.Account(xrplKeyName)
if err != nil {
return err
}
log.Info(ctx, "XRPL bridge address", zap.String("address", xrplBridgeAddress.String()))
coreumKeyName, err := cmd.Flags().GetString(FlagCoreumKeyName)
if err != nil {
return errors.Wrapf(err, "failed to get %s", FlagCoreumKeyName)
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
coreumKRRecord, err := coreumClientCtx.Keyring.Key(coreumKeyName)
if err != nil {
return errors.Wrapf(err, "failed to get key by name:%s", coreumKeyName)
}
coreumAddress, err := coreumKRRecord.GetAddress()
if err != nil {
return errors.Wrapf(err, "failed to address for key name:%s", coreumKeyName)
}
log.Info(ctx, "Coreum deployer address", zap.String("address", coreumAddress.String()))
filePath := args[0]
initOnly, err := cmd.Flags().GetBool(FlagInitOnly)
if err != nil {
return errors.Wrapf(err, "failed to get %s", FlagInitOnly)
}
if initOnly {
log.Info(ctx, "Initializing default bootstrapping config", zap.String("path", filePath))
if err := bridgeclient.InitBootstrappingConfig(filePath); err != nil {
return err
}
relayersCount, err := cmd.Flags().GetInt(FlagRelayersCount)
if err != nil {
return errors.Wrapf(err, "failed to get %s", FlagRelayersCount)
}
if relayersCount > 0 {
minXrplBridgeBalance := bridgeclient.ComputeXRPLBrideAccountBalance()
log.Info(ctx, "Computed minimum XRPL bridge balance", zap.Float64("balance", minXrplBridgeBalance))
}
return nil
}
cfg, err := bridgeclient.ReadBootstrappingConfig(filePath)
if err != nil {
return err
}
log.Info(ctx, "Bootstrapping XRPL bridge", zap.Any("config", cfg))
log.Info(ctx, "Press any key to continue.")
input := bufio.NewScanner(os.Stdin)
input.Scan()
_, err = bridgeClient.Bootstrap(ctx, coreumAddress, xrplKeyName, cfg)
return err
},
}
addKeyringFlags(cmd)
addHomeFlag(cmd)
cmd.PersistentFlags().Bool(FlagInitOnly, false, "Init default config")
cmd.PersistentFlags().Int(FlagRelayersCount, 0, "Relayers count")
cmd.PersistentFlags().String(FlagCoreumKeyName, "", "Key name from the Coreum keyring")
cmd.PersistentFlags().String(FlagXRPLKeyName, "", "Key name from the XRPL keyring")
return cmd
}
// ContractConfigCmd prints contracts config.
func ContractConfigCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "contract-config",
Short: "Prints contract config.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
cfg, err := bridgeClient.GetContractConfig(ctx)
if err != nil {
return err
}
log, err := GetCLILogger()
if err != nil {
return err
}
log.Info(ctx, "Got contract config", zap.Any("config", cfg))
return nil
},
}
addHomeFlag(cmd)
return cmd
}
// RecoverTicketsCmd recovers 250 tickets in the bridge contract.
func RecoverTicketsCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "recover-tickets",
Short: "Recovers tickets in the bridge contract.",
Long: strings.TrimSpace(fmt.Sprintf(
`Recovers tickets in the bridge contract.
Example:
$ recover-tickets --%s 250 --%s owner
`, FlagTicketsToAllocate, FlagKeyName)),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
ticketsToAllocated, err := getFlagUint32IfPresent(cmd, FlagTicketsToAllocate)
if err != nil {
return errors.Wrapf(err, "failed to get %s", FlagTicketsToAllocate)
}
xrplClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
sender, err := readAddressFromKeyNameFlag(cmd, xrplClientCtx)
if err != nil {
return err
}
return bridgeClient.RecoverTickets(ctx, sender, ticketsToAllocated)
},
}
addKeyringFlags(cmd)
addKeyNameFlag(cmd)
addHomeFlag(cmd)
addGenerateOnlyFlag(cmd)
cmd.PersistentFlags().Uint32(
FlagTicketsToAllocate, 0, "tickets to allocate (if not provided the contract uses used tickets count)",
)
return cmd
}
// RegisterCoreumTokenCmd registers the Coreum originated token in the bridge contract.
func RegisterCoreumTokenCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "register-coreum-token [denom] [decimals] [sendingPrecision] [maxHoldingAmount] [bridgingFee]",
Short: "Registers Coreum token in the bridge contract.",
Long: strings.TrimSpace(
fmt.Sprintf(`Registers Coreum token in the bridge contract.
Example:
$ register-coreum-token ucore 6 2 500000000000000 4000 --%s owner
`, FlagKeyName)),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
sender, err := readAddressFromKeyNameFlag(cmd, coreumClientCtx)
if err != nil {
return err
}
denom := args[0]
decimals, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return errors.Wrapf(err, "invalid decimals: %s", args[1])
}
sendingPrecision, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return errors.Wrapf(err, "invalid sendingPrecision: %s", args[2])
}
maxHoldingAmount, ok := sdkmath.NewIntFromString(args[3])
if !ok {
return errors.Wrapf(err, "invalid maxHoldingAmount: %s", args[3])
}
bridgingFee, ok := sdkmath.NewIntFromString(args[4])
if !ok {
return errors.Wrapf(err, "invalid bridgingFee: %s", args[4])
}
_, err = bridgeClient.RegisterCoreumToken(
ctx,
sender,
denom,
uint32(decimals),
int32(sendingPrecision),
maxHoldingAmount,
bridgingFee,
)
return err
},
}
addKeyringFlags(cmd)
addKeyNameFlag(cmd)
addHomeFlag(cmd)
addGenerateOnlyFlag(cmd)
return cmd
}
// UpdateCoreumTokenCmd updates the Coreum originated token in the bridge contract.
func UpdateCoreumTokenCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "update-coreum-token [denom]",
Short: "Updates Coreum token in the bridge contract.",
Long: strings.TrimSpace(
fmt.Sprintf(`Updates Coreum token in the bridge contract.
Example:
$ update-coreum-token ucore --%s enabled --%s 2 --%s 10000000 --%s 4000 --%s owner
`, FlagTokenState, FlagSendingPrecision, FlagMaxHoldingAmount, FlagBridgingFee, FlagKeyName)),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
sender, err := readAddressFromKeyNameFlag(cmd, coreumClientCtx)
if err != nil {
return err
}
denom := args[0]
state, sendingPrecision, maxHoldingAmount, bridgingFee, err := readUpdateTokenFlags(cmd)
if err != nil {
return err
}
tokenState, err := convertStateStringTokenState(state)
if err != nil {
return err
}
return bridgeClient.UpdateCoreumToken(
ctx,
sender,
denom,
tokenState,
sendingPrecision,
maxHoldingAmount,
bridgingFee,
)
},
}
addUpdateTokenFlags(cmd)
addKeyringFlags(cmd)
addKeyNameFlag(cmd)
addHomeFlag(cmd)
addGenerateOnlyFlag(cmd)
return cmd
}
// RegisterXRPLTokenCmd registers the XRPL originated token in the bridge contract.
func RegisterXRPLTokenCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "register-xrpl-token [issuer] [currency] [sendingPrecision] [maxHoldingAmount] [bridgeFee]",
Short: "Registers XRPL token in the bridge contract.",
//nolint:lll // example
Long: strings.TrimSpace(
fmt.Sprintf(`Registers XRPL token in the bridge contract.
Example:
$ register-xrpl-token rcoreNywaoz2ZCQ8Lg2EbSLnGuRBmun6D 434F524500000000000000000000000000000000 2 500000000000000 4000 --%s owner
`, FlagKeyName)),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
sender, err := readAddressFromKeyNameFlag(cmd, coreumClientCtx)
if err != nil {
return err
}
issuer, err := rippledata.NewAccountFromAddress(args[0])
if err != nil {
return errors.Wrapf(err, "failed to convert issuer string to rippledata.Account: %s", args[0])
}
currency, err := rippledata.NewCurrency(args[1])
if err != nil {
return errors.Wrapf(err, "failed to convert currency string to rippledata.Currency: %s", args[1])
}
sendingPrecision, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return errors.Wrapf(err, "invalid sendingPrecision: %s", args[2])
}
maxHoldingAmount, ok := sdkmath.NewIntFromString(args[3])
if !ok {
return errors.Wrapf(err, "invalid maxHoldingAmount: %s", args[3])
}
bridgingFee, ok := sdkmath.NewIntFromString(args[4])
if !ok {
return errors.Wrapf(err, "invalid bridgeFee: %s", args[4])
}
_, err = bridgeClient.RegisterXRPLToken(
ctx,
sender,
*issuer,
currency,
int32(sendingPrecision),
maxHoldingAmount,
bridgingFee,
)
return err
},
}
addKeyringFlags(cmd)
addKeyNameFlag(cmd)
addHomeFlag(cmd)
addGenerateOnlyFlag(cmd)
return cmd
}
// RecoverXRPLTokenRegistrationCmd recovers xrpl token registration.
func RecoverXRPLTokenRegistrationCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "recover-xrpl-token-registration [issuer] [currency]",
Short: "Recovers XRPL token registration.",
Long: strings.TrimSpace(fmt.Sprintf(
`Recovers XRPL token registration.
Example:
$ recover-xrpl-token-registration [issuer] [currency] --%s owner
`, FlagKeyName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
sender, err := readAddressFromKeyNameFlag(cmd, coreumClientCtx)
if err != nil {
return err
}
issuer, err := rippledata.NewAccountFromAddress(args[0])
if err != nil {
return errors.Wrapf(err, "failed to convert issuer string to rippledata.Account: %s", args[0])
}
currency, err := rippledata.NewCurrency(args[1])
if err != nil {
return errors.Wrapf(err, "failed to convert currency string to rippledata.Currency: %s", args[1])
}
return bridgeClient.RecoverXRPLTokenRegistration(ctx, sender, issuer.String(), currency.String())
},
}
addKeyringFlags(cmd)
addKeyNameFlag(cmd)
addHomeFlag(cmd)
addGenerateOnlyFlag(cmd)
return cmd
}
// UpdateXRPLTokenCmd updates the XRPL originated token in the bridge contract.
func UpdateXRPLTokenCmd(bcp BridgeClientProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "update-xrpl-token [issuer] [currency]",
Short: "Updates XRPL token in the bridge contract.",
//nolint:lll // long example
Long: strings.TrimSpace(
fmt.Sprintf(`Updates XRPL token in the bridge contract.
Example:
$ update-xrpl-token rcoreNywaoz2ZCQ8Lg2EbSLnGuRBmun6D 434F524500000000000000000000000000000000 --%s enabled --%s 2 --%s 10000000 --%s 4000 --%s owner
`, FlagTokenState, FlagSendingPrecision, FlagMaxHoldingAmount, FlagBridgingFee, FlagKeyName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// get bridgeClient first to set cosmos SDK config
bridgeClient, err := bcp(cmd)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return errors.Wrap(err, "failed to get client context")
}
coreumClientCtx, err := WithKeyring(clientCtx, cmd.Flags(), coreum.KeyringSuffix)
if err != nil {
return err
}
sender, err := readAddressFromKeyNameFlag(cmd, coreumClientCtx)
if err != nil {
return err
}
issuer := args[0]
currency := args[1]
state, sendingPrecision, maxHoldingAmount, bridgingFee, err := readUpdateTokenFlags(cmd)
if err != nil {
return err
}