-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from InjectiveLabs/fix/update_creation_msg_li…
…quidate_position (feat) Added script example for the MsgLiquidatePosition message
- Loading branch information
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |