forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
median.go
197 lines (177 loc) · 5.43 KB
/
median.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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"
"errors"
"fmt"
"math/big"
"sort"
"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"
)
// MedianPricePrecision is the precision of the price value in the Median contract
// as a number of decimal places after the decimal point.
const MedianPricePrecision = 18
type MedianVal struct {
Val *bn.DecFixedPointNumber
Age time.Time
V uint8
R *big.Int
S *big.Int
}
// Median allows interacting with the Median contract.
type Median struct {
client rpc.RPC
address types.Address
}
// NewMedian creates a new Median instance.
func NewMedian(client rpc.RPC, address types.Address) *Median {
return &Median{
client: client,
address: address,
}
}
// Client returns the RPC client used to interact with the Median.
func (m *Median) Client() rpc.RPC {
return m.client
}
// Address returns the address of the Median contract.
func (m *Median) Address() types.Address {
return m.address
}
// Val returns the current median price value.
func (m *Median) Val(ctx context.Context) (*bn.DecFixedPointNumber, error) {
const (
offset = 16
length = 16
)
b, err := m.client.GetStorageAt(
ctx,
m.address,
types.MustHashFromBigInt(big.NewInt(1)),
types.LatestBlockNumber,
)
if err != nil {
return nil, fmt.Errorf("median: val query failed: %w", err)
}
if len(b) < (offset + length) {
return nil, errors.New("median: val query failed: result is too short")
}
return bn.DecFixedPointFromRawBigInt(
new(big.Int).SetBytes(b[length:offset+length]),
MedianPricePrecision,
), nil
}
// Age returns the current median price age.
func (m *Median) Age() contract.TypedSelfCaller[time.Time] {
method := abiMedian.Methods["age"]
return contract.NewTypedCall[time.Time](
contract.CallOpts{
Client: m.client,
Address: m.address,
Encoder: contract.NewCallEncoder(method),
Decoder: func(data []byte, res any) error {
*res.(*time.Time) = time.Unix(new(big.Int).SetBytes(data).Int64(), 0)
return nil
},
ErrorDecoder: contract.NewContractErrorDecoder(abiMedian),
},
)
}
// Wat returns the median price asset name.
func (m *Median) Wat() contract.TypedSelfCaller[string] {
method := abiMedian.Methods["wat"]
return contract.NewTypedCall[string](
contract.CallOpts{
Client: m.client,
Address: m.address,
Encoder: contract.NewCallEncoder(method),
Decoder: contract.NewCallDecoder(method),
ErrorDecoder: contract.NewContractErrorDecoder(abiMedian),
},
)
}
// Bar returns the median price bar.
func (m *Median) Bar() contract.TypedSelfCaller[int] {
method := abiMedian.Methods["bar"]
return contract.NewTypedCall[int](
contract.CallOpts{
Client: m.client,
Address: m.address,
Encoder: contract.NewCallEncoder(method),
Decoder: contract.NewCallDecoder(method),
ErrorDecoder: contract.NewContractErrorDecoder(abiMedian),
},
)
}
// Poke updates the median price value.
func (m *Median) Poke(vals []MedianVal) contract.SelfTransactableCaller {
sort.Slice(vals, func(i, j int) bool {
return vals[i].Val.Cmp(vals[j].Val) < 0
})
valSlice := make([]*big.Int, len(vals))
ageSlice := make([]uint64, len(vals))
vSlice := make([]uint8, len(vals))
rSlice := make([]*big.Int, len(vals))
sSlice := make([]*big.Int, len(vals))
for i, v := range vals {
valSlice[i] = v.Val.SetPrec(MedianPricePrecision).RawBigInt()
ageSlice[i] = uint64(v.Age.Unix())
vSlice[i] = v.V
rSlice[i] = v.R
sSlice[i] = v.S
}
return contract.NewTransactableCall(
contract.CallOpts{
Client: m.client,
Address: m.address,
Encoder: contract.NewCallEncoder(abiMedian.Methods["poke"], valSlice, ageSlice, vSlice, rSlice, sSlice),
ErrorDecoder: contract.NewContractErrorDecoder(abiMedian),
},
)
}
// ConstructMedianPokeMessage returns the message expected to be signed via ECDSA for calling
// Median.poke method.
//
// The message structure is defined as:
// H(val ‖ age ‖ wat)
//
// Where:
// - val: a price value
// - age: a time when the price was observed
// - wat: an asset name
func ConstructMedianPokeMessage(wat string, val *bn.DecFloatPointNumber, age time.Time) []byte {
// Price (val):
uint256Val := make([]byte, 32)
val.DecFixedPoint(MedianPricePrecision).RawBigInt().FillBytes(uint256Val)
// Time (age):
uint256Age := make([]byte, 32)
binary.BigEndian.PutUint64(uint256Age[24:], uint64(age.Unix()))
// Asset name (wat):
bytes32Wat := make([]byte, 32)
copy(bytes32Wat, wat)
// Hash:
data := make([]byte, 96)
copy(data[0:32], uint256Val)
copy(data[32:64], uint256Age)
copy(data[64:96], bytes32Wat)
return crypto.Keccak256(data).Bytes()
}