code.vegaprotocol.io/vega@v0.79.0/core/risk/statevar.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package risk
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/core/events"
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/core/types/statevar"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	"code.vegaprotocol.io/vega/logging"
    26  )
    27  
    28  type FactorConverter struct{}
    29  
    30  var riskFactorTolerance = num.MustDecimalFromString("1e-6")
    31  
    32  func (FactorConverter) BundleToInterface(kvb *statevar.KeyValueBundle) statevar.StateVariableResult {
    33  	return &types.RiskFactor{
    34  		Short: kvb.KVT[0].Val.(*statevar.DecimalScalar).Val,
    35  		Long:  kvb.KVT[1].Val.(*statevar.DecimalScalar).Val,
    36  	}
    37  }
    38  
    39  func (FactorConverter) InterfaceToBundle(res statevar.StateVariableResult) *statevar.KeyValueBundle {
    40  	value := res.(*types.RiskFactor)
    41  	return &statevar.KeyValueBundle{
    42  		KVT: []statevar.KeyValueTol{
    43  			{Key: "short", Val: &statevar.DecimalScalar{Val: value.Short}, Tolerance: riskFactorTolerance},
    44  			{Key: "long", Val: &statevar.DecimalScalar{Val: value.Long}, Tolerance: riskFactorTolerance},
    45  		},
    46  	}
    47  }
    48  
    49  // startRiskFactorsCalculation kicks off the risk factors calculation, done asynchronously for illustration.
    50  func (e *Engine) startRiskFactorsCalculation(eventID string, endOfCalcCallback statevar.FinaliseCalculation) {
    51  	rf := e.model.CalculateRiskFactors()
    52  	e.log.Info("risk factors calculated", logging.String("event-id", eventID), logging.Decimal("short", rf.Short), logging.Decimal("long", rf.Long))
    53  	endOfCalcCallback.CalculationFinished(eventID, rf, nil)
    54  }
    55  
    56  // CalculateRiskFactorsForTest is a hack for testing for setting directly the risk factors for a market.
    57  func (e *Engine) CalculateRiskFactorsForTest() {
    58  	e.cfgMu.Lock()
    59  	defer e.cfgMu.Unlock()
    60  	e.factors = e.model.CalculateRiskFactors()
    61  	e.factors.Market = e.mktID
    62  }
    63  
    64  // updateRiskFactor sets the risk factor value to that of the decimal consensus value.
    65  func (e *Engine) updateRiskFactor(ctx context.Context, res statevar.StateVariableResult) error {
    66  	e.cfgMu.Lock()
    67  	defer e.cfgMu.Unlock()
    68  	e.factors = res.(*types.RiskFactor)
    69  	e.factors.Market = e.mktID
    70  	e.riskFactorsInitialised = true
    71  	e.log.Info("consensus reached for risk factors", logging.String("market", e.mktID), logging.Decimal("short", e.factors.Short), logging.Decimal("long", e.factors.Long))
    72  	// then we can send in the broker
    73  	e.broker.Send(events.NewRiskFactorEvent(ctx, *e.factors))
    74  	return nil
    75  }
    76  
    77  func (e *Engine) IsRiskFactorInitialised() bool {
    78  	e.cfgMu.RLock()
    79  	defer e.cfgMu.RUnlock()
    80  	return e.riskFactorsInitialised
    81  }