Skip to content

Commit

Permalink
F/msg liquidate plus lower log level (#122)
Browse files Browse the repository at this point in the history
* chore: add msg liquidate example

* chore: lower log level for reading chain cookie
  • Loading branch information
Tofel committed Apr 21, 2023
1 parent 7bf5a6e commit 846dd47
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 12 deletions.
20 changes: 8 additions & 12 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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++
Expand Down
103 changes: 103 additions & 0 deletions examples/chain/37_MsgLiquidate/example.go
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")
}

0 comments on commit 846dd47

Please sign in to comment.