Skip to content

Commit e2097ca

Browse files
authored
DS-1596 Add support for V13. (#46)
This PR adds support for the new "Best Bid/Ask" stream type. Jira story: https://smartcontract-it.atlassian.net/browse/DS-1246
2 parents 2326e6f + b4c2bbd commit e2097ca

File tree

29 files changed

+661
-3495
lines changed

29 files changed

+661
-3495
lines changed

go/feed/feed.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
FeedVersion8
2222
FeedVersion9
2323
FeedVersion10
24+
FeedVersion13
2425
_
2526
)
2627

go/report/report.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55

66
"github.com/ethereum/go-ethereum/accounts/abi"
7+
78
v1 "github.com/smartcontractkit/data-streams-sdk/go/report/v1"
89
v10 "github.com/smartcontractkit/data-streams-sdk/go/report/v10"
10+
v13 "github.com/smartcontractkit/data-streams-sdk/go/report/v13"
911
v2 "github.com/smartcontractkit/data-streams-sdk/go/report/v2"
1012
v3 "github.com/smartcontractkit/data-streams-sdk/go/report/v3"
1113
v4 "github.com/smartcontractkit/data-streams-sdk/go/report/v4"
@@ -18,7 +20,7 @@ import (
1820

1921
// Data represents the actual report data and attributes
2022
type Data interface {
21-
v1.Data | v2.Data | v3.Data | v4.Data | v5.Data | v6.Data | v7.Data | v8.Data | v9.Data | v10.Data
23+
v1.Data | v2.Data | v3.Data | v4.Data | v5.Data | v6.Data | v7.Data | v8.Data | v9.Data | v10.Data | v13.Data
2224
Schema() abi.Arguments
2325
}
2426

go/report/report_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"time"
99

1010
"github.com/ethereum/go-ethereum/accounts/abi"
11+
1112
v1 "github.com/smartcontractkit/data-streams-sdk/go/report/v1"
1213
v10 "github.com/smartcontractkit/data-streams-sdk/go/report/v10"
14+
v13 "github.com/smartcontractkit/data-streams-sdk/go/report/v13"
1315
v2 "github.com/smartcontractkit/data-streams-sdk/go/report/v2"
1416
v3 "github.com/smartcontractkit/data-streams-sdk/go/report/v3"
1517
v4 "github.com/smartcontractkit/data-streams-sdk/go/report/v4"
@@ -160,6 +162,20 @@ func TestReport(t *testing.T) {
160162
if !reflect.DeepEqual(v10Report, rv10) {
161163
t.Errorf("expected: %#v, got: %#v", v10Report, rv10)
162164
}
165+
166+
b, err = schema.Pack(v13Report.ReportContext, v13Report.ReportBlob, v13Report.RawRs, v13Report.RawSs, v13Report.RawVs)
167+
if err != nil {
168+
t.Errorf("failed to encode report: %s", err)
169+
}
170+
171+
rv13, err := Decode[v13.Data](b)
172+
if err != nil {
173+
t.Errorf("failed to decode report: %s", err)
174+
}
175+
176+
if !reflect.DeepEqual(v13Report, rv13) {
177+
t.Errorf("expected: %#v, got: %#v", v13Report, rv13)
178+
}
163179
}
164180

165181
var v1Report = &Report[v1.Data]{
@@ -252,6 +268,15 @@ var v10Report = &Report[v10.Data]{
252268
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},
253269
}
254270

271+
var v13Report = &Report[v13.Data]{
272+
Data: v13Data,
273+
ReportContext: [3][32]uint8{},
274+
ReportBlob: mustPackData(v13Data),
275+
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}},
276+
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}},
277+
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},
278+
}
279+
255280
var v1Data = v1.Data{
256281
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},
257282
ObservationsTimestamp: uint32(time.Now().Unix()),
@@ -374,6 +399,20 @@ var v10Data = v10.Data{
374399
TokenizedPrice: big.NewInt(1001),
375400
}
376401

