Skip to content

Commit b98e01d

Browse files
authored
Add V8 and V9 schemas to Go sdk. (#19)
2 parents a7efa82 + 1bc1a46 commit b98e01d

File tree

6 files changed

+335
-1
lines changed

6 files changed

+335
-1
lines changed

go/report/report.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
v2 "github.com/smartcontractkit/data-streams-sdk/go/report/v2"
99
v3 "github.com/smartcontractkit/data-streams-sdk/go/report/v3"
1010
v4 "github.com/smartcontractkit/data-streams-sdk/go/report/v4"
11+
v8 "github.com/smartcontractkit/data-streams-sdk/go/report/v8"
12+
v9 "github.com/smartcontractkit/data-streams-sdk/go/report/v9"
1113
)
1214

1315
// Data represents the actual report data and attributes
1416
type Data interface {
15-
v1.Data | v2.Data | v3.Data | v4.Data
17+
v1.Data | v2.Data | v3.Data | v4.Data | v8.Data | v9.Data
1618
Schema() abi.Arguments
1719
}
1820

go/report/report_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
v2 "github.com/smartcontractkit/data-streams-sdk/go/report/v2"
1313
v3 "github.com/smartcontractkit/data-streams-sdk/go/report/v3"
1414
v4 "github.com/smartcontractkit/data-streams-sdk/go/report/v4"
15+
v8 "github.com/smartcontractkit/data-streams-sdk/go/report/v8"
16+
v9 "github.com/smartcontractkit/data-streams-sdk/go/report/v9"
1517
)
1618

