Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli for swap from/to ERC20 #402

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions modules/token/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,54 @@ func (s *IntegrationTestSuite) TestToken() {
token3 := tokentestutil.QueryTokenExec(s.T(), s.network, clientCtx, tokenSymbol)
s.Require().Equal(to, token3.GetOwner())
// ---------------------------------------------------------------------------

//------test GetCmdSwapToErc20()-------------
args = []string{
fmt.Sprintf("--%s=%s", tokencli.FlagTo, to.String()),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf(
"--%s=%s",
flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(s.network.BondDenom, sdk.NewInt(10))).String(),
),
}

txResult = tokentestutil.SwapToERC20Exec(
s.T(),
s.network,
clientCtx,
from.String(),
sdk.NewCoins(sdk.NewCoin(s.network.BondDenom, sdk.NewInt(1))).String(),
args...)

// TODO assert
// s.Require().Equal(expectedCode, txResult.Code)
// ---------------------------------------------------------------------------

//------test GetCmdSwapFromErc20()-------------
args = []string{
fmt.Sprintf("--%s=%s", tokencli.FlagTo, to.String()),

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf(
"--%s=%s",
flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(s.network.BondDenom, sdk.NewInt(10))).String(),
),
}

txResult = tokentestutil.SwapFromERC20Exec(
s.T(),
s.network,
clientCtx,
from.String(),
sdk.NewCoins(sdk.NewCoin(s.network.BondDenom, sdk.NewInt(1))).String(),
args...)

// TODO assert
// s.Require().Equal(expectedCode, txResult.Code)
// ---------------------------------------------------------------------------
}
6 changes: 5 additions & 1 deletion modules/token/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
FsTransferTokenOwner = flag.NewFlagSet("", flag.ContinueOnError)
FsMintToken = flag.NewFlagSet("", flag.ContinueOnError)
FsSwapFeeToken = flag.NewFlagSet("", flag.ContinueOnError)
FsSwapToErc20 = flag.NewFlagSet("", flag.ContinueOnError)
FsSwapFromErc20 = flag.NewFlagSet("", flag.ContinueOnError)
)

