0g-chain/client/grpc/client.go

51 lines
1.3 KiB
Go
Raw Normal View History

package grpc
import (
"errors"
2024-08-03 09:51:56 +00:00
"github.com/0glabs/0g-chain/client/grpc/query"
"github.com/0glabs/0g-chain/client/grpc/util"
)
2024-08-03 13:44:37 +00:00
// ZgChainGrpcClient enables the usage of 0gchaind grpc query clients and query utils
type ZgChainGrpcClient struct {
config ZgChainGrpcClientConfig
2024-08-03 13:44:37 +00:00
// Query clients for cosmos and 0gchaind modules
Query *query.QueryClient
// Utils for common queries (ie fetch an unpacked BaseAccount)
*util.Util
}
2024-08-03 13:44:37 +00:00
// ZgChainGrpcClientConfig is a configuration struct for a ZgChainGrpcClient
type ZgChainGrpcClientConfig struct {
// note: add future config options here
}
2024-08-03 13:44:37 +00:00
// NewClient creates a new ZgChainGrpcClient via a grpc url
func NewClient(grpcUrl string) (*ZgChainGrpcClient, error) {
return NewClientWithConfig(grpcUrl, NewDefaultConfig())
}
2024-08-03 13:44:37 +00:00
// NewClientWithConfig creates a new ZgChainGrpcClient via a grpc url and config
func NewClientWithConfig(grpcUrl string, config ZgChainGrpcClientConfig) (*ZgChainGrpcClient, error) {
if grpcUrl == "" {
return nil, errors.New("grpc url cannot be empty")
}
query, error := query.NewQueryClient(grpcUrl)
if error != nil {
return nil, error
}
2024-08-03 13:44:37 +00:00
client := &ZgChainGrpcClient{
Query: query,
Util: util.NewUtil(query),
config: config,
}
return client, nil
}
2024-08-03 13:44:37 +00:00
func NewDefaultConfig() ZgChainGrpcClientConfig {
return ZgChainGrpcClientConfig{}
}