-
Notifications
You must be signed in to change notification settings - Fork 19
/
oracles.go
259 lines (229 loc) · 8.97 KB
/
oracles.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (C) 2023 Gobalsky Labs Limited
//
// 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 products
import (
"context"
"errors"
"fmt"
"strings"
"code.vegaprotocol.io/vega/core/datasource"
"code.vegaprotocol.io/vega/core/datasource/spec"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
)
type scheduledOracle struct {
settlementSubscriptionID spec.SubscriptionID
scheduleSubscriptionID spec.SubscriptionID
binding scheduledBinding
settleUnsub spec.Unsubscriber
scheduleUnsub spec.Unsubscriber
}
type terminatingOracle struct {
settlementSubscriptionID spec.SubscriptionID
terminationSubscriptionID spec.SubscriptionID
settleUnsub spec.Unsubscriber
terminationUnsub spec.Unsubscriber
binding terminatingBinding
data oracleData
}
type scheduledBinding struct {
settlementProperty string
settlementType datapb.PropertyKey_Type
settlementDecimals uint64
scheduleProperty string
scheduleType datapb.PropertyKey_Type
}
type terminatingBinding struct {
settlementProperty string
settlementType datapb.PropertyKey_Type
settlementDecimals uint64
terminationProperty string
terminationType datapb.PropertyKey_Type
}
type oracleData struct {
settlData *num.Numeric
tradingTerminated bool
}
func newFutureOracle(f *types.Future) (terminatingOracle, error) {
bind, err := newFutureBinding(f)
if err != nil {
return terminatingOracle{}, err
}
return terminatingOracle{
binding: bind,
}, nil
}
func newFutureBinding(f *types.Future) (terminatingBinding, error) {
settlementProperty := strings.TrimSpace(f.DataSourceSpecBinding.SettlementDataProperty)
if len(settlementProperty) == 0 {
return terminatingBinding{}, errors.New("binding for settlement data cannot be blank")
}
tradingTerminationProperty := strings.TrimSpace(f.DataSourceSpecBinding.TradingTerminationProperty)
if len(tradingTerminationProperty) == 0 {
return terminatingBinding{}, errors.New("binding for trading termination market cannot be blank")
}
// assume bool for now, check for built-in timestamp
// this can be set to anything else by the caller
termType := datapb.PropertyKey_TYPE_BOOLEAN
if tradingTerminationProperty == spec.BuiltinTimestamp {
termType = datapb.PropertyKey_TYPE_TIMESTAMP
}
t, dec := getSettleTypeAndDec(f.DataSourceSpecForSettlementData)
return terminatingBinding{
settlementProperty: settlementProperty,
settlementType: t,
settlementDecimals: dec,
terminationProperty: tradingTerminationProperty,
terminationType: termType,
}, nil
}
func (t *terminatingOracle) bindAll(ctx context.Context, oe OracleEngine, settle, term *spec.Spec, settleCB, termCB spec.OnMatchedData) error {
if err := t.bindSettlement(ctx, oe, settle, settleCB); err != nil {
return err
}
return t.bindTermination(ctx, oe, term, termCB)
}
func (t *terminatingOracle) bindSettlement(ctx context.Context, oe OracleEngine, osForSettle *spec.Spec, cb spec.OnMatchedData) error {
err := osForSettle.EnsureBoundableProperty(t.binding.settlementProperty, t.binding.settlementType)
if err != nil {
return fmt.Errorf("invalid oracle spec binding for settlement data: %w", err)
}
if t.settlementSubscriptionID, t.settleUnsub, err = oe.Subscribe(ctx, *osForSettle, cb); err != nil {
return fmt.Errorf("could not subscribe to oracle engine for settlement data: %w", err)
}
return nil
}
func (t *terminatingOracle) bindTermination(ctx context.Context, oe OracleEngine, osForTerm *spec.Spec, cb spec.OnMatchedData) error {
err := osForTerm.EnsureBoundableProperty(t.binding.terminationProperty, t.binding.terminationType)
if err != nil {
return fmt.Errorf("invalid oracle spec binding for trading termination: %w", err)
}
if t.terminationSubscriptionID, t.terminationUnsub, err = oe.Subscribe(ctx, *osForTerm, cb); err != nil {
return fmt.Errorf("could not subscribe to oracle engine for trading termination: %w", err)
}
return nil
}
func (t *terminatingOracle) unsubSettle(ctx context.Context) {
if t.settleUnsub != nil {
t.settleUnsub(ctx, t.settlementSubscriptionID)
t.settleUnsub = nil
}
}
func (t *terminatingOracle) unsubTerm(ctx context.Context) {
if t.terminationUnsub != nil {
t.terminationUnsub(ctx, t.terminationSubscriptionID)
t.terminationUnsub = nil
}
}
func newPerpOracle(p *types.Perps) (scheduledOracle, error) {
bind, err := newPerpBinding(p)
if err != nil {
return scheduledOracle{}, err
}
return scheduledOracle{
binding: bind,
}, nil
}
func newPerpBinding(p *types.Perps) (scheduledBinding, error) {
settleDataProp := strings.TrimSpace(p.DataSourceSpecBinding.SettlementDataProperty)
settleScheduleProp := strings.TrimSpace(p.DataSourceSpecBinding.SettlementScheduleProperty)
if len(settleDataProp) == 0 {
return scheduledBinding{}, errors.New("binding for settlement data cannot be blank")
}
if len(settleScheduleProp) == 0 {
return scheduledBinding{}, errors.New("binding for settlement schedule cannot be blank")
}
setT, dec := getSettleTypeAndDec(p.DataSourceSpecForSettlementData)
return scheduledBinding{
settlementProperty: settleDataProp,
settlementType: setT,
settlementDecimals: dec,
scheduleProperty: settleScheduleProp,
scheduleType: datapb.PropertyKey_TYPE_TIMESTAMP, // default to timestamp
}, nil
}
func (s *scheduledOracle) bindAll(ctx context.Context, oe OracleEngine, settle, schedule *spec.Spec, settleCB, scheduleCB spec.OnMatchedData) error {
if err := s.bindSettlement(ctx, oe, settle, settleCB); err != nil {
return err
}
return s.bindSchedule(ctx, oe, schedule, scheduleCB)
}
func (s *scheduledOracle) bindSettlement(ctx context.Context, oe OracleEngine, osForSettle *spec.Spec, cb spec.OnMatchedData) error {
err := osForSettle.EnsureBoundableProperty(s.binding.settlementProperty, s.binding.settlementType)
if err != nil {
return fmt.Errorf("invalid oracle spec binding for settlement data: %w", err)
}
if s.settlementSubscriptionID, s.settleUnsub, err = oe.Subscribe(ctx, *osForSettle, cb); err != nil {
return fmt.Errorf("could not subscribe to oracle engine for settlement data: %w", err)
}
return nil
}
func (s *scheduledOracle) bindSchedule(ctx context.Context, oe OracleEngine, osForSchedule *spec.Spec, cb spec.OnMatchedData) error {
err := osForSchedule.EnsureBoundableProperty(s.binding.scheduleProperty, s.binding.scheduleType)
if err != nil {
return fmt.Errorf("invalid oracle spec binding for schedule data: %w", err)
}
if s.scheduleSubscriptionID, s.scheduleUnsub, err = oe.Subscribe(ctx, *osForSchedule, cb); err != nil {
return fmt.Errorf("could not subscribe to oracle engine for schedule data: %w", err)
}
return nil
}
func (s *scheduledOracle) unsubAll(ctx context.Context) {
if s.settleUnsub != nil {
s.settleUnsub(ctx, s.settlementSubscriptionID)
s.settleUnsub = nil
}
if s.scheduleUnsub != nil {
s.scheduleUnsub(ctx, s.scheduleSubscriptionID)
s.scheduleUnsub = nil
}
}
// SettlementData returns oracle data settlement data scaled as Uint.
func (o *oracleData) SettlementData(op, ap uint32) (*num.Uint, error) {
if o.settlData.Decimal() == nil && o.settlData.Uint() == nil {
return nil, ErrDataSourceSettlementDataNotSet
}
if !o.settlData.SupportDecimalPlaces(int64(ap)) {
return nil, ErrSettlementDataDecimalsNotSupportedByAsset
}
// scale to given target decimals by multiplying by 10^(targetDP - oracleDP)
// if targetDP > oracleDP - this scales up the decimals of settlement data
// if targetDP < oracleDP - this scaled down the decimals of settlement data and can lead to loss of accuracy
// if there're equal - no scaling happens
return o.settlData.ScaleTo(int64(op), int64(ap))
}
// IsTradingTerminated returns true when oracle has signalled termination of trading.
func (o *oracleData) IsTradingTerminated() bool {
return o.tradingTerminated
}
func getSettleTypeAndDec(s *datasource.Spec) (datapb.PropertyKey_Type, uint64) {
def := s.GetDefinition()
defType := datapb.PropertyKey_TYPE_INTEGER
dec := uint64(0)
for _, f := range def.GetFilters() {
if f.Key.Type == datapb.PropertyKey_TYPE_INTEGER && f.Key.NumberDecimalPlaces != nil {
dec = *f.Key.NumberDecimalPlaces
break
} else if f.Key.Type == datapb.PropertyKey_TYPE_DECIMAL {
defType = f.Key.Type
if f.Key.NumberDecimalPlaces != nil {
dec = *f.Key.NumberDecimalPlaces
}
break
}
}
return defType, dec
}