Skip to content

Commit 2d0c274

Browse files
committed
go: add support for v8 schema
1 parent a7efa82 commit 2d0c274

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

go/report/report.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ 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"
1112
)
1213

1314
// Data represents the actual report data and attributes
1415
type Data interface {
15-
v1.Data | v2.Data | v3.Data | v4.Data
16+
v1.Data | v2.Data | v3.Data | v4.Data | v8.Data
1617
Schema() abi.Arguments
1718
}
1819

go/report/report_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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"
1516
)
1617

1718
func TestReport(t *testing.T) {
@@ -70,6 +71,20 @@ func TestReport(t *testing.T) {
7071
if !reflect.DeepEqual(v4Report, rv4) {
7172
t.Errorf("expected: %#v, got: %#v", v4Report, rv4)
7273
}
74+
75+
b, err = schema.Pack(v8Report.ReportContext, v8Report.ReportBlob, v8Report.RawRs, v8Report.RawSs, v8Report.RawVs)
76+
if err != nil {
77+
t.Errorf("failed to encode report: %s", err)
78+
}
79+
80+
rv8, err := Decode[v8.Data](b)
81+
if err != nil {
82+
t.Errorf("failed to decode report: %s", err)
83+
}
84+
85+
if !reflect.DeepEqual(v8Report, rv8) {
86+
t.Errorf("expected: %#v, got: %#v", v8Report, rv8)
87+
}
7388
}
7489

7590
var v1Report = &Report[v1.Data]{
@@ -108,6 +123,15 @@ var v4Report = &Report[v4.Data]{
108123
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},
109124
}
110125

126+
var v8Report = &Report[v8.Data]{
127+
Data: v8Data,
128+
ReportContext: [3][32]uint8{},
129+
ReportBlob: mustPackData(v8Data),
130+
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}},
131+
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}},
132+
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},
133+
}
134+
111135
var v1Data = v1.Data{
112136
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},
113137
ObservationsTimestamp: uint32(time.Now().Unix()),
@@ -153,6 +177,18 @@ var v4Data = v4.Data{
153177
MarketStatus: v4.MarketStatusOpen,
154178
}
155179

180+
var v8Data = v8.Data{
181+
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},
182+
ValidFromTimestamp: uint32(time.Now().Unix()),
183+
ObservationsTimestamp: uint32(time.Now().Unix()),
184+
NativeFee: big.NewInt(10),
185+
LinkFee: big.NewInt(10),
186+
ExpiresAt: uint32(time.Now().Unix()) + 100,
187+
LastUpdateTimestamp: uint64(time.Now().UnixNano() - int64(10*time.Second)),
188+
MidPrice: big.NewInt(100),
189+
MarketStatus: 1,
190+
}
191+
156192
func mustPackData(d interface{}) []byte {
157193
var args []interface{}
158194
var dataSchema abi.Arguments
@@ -207,6 +243,19 @@ func mustPackData(d interface{}) []byte {
207243
v.BenchmarkPrice,
208244
v.MarketStatus,
209245
}
246+
case v8.Data:
247+
dataSchema = v8.Schema()
248+
args = []interface{}{
249+
v.FeedID,
250+
v.ValidFromTimestamp,
251+
v.ObservationsTimestamp,
252+
v.NativeFee,
253+
v.LinkFee,
254+
v.ExpiresAt,
255+
v.LastUpdateTimestamp,
256+
v.MidPrice,
257+
v.MarketStatus,
258+
}
210259
default:
211260
panic(fmt.Sprintf("invalid type to pack: %#v", v))
212261
}

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+
}

0 commit comments

Comments
 (0)