402+
var v13Data = v13.Data{
403+
FeedID: [32]uint8{00, 13, 19, 169, 185, 197, 227, 122, 9, 159, 55, 78, 146, 195, 121, 20, 175, 92, 38, 143, 58, 138, 151, 33, 241, 114, 81, 53, 191, 180, 203, 184},
404+
ValidFromTimestamp: uint32(time.Now().Unix()),
405+
ObservationsTimestamp: uint32(time.Now().Unix()),
406+
NativeFee: big.NewInt(10),
407+
LinkFee: big.NewInt(10),
408+
ExpiresAt: uint32(time.Now().Unix()) + 100,
409+
BestAsk: big.NewInt(75),
410+
BestBid: big.NewInt(78),
411+
AskVolume: 10000,
412+
BidVolume: 11000,
413+
LastTradedPrice: big.NewInt(76),
414+
}
415+
377416
func mustPackData(d interface{}) []byte {
378417
var args []interface{}
379418
var dataSchema abi.Arguments
@@ -511,6 +550,21 @@ func mustPackData(d interface{}) []byte {
511550
v.ActivationDateTime,
512551
v.TokenizedPrice,
513552
}
553+
case v13.Data:
554+
dataSchema = v13.Schema()
555+
args = []interface{}{
556+
v.FeedID,
557+
v.ValidFromTimestamp,
558+
v.ObservationsTimestamp,
559+
v.NativeFee,
560+
v.LinkFee,
561+
v.ExpiresAt,
562+
v.BestAsk,
563+
v.BestBid,
564+
v.AskVolume,
565+
v.BidVolume,
566+
v.LastTradedPrice,
567+
}
514568
default:
515569
panic(fmt.Sprintf("invalid type to pack: %#v", v))
516570
}

go/report/v13/data.go

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

go/report/v13/data_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package v13
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{0, 13, 19, 169, 185, 197, 227, 122, 9, 159, 55, 78, 146, 195, 121, 20, 175, 92, 38, 143, 58, 138, 151, 33, 241, 114, 81, 53, 191, 180, 203, 184},
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+
BestAsk: big.NewInt(105),
19+
BestBid: big.NewInt(101),
20+
AskVolume: 10001,
21+
BidVolume: 10002,
22+
LastTradedPrice: big.NewInt(103),
23+
}
24+
25+
b, err := schema.Pack(
26+
r.FeedID,
27+
r.ValidFromTimestamp,
28+
r.ObservationsTimestamp,
29+
r.NativeFee,
30+
r.LinkFee,
31+
r.ExpiresAt,
32+
r.BestAsk,
33+
r.BestBid,
34+
r.AskVolume,
35+
r.BidVolume,
36+
r.LastTradedPrice,
37+
)
38+
39+
if err != nil {
40+
t.Errorf("failed to serialize report: %s", err)
41+
}
42+
43+
d, err := Decode(b)
44+
if err != nil {
45+
t.Errorf("failed to deserialize report: %s", err)
46+
}
47+
48+
if !reflect.DeepEqual(r, d) {
49+
t.Errorf("expected: %#v, got %#v", r, d)
50+
}
51+
}

rust/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Add the following to your `Cargo.toml`:
2020

2121
```toml
2222
[dependencies]
23-
chainlink-data-streams-report = "1.0.3"
24-
chainlink-data-streams-sdk = { version = "1.0.3", features = ["full"] }
23+
chainlink-data-streams-report = "1.1.0"
24+
chainlink-data-streams-sdk = { version = "1.1.0", features = ["full"] }
2525
```
2626

2727
#### Features

rust/crates/report/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chainlink-data-streams-report"
3-
version = "1.0.3"
3+
version = "1.1.0"
44
edition = "2021"
55
description = "Chainlink Data Streams Report"
66
license = "MIT"

0 commit comments

Comments
 (0)