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