code.vegaprotocol.io/vega@v0.79.0/datanode/entities/risk_factors.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 entities
    17  
    18  import (
    19  	"fmt"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/protos/vega"
    23  
    24  	"github.com/shopspring/decimal"
    25  )
    26  
    27  type RiskFactor struct {
    28  	MarketID MarketID
    29  	Short    decimal.Decimal
    30  	Long     decimal.Decimal
    31  	TxHash   TxHash
    32  	VegaTime time.Time
    33  }
    34  
    35  func RiskFactorFromProto(factor *vega.RiskFactor, txHash TxHash, vegaTime time.Time) (*RiskFactor, error) {
    36  	var short, long decimal.Decimal
    37  	var err error
    38  
    39  	if short, err = decimal.NewFromString(factor.Short); err != nil {
    40  		return nil, fmt.Errorf("invalid value for short: %s - %v", factor.Short, err)
    41  	}
    42  
    43  	if long, err = decimal.NewFromString(factor.Long); err != nil {
    44  		return nil, fmt.Errorf("invalid value for long: %s - %v", factor.Long, err)
    45  	}
    46  
    47  	return &RiskFactor{
    48  		MarketID: MarketID(factor.Market),
    49  		Short:    short,
    50  		Long:     long,
    51  		TxHash:   txHash,
    52  		VegaTime: vegaTime,
    53  	}, nil
    54  }
    55  
    56  func (rf *RiskFactor) ToProto() *vega.RiskFactor {
    57  	return &vega.RiskFactor{
    58  		Market: rf.MarketID.String(),
    59  		Short:  rf.Short.String(),
    60  		Long:   rf.Long.String(),
    61  	}
    62  }