-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomposed_stream.go
More file actions
183 lines (156 loc) · 5.01 KB
/
composed_stream.go
File metadata and controls
183 lines (156 loc) · 5.01 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
178
179
180
181
182
183
package contractsapi
import (
"context"
"strconv"
"github.com/pkg/errors"
kwiltypes "github.com/trufnetwork/kwil-db/core/types"
"github.com/trufnetwork/sdk-go/core/types"
"github.com/trufnetwork/sdk-go/core/util"
)
type ComposedAction struct {
Action
}
var _ types.IComposedAction = (*ComposedAction)(nil)
var (
ErrorStreamNotComposed = errors.New("stream is not a composed stream")
)
func ComposedStreamFromStream(stream Action) (*ComposedAction, error) {
return &ComposedAction{
Action: stream,
}, nil
}
func LoadComposedActions(opts NewActionOptions) (*ComposedAction, error) {
stream, err := LoadAction(opts)
if err != nil {
return nil, errors.WithStack(err)
}
return ComposedStreamFromStream(*stream)
}
// CheckValidComposedStream checks if the stream is a valid composed stream
// and returns an error if it is not. Valid means:
// - the stream is initialized
// - the stream is a composed stream
func (c *ComposedAction) CheckValidComposedStream(ctx context.Context, locator types.StreamLocator) error {
// then check if is composed
streamType, err := c.GetType(ctx, locator)
if err != nil {
return errors.WithStack(err)
}
if streamType != types.StreamTypeComposed {
return ErrorStreamNotComposed
}
return nil
}
type DescribeTaxonomiesResult struct {
DataProvider string `json:"data_provider"`
StreamId string `json:"stream_id"`
ChildDataProvider string `json:"child_data_provider"`
ChildStreamId string `json:"child_stream_id"`
// decimals are received as strings by kwil to avoid precision loss
// as decimal are more arbitrary than golang's float64
Weight string `json:"weight"`
CreatedAt string `json:"created_at"`
GroupSequence string `json:"group_sequence"`
StartDate string `json:"start_date"`
}
func (c *ComposedAction) DescribeTaxonomies(ctx context.Context, params types.DescribeTaxonomiesParams) (types.Taxonomy, error) {
records, err := c.call(ctx, "describe_taxonomies", []any{
params.Stream.DataProvider.Address(),
params.Stream.StreamId.String(),
params.LatestVersion})
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
result, err := DecodeCallResult[DescribeTaxonomiesResult](records)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
var taxonomyItems []types.TaxonomyItem
for _, r := range result {
dpAddress, err := util.NewEthereumAddressFromString(r.ChildDataProvider)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
weight, err := strconv.ParseFloat(r.Weight, 64)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
childStreamId, err := util.NewStreamId(r.ChildStreamId)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
taxonomyItems = append(taxonomyItems, types.TaxonomyItem{
ChildStream: types.StreamLocator{
StreamId: *childStreamId,
DataProvider: dpAddress,
},
Weight: weight,
})
}
var (
startDate *int
createdAt int
groupSequence int
)
if len(result) > 0 {
if result[0].StartDate != "" {
startDateInt, err := strconv.Atoi(result[0].StartDate)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
startDate = &startDateInt
}
if result[0].CreatedAt != "" {
createdAtInt, err := strconv.Atoi(result[0].CreatedAt)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
createdAt = createdAtInt
}
if result[0].GroupSequence != "" {
groupSequenceInt, err := strconv.Atoi(result[0].GroupSequence)
if err != nil {
return types.Taxonomy{}, errors.WithStack(err)
}
groupSequence = groupSequenceInt
}
}
return types.Taxonomy{
ParentStream: types.StreamLocator{StreamId: params.Stream.StreamId, DataProvider: params.Stream.DataProvider},
TaxonomyItems: taxonomyItems,
CreatedAt: createdAt,
GroupSequence: groupSequence,
StartDate: startDate,
}, nil
}
func (c *ComposedAction) InsertTaxonomy(ctx context.Context, taxonomies types.Taxonomy) (kwiltypes.Hash, error) {
var (
childDataProviders []string
childStreamIDs util.StreamIdSlice
weights kwiltypes.DecimalArray
startDate int
)
//parentDataProviderHexString := taxonomies.ParentStream.DataProvider.Address()
// kwil expects no 0x prefix
//parentDataProviderHex := parentDataProviderHexString[2:]
for _, taxonomy := range taxonomies.TaxonomyItems {
childDataProviders = append(childDataProviders, taxonomy.ChildStream.DataProvider.Address())
childStreamIDs = append(childStreamIDs, taxonomy.ChildStream.StreamId)
weightNumeric, err := kwiltypes.ParseDecimalExplicit(strconv.FormatFloat(taxonomy.Weight, 'f', -1, 64), 36, 18)
if err != nil {
return kwiltypes.Hash{}, errors.WithStack(err)
}
weights = append(weights, weightNumeric)
}
if taxonomies.StartDate != nil {
startDate = *taxonomies.StartDate
}
return c.execute(ctx, "insert_taxonomy", [][]any{{
taxonomies.ParentStream.DataProvider.Address(),
taxonomies.ParentStream.StreamId.String(),
childDataProviders,
childStreamIDs.Strings(),
weights,
startDate,
}})
}