-
Notifications
You must be signed in to change notification settings - Fork 19
/
statevar.go
110 lines (94 loc) · 3.86 KB
/
statevar.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
// 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 price
import (
"context"
"errors"
"code.vegaprotocol.io/vega/core/types/statevar"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
type boundFactorsConverter struct{}
func (boundFactorsConverter) BundleToInterface(kvb *statevar.KeyValueBundle) statevar.StateVariableResult {
return &boundFactors{
up: kvb.KVT[0].Val.(*statevar.DecimalVector).Val,
down: kvb.KVT[1].Val.(*statevar.DecimalVector).Val,
}
}
func (boundFactorsConverter) InterfaceToBundle(res statevar.StateVariableResult) *statevar.KeyValueBundle {
value := res.(*boundFactors)
return &statevar.KeyValueBundle{
KVT: []statevar.KeyValueTol{
{Key: "up", Val: &statevar.DecimalVector{Val: value.up}, Tolerance: tolerance},
{Key: "down", Val: &statevar.DecimalVector{Val: value.down}, Tolerance: tolerance},
},
}
}
func (e *Engine) IsBoundFactorsInitialised() bool {
return e.boundFactorsInitialised
}
// startCalcPriceRanges kicks off the bounds factors calculation, done asynchronously for illustration.
func (e *Engine) startCalcPriceRanges(eventID string, endOfCalcCallback statevar.FinaliseCalculation) {
if e.log.GetLevel() <= logging.DebugLevel {
e.log.Debug("price range factors calculation started", logging.String("event-id", eventID))
}
down := make([]num.Decimal, 0, len(e.bounds))
up := make([]num.Decimal, 0, len(e.bounds))
// if we have no reference price, just abort and wait for the next round
if len(e.pricesPast) < 1 && len(e.pricesNow) < 1 {
e.log.Info("no reference price available for market - cannot calculate price ranges", logging.String("event-id", eventID))
endOfCalcCallback.CalculationFinished(eventID, nil, errors.New("no reference price available for market - cannot calculate price ranges"))
return
}
for _, b := range e.bounds {
ref := e.getRefPriceNoUpdate(b.Trigger.Horizon)
minPrice, maxPrice := e.riskModel.PriceRange(ref, e.fpHorizons[b.Trigger.Horizon], b.Trigger.Probability)
down = append(down, minPrice.Div(ref))
up = append(up, maxPrice.Div(ref))
}
res := &boundFactors{
down: down,
up: up,
}
if e.log.GetLevel() <= logging.DebugLevel {
e.log.Debug("price range factors calculation completed", logging.String("event-id", eventID), logging.String("asset", e.asset), logging.String("market", e.market))
}
endOfCalcCallback.CalculationFinished(eventID, res, nil)
}
// updatePriceBounds is called back from the state variable consensus engine when consensus is reached for the down/up factors and updates the price bounds.
func (e *Engine) updatePriceBounds(ctx context.Context, res statevar.StateVariableResult) error {
bRes := res.(*boundFactors)
e.updateFactors(bRes.down, bRes.up)
if e.log.GetLevel() <= logging.DebugLevel {
e.log.Debug("consensus reached for price ranges", logging.String("asset", e.asset), logging.String("market", e.market))
}
return nil
}
func (e *Engine) updateFactors(down, up []num.Decimal) {
for i, b := range e.bounds {
if !b.Active {
continue
}
b.DownFactor = down[i]
b.UpFactor = up[i]
}
e.boundFactorsInitialised = true
// force invalidation of the price range cache
if len(e.pricesNow) > 0 {
e.getCurrentPriceRanges(true)
}
e.clearStalePrices()
e.stateChanged = true
}