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

feat: update margin CLI api and add CLI examples #200

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: 1
accounts:
- name: treasury
coins:
- 100000000uatom
- 100000000uusdt
- 1000000000000uatom
- 1000000000000uusdc
cosmic-vagabond marked this conversation as resolved.
Show resolved Hide resolved
- 9000000000000000uelys
- 100000000ueden
mnemonic: olympic slide park figure frost benefit deer reform fly pull price airport submit monitor silk insect uphold convince pupil project ignore roof warfare slight
Expand Down
2 changes: 1 addition & 1 deletion x/amm/client/cli/tx_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CmdCreatePool() *cobra.Command {
cmd := &cobra.Command{
Use: "create-pool [weights] [initial-deposit] [swap-fee] [exit-fee]",
Short: "create a new pool and provide the liquidity to it",
Example: `elysd tx amm create-pool 100ueden,100uelys 1000000ueden,1000000uelys 0.00 0.00 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Example: `elysd tx amm create-pool 100uatom,100uusdc 100000000000uatom,100000000000uusdc 0.00 0.00 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argWeights, err := sdk.ParseCoinsNormalized(args[0])
Expand Down
7 changes: 4 additions & 3 deletions x/amm/client/cli/tx_exit_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ var _ = strconv.Itoa(0)

func CmdExitPool() *cobra.Command {
cmd := &cobra.Command{
Use: "exit-pool [pool-id] [min-amounts-out] [share-amount-in]",
Short: "exit a new pool and withdraw the liquidity from it",
Args: cobra.ExactArgs(3),
Use: "exit-pool [pool-id] [min-amounts-out] [share-amount-in]",
Short: "exit a new pool and withdraw the liquidity from it",
Example: `elysd tx amm exit-pool 0 1000uatom,1000uusdc 200000000000000000 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argPoolId, err := cast.ToUint64E(args[0])
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions x/amm/client/cli/tx_join_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ var _ = strconv.Itoa(0)

func CmdJoinPool() *cobra.Command {
cmd := &cobra.Command{
Use: "join-pool [pool-id] [max-amounts-in] [share-amount-out]",
Short: "join a new pool and provide the liquidity to it",
Args: cobra.ExactArgs(3),
Use: "join-pool [pool-id] [max-amounts-in] [share-amount-out]",
Short: "join a new pool and provide the liquidity to it",
Example: `elysd tx amm join-pool 0 2000uatom,2000uusdc 200000000000000000 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argPoolId, err := cast.ToUint64E(args[0])
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions x/amm/client/cli/tx_swap_exact_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ var _ = strconv.Itoa(0)

func CmdSwapExactAmountIn() *cobra.Command {
cmd := &cobra.Command{
Use: "swap-exact-amount-in [token-in] [token-out-min-amount] [swap-route-pool-ids] [swap-route-denoms]",
Short: "Swap an exact amount of tokens for a minimum of another token, similar to swapping a token on the trade screen GUI.",
Args: cobra.ExactArgs(4),
Use: "swap-exact-amount-in [token-in] [token-out-min-amount] [swap-route-pool-ids] [swap-route-denoms]",
Short: "Swap an exact amount of tokens for a minimum of another token, similar to swapping a token on the trade screen GUI.",
Example: `elysd tx amm swap-exact-amount-in 100000uusdc 10000 0 uatom --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argTokenIn, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions x/amm/client/cli/tx_swap_exact_amount_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ var _ = strconv.Itoa(0)

func CmdSwapExactAmountOut() *cobra.Command {
cmd := &cobra.Command{
Use: "swap-exact-amount-out [token-out] [token-out-max-amount] [swap-route-pool-ids] [swap-route-denoms]",
Short: "Swap a maximum amount of tokens for an exact amount of another token, similar to swapping a token on the trade screen GUI.",
Args: cobra.ExactArgs(4),
Use: "swap-exact-amount-out [token-out] [token-out-max-amount] [swap-route-pool-ids] [swap-route-denoms]",
Short: "Swap a maximum amount of tokens for an exact amount of another token, similar to swapping a token on the trade screen GUI.",
Example: `elysd tx amm swap-exact-amount-out 100000uatom 200000 0 uusdc --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argTokenOut, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions x/margin/client/cli/tx_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ var _ = strconv.Itoa(0)

func CmdClose() *cobra.Command {
cmd := &cobra.Command{
Use: "close",
Short: "Close margin position",
Use: "close [mtp-id] [flags]",
Short: "Close margin position",
Example: `elysd tx margin close 1 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -27,23 +29,22 @@ func CmdClose() *cobra.Command {
return errors.New("signer address is missing")
}

id, err := cmd.Flags().GetUint64("id")
if err != nil {
return err
argMtpId, ok := strconv.ParseUint(args[0], 10, 64)
if ok != nil {
return errors.New("invalid mtp id")
}

msg := types.NewMsgClose(
signer.String(),
id,
argMtpId,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().Uint64("id", 0, "id of the position")
_ = cmd.MarkFlagRequired("id")

flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
2 changes: 1 addition & 1 deletion x/margin/client/cli/tx_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestClosePosition(t *testing.T) {
// Tendermint RPC calls.
// ...
args := []string{
"--id=1",
"1",
"--from=" + val.Address.String(),
"-y",
}
Expand Down
61 changes: 17 additions & 44 deletions x/margin/client/cli/tx_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ var _ = strconv.Itoa(0)

func CmdOpen() *cobra.Command {
cmd := &cobra.Command{
Use: "open",
Short: "Open margin position",
Use: "open [position] [leverage] [collateral-asset] [collateral-amount] [borrow-asset] [flags]",
Short: "Open margin position",
Example: `elysd tx margin open long 5 uusdc 100000000 uatom --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000`,
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -29,49 +31,29 @@ func CmdOpen() *cobra.Command {
return errors.New("signer address is missing")
}

collateralAsset, err := cmd.Flags().GetString("collateral_asset")
if err != nil {
return err
}

collateralAmount, err := cmd.Flags().GetString("collateral_amount")
if err != nil {
return err
}

borrowAsset, err := cmd.Flags().GetString("borrow_asset")
if err != nil {
return err
}

position, err := cmd.Flags().GetString("position")
if err != nil {
return err
}
positionEnum := types.GetPositionFromString(position)
argPosition := types.GetPositionFromString(args[0])

leverage, err := cmd.Flags().GetString("leverage")
argLeverage, err := sdk.NewDecFromStr(args[1])
if err != nil {
return err
}

leverageDec, err := sdk.NewDecFromStr(leverage)
if err != nil {
return err
}
argCollateralAsset := args[2]

collateralAmt, ok := sdk.NewIntFromString(collateralAmount)
argCollateralAmount, ok := sdk.NewIntFromString(args[3])
if !ok {
return errors.New("invalid collateral amount")
}

argBorrowAsset := args[4]

msg := types.NewMsgOpen(
clientCtx.GetFromAddress().String(),
collateralAsset,
collateralAmt,
borrowAsset,
positionEnum,
leverageDec,
signer.String(),
argCollateralAsset,
argCollateralAmount,
argBorrowAsset,
argPosition,
argLeverage,
)

if err := msg.ValidateBasic(); err != nil {
Expand All @@ -80,16 +62,7 @@ func CmdOpen() *cobra.Command {
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String("collateral_amount", "0", "amount of collateral asset")
cmd.Flags().String("collateral_asset", "", "symbol of asset")
cmd.Flags().String("borrow_asset", "", "symbol of asset")
cmd.Flags().String("position", "", "type of position")
cmd.Flags().String("leverage", "", "leverage of position")
_ = cmd.MarkFlagRequired("collateral_amount")
_ = cmd.MarkFlagRequired("collateral_asset")
_ = cmd.MarkFlagRequired("borrow_asset")
_ = cmd.MarkFlagRequired("position")
_ = cmd.MarkFlagRequired("leverage")

flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
10 changes: 5 additions & 5 deletions x/margin/client/cli/tx_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func TestOpenPosition(t *testing.T) {
// Tendermint RPC calls.
// ...
args := []string{
"--collateral_asset=" + ptypes.BaseCurrency,
"--borrow_asset=uatom",
"--collateral_amount=1000",
"--position=long",
"--leverage=1.5",
"open",
"1.5",
ptypes.BaseCurrency,
"1000",
"uatom",
"--from=" + val.Address.String(),
"-y",
}
Expand Down
Loading