-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
types.go
56 lines (50 loc) · 1.6 KB
/
types.go
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
package reporttypes
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
)
var schema = GetSchema()
func GetSchema() abi.Arguments {
mustNewType := func(t string) abi.Type {
result, err := abi.NewType(t, "", []abi.ArgumentMarshaling{})
if err != nil {
panic(fmt.Sprintf("Unexpected error during abi.NewType: %s", err))
}
return result
}
return abi.Arguments([]abi.Argument{
{Name: "feedId", Type: mustNewType("bytes32")},
{Name: "observationsTimestamp", Type: mustNewType("uint32")},
{Name: "benchmarkPrice", Type: mustNewType("int192")},
{Name: "bid", Type: mustNewType("int192")},
{Name: "ask", Type: mustNewType("int192")},
{Name: "currentBlockNum", Type: mustNewType("uint64")},
{Name: "currentBlockHash", Type: mustNewType("bytes32")},
{Name: "validFromBlockNum", Type: mustNewType("uint64")},
{Name: "currentBlockTimestamp", Type: mustNewType("uint64")},
})
}
type Report struct {
FeedId [32]byte
ObservationsTimestamp uint32
BenchmarkPrice *big.Int
Bid *big.Int
Ask *big.Int
CurrentBlockNum uint64
CurrentBlockHash [32]byte
ValidFromBlockNum uint64
CurrentBlockTimestamp uint64
}
// Decode is made available to external users (i.e. mercury server)
func Decode(report []byte) (*Report, error) {
values, err := schema.Unpack(report)
if err != nil {
return nil, fmt.Errorf("failed to decode report: %w", err)
}
decoded := new(Report)
if err = schema.Copy(decoded, values); err != nil {
return nil, fmt.Errorf("failed to copy report values to struct: %w", err)
}
return decoded, nil
}