-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.go
More file actions
177 lines (150 loc) · 5.42 KB
/
client.go
File metadata and controls
177 lines (150 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package tnclient
import (
"context"
"time"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
kwilClientType "github.com/trufnetwork/kwil-db/core/client/types"
"github.com/trufnetwork/kwil-db/core/crypto/auth"
"github.com/trufnetwork/kwil-db/core/gatewayclient"
"github.com/trufnetwork/kwil-db/core/log"
kwilType "github.com/trufnetwork/kwil-db/core/types"
"github.com/trufnetwork/kwil-db/node/types"
tn_api "github.com/trufnetwork/sdk-go/core/contractsapi"
"github.com/trufnetwork/sdk-go/core/logging"
clientType "github.com/trufnetwork/sdk-go/core/types"
"github.com/trufnetwork/sdk-go/core/util"
"go.uber.org/zap"
)
type Client struct {
Signer auth.Signer `validate:"required"`
logger *log.Logger
kwilClient *gatewayclient.GatewayClient `validate:"required"`
kwilOptions *gatewayclient.GatewayOptions
}
var _ clientType.Client = (*Client)(nil)
type Option func(*Client)
func NewClient(ctx context.Context, provider string, options ...Option) (*Client, error) {
c := &Client{}
c.kwilOptions = &gatewayclient.GatewayOptions{
Options: *kwilClientType.DefaultOptions(),
}
for _, option := range options {
option(c)
}
kwilClient, err := gatewayclient.NewClient(ctx, provider, c.kwilOptions)
if err != nil {
return nil, errors.WithStack(err)
}
c.kwilClient = kwilClient
// Validate the client
if err = c.Validate(); err != nil {
return nil, errors.WithStack(err)
}
return c, nil
}
func (c *Client) Validate() error {
validate := validator.New()
return validate.Struct(c)
}
func WithSigner(signer auth.Signer) Option {
return func(c *Client) {
c.kwilOptions.Signer = signer
c.Signer = signer
}
}
func WithLogger(logger log.Logger) Option {
return func(c *Client) {
c.logger = &logger
c.kwilOptions.Logger = logger
}
}
func (c *Client) GetSigner() auth.Signer {
return c.kwilClient.Signer()
}
func (c *Client) WaitForTx(ctx context.Context, txHash kwilType.Hash, interval time.Duration) (*kwilType.TxQueryResponse, error) {
return c.kwilClient.WaitTx(ctx, txHash, interval)
}
func (c *Client) GetKwilClient() *gatewayclient.GatewayClient {
return c.kwilClient
}
func (c *Client) DeployStream(ctx context.Context, streamId util.StreamId, streamType clientType.StreamType) (types.Hash, error) {
return tn_api.DeployStream(ctx, tn_api.DeployStreamInput{
StreamId: streamId,
StreamType: streamType,
KwilClient: c.GetKwilClient(),
})
}
func (c *Client) DestroyStream(ctx context.Context, streamId util.StreamId) (types.Hash, error) {
return tn_api.DestroyStream(ctx, tn_api.DestroyStreamInput{
StreamId: streamId,
KwilClient: c.GetKwilClient(),
})
}
func (c *Client) LoadActions() (clientType.IAction, error) {
return tn_api.LoadAction(tn_api.NewActionOptions{
Client: c.kwilClient,
})
}
func (c *Client) LoadPrimitiveActions() (clientType.IPrimitiveAction, error) {
return tn_api.LoadPrimitiveActions(tn_api.NewActionOptions{
Client: c.kwilClient,
})
}
func (c *Client) LoadComposedActions() (clientType.IComposedAction, error) {
return tn_api.LoadComposedActions(tn_api.NewActionOptions{
Client: c.kwilClient,
})
}
func (c *Client) LoadRoleManagementActions() (clientType.IRoleManagement, error) {
return tn_api.LoadRoleManagementActions(tn_api.NewRoleManagementOptions{
Client: c.kwilClient,
})
}
func (c *Client) OwnStreamLocator(streamId util.StreamId) clientType.StreamLocator {
return clientType.StreamLocator{
StreamId: streamId,
DataProvider: c.Address(),
}
}
func (c *Client) Address() util.EthereumAddress {
addr, err := auth.EthSecp256k1Authenticator{}.Identifier(c.kwilClient.Signer().CompactID())
if err != nil {
// should never happen
logging.Logger.Panic("failed to get address from signer", zap.Error(err))
}
address, err := util.NewEthereumAddressFromString(addr)
if err != nil {
logging.Logger.Panic("failed to create address from string", zap.Error(err))
}
return address
}
// BatchDeployStreams deploys multiple streams (primitive and composed).
// It returns the transaction hash of the batch operation.
func (c *Client) BatchDeployStreams(ctx context.Context, streamDefs []clientType.StreamDefinition) (kwilType.Hash, error) {
// Assuming SchemaName for "create_streams" is obtained from somewhere or is a known constant.
// For now, using an empty string as a placeholder if it's a root/global procedure.
schemaName := "" // Or c.config.SchemaName, etc.
return tn_api.BatchDeployStreams(ctx, tn_api.BatchDeployStreamsInput{
KwilClient: c.GetKwilClient(),
Definitions: streamDefs,
SchemaName: schemaName,
})
}
// BatchStreamExists checks for the existence of multiple streams.
func (c *Client) BatchStreamExists(ctx context.Context, streams []clientType.StreamLocator) ([]clientType.StreamExistsResult, error) {
actions, err := c.LoadActions()
if err != nil {
return nil, errors.Wrap(err, "failed to load actions for BatchStreamExists")
}
return actions.BatchStreamExists(ctx, streams)
}
// BatchFilterStreamsByExistence filters a list of streams based on their existence in the database.
// Use this instead of BatchStreamExists if you want less data returned.
func (c *Client) BatchFilterStreamsByExistence(ctx context.Context, streams []clientType.StreamLocator, returnExisting bool) ([]clientType.StreamLocator, error) {
actions, err := c.LoadActions()
if err != nil {
return nil, errors.Wrap(err, "failed to load actions for BatchFilterStreamsByExistence")
}
return actions.BatchFilterStreamsByExistence(ctx, streams, returnExisting)
}