From 27cb844c8371b4568c469ffe178f827ce01a5fec Mon Sep 17 00:00:00 2001 From: vinhphuctadang Date: Tue, 2 May 2023 10:07:11 +0700 Subject: [PATCH] feat: add get tx --- client/chain/chain.go | 8 +++ .../example.go | 0 examples/chain/39_GetTx/example.go | 70 +++++++++++++++++++ 3 files changed, 78 insertions(+) rename examples/chain/{37_MsgLiquidate => 38_MsgLiquidate}/example.go (100%) create mode 100644 examples/chain/39_GetTx/example.go diff --git a/client/chain/chain.go b/client/chain/chain.go index 52b709e0..289f28a1 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -128,6 +128,8 @@ type ChainClient interface { StreamEventOrderFail(sender string, failEventCh chan map[string]uint) StreamOrderbookUpdateEvents(orderbookType OrderbookType, marketIds []string, orderbookCh chan exchangetypes.Orderbook) + // get tx from chain node + GetTx(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error) Close() } @@ -1278,6 +1280,12 @@ func (c *chainClient) StreamOrderbookUpdateEvents(orderbookType OrderbookType, m } } +func (c *chainClient) GetTx(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error) { + return c.txClient.GetTx(ctx, &txtypes.GetTxRequest{ + Hash: txHash, + }) +} + type DerivativeOrderData struct { OrderType exchangetypes.OrderType Price cosmtypes.Dec diff --git a/examples/chain/37_MsgLiquidate/example.go b/examples/chain/38_MsgLiquidate/example.go similarity index 100% rename from examples/chain/37_MsgLiquidate/example.go rename to examples/chain/38_MsgLiquidate/example.go diff --git a/examples/chain/39_GetTx/example.go b/examples/chain/39_GetTx/example.go new file mode 100644 index 00000000..166007ac --- /dev/null +++ b/examples/chain/39_GetTx/example.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "fmt" + "os" + "time" + + "github.com/InjectiveLabs/sdk-go/client/common" + + 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("mainnet", "k8s") + 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) + } + + timeOutCtx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second) + defer cancelFn() + + resp, err := chainClient.GetTx(timeOutCtx, "A2B2B971C690AE7977451D24D6F450AECE6BCCB271E91E32C2563342DDA5254B") + if err != nil { + panic(err) + } + + fmt.Println(resp.TxResponse) +}