From c60e9611decbb5541bc0d1b6fcfab9ea94e85398 Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 18 Jan 2024 16:39:52 -0300 Subject: [PATCH 1/2] (feat) Added script example for the MsgLiquidatePosition message --- .../chain/70_MsgLiquidatePosition/example.go | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 examples/chain/70_MsgLiquidatePosition/example.go diff --git a/examples/chain/70_MsgLiquidatePosition/example.go b/examples/chain/70_MsgLiquidatePosition/example.go new file mode 100644 index 00000000..6fa8324b --- /dev/null +++ b/examples/chain/70_MsgLiquidatePosition/example.go @@ -0,0 +1,112 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" + "github.com/google/uuid" + "os" + + "github.com/InjectiveLabs/sdk-go/client/common" + "github.com/shopspring/decimal" + + exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket") + if err != nil { + panic(err) + } + + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + senderAddress.String(), + cosmosKeyring, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + exchangeClient, err := exchangeclient.NewExchangeClient(network) + if err != nil { + panic(err) + } + + ctx := context.Background() + marketsAssistant, err := chainclient.NewMarketsAssistantInitializedFromChain(ctx, exchangeClient) + if err != nil { + panic(err) + } + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + defaultSubaccountID := chainClient.DefaultSubaccount(senderAddress) + + marketId := "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6" + amount := decimal.NewFromFloat(0.147) + price := decimal.RequireFromString("39.01") + leverage := decimal.RequireFromString("1") + + order := chainClient.CreateDerivativeOrder( + defaultSubaccountID, + network, + &chainclient.DerivativeOrderData{ + OrderType: exchangetypes.OrderType_SELL, + Quantity: amount, + Price: price, + Leverage: leverage, + FeeRecipient: senderAddress.String(), + MarketId: marketId, + Cid: uuid.NewString(), + }, + marketsAssistant, + ) + + msg := &exchangetypes.MsgLiquidatePosition{ + Sender: senderAddress.String(), + SubaccountId: "0x156df4d5bc8e7dd9191433e54bd6a11eeb390921000000000000000000000000", + MarketId: marketId, + Order: order, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.AsyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} From 718dbd6cdb4e0637f71fe84bb4f23d074edde971 Mon Sep 17 00:00:00 2001 From: abel Date: Thu, 18 Jan 2024 17:41:39 -0300 Subject: [PATCH 2/2] (fix) Fixed lint errors --- examples/chain/70_MsgLiquidatePosition/example.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/chain/70_MsgLiquidatePosition/example.go b/examples/chain/70_MsgLiquidatePosition/example.go index 6fa8324b..23cbb1e9 100644 --- a/examples/chain/70_MsgLiquidatePosition/example.go +++ b/examples/chain/70_MsgLiquidatePosition/example.go @@ -4,9 +4,10 @@ import ( "context" "encoding/json" "fmt" + "os" + exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" "github.com/google/uuid" - "os" "github.com/InjectiveLabs/sdk-go/client/common" "github.com/shopspring/decimal"