Skip to content

Commit 29ae48c

Browse files
authored
Go: Add FeedVersion10 (#25)
This PR adds FeedVersion10 to Go version of the Chainlink Data Streams SDK
1 parent 5d545e9 commit 29ae48c

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

go/feed/feed.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
FeedVersion4
1818
FeedVersion8
1919
FeedVersion9
20+
FeedVersion10
2021
_
2122
)
2223

go/report/report.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/ethereum/go-ethereum/accounts/abi"
77
v1 "github.com/smartcontractkit/data-streams-sdk/go/report/v1"
8+
v10 "github.com/smartcontractkit/data-streams-sdk/go/report/v10"
89
v2 "github.com/smartcontractkit/data-streams-sdk/go/report/v2"
910
v3 "github.com/smartcontractkit/data-streams-sdk/go/report/v3"
1011
v4 "github.com/smartcontractkit/data-streams-sdk/go/report/v4"
@@ -14,7 +15,7 @@ import (
1415

1516
// Data represents the actual report data and attributes
1617
type Data interface {
17-
v1.Data | v2.Data | v3.Data | v4.Data | v8.Data | v9.Data
18+
v1.Data | v2.Data | v3.Data | v4.Data | v8.Data | v9.Data | v10.Data
1819
Schema() abi.Arguments
1920
}
2021

go/report/v10/data.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package v10
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: "price", Type: mustNewType("int192")},
31+
{Name: "marketStatus", Type: mustNewType("uint32")},
32+
{Name: "currentMultiplier", Type: mustNewType("int192")},
33+
{Name: "newMultiplier", Type: mustNewType("int192")},
34+
{Name: "activationDateTime", Type: mustNewType("uint32")},
35+
{Name: "tokenizedPrice", Type: mustNewType("int192")},
36+
})
37+
}
38+
39+
// Data is the container for this schema attributes
40+
type Data struct {
41+
FeedID feed.ID `abi:"feedId"`
42+
ObservationsTimestamp uint32
43+
ValidFromTimestamp uint32
44+
ExpiresAt uint32
45+
LinkFee *big.Int
46+
NativeFee *big.Int
47+
LastUpdateTimestamp uint64
48+
Price *big.Int
49+
MarketStatus uint32
50+
CurrentMultiplier *big.Int
51+
NewMultiplier *big.Int
52+
ActivationDateTime uint32
53+
TokenizedPrice *big.Int
54+
}
55+
56+
// Schema returns this data version schema
57+
func (Data) Schema() abi.Arguments {
58+
return Schema()
59+
}
60+
61+
// Decode decodes the serialized data bytes
62+
func Decode(data []byte) (*Data, error) {
63+
values, err := schema.Unpack(data)
64+
if err != nil {
65+
return nil, fmt.Errorf("failed to decode report: %w", err)
66+
}
67+
decoded := new(Data)
68+
if err = schema.Copy(decoded, values); err != nil {
69+
return nil, fmt.Errorf("failed to copy report values to struct: %w", err)
70+
}
71+
return decoded, nil
72+
}

go/report/v10/data_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package v10
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, 10, 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()) - 100,
19+
Price: big.NewInt(1100),
20+
MarketStatus: 1,
21+
CurrentMultiplier: big.NewInt(1000),
22+
NewMultiplier: big.NewInt(1050),
23+
ActivationDateTime: uint32(time.Now().Unix()) + 200,
24+
TokenizedPrice: big.NewInt(11009),
25+
}
26+
27+
b, err := schema.Pack(
28+
r.FeedID,
29+
r.ValidFromTimestamp,
30+
r.ObservationsTimestamp,
31+
r.NativeFee,
32+
r.LinkFee,
33+
r.ExpiresAt,
34+
r.LastUpdateTimestamp,
35+
r.Price,
36+
r.MarketStatus,
37+
r.CurrentMultiplier,
38+
r.NewMultiplier,
39+
r.ActivationDateTime,
40+
r.TokenizedPrice,
41+
)
42+
43+
if err != nil {
44+
t.Errorf("failed to serialize report: %s", err)
45+
}
46+
47+
d, err := Decode(b)
48+
if err != nil {
49+
t.Errorf("failed to deserialize report: %s", err)
50+
}
51+
52+
if !reflect.DeepEqual(r, d) {
53+
t.Errorf("expected: %#v, got %#v", r, d)
54+
}
55+
}

0 commit comments

Comments
 (0)