Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add missing explorer endpoints #274

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 119 additions & 26 deletions client/explorer/explorer.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
package exchange
package explorer

import (
"context"
"fmt"

log "github.com/InjectiveLabs/suplog"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/InjectiveLabs/sdk-go/client/common"
explorerPB "github.com/InjectiveLabs/sdk-go/exchange/explorer_rpc/pb"
log "github.com/InjectiveLabs/suplog"
"github.com/pkg/errors"
"google.golang.org/grpc"
)

type ExplorerClient interface {
QueryClient() *grpc.ClientConn
GetTxByTxHash(ctx context.Context, hash string) (*explorerPB.GetTxByTxHashResponse, error)
GetTxs(ctx context.Context, req *explorerPB.GetTxsRequest) (*explorerPB.GetTxsResponse, error)
GetBlock(ctx context.Context, blockHeight string) (*explorerPB.GetBlockResponse, error)
GetBlocks(ctx context.Context) (*explorerPB.GetBlocksResponse, error)
GetAccountTxs(ctx context.Context, req *explorerPB.GetAccountTxsRequest) (*explorerPB.GetAccountTxsResponse, error)
FetchContractTxs(ctx context.Context, req *explorerPB.GetContractTxsRequest) (*explorerPB.GetContractTxsResponse, error)
FetchContractTxsV2(ctx context.Context, req *explorerPB.GetContractTxsV2Request) (*explorerPB.GetContractTxsV2Response, error)
GetBlocks(ctx context.Context) (*explorerPB.GetBlocksResponse, error)
GetBlock(ctx context.Context, blockHeight string) (*explorerPB.GetBlockResponse, error)
FetchValidators(ctx context.Context) (*explorerPB.GetValidatorsResponse, error)
FetchValidator(ctx context.Context, address string) (*explorerPB.GetValidatorResponse, error)
FetchValidatorUptime(ctx context.Context, address string) (*explorerPB.GetValidatorUptimeResponse, error)
GetTxs(ctx context.Context, req *explorerPB.GetTxsRequest) (*explorerPB.GetTxsResponse, error)
GetTxByTxHash(ctx context.Context, hash string) (*explorerPB.GetTxByTxHashResponse, error)
GetPeggyDeposits(ctx context.Context, req *explorerPB.GetPeggyDepositTxsRequest) (*explorerPB.GetPeggyDepositTxsResponse, error)
GetPeggyWithdrawals(ctx context.Context, req *explorerPB.GetPeggyWithdrawalTxsRequest) (*explorerPB.GetPeggyWithdrawalTxsResponse, error)
GetIBCTransfers(ctx context.Context, req *explorerPB.GetIBCTransferTxsRequest) (*explorerPB.GetIBCTransferTxsResponse, error)
StreamTxs(ctx context.Context) (explorerPB.InjectiveExplorerRPC_StreamTxsClient, error)
StreamBlocks(ctx context.Context) (explorerPB.InjectiveExplorerRPC_StreamBlocksClient, error)
GetWasmCodes(ctx context.Context, req *explorerPB.GetWasmCodesRequest) (*explorerPB.GetWasmCodesResponse, error)
GetWasmCodeByID(ctx context.Context, req *explorerPB.GetWasmCodeByIDRequest) (*explorerPB.GetWasmCodeByIDResponse, error)
GetWasmContracts(ctx context.Context, req *explorerPB.GetWasmContractsRequest) (*explorerPB.GetWasmContractsResponse, error)
GetWasmContractByAddress(ctx context.Context, req *explorerPB.GetWasmContractByAddressRequest) (*explorerPB.GetWasmContractByAddressResponse, error)
GetCW20Balance(ctx context.Context, req *explorerPB.GetCw20BalanceRequest) (*explorerPB.GetCw20BalanceResponse, error)
FetchRelayers(ctx context.Context, marketIDs []string) (*explorerPB.RelayersResponse, error)
FetchBankTransfers(ctx context.Context, req *explorerPB.GetBankTransfersRequest) (*explorerPB.GetBankTransfersResponse, error)
StreamTxs(ctx context.Context) (explorerPB.InjectiveExplorerRPC_StreamTxsClient, error)
StreamBlocks(ctx context.Context) (explorerPB.InjectiveExplorerRPC_StreamBlocksClient, error)
Close()
}

Expand Down Expand Up @@ -96,7 +102,6 @@ func (c *explorerClient) GetTxByTxHash(ctx context.Context, hash string) (*explo
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetTxByTxHash, &req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetTxByTxHashResponse{}, err
}

