Skip to content

Commit

Permalink
first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
linhpn99 committed Jul 30, 2024
1 parent e844fc1 commit 129efce
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
32 changes: 31 additions & 1 deletion gno.land/pkg/gnoclient/client_queries.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gnoclient

import (
"encoding/base64"
"fmt"

"github.com/gnolang/gno/tm2/pkg/amino"
Expand All @@ -11,7 +12,11 @@ import (
"github.com/gnolang/gno/tm2/pkg/std"
)

var ErrInvalidBlockHeight = errors.New("invalid block height provided")
var (
ErrInvalidBlockHeight = errors.New("invalid block height provided")
ErrEmptyTxHash = errors.New("empty tx hash")
ErrInvalidTxHashFormat = errors.New("invalid tx hash format")
)

// QueryCfg contains configuration options for performing ABCI queries.
type QueryCfg struct {
Expand Down Expand Up @@ -177,3 +182,28 @@ func (c *Client) LatestBlockHeight() (int64, error) {

return status.SyncInfo.LatestBlockHeight, nil
}

// GetTransaction retrieves the transaction details for a given transaction hash
// The provided hash must be a valid base64 encoded string
func (c *Client) Transaction(hash string) (*ctypes.ResultTx, error) {
// Validate the RPC client
if err := c.validateRPCClient(); err != nil {
return nil, ErrMissingRPCClient

Check warning on line 191 in gno.land/pkg/gnoclient/client_queries.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L191

Added line #L191 was not covered by tests
}

if hash == "" {
return nil, ErrEmptyTxHash
}

data, err := base64.StdEncoding.DecodeString(hash)
if err != nil {
return nil, ErrInvalidTxHashFormat
}

tx, err := c.RPCClient.Tx(data)
if err != nil {
return nil, fmt.Errorf("transaction query failed: %w", err)
}

return tx, nil
}
72 changes: 72 additions & 0 deletions gno.land/pkg/gnoclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gnoclient

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1266,3 +1267,74 @@ func TestLatestBlockHeightErrors(t *testing.T) {
})
}
}

// Transaction tests
func TestGetTransaction(t *testing.T) {
t.Parallel()

// Test cases
tests := []struct {
name string
hash string
txResult *ctypes.ResultTx
mockError error
wantError error
}{
{
name: "valid hash",
hash: "dGhpcyBpcyBhIHRlc3QgaGFzaA==", // "this is a test hash" in base64
txResult: &ctypes.ResultTx{Hash: []byte("dGhpcyBpcyBhIHRlc3QgaGFzaA==")},
mockError: nil,
wantError: nil,
},
{
name: "empty hash",
hash: "",
txResult: nil,
mockError: nil,
wantError: ErrEmptyTxHash,
},
{
name: "invalid base64 hash",
hash: "invalid-base64",
txResult: nil,
mockError: nil,
wantError: ErrInvalidTxHashFormat,
},
{
name: "rpc client error",
hash: "dGhpcyBpcyBhIHRlc3QgaGFzaA==",
txResult: nil,
mockError: fmt.Errorf("RPC error"),
wantError: fmt.Errorf("transaction query failed: RPC error"),
},
}

for _, tt := range tests {
tt := tt // Capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

client := &Client{
Signer: &mockSigner{},
RPCClient: &mockRPCClient{
tx: func(hash []byte) (*ctypes.ResultTx, error) {
if tt.mockError != nil {
return nil, tt.mockError
}
return tt.txResult, nil
},
},
}

tx, err := client.Transaction(tt.hash)
if tt.wantError != nil {
require.Error(t, err)
assert.Equal(t, tt.wantError.Error(), err.Error())
} else {
require.NoError(t, err)
assert.Equal(t, tt.txResult.Hash, tx.Hash)
}
})
}
}

0 comments on commit 129efce

Please sign in to comment.