1719
func TestReport(t *testing.T) {
@@ -70,6 +72,34 @@ func TestReport(t *testing.T) {
7072
if !reflect.DeepEqual(v4Report, rv4) {
7173
t.Errorf("expected: %#v, got: %#v", v4Report, rv4)
7274
}
75+
76+
b, err = schema.Pack(v8Report.ReportContext, v8Report.ReportBlob, v8Report.RawRs, v8Report.RawSs, v8Report.RawVs)
77+
if err != nil {
78+
t.Errorf("failed to encode report: %s", err)
79+
}
80+
81+
rv8, err := Decode[v8.Data](b)
82+
if err != nil {
83+
t.Errorf("failed to decode report: %s", err)
84+
}
85+
86+
if !reflect.DeepEqual(v8Report, rv8) {
87+
t.Errorf("expected: %#v, got: %#v", v8Report, rv8)
88+
}
89+
90+
b, err = schema.Pack(v9Report.ReportContext, v9Report.ReportBlob, v9Report.RawRs, v9Report.RawSs, v9Report.RawVs)
91+
if err != nil {
92+
t.Errorf("failed to encode report: %s", err)
93+
}
94+
95+
rv9, err := Decode[v9.Data](b)
96+
if err != nil {
97+
t.Errorf("failed to decode report: %s", err)
98+
}
99+
100+
if !reflect.DeepEqual(v8Report, rv8) {
101+
t.Errorf("expected: %#v, got: %#v", v9Report, rv9)
102+
}
73103
}
74104

75105
var v1Report = &Report[v1.Data]{
@@ -108,6 +138,24 @@ var v4Report = &Report[v4.Data]{
108138
RawVs: [32]uint8{00, 01, 10, 74, 67, 29, 24, 17, 12, 18, 22, 11, 69, 11, 63, 86, 12, 86, 23, 58, 13, 53, 29, 12, 17, 10, 17, 12, 63, 27, 12, 14},
109139
}
110140

141+
var v8Report = &Report[v8.Data]{
142+
Data: v8Data,
143+
ReportContext: [3][32]uint8{},
144+
ReportBlob: mustPackData(v8Data),
145+
RawRs: [][32]uint8{{00, 01, 10, 74, 67, 29, 24, 17, 12, 18, 22, 11, 69, 11, 63, 86, 12, 86, 23, 58, 13, 53, 29, 12, 17, 10, 17, 12, 63, 27, 12, 14}},
146+
RawSs: [][32]uint8{{01, 02, 10, 73, 65, 19, 14, 27, 42, 48, 52, 18, 39, 116, 67, 85, 13, 82, 33, 48, 23, 33, 49, 32, 67, 50, 37, 32, 63, 77, 14, 64}},
147+
RawVs: [32]uint8{00, 01, 10, 74, 67, 29, 24, 17, 12, 18, 22, 11, 69, 11, 63, 86, 12, 86, 23, 58, 13, 53, 29, 12, 17, 10, 17, 12, 63, 27, 12, 14},
148+
}
149+
150+
var v9Report = &Report[v9.Data]{
151+
Data: v9Data,
152+
ReportContext: [3][32]uint8{},
153+
ReportBlob: mustPackData(v9Data),
154+
RawRs: [][32]uint8{{00, 01, 10, 74, 67, 29, 24, 17, 12, 18, 22, 11, 69, 11, 63, 86, 12, 86, 23, 58, 13, 53, 29, 12, 17, 10, 17, 12, 63, 27, 12, 14}},
155+
RawSs: [][32]uint8{{01, 02, 10, 73, 65, 19, 14, 27, 42, 48, 52, 18, 39, 116, 67, 85, 13, 82, 33, 48, 23, 33, 49, 32, 67, 50, 37, 32, 63, 77, 14, 64}},
156+
RawVs: [32]uint8{00, 01, 10, 74, 67, 29, 24, 17, 12, 18, 22, 11, 69, 11, 63, 86, 12, 86, 23, 58, 13, 53, 29, 12, 17, 10, 17, 12, 63, 27, 12, 14},
157+
}
158+
111159
var v1Data = v1.Data{
112160
FeedID: [32]uint8{00, 01, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58, 163, 53, 239, 127, 174, 105, 107, 102, 63, 27, 132, 114},
113161
ObservationsTimestamp: uint32(time.Now().Unix()),
@@ -153,6 +201,31 @@ var v4Data = v4.Data{
153201
MarketStatus: v4.MarketStatusOpen,
154202
}
155203

204+
var v8Data = v8.Data{
205+
FeedID: [32]uint8{00, 8, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58, 163, 53, 239, 127, 174, 105, 107, 102, 63, 27, 132, 114},
206+
ValidFromTimestamp: uint32(time.Now().Unix()),
207+
ObservationsTimestamp: uint32(time.Now().Unix()),
208+
NativeFee: big.NewInt(10),
209+
LinkFee: big.NewInt(10),
210+
ExpiresAt: uint32(time.Now().Unix()) + 100,
211+
LastUpdateTimestamp: uint64(time.Now().UnixNano() - int64(10*time.Second)),
212+
MidPrice: big.NewInt(100),
213+
MarketStatus: 1,
214+
}
215+
216+
var v9Data = v9.Data{
217+
FeedID: [32]uint8{00, 9, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58, 163, 53, 239, 127, 174, 105, 107, 102, 63, 27, 132, 114},
218+
ValidFromTimestamp: uint32(time.Now().Unix()),
219+
ObservationsTimestamp: uint32(time.Now().Unix()),
220+
NativeFee: big.NewInt(10),
221+
LinkFee: big.NewInt(10),
222+
ExpiresAt: uint32(time.Now().Unix()) + 100,
223+
NavPerShare: big.NewInt(1100),
224+
NavDate: uint64(time.Now().UnixNano()) - 100,
225+
Aum: big.NewInt(11009),
226+
Ripcord: 108,
227+
}
228+
156229
func mustPackData(d interface{}) []byte {
157230
var args []interface{}
158231
var dataSchema abi.Arguments
@@ -207,6 +280,33 @@ func mustPackData(d interface{}) []byte {
207280
v.BenchmarkPrice,
208281
v.MarketStatus,
209282
}
283+
case v8.Data:
284+
dataSchema = v8.Schema()
285+
args = []interface{}{
286+
v.FeedID,
287+
v.ValidFromTimestamp,
288+
v.ObservationsTimestamp,
289+
v.NativeFee,
290+
v.LinkFee,
291+
v.ExpiresAt,
292+
v.LastUpdateTimestamp,
293+
v.MidPrice,
294+
v.MarketStatus,
295+
}
296+
case v9.Data:
297+
dataSchema = v9.Schema()
298+
args = []interface{}{
299+
v.FeedID,
300+
v.ValidFromTimestamp,
301+
v.ObservationsTimestamp,
302+
v.NativeFee,
303+
v.LinkFee,
304+
v.ExpiresAt,
305+
v.NavPerShare,
306+
v.NavDate,
307+
v.Aum,
308+
v.Ripcord,
309+
}
210310
default:
211311
panic(fmt.Sprintf("invalid type to pack: %#v", v))
212312
}

go/report/v8/data.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package v8
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
7+
"github.com/ethereum/go-ethereum/accounts/abi"
8+
"github.com/smartcontractkit/data-streams-sdk/go/feed"
9+
)
10+
11+
var schema = Schema()
12+
13+
// Schema returns this data version schema
14+
func Schema() abi.Arguments {
15+
mustNewType := func(t string) abi.Type {
16+
result, err := abi.NewType(t, "", []abi.ArgumentMarshaling{})
17+
if err != nil {
18+
panic(fmt.Sprintf("Unexpected error during abi.NewType: %s", err))
19+
}
20+
return result
21+
}
22+
return abi.Arguments([]abi.Argument{
23+
{Name: "feedId", Type: mustNewType("bytes32")},
24+
{Name: "validFromTimestamp", Type: mustNewType("uint32")},
25+
{Name: "observationsTimestamp", Type: mustNewType("uint32")},
26+
{Name: "nativeFee", Type: mustNewType("uint192")},
27+
{Name: "linkFee", Type: mustNewType("uint192")},
28+
{Name: "expiresAt", Type: mustNewType("uint32")},
29+
{Name: "lastUpdateTimestamp", Type: mustNewType("uint64")},
30+
{Name: "midPrice", Type: mustNewType("int192")},
31+
{Name: "marketStatus", Type: mustNewType("uint32")},
32+
})
33+
}
34+
35+
const (
36+
MarketStatusUnknown uint32 = iota
37+
MarketStatusClosed
38+
MarketStatusOpen
39+
)
40+
41+
// Data is the container for this schema attributes
42+
type Data struct {
43+
FeedID feed.ID `abi:"feedId"`
44+
ObservationsTimestamp uint32
45+
MidPrice *big.Int
46+
MarketStatus uint32
47+
LastUpdateTimestamp uint64
48+
ValidFromTimestamp uint32
49+
ExpiresAt uint32
50+
LinkFee *big.Int
51+
NativeFee *big.Int
52+
}
53+
54+
// Schema returns this data version schema
55+
func (Data) Schema() abi.Arguments {
56+
return Schema()
57+
}
58+
59+
// Decode decodes the serialized data bytes
60+
func Decode(data []byte) (*Data, error) {
61+
values, err := schema.Unpack(data)
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to decode report: %w", err)
64+
}
65+
decoded := new(Data)
66+
if err = schema.Copy(decoded, values); err != nil {
67+
return nil, fmt.Errorf("failed to copy report values to struct: %w", err)
68+
}
69+
return decoded, nil
70+
}

go/report/v8/data_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package v8
2+
3+
import (
4+
"math/big"
5+
"reflect"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestData(t *testing.T) {
11+
r := &Data{
12+
FeedID: [32]uint8{00, 8, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58, 163, 53, 239, 127, 174, 105, 107, 102, 63, 27, 132, 114},
13+
ValidFromTimestamp: uint32(time.Now().Unix()),
14+
ObservationsTimestamp: uint32(time.Now().Unix()),
15+
NativeFee: big.NewInt(10),
16+
LinkFee: big.NewInt(10),
17+
ExpiresAt: uint32(time.Now().Unix()) + 100,
18+
LastUpdateTimestamp: uint64(time.Now().UnixNano() - int64(10*time.Second)),
19+
MidPrice: big.NewInt(100),
20+
MarketStatus: MarketStatusOpen,
21+
}
22+
23+
b, err := schema.Pack(
24+
r.FeedID,
25+
r.ValidFromTimestamp,
26+
r.ObservationsTimestamp,
27+
r.NativeFee,
28+
r.LinkFee,
29+
r.ExpiresAt,
30+
r.LastUpdateTimestamp,
31+
r.MidPrice,
32+
r.MarketStatus,
33+
)
34+
35+
if err != nil {
36+
t.Errorf("failed to serialize report: %s", err)
37+
}
38+
39+
d, err := Decode(b)
40+
if err != nil {
41+
t.Errorf("failed to deserialize report: %s", err)
42+
}
43+
44+
if !reflect.DeepEqual(r, d) {
45+
t.Errorf("expected: %#v, got %#v", r, d)
46+
}
47+
}

go/report/v9/data.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package v9
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
7+
"github.com/ethereum/go-ethereum/accounts/abi"
8+
"github.com/smartcontractkit/data-streams-sdk/go/feed"
9+
)
10+
11+
var schema = Schema()
12+
13+
// Schema returns this data version schema
14+
func Schema() abi.Arguments {
15+
mustNewType := func(t string) abi.Type {
16+
result, err := abi.NewType(t, "", []abi.ArgumentMarshaling{})
17+
if err != nil {
18+
panic(fmt.Sprintf("Unexpected error during abi.NewType: %s", err))
19+
}
20+
return result
21+
}
22+
return abi.Arguments([]abi.Argument{
23+
{Name: "feedId", Type: mustNewType("bytes32")},
24+
{Name: "validFromTimestamp", Type: mustNewType("uint32")},
25+
{Name: "observationsTimestamp", Type: mustNewType("uint32")},
26+
{Name: "nativeFee", Type: mustNewType("uint192")},
27+
{Name: "linkFee", Type: mustNewType("uint192")},
28+
{Name: "expiresAt", Type: mustNewType("uint32")},
29+
{Name: "navPerShare", Type: mustNewType("int192")},
30+
{Name: "navDate", Type: mustNewType("uint64")},
31+
{Name: "aum", Type: mustNewType("int192")},
32+
{Name: "ripcord", Type: mustNewType("uint32")},
33+
})
34+
}
35+
36+
// Data is the container for this schema attributes
37+
type Data struct {
38+
FeedID feed.ID `abi:"feedId"`
39+
ObservationsTimestamp uint32
40+
ValidFromTimestamp uint32
41+
ExpiresAt uint32
42+
LinkFee *big.Int
43+
NativeFee *big.Int
44+
NavPerShare *big.Int
45+
NavDate uint64
46+
Aum *big.Int
47+
Ripcord uint32
48+
}
49+
50+
// Schema returns this data version schema
51+
func (Data) Schema() abi.Arguments {
52+
return Schema()
53+
}
54+
55+
// Decode decodes the serialized data bytes
56+
func Decode(data []byte) (*Data, error) {
57+
values, err := schema.Unpack(data)
58+
if err != nil {
59+
return nil, fmt.Errorf("failed to decode report: %w", err)
60+
}
61+
decoded := new(Data)
62+
if err = schema.Copy(decoded, values); err != nil {
63+
return nil, fmt.Errorf("failed to copy report values to struct: %w", err)
64+
}
65+
return decoded, nil
66+
}

go/report/v9/data_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package v9
2+
3+
import (
4+
"math/big"
5+
"reflect"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestData(t *testing.T) {
11+
r := &Data{
12+
FeedID: [32]uint8{00, 9, 107, 74, 167, 229, 124, 167, 182, 138, 225, 191, 69, 101, 63, 86, 182, 86, 253, 58, 163, 53, 239, 127, 174, 105, 107, 102, 63, 27, 132, 114},
13+
ValidFromTimestamp: uint32(time.Now().Unix()),
14+
ObservationsTimestamp: uint32(time.Now().Unix()),
15+
NativeFee: big.NewInt(10),
16+
LinkFee: big.NewInt(10),
17+
ExpiresAt: uint32(time.Now().Unix()) + 100,
18+
NavPerShare: big.NewInt(1100),
19+
NavDate: uint64(time.Now().UnixNano()) - 100,
20+
Aum: big.NewInt(11009),
21+
Ripcord: 108,
22+
}
23+
24+
b, err := schema.Pack(
25+
r.FeedID,
26+
r.ValidFromTimestamp,
27+
r.ObservationsTimestamp,
28+
r.NativeFee,
29+
r.LinkFee,
30+
r.ExpiresAt,
31+
r.NavPerShare,
32+
r.NavDate,
33+
r.Aum,
34+
r.Ripcord,
35+
)
36+
37+
if err != nil {
38+
t.Errorf("failed to serialize report: %s", err)
39+
}
40+
41+
d, err := Decode(b)
42+
if err != nil {
43+
t.Errorf("failed to deserialize report: %s", err)
44+
}
45+
46+
if !reflect.DeepEqual(r, d) {
47+
t.Errorf("expected: %#v, got %#v", r, d)
48+
}
49+
}

0 commit comments

Comments
 (0)