Expand All @@ -107,7 +112,6 @@ func (c *explorerClient) GetAccountTxs(ctx context.Context, req *explorerPB.GetA
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetAccountTxs, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetAccountTxsResponse{}, err
}

Expand All @@ -120,7 +124,6 @@ func (c *explorerClient) GetBlocks(ctx context.Context) (*explorerPB.GetBlocksRe
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetBlocks, &req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetBlocksResponse{}, err
}

Expand All @@ -135,18 +138,62 @@ func (c *explorerClient) GetBlock(ctx context.Context, blockHeight string) (*exp
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetBlock, &req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetBlockResponse{}, err
}

return res, nil
}

func (c *explorerClient) FetchValidators(ctx context.Context) (*explorerPB.GetValidatorsResponse, error) {
req := &explorerPB.GetValidatorsRequest{}

res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetValidators, req)

if err != nil {
return &explorerPB.GetValidatorsResponse{}, err
}

return res, nil
}

func (c *explorerClient) FetchValidator(
ctx context.Context,
address string,
) (*explorerPB.GetValidatorResponse, error) {
req := &explorerPB.GetValidatorRequest{
Address: address,
}

res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetValidator, req)

if err != nil {
return &explorerPB.GetValidatorResponse{}, err
}

return res, nil
}

func (c *explorerClient) FetchValidatorUptime(
ctx context.Context,
address string,
) (*explorerPB.GetValidatorUptimeResponse, error) {
req := &explorerPB.GetValidatorUptimeRequest{
Address: address,
}

res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetValidatorUptime, req)

if err != nil {
return &explorerPB.GetValidatorUptimeResponse{}, err
}

return res, nil
}

func (c *explorerClient) GetTxs(ctx context.Context, req *explorerPB.GetTxsRequest) (*explorerPB.GetTxsResponse, error) {
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetTxs, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetTxsResponse{}, err
}

Expand All @@ -157,7 +204,6 @@ func (c *explorerClient) GetPeggyDeposits(ctx context.Context, req *explorerPB.G
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetPeggyDepositTxs, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetPeggyDepositTxsResponse{}, err
}

Expand All @@ -168,7 +214,6 @@ func (c *explorerClient) GetPeggyWithdrawals(ctx context.Context, req *explorerP
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetPeggyWithdrawalTxs, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetPeggyWithdrawalTxsResponse{}, err
}

Expand All @@ -179,7 +224,6 @@ func (c *explorerClient) GetIBCTransfers(ctx context.Context, req *explorerPB.Ge
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetIBCTransferTxs, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetIBCTransferTxsResponse{}, err
}

Expand All @@ -192,7 +236,6 @@ func (c *explorerClient) StreamTxs(ctx context.Context) (explorerPB.InjectiveExp
stream, err := common.ExecuteStreamCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.StreamTxs, &req)

if err != nil {
fmt.Println(err)
return nil, err
}

Expand All @@ -205,7 +248,6 @@ func (c *explorerClient) StreamBlocks(ctx context.Context) (explorerPB.Injective
stream, err := common.ExecuteStreamCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.StreamBlocks, &req)

if err != nil {
fmt.Println(err)
return nil, err
}

Expand All @@ -216,7 +258,6 @@ func (c *explorerClient) GetWasmCodes(ctx context.Context, req *explorerPB.GetWa
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetWasmCodes, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetWasmCodesResponse{}, err
}

Expand All @@ -227,7 +268,6 @@ func (c *explorerClient) GetWasmCodeByID(ctx context.Context, req *explorerPB.Ge
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetWasmCodeByID, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetWasmCodeByIDResponse{}, err
}

Expand All @@ -238,7 +278,6 @@ func (c *explorerClient) GetWasmContracts(ctx context.Context, req *explorerPB.G
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetWasmContracts, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetWasmContractsResponse{}, err
}

Expand All @@ -249,7 +288,6 @@ func (c *explorerClient) GetWasmContractByAddress(ctx context.Context, req *expl
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetWasmContractByAddress, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetWasmContractByAddressResponse{}, err
}

Expand All @@ -260,13 +298,68 @@ func (c *explorerClient) GetCW20Balance(ctx context.Context, req *explorerPB.Get
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetCw20Balance, req)

if err != nil {
fmt.Println(err)
return &explorerPB.GetCw20BalanceResponse{}, err
}

return res, nil
}

