code.vegaprotocol.io/vega@v0.79.0/core/datasource/datasource.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  //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
    17  
    18  package datasource
    19  
    20  import (
    21  	"encoding/hex"
    22  	"fmt"
    23  
    24  	"code.vegaprotocol.io/vega/core/datasource/common"
    25  	"code.vegaprotocol.io/vega/core/datasource/definition"
    26  	"code.vegaprotocol.io/vega/libs/crypto"
    27  	"code.vegaprotocol.io/vega/libs/proto"
    28  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    29  )
    30  
    31  type Spec struct {
    32  	ID        string
    33  	CreatedAt int64
    34  	UpdatedAt int64
    35  	Data      *definition.Definition
    36  	Status    common.SpecStatus
    37  }
    38  
    39  func (s *Spec) IntoProto() *vegapb.DataSourceSpec {
    40  	config := &vegapb.DataSourceDefinition{}
    41  	if s.Data != nil {
    42  		config = s.Data.IntoProto()
    43  	}
    44  
    45  	return &vegapb.DataSourceSpec{
    46  		Id:        s.ID,
    47  		CreatedAt: s.CreatedAt,
    48  		UpdatedAt: s.UpdatedAt,
    49  		Data:      config,
    50  		Status:    s.Status,
    51  	}
    52  }
    53  
    54  func (s *Spec) String() string {
    55  	configAsString := ""
    56  	if s.Data != nil {
    57  		configAsString = s.Data.String()
    58  	}
    59  
    60  	return fmt.Sprintf(
    61  		"ID(%s) createdAt(%v) updatedAt(%v) data(%s) status(%s)",
    62  		s.ID,
    63  		s.CreatedAt,
    64  		s.UpdatedAt,
    65  		configAsString,
    66  		s.Status.String(),
    67  	)
    68  }
    69  
    70  func SpecFromProto(specProto *vegapb.DataSourceSpec) *Spec {
    71  	d, _ := definition.FromProto(specProto.Data, nil)
    72  	//if err != nil {
    73  	// TODO: bubble error
    74  	//}
    75  	return &Spec{
    76  		ID:        specProto.Id,
    77  		CreatedAt: specProto.CreatedAt,
    78  		UpdatedAt: specProto.UpdatedAt,
    79  		Data:      definition.NewWith(d),
    80  		Status:    specProto.Status,
    81  	}
    82  }
    83  
    84  func (s *Spec) FromDefinition(d *definition.Definition) *Spec {
    85  	if d != nil {
    86  		bytes, _ := proto.Marshal(d.IntoProto())
    87  		specID := hex.EncodeToString(crypto.Hash(bytes))
    88  		return &Spec{
    89  			ID:   specID,
    90  			Data: d,
    91  		}
    92  	}
    93  
    94  	return &Spec{}
    95  }
    96  
    97  func SpecFromDefinition(d definition.Definition) *Spec {
    98  	bytes, _ := proto.Marshal(d.IntoProto())
    99  	specID := hex.EncodeToString(crypto.Hash(bytes))
   100  	return &Spec{
   101  		ID:   specID,
   102  		Data: &d,
   103  	}
   104  }
   105  
   106  func (s Spec) GetDefinition() definition.Definition {
   107  	if s.Data == nil {
   108  		return definition.Definition{}
   109  	}
   110  
   111  	return *s.Data
   112  }
   113  
   114  type SpecBindingForCompositePrice struct {
   115  	PriceSourceProperty string
   116  }
   117  
   118  func (b SpecBindingForCompositePrice) String() string {
   119  	return fmt.Sprintf(
   120  		"priceSource(%s)",
   121  		b.PriceSourceProperty,
   122  	)
   123  }
   124  
   125  func (b SpecBindingForCompositePrice) IntoProto() *vegapb.SpecBindingForCompositePrice {
   126  	return &vegapb.SpecBindingForCompositePrice{
   127  		PriceSourceProperty: b.PriceSourceProperty,
   128  	}
   129  }
   130  
   131  func (b SpecBindingForCompositePrice) DeepClone() *SpecBindingForCompositePrice {
   132  	return &SpecBindingForCompositePrice{
   133  		PriceSourceProperty: b.PriceSourceProperty,
   134  	}
   135  }
   136  
   137  func SpecBindingForCompositePriceFromProto(o *vegapb.SpecBindingForCompositePrice) *SpecBindingForCompositePrice {
   138  	return &SpecBindingForCompositePrice{
   139  		PriceSourceProperty: o.PriceSourceProperty,
   140  	}
   141  }
   142  
   143  type SpecBindingForFuture struct {
   144  	SettlementDataProperty     string
   145  	TradingTerminationProperty string
   146  }
   147  
   148  func (b SpecBindingForFuture) String() string {
   149  	return fmt.Sprintf(
   150  		"settlementData(%s) tradingTermination(%s)",
   151  		b.SettlementDataProperty,
   152  		b.TradingTerminationProperty,
   153  	)
   154  }
   155  
   156  func (b SpecBindingForFuture) IntoProto() *vegapb.DataSourceSpecToFutureBinding {
   157  	return &vegapb.DataSourceSpecToFutureBinding{
   158  		SettlementDataProperty:     b.SettlementDataProperty,
   159  		TradingTerminationProperty: b.TradingTerminationProperty,
   160  	}
   161  }
   162  
   163  func (b SpecBindingForFuture) DeepClone() *SpecBindingForFuture {
   164  	return &SpecBindingForFuture{
   165  		SettlementDataProperty:     b.SettlementDataProperty,
   166  		TradingTerminationProperty: b.TradingTerminationProperty,
   167  	}
   168  }
   169  
   170  func SpecBindingForFutureFromProto(o *vegapb.DataSourceSpecToFutureBinding) *SpecBindingForFuture {
   171  	return &SpecBindingForFuture{
   172  		SettlementDataProperty:     o.SettlementDataProperty,
   173  		TradingTerminationProperty: o.TradingTerminationProperty,
   174  	}
   175  }
   176  
   177  type SpecBindingForAutomatedPurchase struct {
   178  	AuctionScheduleProperty       string
   179  	AuctionVolumeSnapshotProperty string
   180  }
   181  
   182  func (b SpecBindingForAutomatedPurchase) String() string {
   183  	return fmt.Sprintf(
   184  		"auctionScheduleProperty(%s) auctionVolumeSnapshotProperty(%s)",
   185  		b.AuctionScheduleProperty,
   186  		b.AuctionVolumeSnapshotProperty,
   187  	)
   188  }
   189  
   190  func (b SpecBindingForAutomatedPurchase) IntoProto() *vegapb.DataSourceSpecToAutomatedPurchaseBinding {
   191  	return &vegapb.DataSourceSpecToAutomatedPurchaseBinding{
   192  		AuctionScheduleProperty:               b.AuctionScheduleProperty,
   193  		AuctionVolumeSnapshotScheduleProperty: b.AuctionVolumeSnapshotProperty,
   194  	}
   195  }
   196  
   197  func (b SpecBindingForAutomatedPurchase) DeepClone() *SpecBindingForAutomatedPurchase {
   198  	return &SpecBindingForAutomatedPurchase{
   199  		AuctionScheduleProperty:       b.AuctionScheduleProperty,
   200  		AuctionVolumeSnapshotProperty: b.AuctionVolumeSnapshotProperty,
   201  	}
   202  }
   203  
   204  func SpecBindingForAutomatedPurchaseFromProto(o *vegapb.DataSourceSpecToAutomatedPurchaseBinding) *SpecBindingForAutomatedPurchase {
   205  	return &SpecBindingForAutomatedPurchase{
   206  		AuctionScheduleProperty:       o.AuctionScheduleProperty,
   207  		AuctionVolumeSnapshotProperty: o.AuctionVolumeSnapshotScheduleProperty,
   208  	}
   209  }
   210  
   211  type SpecBindingForPerps struct {
   212  	SettlementDataProperty     string
   213  	SettlementScheduleProperty string
   214  }
   215  
   216  func (b SpecBindingForPerps) String() string {
   217  	return fmt.Sprintf(
   218  		"settlementData(%s) settlementSchedule(%s)",
   219  		b.SettlementDataProperty,
   220  		b.SettlementScheduleProperty,
   221  	)
   222  }
   223  
   224  func (b SpecBindingForPerps) IntoProto() *vegapb.DataSourceSpecToPerpetualBinding {
   225  	return &vegapb.DataSourceSpecToPerpetualBinding{
   226  		SettlementDataProperty:     b.SettlementDataProperty,
   227  		SettlementScheduleProperty: b.SettlementScheduleProperty,
   228  	}
   229  }
   230  
   231  func (b SpecBindingForPerps) DeepClone() *SpecBindingForPerps {
   232  	return &SpecBindingForPerps{
   233  		SettlementDataProperty:     b.SettlementDataProperty,
   234  		SettlementScheduleProperty: b.SettlementScheduleProperty,
   235  	}
   236  }
   237  
   238  func SpecBindingForPerpsFromProto(o *vegapb.DataSourceSpecToPerpetualBinding) *SpecBindingForPerps {
   239  	return &SpecBindingForPerps{
   240  		SettlementDataProperty:     o.SettlementDataProperty,
   241  		SettlementScheduleProperty: o.SettlementScheduleProperty,
   242  	}
   243  }
   244  
   245  func FromOracleSpecProto(specProto *vegapb.OracleSpec) *Spec {
   246  	if specProto.ExternalDataSourceSpec != nil {
   247  		if specProto.ExternalDataSourceSpec.Spec != nil {
   248  			return SpecFromProto(specProto.ExternalDataSourceSpec.Spec)
   249  		}
   250  	}
   251  
   252  	return &Spec{}
   253  }
   254  
   255  const (
   256  	ContentTypeInvalid                        = definition.ContentTypeInvalid
   257  	ContentTypeOracle                         = definition.ContentTypeOracle
   258  	ContentTypeEthOracle                      = definition.ContentTypeEthOracle
   259  	ContentTypeInternalTimeTermination        = definition.ContentTypeInternalTimeTermination
   260  	ContentTypeInternalTimeTriggerTermination = definition.ContentTypeInternalTimeTriggerTermination
   261  )
   262  
   263  func NewDefinitionWith(tp common.DataSourceType) *definition.Definition {
   264  	return definition.NewWith(tp)
   265  }
   266  
   267  func NewDefinition(tp definition.ContentType) *definition.Definition {
   268  	return definition.New(tp)
   269  }
   270  
   271  func DefinitionFromProto(protoConfig *vegapb.DataSourceDefinition) (common.DataSourceType, error) {
   272  	return definition.FromProto(protoConfig, nil)
   273  }