forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scribe.go
182 lines (162 loc) · 5.06 KB
/
scribe.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2021-2023 Chronicle Labs, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package chronicle
import (
"context"
"encoding/binary"
"math/big"
"time"
"github.com/defiweb/go-eth/crypto"
"github.com/defiweb/go-eth/rpc"
"github.com/defiweb/go-eth/types"
"github.com/orcfax/oracle-suite/pkg/contract"
"github.com/orcfax/oracle-suite/pkg/util/bn"
)
// ScribePricePrecision is the precision of the price value in the Scribe contract
// as a number of decimal places after the decimal point.
const ScribePricePrecision = 18
// Scribe allows interacting with the Scribe contract.
type Scribe struct {
client rpc.RPC
address types.Address
}
// NewScribe creates a new Scribe instance.
func NewScribe(client rpc.RPC, address types.Address) *Scribe {
return &Scribe{
client: client,
address: address,
}
}
// Client returns the RPC client used to interact with the Scribe.
func (s *Scribe) Client() rpc.RPC {
return s.client
}
// Address returns the address of the Scribe contract.
func (s *Scribe) Address() types.Address {
return s.address
}
// Read reads the poke data from the contract.
func (s *Scribe) Read(ctx context.Context) (PokeData, error) {
return s.readPokeData(ctx, pokeStorageSlot, types.LatestBlockNumber)
}
// Wat returns the wat value from the contract.
func (s *Scribe) Wat() contract.TypedSelfCaller[string] {
method := abiScribe.Methods["wat"]
return contract.NewTypedCall[string](
contract.CallOpts{
Client: s.client,
Address: s.address,
Encoder: contract.NewCallEncoder(method),
Decoder: contract.NewCallDecoder(method),
ErrorDecoder: contract.NewContractErrorDecoder(abiScribe),
},
)
}
// Bar returns the bar value from the contract.
func (s *Scribe) Bar() contract.TypedSelfCaller[int] {
method := abiScribe.Methods["bar"]
return contract.NewTypedCall[int](
contract.CallOpts{
Client: s.client,
Address: s.address,
Encoder: contract.NewCallEncoder(method),
Decoder: contract.NewCallDecoder(method),
ErrorDecoder: contract.NewContractErrorDecoder(abiScribe),
},
)
}
// Feeds returns Chronicle Protocol's feeds that are lifted in the contract.
func (s *Scribe) Feeds() contract.TypedSelfCaller[[]types.Address] {
method := abiScribe.Methods["feeds"]
return contract.NewTypedCall[[]types.Address](
contract.CallOpts{
Client: s.client,
Address: s.address,
Encoder: contract.NewCallEncoder(method),
Decoder: contract.NewCallDecoder(method),
ErrorDecoder: contract.NewContractErrorDecoder(abiScribe),
},
)
}
// Poke updates the poke data in the contract.
func (s *Scribe) Poke(pokeData PokeData, schnorrData SchnorrData) contract.SelfTransactableCaller {
return contract.NewTransactableCall(
contract.CallOpts{
Client: s.client,
Address: s.address,
Encoder: contract.NewCallEncoder(
abiScribe.Methods["poke"],
toPokeDataStruct(pokeData),
toSchnorrDataStruct(schnorrData),
),
ErrorDecoder: contract.NewContractErrorDecoder(abiScribe),
},
)
}
func (s *Scribe) readPokeData(ctx context.Context, storageSlot int, block types.BlockNumber) (PokeData, error) {
const (
ageOffset = 0
valOffset = 16
ageLength = 16
valLength = 16
)
b, err := s.client.GetStorageAt(
ctx,
s.address,
types.MustHashFromBigInt(big.NewInt(int64(storageSlot))),
block,
)
if err != nil {
return PokeData{}, err
}
val := bn.DecFixedPointFromRawBigInt(
new(big.Int).SetBytes(b[valOffset:valOffset+valLength]),
ScribePricePrecision,
)
age := time.Unix(
new(big.Int).SetBytes(b[ageOffset:ageOffset+ageLength]).Int64(),
0,
)
return PokeData{
Val: val,
Age: age,
}, nil
}
// ConstructScribePokeMessage returns the message expected to be signed via ECDSA for calling
// Scribe.poke method.
//
// The message is defined as:
// H(wat ‖ val ‖ age)
//
// Where:
// - wat: an asset name
// - val: a price value
// - age: a time when the price was observed
func ConstructScribePokeMessage(wat string, pokeData PokeData) []byte {
// Asset name (wat):
bytes32Wat := make([]byte, 32)
copy(bytes32Wat, wat)
// Price (val):
uint128Val := make([]byte, 16)
pokeData.Val.SetPrec(ScribePricePrecision).RawBigInt().FillBytes(uint128Val)
// Time (age):
uint32Age := make([]byte, 4)
binary.BigEndian.PutUint32(uint32Age, uint32(pokeData.Age.Unix()))
data := make([]byte, 52) //nolint:gomnd
copy(data[0:32], bytes32Wat)
copy(data[32:48], uint128Val)
copy(data[48:52], uint32Age)
return crypto.Keccak256(data).Bytes()
}