func init() {
Expand All @@ -42,5 +44,7 @@ func init() {

FsTransferTokenOwner.String(FlagTo, "", "The new owner")
FsMintToken.String(FlagTo, "", "Address to which the token is to be minted")
FsSwapFeeToken.String(FlagTo, "", "Address to which the token is to be receiver")
FsSwapFeeToken.String(FlagTo, "", "Address to which the token is to be received")
FsSwapToErc20.String(FlagTo, "", "Address to which the token is to be received")
FsSwapFromErc20.String(FlagTo, "", "Address to which the token is to be received")
}
124 changes: 124 additions & 0 deletions modules/token/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func NewTxCmd() *cobra.Command {
GetCmdBurnToken(),
GetCmdTransferTokenOwner(),
GetCmdSwapFeeToken(),
GetCmdSwapToErc20(),
GetCmdSwapFromErc20(),
)

return txCmd
Expand Down Expand Up @@ -415,3 +417,125 @@ func GetCmdSwapFeeToken() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// GetCmdSwapToErc20 implements the swap-to-erc20 command
func GetCmdSwapToErc20() *cobra.Command {
mitch1024 marked this conversation as resolved.
Show resolved Hide resolved
cmd := &cobra.Command{
Use: "swap-to-erc20",
Long: "Swap native token to the corresponding ERC20 at 1:1.",
Example: fmt.Sprintf(
"$ %s tx token swap-to-erc20 [paid_amount]"+
"--to=\"0x0eeb8ec40c6705b669469346ff8f9ce5cad57ed5\" "+
"--from=<key-name> "+
"--chain-id=<chain-id> "+
"--fees=<fee>",
version.AppName,
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

from := clientCtx.GetFromAddress().String()
paidAmount, token, err := parseMainCoin(clientCtx, args[0])
if err != nil {
return err
}
if len(token.GetContract()) <= 0 {
return fmt.Errorf("corresponding erc20 contract of %s does not exist", paidAmount.Denom)
}

receiver, err := cmd.Flags().GetString(FlagTo)
if err != nil {
return err
}
if len(receiver) <= 0 {
// set default receiver
receiver = from
}

msg := &v1.MsgSwapToERC20{
Amount: paidAmount,
Sender: from,
Receiver: receiver,
}

if err := msg.ValidateBasic(); err != nil {
return err
}

var prompt = fmt.Sprintf("Swapping native token to ERC20, sender: %s, receiver: %s, contract: %s, amount: %s", from, receiver, token.GetContract(), paidAmount)

fmt.Println(prompt)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().AddFlagSet(FsSwapToErc20)
flags.AddTxFlagsToCmd(cmd)

return cmd
}

// GetCmdSwapFromErc20 implements the swap-from-erc20 command
func GetCmdSwapFromErc20() *cobra.Command {
mitch1024 marked this conversation as resolved.
Show resolved Hide resolved
cmd := &cobra.Command{
Use: "swap-from-erc20",
Long: "Swap native token from the corresponding ERC20 at 1:1.",
Example: fmt.Sprintf(
"$ %s tx token swap-from-erc20 [wanted_amount]"+
"--to=\"iaaeeb8ec40c6705b669469346ff8f9ce5cad57ed5\" "+
"--from=<key-name> "+
"--chain-id=<chain-id> "+
"--fees=<fee>",
version.AppName,
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

from := clientCtx.GetFromAddress().String()
wantedAmount, token, err := parseMainCoin(clientCtx, args[0])
if err != nil {
return err
}
if len(token.GetContract()) <= 0 {
return fmt.Errorf("corresponding erc20 contract of %s does not exist", wantedAmount.Denom)
}

receiver, err := cmd.Flags().GetString(FlagTo)
if err != nil {
return err
}
if len(receiver) <= 0 {
// set default receiver
receiver = from
}

msg := &v1.MsgSwapFromERC20{
WantedAmount: wantedAmount,
Sender: from,
Receiver: receiver,
}

if err := msg.ValidateBasic(); err != nil {
return err
}

var prompt = fmt.Sprintf("Swapping native token from ERC20, sender: %s, receiver: %s, contract: %s, amount: %s", from, receiver, token.GetContract(), wantedAmount)

fmt.Println(prompt)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().AddFlagSet(FsSwapFromErc20)
flags.AddTxFlagsToCmd(cmd)

return cmd
}
32 changes: 32 additions & 0 deletions modules/token/client/testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ func TransferTokenOwnerExec(t *testing.T,
return network.ExecTxCmdWithResult(t, clientCtx, tokencli.GetCmdTransferTokenOwner(), args)
}

func SwapToERC20Exec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
coinStr string,
extraArgs ...string,
) *simapp.ResponseTx {
args := []string{
coinStr,
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, tokencli.GetCmdSwapToErc20(), args)
}

func SwapFromERC20Exec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
from string,
coinStr string,
extraArgs ...string,
) *simapp.ResponseTx {
args := []string{
coinStr,
fmt.Sprintf("--%s=%s", flags.FlagFrom, from),
}
args = append(args, extraArgs...)

return network.ExecTxCmdWithResult(t, clientCtx, tokencli.GetCmdSwapFromErc20(), args)
}

func QueryTokenExec(t *testing.T,
network simapp.Network,
clientCtx client.Context,
Expand Down
5 changes: 5 additions & 0 deletions modules/token/types/v1/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type TokenI interface {
GetMaxSupply() uint64
GetMintable() bool
GetOwner() sdk.AccAddress
GetContract() string

ToMainCoin(coin sdk.Coin) (sdk.DecCoin, error)
ToMinCoin(coin sdk.DecCoin) (sdk.Coin, error)
Expand Down Expand Up @@ -110,6 +111,10 @@ func (t Token) GetOwner() sdk.AccAddress {
return owner
}

func (t Token) GetContract() string {
return t.Contract
}

// ToMainCoin returns the main denom coin from args
func (t Token) ToMainCoin(coin sdk.Coin) (sdk.DecCoin, error) {
if t.MinUnit != coin.Denom {
Expand Down
Loading