-
Notifications
You must be signed in to change notification settings - Fork 131
/
tx.go
113 lines (100 loc) · 3.38 KB
/
tx.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
package cli
import (
"strconv"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/palomachain/paloma/x/gravity/types"
vtypes "github.com/palomachain/paloma/x/valset/types"
"github.com/spf13/cobra"
)
// GetTxCmd bundles all the subcmds together so they appear under `gravity tx`
func GetTxCmd(storeKey string) *cobra.Command {
// nolint: exhaustruct
gravityTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Gravity transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
gravityTxCmd.AddCommand([]*cobra.Command{
CmdSendToEth(),
CmdCancelSendToEth(),
}...)
return gravityTxCmd
}
// CmdSendToEth sends tokens to Ethereum. Locks Cosmos-side tokens into the Transaction pool for batching.
func CmdSendToEth() *cobra.Command {
// nolint: exhaustruct
cmd := &cobra.Command{
Use: "send-to-eth [eth-dest] [amount] [chain-reference-id]",
Short: "Adds a new entry to the transaction pool to withdraw an amount from the Ethereum bridge contract. This will not execute until a batch is requested and then actually relayed. Your funds can be reclaimed using cancel-send-to-eth so long as they remain in the pool",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cosmosAddr := cliCtx.GetFromAddress()
ethDest := args[0]
amount, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return sdkerrors.Wrap(err, "amount")
}
chainReferenceID := args[2]
// Make the message
msg := types.MsgSendToEth{
EthDest: ethDest,
Amount: amount[0],
ChainReferenceId: chainReferenceID,
Metadata: vtypes.MsgMetadata{
Creator: cosmosAddr.String(),
Signers: []string{cosmosAddr.String()},
},
}
if err := msg.ValidateBasic(); err != nil {
return err
}
// Send it
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// CmdCancelSendToEth enables users to take their Transaction out of the pool. Note that this cannot be done if it is
// locked up in a pending batch or if it has already been executed on Ethereum
func CmdCancelSendToEth() *cobra.Command {
// nolint: exhaustruct
cmd := &cobra.Command{
Use: "cancel-send-to-eth [transaction id]",
Short: "Removes an entry from the transaction pool, preventing your tokens from going to Ethereum and refunding the send.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cosmosAddr := cliCtx.GetFromAddress()
txId, err := strconv.ParseUint(args[0], 0, 64)
if err != nil {
return sdkerrors.Wrap(err, "failed to parse transaction id")
}
// Make the message
msg := types.MsgCancelSendToEth{
TransactionId: txId,
Metadata: vtypes.MsgMetadata{
Creator: cosmosAddr.String(),
Signers: []string{cosmosAddr.String()},
},
}
// Send it
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}