Skip to content

Commit

Permalink
feat: add node client for interacting with nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Jan 22, 2025
1 parent b1e856c commit 2a8ab24
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 127 deletions.
4 changes: 2 additions & 2 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

// Account retrieves an account by its address using a gRPC query.
// Returns the account interface and any potential error encountered.
func (c *Client) Account(ctx context.Context, accAddr cosmossdk.AccAddress) (res auth.AccountI, err error) {
func (c *BaseClient) Account(ctx context.Context, accAddr cosmossdk.AccAddress) (res auth.AccountI, err error) {
var (
resp auth.QueryAccountResponse
req = &auth.QueryAccountRequest{Address: accAddr.String()}
Expand All @@ -37,7 +37,7 @@ func (c *Client) Account(ctx context.Context, accAddr cosmossdk.AccAddress) (res

// Accounts retrieves a list of accounts with pagination support using a gRPC query.
// Returns a slice of account interfaces, pagination details, and any potential error.
func (c *Client) Accounts(ctx context.Context, pageReq *query.PageRequest) (res []auth.AccountI, pageRes *query.PageResponse, err error) {
func (c *BaseClient) Accounts(ctx context.Context, pageReq *query.PageRequest) (res []auth.AccountI, pageRes *query.PageResponse, err error) {
var (
resp auth.QueryAccountsResponse
req = &auth.QueryAccountsRequest{Pagination: pageReq}
Expand Down
125 changes: 73 additions & 52 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
cosmossdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sentinel-official/hub/v12/types"
)

// contextKey is a custom type used as a key to store the Client struct in a context.
type contextKey byte

// ContextKey is the constant key value used for storing and retrieving the Client struct from a context.
const ContextKey contextKey = 0

// Client contains all necessary components for transaction handling, query management, and configuration settings.
type Client struct {
// BaseClient contains all necessary components for transaction handling, query management, and configuration settings.
type BaseClient struct {
chainID string // The chain ID used to identify the blockchain network
keyring keyring.Keyring // Keyring for managing private keys and signatures
protoCodec codec.ProtoCodecMarshaler // Used for marshaling and unmarshaling protobuf data
Expand All @@ -28,133 +23,159 @@ type Client struct {
rpcAddr string // RPC server address
rpcTimeout time.Duration // RPC timeout duration
txConfig client.TxConfig // Configuration related to transactions (e.g., signing modes)
txFeeGranterAddr types.AccAddress // Address that grants transaction fees
txFees types.Coins // Fees for transactions
txFeeGranterAddr cosmossdk.AccAddress // Address that grants transaction fees
txFees cosmossdk.Coins // Fees for transactions
txFromName string // Sender name for transactions
txGasAdjustment float64 // Adjustment factor for gas estimation
txGasPrices types.DecCoins // Gas price settings for transactions
txGasPrices cosmossdk.DecCoins // Gas price settings for transactions
txGas uint64 // Gas limit for transactions
txMemo string // Memo attached to transactions
txSimulateAndExecute bool // Flag for simulating and executing transactions
txTimeoutHeight uint64 // Transaction timeout height
}

// New initializes a new Client instance.
func New() *Client {
return &Client{}
// NewBaseClient initializes a new BaseClient instance.
func NewBaseClient() *BaseClient {
return &BaseClient{}
}

// WithChainID sets the blockchain chain ID and returns the updated Client.
func (c *Client) WithChainID(chainID string) *Client {
// WithChainID sets the blockchain chain ID and returns the updated BaseClient.
func (c *BaseClient) WithChainID(chainID string) *BaseClient {
c.chainID = chainID
return c
}

// WithKeyring assigns the keyring to the Client and returns the updated Client.
func (c *Client) WithKeyring(keyring keyring.Keyring) *Client {
// WithKeyring assigns the keyring to the BaseClient and returns the updated BaseClient.
func (c *BaseClient) WithKeyring(keyring keyring.Keyring) *BaseClient {
c.keyring = keyring
return c
}

// WithProtoCodec sets the protobuf codec and returns the updated Client.
func (c *Client) WithProtoCodec(protoCodec codec.ProtoCodecMarshaler) *Client {
// WithProtoCodec sets the protobuf codec and returns the updated BaseClient.
func (c *BaseClient) WithProtoCodec(protoCodec codec.ProtoCodecMarshaler) *BaseClient {
c.protoCodec = protoCodec
return c
}

// WithQueryProve sets the prove flag for queries and returns the updated Client.
func (c *Client) WithQueryProve(prove bool) *Client {
// WithQueryProve sets the prove flag for queries and returns the updated BaseClient.
func (c *BaseClient) WithQueryProve(prove bool) *BaseClient {
c.queryProve = prove
return c
}

// WithQueryRetries sets the number of retries for queries and returns the updated Client.
func (c *Client) WithQueryRetries(retries uint) *Client {
// WithQueryRetries sets the number of retries for queries and returns the updated BaseClient.
func (c *BaseClient) WithQueryRetries(retries uint) *BaseClient {
c.queryRetries = retries
return c
}

// WithQueryRetryDelay sets the retry delay duration for queries and returns the updated Client.
func (c *Client) WithQueryRetryDelay(delay time.Duration) *Client {
// WithQueryRetryDelay sets the retry delay duration for queries and returns the updated BaseClient.
func (c *BaseClient) WithQueryRetryDelay(delay time.Duration) *BaseClient {
c.queryRetryDelay = delay
return c
}

// WithRPCAddr sets the RPC server address and returns the updated Client.
func (c *Client) WithRPCAddr(rpcAddr string) *Client {
// WithRPCAddr sets the RPC server address and returns the updated BaseClient.
func (c *BaseClient) WithRPCAddr(rpcAddr string) *BaseClient {
c.rpcAddr = rpcAddr
return c
}

// WithRPCTimeout sets the RPC timeout duration and returns the updated Client.
func (c *Client) WithRPCTimeout(timeout time.Duration) *Client {
// WithRPCTimeout sets the RPC timeout duration and returns the updated BaseClient.
func (c *BaseClient) WithRPCTimeout(timeout time.Duration) *BaseClient {
c.rpcTimeout = timeout
return c
}

// WithTxConfig sets the transaction configuration and returns the updated Client.
func (c *Client) WithTxConfig(txConfig client.TxConfig) *Client {
// WithTxConfig sets the transaction configuration and returns the updated BaseClient.
func (c *BaseClient) WithTxConfig(txConfig client.TxConfig) *BaseClient {
c.txConfig = txConfig
return c
}

// WithTxFeeGranterAddr sets the transaction fee granter address and returns the updated Client.
func (c *Client) WithTxFeeGranterAddr(addr types.AccAddress) *Client {
// WithTxFeeGranterAddr sets the transaction fee granter address and returns the updated BaseClient.
func (c *BaseClient) WithTxFeeGranterAddr(addr cosmossdk.AccAddress) *BaseClient {
c.txFeeGranterAddr = addr
return c
}

// WithTxFees assigns transaction fees and returns the updated Client.
func (c *Client) WithTxFees(fees types.Coins) *Client {
// WithTxFees assigns transaction fees and returns the updated BaseClient.
func (c *BaseClient) WithTxFees(fees cosmossdk.Coins) *BaseClient {
c.txFees = fees
return c
}

// WithTxFromName sets the "from" name for transactions and returns the updated Client.
func (c *Client) WithTxFromName(name string) *Client {
// WithTxFromName sets the "from" name for transactions and returns the updated BaseClient.
func (c *BaseClient) WithTxFromName(name string) *BaseClient {
c.txFromName = name
return c
}

// WithTxGasAdjustment sets the gas adjustment factor for transactions and returns the updated Client.
func (c *Client) WithTxGasAdjustment(adjustment float64) *Client {
// WithTxGasAdjustment sets the gas adjustment factor for transactions and returns the updated BaseClient.
func (c *BaseClient) WithTxGasAdjustment(adjustment float64) *BaseClient {
c.txGasAdjustment = adjustment
return c
}

// WithTxGasPrices sets the gas prices for transactions and returns the updated Client.
func (c *Client) WithTxGasPrices(prices types.DecCoins) *Client {
// WithTxGasPrices sets the gas prices for transactions and returns the updated BaseClient.
func (c *BaseClient) WithTxGasPrices(prices cosmossdk.DecCoins) *BaseClient {
c.txGasPrices = prices
return c
}

// WithTxGas sets the gas limit for transactions and returns the updated Client.
func (c *Client) WithTxGas(gas uint64) *Client {
// WithTxGas sets the gas limit for transactions and returns the updated BaseClient.
func (c *BaseClient) WithTxGas(gas uint64) *BaseClient {
c.txGas = gas
return c
}

// WithTxMemo sets the memo for transactions and returns the updated Client.
func (c *Client) WithTxMemo(memo string) *Client {
// WithTxMemo sets the memo for transactions and returns the updated BaseClient.
func (c *BaseClient) WithTxMemo(memo string) *BaseClient {
c.txMemo = memo
return c
}

// WithTxSimulateAndExecute sets the simulate and execute flag and returns the updated Client.
func (c *Client) WithTxSimulateAndExecute(simulate bool) *Client {
// WithTxSimulateAndExecute sets the simulate and execute flag and returns the updated BaseClient.
func (c *BaseClient) WithTxSimulateAndExecute(simulate bool) *BaseClient {
c.txSimulateAndExecute = simulate
return c
}

// WithTxTimeoutHeight sets the timeout height for transactions and returns the updated Client.
func (c *Client) WithTxTimeoutHeight(height uint64) *Client {
// WithTxTimeoutHeight sets the timeout height for transactions and returns the updated BaseClient.
func (c *BaseClient) WithTxTimeoutHeight(height uint64) *BaseClient {
c.txTimeoutHeight = height
return c
}

// HTTP creates an HTTP client for the given RPC address and timeout configuration.
// Returns the HTTP client or an error if initialization fails.
func (c *Client) HTTP() (*http.HTTP, error) {
func (c *BaseClient) HTTP() (*http.HTTP, error) {
timeout := uint(c.rpcTimeout / time.Second)
return http.NewWithTimeout(c.rpcAddr, "/websocket", timeout)
}

// NodeClient is a struct for interacting with nodes.
type NodeClient struct {
*BaseClient
addr types.NodeAddress
timeout time.Duration
}

// NewNodeClient creates a new instance of NodeClient.
func NewNodeClient(base *BaseClient) *NodeClient {
return &NodeClient{
BaseClient: base,
}
}

// WithAddr sets the address of the NodeClient and returns the updated instance.
func (c *NodeClient) WithAddr(addr types.NodeAddress) *NodeClient {
c.addr = addr
return c
}

// WithTimeout sets the timeout of the NodeClient and returns the updated instance.
func (c *NodeClient) WithTimeout(timeout time.Duration) *NodeClient {
c.timeout = timeout
return c
}
12 changes: 6 additions & 6 deletions client/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Key retrieves key information from the keyring based on the provided name.
// Returns the key record or an error if the key cannot be found.
func (c *Client) Key(name string) (*keyring.Record, error) {
func (c *BaseClient) Key(name string) (*keyring.Record, error) {
key, err := c.keyring.Key(name)
if err != nil {
return nil, fmt.Errorf("failed to retrieve key: %w", err)
Expand All @@ -22,7 +22,7 @@ func (c *Client) Key(name string) (*keyring.Record, error) {

// Sign signs the provided data using the key from the keyring identified by the given name.
// Returns the signed bytes, the public key, and any error encountered.
func (c *Client) Sign(name string, buf []byte) ([]byte, types.PubKey, error) {
func (c *BaseClient) Sign(name string, buf []byte) ([]byte, types.PubKey, error) {
sig, pubKey, err := c.keyring.Sign(name, buf)
if err != nil {
return nil, nil, fmt.Errorf("failed to sign data: %w", err)
Expand All @@ -33,7 +33,7 @@ func (c *Client) Sign(name string, buf []byte) ([]byte, types.PubKey, error) {

// Keys retrieves a list of all keys from the keyring.
// Returns the list of key records or an error if the operation fails.
func (c *Client) Keys() ([]*keyring.Record, error) {
func (c *BaseClient) Keys() ([]*keyring.Record, error) {
keys, err := c.keyring.List()
if err != nil {
return nil, fmt.Errorf("failed to retrieve keys: %w", err)
Expand All @@ -44,7 +44,7 @@ func (c *Client) Keys() ([]*keyring.Record, error) {

// DeleteKey removes a key from the keyring based on the provided name.
// Returns an error if the key cannot be deleted.
func (c *Client) DeleteKey(name string) error {
func (c *BaseClient) DeleteKey(name string) error {
if err := c.keyring.Delete(name); err != nil {
return fmt.Errorf("failed to delete key: %w", err)
}
Expand All @@ -54,7 +54,7 @@ func (c *Client) DeleteKey(name string) error {

// NewMnemonic generates a new mnemonic phrase using bip39 with 256 bits of entropy.
// Returns the mnemonic or an error if the operation fails.
func (c *Client) NewMnemonic() (string, error) {
func (c *BaseClient) NewMnemonic() (string, error) {
// Generate new entropy for the mnemonic.
entropy, err := bip39.NewEntropy(256)
if err != nil {
Expand All @@ -73,7 +73,7 @@ func (c *Client) NewMnemonic() (string, error) {
// CreateKey generates and stores a new key in the keyring with the provided name, mnemonic, and options.
// If no mnemonic is provided, it generates a new one.
// Returns the mnemonic, the created key record, and any error encountered.
func (c *Client) CreateKey(name, mnemonic, bip39Pass string, coinType, account, index uint32) (s string, k *keyring.Record, err error) {
func (c *BaseClient) CreateKey(name, mnemonic, bip39Pass string, coinType, account, index uint32) (s string, k *keyring.Record, err error) {
// Generate a new mnemonic if none is provided.
if mnemonic == "" {
mnemonic, err = c.NewMnemonic()
Expand Down
8 changes: 4 additions & 4 deletions client/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

// Lease retrieves details of a specific lease by ID.
// Returns the lease information and any error encountered.
func (c *Client) Lease(ctx context.Context, id uint64) (res *v1.Lease, err error) {
func (c *BaseClient) Lease(ctx context.Context, id uint64) (res *v1.Lease, err error) {
var (
resp v1.QueryLeaseResponse
req = &v1.QueryLeaseRequest{Id: id}
Expand All @@ -34,7 +34,7 @@ func (c *Client) Lease(ctx context.Context, id uint64) (res *v1.Lease, err error

// Leases retrieves a paginated list of all leases.
// Returns the leases, pagination details, and any error encountered.
func (c *Client) Leases(ctx context.Context, pageReq *query.PageRequest) (res []v1.Lease, pageRes *query.PageResponse, err error) {
func (c *BaseClient) Leases(ctx context.Context, pageReq *query.PageRequest) (res []v1.Lease, pageRes *query.PageResponse, err error) {
var (
resp v1.QueryLeasesResponse
req = &v1.QueryLeasesRequest{Pagination: pageReq}
Expand All @@ -50,7 +50,7 @@ func (c *Client) Leases(ctx context.Context, pageReq *query.PageRequest) (res []

// LeasesForNode retrieves leases associated with a specific node address.
// Returns the leases, pagination details, and any error encountered.
func (c *Client) LeasesForNode(ctx context.Context, nodeAddr types.NodeAddress, pageReq *query.PageRequest) (res []v1.Lease, pageRes *query.PageResponse, err error) {
func (c *BaseClient) LeasesForNode(ctx context.Context, nodeAddr types.NodeAddress, pageReq *query.PageRequest) (res []v1.Lease, pageRes *query.PageResponse, err error) {
var (
resp v1.QueryLeasesForNodeResponse
req = &v1.QueryLeasesForNodeRequest{
Expand All @@ -69,7 +69,7 @@ func (c *Client) LeasesForNode(ctx context.Context, nodeAddr types.NodeAddress,

// LeasesForProvider retrieves leases associated with a specific provider address.
// Returns the leases, pagination details, and any error encountered.
func (c *Client) LeasesForProvider(ctx context.Context, provAddr types.ProvAddress, pageReq *query.PageRequest) (res []v1.Lease, pageRes *query.PageResponse, err error) {
func (c *BaseClient) LeasesForProvider(ctx context.Context, provAddr types.ProvAddress, pageReq *query.PageRequest) (res []v1.Lease, pageRes *query.PageResponse, err error) {
var (
resp v1.QueryLeasesForProviderResponse
req = &v1.QueryLeasesForProviderRequest{
Expand Down
Loading

0 comments on commit 2a8ab24

Please sign in to comment.