code.vegaprotocol.io/vega@v0.79.0/datanode/entities/data_source_definition.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  
    21  	dstypes "code.vegaprotocol.io/vega/core/datasource/common"
    22  	ethcallcommon "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common"
    23  	"code.vegaprotocol.io/vega/protos/vega"
    24  
    25  	"google.golang.org/protobuf/encoding/protojson"
    26  )
    27  
    28  type DataSourceDefinition struct {
    29  	*vega.DataSourceDefinition
    30  }
    31  
    32  func (s DataSourceDefinition) MarshalJSON() ([]byte, error) {
    33  	return protojson.Marshal(s)
    34  }
    35  
    36  func (s *DataSourceDefinition) UnmarshalJSON(b []byte) error {
    37  	s.DataSourceDefinition = &vega.DataSourceDefinition{}
    38  	return protojson.Unmarshal(b, s)
    39  }
    40  
    41  func (s *DataSourceDefinition) GetOracle() (*DataSourceSpecConfiguration, error) {
    42  	ds := &DataSourceSpecConfiguration{
    43  		Signers: Signers{},
    44  		Filters: []Filter{},
    45  	}
    46  
    47  	data := s.Content()
    48  	if data != nil {
    49  		switch tp := data.(type) {
    50  		case *vega.DataSourceSpecConfiguration:
    51  			signers, err := SerializeSigners(dstypes.SignersFromProto(tp.GetSigners()))
    52  			if err != nil {
    53  				return nil, err
    54  			}
    55  			ds.Signers = signers
    56  			ds.Filters = FiltersFromProto(tp.GetFilters())
    57  		}
    58  	}
    59  
    60  	return ds, nil
    61  }
    62  
    63  func (s *DataSourceDefinition) GetEthOracle() (*EthCallSpec, error) {
    64  	ds := &EthCallSpec{
    65  		ArgsJson:    []string{},
    66  		Trigger:     EthCallTrigger{},
    67  		Filters:     []Filter{},
    68  		Normalisers: []Normaliser{},
    69  	}
    70  	data := s.Content()
    71  	if data != nil {
    72  		switch tp := data.(type) {
    73  		case *vega.EthCallSpec:
    74  			ds.Address = tp.Address
    75  			abi := tp.GetAbi()
    76  			ds.Abi = []byte(abi)
    77  			ds.Method = tp.Method
    78  			args := tp.GetArgs()
    79  			for _, arg := range args {
    80  				jsonArg, err := arg.MarshalJSON()
    81  				if err != nil {
    82  					return nil, err // TODO: Fix all of the errors
    83  				}
    84  				ds.ArgsJson = append(ds.ArgsJson, string(jsonArg))
    85  			}
    86  			trigger, err := ethcallcommon.TriggerFromProto(tp.Trigger)
    87  			if err != nil {
    88  				return nil, fmt.Errorf("failed to get trigger from proto: %w", err)
    89  			}
    90  			ds.Trigger = EthCallTrigger{Trigger: trigger}
    91  			ds.RequiredConfirmations = tp.RequiredConfirmations
    92  			ds.Filters = FiltersFromProto(tp.GetFilters())
    93  
    94  			normalisers := []Normaliser{}
    95  			for _, n := range tp.Normalisers {
    96  				normalisers = append(normalisers, Normaliser{
    97  					Name:       n.Name,
    98  					Expression: n.Expression,
    99  				})
   100  			}
   101  			ds.Normalisers = normalisers
   102  		}
   103  	}
   104  
   105  	return ds, nil
   106  }
   107  
   108  func (s *DataSourceDefinition) GetInternalTimeTrigger() *DataSourceSpecConfigurationTime {
   109  	ds := &DataSourceSpecConfigurationTime{
   110  		Conditions: []Condition{},
   111  	}
   112  
   113  	data := s.Content()
   114  	if data != nil {
   115  		switch tp := data.(type) {
   116  		case *vega.DataSourceSpecConfigurationTime:
   117  			for _, c := range tp.Conditions {
   118  				ds.Conditions = append(ds.Conditions, ConditionFromProto(c))
   119  			}
   120  		}
   121  	}
   122  
   123  	return ds
   124  }
   125  
   126  func (s *DataSourceDefinition) GetSigners() (Signers, error) {
   127  	signers := Signers{}
   128  
   129  	data := s.Content()
   130  	if data != nil {
   131  		switch tp := data.(type) {
   132  		case *vega.DataSourceSpecConfiguration:
   133  			var err error
   134  			signers, err = SerializeSigners(dstypes.SignersFromProto(tp.GetSigners()))
   135  			if err != nil {
   136  				return nil, err
   137  			}
   138  		}
   139  	}
   140  
   141  	return signers, nil
   142  }
   143  
   144  func (s *DataSourceDefinition) GetFilters() []Filter {
   145  	filters := []Filter{}
   146  
   147  	data := s.Content()
   148  	if data != nil {
   149  		switch tp := data.(type) {
   150  		case *vega.DataSourceSpecConfiguration:
   151  			filters = FiltersFromProto(tp.Filters)
   152  		case *vega.EthCallSpec:
   153  			filters = FiltersFromProto(tp.Filters)
   154  		}
   155  	}
   156  
   157  	return filters
   158  }
   159  
   160  func (s *DataSourceDefinition) GetConditions() []Condition {
   161  	conditions := []Condition{}
   162  
   163  	data := s.Content()
   164  	if data != nil {
   165  		switch tp := data.(type) {
   166  		case *vega.DataSourceSpecConfigurationTime:
   167  			for _, c := range tp.Conditions {
   168  				conditions = append(conditions, ConditionFromProto(c))
   169  			}
   170  		case *vega.DataSourceSpecConfiguration:
   171  			if tp.Filters != nil {
   172  				for _, f := range tp.Filters {
   173  					if f.Conditions != nil {
   174  						for _, c := range f.Conditions {
   175  							conditions = append(conditions, ConditionFromProto(c))
   176  						}
   177  					}
   178  				}
   179  			}
   180  
   181  		case *vega.EthCallSpec:
   182  			if tp.Filters != nil {
   183  				for _, f := range tp.Filters {
   184  					if f.Conditions != nil {
   185  						for _, c := range f.Conditions {
   186  							conditions = append(conditions, ConditionFromProto(c))
   187  						}
   188  					}
   189  				}
   190  			}
   191  		}
   192  	}
   193  
   194  	return conditions
   195  }
   196  
   197  func DataSourceDefinitionFromProto(dsp *vega.DataSourceDefinition) DataSourceDefinition {
   198  	return DataSourceDefinition{dsp}
   199  }
   200  
   201  // DataSourceSpecConfiguration is a simplified version of the oracle content.
   202  // In the future it is intended to be part of an interface, not a hardcoded objcet.
   203  type DataSourceSpecConfiguration struct {
   204  	Signers Signers
   205  	Filters []Filter
   206  }
   207  
   208  type EthCallTrigger struct {
   209  	ethcallcommon.Trigger
   210  }
   211  
   212  type Normaliser struct {
   213  	Name       string
   214  	Expression string
   215  }
   216  
   217  type EthCallSpec struct {
   218  	Address               string
   219  	Abi                   []byte
   220  	Method                string
   221  	ArgsJson              []string
   222  	Trigger               EthCallTrigger
   223  	RequiredConfirmations uint64
   224  	Filters               []Filter
   225  	Normalisers           []Normaliser
   226  }
   227  
   228  func (es *EthCallSpec) GetFilters() []Filter {
   229  	if es != nil {
   230  		return es.Filters
   231  	}
   232  
   233  	return []Filter{}
   234  }
   235  
   236  func (es *EthCallSpec) GetAddress() string {
   237  	if es != nil {
   238  		return es.Address
   239  	}
   240  
   241  	return ""
   242  }
   243  
   244  func (es *EthCallSpec) GetAbi() []byte {
   245  	if es != nil {
   246  		return es.Abi
   247  	}
   248  
   249  	return nil
   250  }
   251  
   252  func (es *EthCallSpec) GetMethod() string {
   253  	if es != nil {
   254  		return es.Method
   255  	}
   256  
   257  	return ""
   258  }
   259  
   260  func (es *EthCallSpec) GetArgs() []string {
   261  	if es != nil {
   262  		return es.ArgsJson
   263  	}
   264  
   265  	return []string{}
   266  }
   267  
   268  func (es *EthCallSpec) GetTrigger() EthCallTrigger {
   269  	if es != nil {
   270  		return es.Trigger
   271  	}
   272  
   273  	return EthCallTrigger{}
   274  }
   275  
   276  func (es *EthCallSpec) GetRequiredConfirmations() uint64 {
   277  	if es != nil {
   278  		return es.RequiredConfirmations
   279  	}
   280  
   281  	return uint64(0)
   282  }
   283  
   284  func (es *EthCallSpec) GetNormalisers() []Normaliser {
   285  	n := []Normaliser{}
   286  	if es != nil {
   287  		n = es.Normalisers
   288  	}
   289  
   290  	return n
   291  }
   292  
   293  // DataSourceSpecConfigurationTime is a simplified version of the internal time
   294  // termination data source; only for internal use;
   295  // New internal types will be created for Cosmic Elevator new internal terminations.
   296  type DataSourceSpecConfigurationTime struct {
   297  	Conditions []Condition
   298  }
   299  
   300  func (ds *DataSourceSpecConfiguration) GetSigners() Signers {
   301  	if ds != nil {
   302  		return ds.Signers
   303  	}
   304  	return Signers{}
   305  }
   306  
   307  func (ds *DataSourceSpecConfiguration) GetFilters() []Filter {
   308  	if ds != nil {
   309  		return ds.Filters
   310  	}
   311  	return []Filter{}
   312  }
   313  
   314  func (d *DataSourceSpecConfigurationTime) GetConditions() []Condition {
   315  	if d != nil {
   316  		return d.Conditions
   317  	}
   318  	return []Condition{}
   319  }