diff --git a/client/chain/chain.go b/client/chain/chain.go index a7815c23..3dbf1314 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -258,7 +258,7 @@ func NewChainClient( cc.logger.Errorln(err) } else { cc.sessionCookie = string(data) - cc.logger.Infoln("chain session cookie loaded from disk") + cc.logger.Debugln("chain session cookie loaded from disk") } return cc, nil @@ -489,7 +489,7 @@ func (c *chainClient) SyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRes } if err != nil { resJSON, _ := json.MarshalIndent(res, "", "\t") - c.logger.WithField("size", len(msgs)).WithError(err).Errorln("failed to commit msg batch:", string(resJSON)) + c.logger.WithField("size", len(msgs)).WithError(err).Errorln("failed synchronously broadcast messages:", string(resJSON)) return nil, err } } @@ -553,7 +553,7 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe } if err != nil { resJSON, _ := json.MarshalIndent(res, "", "\t") - c.logger.WithField("size", len(msgs)).WithError(err).Errorln("failed to commit msg batch:", string(resJSON)) + c.logger.WithField("size", len(msgs)).WithError(err).Errorln("failed to asynchronously broadcast messagess:", string(resJSON)) return nil, err } } @@ -640,8 +640,6 @@ func (c *chainClient) SyncBroadcastSignedTx(txBytes []byte) (*txtypes.BroadcastT return &txtypes.BroadcastTxResponse{TxResponse: errRes}, err } - // log.WithError(err).Warningln("Tx Error for Hash:", res.TxHash) - t.Reset(defaultBroadcastStatusPoll) continue @@ -728,8 +726,8 @@ func (c *chainClient) broadcastTx( } req := txtypes.BroadcastTxRequest{ - txBytes, - txtypes.BroadcastMode_BROADCAST_MODE_SYNC, + TxBytes: txBytes, + Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC, } // use our own client to broadcast tx var header metadata.MD @@ -758,8 +756,6 @@ func (c *chainClient) broadcastTx( return &txtypes.BroadcastTxResponse{TxResponse: errRes}, err } - // log.WithError(err).Warningln("Tx Error for Hash:", res.TxHash) - t.Reset(defaultBroadcastStatusPoll) continue @@ -818,16 +814,16 @@ func (c *chainClient) runBatchBroadcast() { } if err != nil { resJSON, _ := json.MarshalIndent(res, "", "\t") - c.logger.WithField("size", len(toSubmit)).WithError(err).Errorln("failed to commit msg batch:", string(resJSON)) + c.logger.WithField("size", len(toSubmit)).WithError(err).Errorln("failed to broadcast messages batch:", string(resJSON)) return } } if res.TxResponse.Code != 0 { err = errors.Errorf("error %d (%s): %s", res.TxResponse.Code, res.TxResponse.Codespace, res.TxResponse.RawLog) - log.WithField("txHash", res.TxResponse.TxHash).WithError(err).Errorln("failed to commit msg batch") + log.WithField("txHash", res.TxResponse.TxHash).WithError(err).Errorln("failed to broadcast messages batch") } else { - log.WithField("txHash", res.TxResponse.TxHash).Debugln("msg batch committed successfully at height", res.TxResponse.Height) + log.WithField("txHash", res.TxResponse.TxHash).Debugln("msg batch broadcasted successfully at height", res.TxResponse.Height) } c.accSeq++ diff --git a/examples/chain/37_MsgLiquidate/example.go b/examples/chain/37_MsgLiquidate/example.go new file mode 100644 index 00000000..d798c439 --- /dev/null +++ b/examples/chain/37_MsgLiquidate/example.go @@ -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") +}