Skip to content

Commit

Permalink
Merge pull request #204 from InjectiveLabs/fix/update_creation_msg_li…
Browse files Browse the repository at this point in the history
…quidate_position

(feat) Added script example for the MsgLiquidatePosition message
  • Loading branch information
aarmoa authored Jan 19, 2024
2 parents 1abe3e3 + 718dbd6 commit 5aa9b38
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions examples/chain/70_MsgLiquidatePosition/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"context"
"encoding/json"
"fmt"
"os"

exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange"
"github.com/google/uuid"

"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))
}

0 comments on commit 5aa9b38

Please sign in to comment.