func (c *explorerClient) FetchContractTxs(
ctx context.Context,
req *explorerPB.GetContractTxsRequest,
) (*explorerPB.GetContractTxsResponse, error) {
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetContractTxs, req)

if err != nil {
return &explorerPB.GetContractTxsResponse{}, err
}

return res, nil
}

func (c *explorerClient) FetchContractTxsV2(
ctx context.Context,
req *explorerPB.GetContractTxsV2Request,
) (*explorerPB.GetContractTxsV2Response, error) {
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetContractTxsV2, req)

if err != nil {
return &explorerPB.GetContractTxsV2Response{}, err
}

return res, nil
}

func (c *explorerClient) FetchRelayers(
ctx context.Context,
marketIDs []string,
) (*explorerPB.RelayersResponse, error) {
req := &explorerPB.RelayersRequest{
MarketIDs: marketIDs,
}

res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.Relayers, req)

if err != nil {
return &explorerPB.RelayersResponse{}, err
}

return res, nil
}

func (c *explorerClient) FetchBankTransfers(
ctx context.Context,
req *explorerPB.GetBankTransfersRequest,
) (*explorerPB.GetBankTransfersResponse, error) {
res, err := common.ExecuteCall(ctx, c.network.ExplorerCookieAssistant, c.explorerClient.GetBankTransfers, req)

if err != nil {
return &explorerPB.GetBankTransfersResponse{}, err
}

return res, nil
}

func (c *explorerClient) Close() {
c.conn.Close()
}
48 changes: 48 additions & 0 deletions examples/explorer/16_GetContractTxs/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/InjectiveLabs/sdk-go/client/common"
"github.com/InjectiveLabs/sdk-go/client/explorer"
explorerPB "github.com/InjectiveLabs/sdk-go/exchange/explorer_rpc/pb"
)

func main() {
network := common.LoadNetwork("testnet", "lb")

explorerClient, err := explorer.NewExplorerClient(network)
if err != nil {
log.Fatalf("Failed to create explorer client: %v", err)
}

Comment on lines +18 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add missing defer for client.Close()

Resource cleanup is incomplete. Add defer explorerClient.Close() after client creation to ensure proper cleanup.

 	if err != nil {
 		log.Fatalf("Failed to create explorer client: %v", err)
 	}
+	defer explorerClient.Close()

Committable suggestion skipped: line range outside the PR's diff.

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Example contract address (replace with an actual contract address)
contractAddress := "inj1ady3s7whq30l4fx8sj3x6muv5mx4dfdlcpv8n7"

req := &explorerPB.GetContractTxsRequest{
Address: contractAddress,
Limit: 10, // Fetch 10 transactions
}

response, err := explorerClient.FetchContractTxs(ctx, req)
if err != nil {
log.Fatalf("Failed to fetch contract transactions: %v", err)
}

fmt.Println("Total Contract Transactions:", len(response.Data))
for _, tx := range response.Data {
fmt.Printf("Tx Hash: %s, Block: %d\n", tx.Hash, tx.BlockNumber)
}

fmt.Printf("\n\n")
fmt.Println("Full response:")
str, _ := json.MarshalIndent(response, "", " ")
fmt.Print(string(str))
}
49 changes: 49 additions & 0 deletions examples/explorer/17_GetContractTxsV2/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/InjectiveLabs/sdk-go/client/common"
"github.com/InjectiveLabs/sdk-go/client/explorer"
explorerPB "github.com/InjectiveLabs/sdk-go/exchange/explorer_rpc/pb"
)

func main() {
network := common.LoadNetwork("testnet", "lb")

explorerClient, err := explorer.NewExplorerClient(network)
if err != nil {
log.Fatalf("Failed to create explorer client: %v", err)
}
defer explorerClient.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Example contract address (replace with an actual contract address)
contractAddress := "inj1ady3s7whq30l4fx8sj3x6muv5mx4dfdlcpv8n7"

req := &explorerPB.GetContractTxsV2Request{
Address: contractAddress,
Limit: 10, // Fetch 10 transactions
}

response, err := explorerClient.FetchContractTxsV2(ctx, req)
if err != nil {
log.Fatalf("Failed to fetch contract transactions V2: %v", err)
}

fmt.Println("Total Contract Transactions:", len(response.Data))
for _, tx := range response.Data {
fmt.Printf("Tx Hash: %s, Block: %d\n", tx.Hash, tx.BlockNumber)
}

fmt.Printf("\n\n")
fmt.Println("Full response:")
str, _ := json.MarshalIndent(response, "", " ")
fmt.Print(string(str))
}
Loading
Loading