-
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.
F/msg liquidate plus lower log level (#122)
* chore: add msg liquidate example * chore: lower log level for reading chain cookie
- Loading branch information
Showing
2 changed files
with
111 additions
and
12 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
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,103 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/InjectiveLabs/sdk-go/client/common" | ||
|
||
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" | ||
chainclient "github.com/InjectiveLabs/sdk-go/client/chain" | ||
rpchttp "github.com/tendermint/tendermint/rpc/client/http" | ||
) | ||
|
||
func main() { | ||
// network := common.LoadNetwork("mainnet", "k8s") | ||
network := common.LoadNetwork("devnet-1", "") | ||
tmRPC, err := rpchttp.New(network.TmEndpoint, "/websocket") | ||
|
||
if err != nil { | ||
fmt.Println(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 { | ||
fmt.Println(err) | ||
} | ||
|
||
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmRPC) | ||
|
||
chainClient, err := chainclient.NewChainClient( | ||
clientCtx, | ||
network.ChainGrpcEndpoint, | ||
common.OptionTLSCert(network.ChainTlsCert), | ||
common.OptionGasPrices("500000000inj"), | ||
) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
defaultSubaccountID := chainClient.DefaultSubaccount(senderAddress) | ||
|
||
marketId := "0x56d0c0293c4415e2d48fc2c8503a56a0c7389247396a2ef9b0a48c01f0646705" | ||
|
||
liqMsg := exchangetypes.MsgLiquidatePosition{ | ||
Sender: senderAddress.String(), | ||
SubaccountId: defaultSubaccountID.String(), | ||
MarketId: marketId, | ||
Order: nil, | ||
} | ||
|
||
simRes, err := chainClient.SimulateMsg(clientCtx, &liqMsg) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
simResMsgs := common.MsgResponse(simRes.Result.Data) | ||
msgLiquidatePositionResponse := exchangetypes.MsgLiquidatePositionResponse{} | ||
msgLiquidatePositionResponse.Unmarshal(simResMsgs[0].Data) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg | ||
err = chainClient.QueueBroadcastMsg(&liqMsg) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
time.Sleep(time.Second * 5) | ||
|
||
gasFee, err := chainClient.GetGasFee() | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println("gas fee:", gasFee, "INJ") | ||
} |