code.vegaprotocol.io/vega@v0.79.0/protos/vega/spec_definition.go (about)

     1  package vega
     2  
     3  import (
     4  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
     5  	"google.golang.org/protobuf/types/known/structpb"
     6  )
     7  
     8  type DataSourceContentType int32
     9  
    10  const (
    11  	DataSurceContentTypeInvalid DataSourceContentType = iota
    12  	DataSourceContentTypeOracle
    13  	DataSourceContentTypeEthOracle
    14  	DataSourceContentTypeInternalTimeTermination
    15  	DataSourceContentTypeInternalTimeTriggerTermination
    16  )
    17  
    18  // GetSigners tries to get the Signers from the DataSourceDefinition object.
    19  func (d DataSourceDefinition) GetSigners() []*datapb.Signer {
    20  	signers := []*datapb.Signer{}
    21  	cnt := d.Content()
    22  	if cnt != nil {
    23  		switch tp := cnt.(type) {
    24  		case *DataSourceSpecConfiguration:
    25  			signers = tp.Signers
    26  		}
    27  	}
    28  
    29  	return signers
    30  }
    31  
    32  func (d DataSourceDefinition) GetFilters() []*datapb.Filter {
    33  	filters := []*datapb.Filter{}
    34  	cnt := d.Content()
    35  	if cnt != nil {
    36  		switch tp := cnt.(type) {
    37  		case *DataSourceSpecConfiguration:
    38  			if tp != nil {
    39  				return tp.Filters
    40  			}
    41  
    42  		case *EthCallSpec:
    43  			if tp != nil {
    44  				if tp.Filters != nil {
    45  					return tp.Filters
    46  				}
    47  			}
    48  
    49  		case *DataSourceSpecConfigurationTime:
    50  			if tp != nil { // wtf?
    51  				if len(tp.Conditions) > 0 {
    52  					filters = append(filters,
    53  						&datapb.Filter{
    54  							Key: &datapb.PropertyKey{
    55  								Name: "vegaprotocol.builtin.timestamp",
    56  								Type: datapb.PropertyKey_TYPE_TIMESTAMP,
    57  							},
    58  							Conditions: []*datapb.Condition{
    59  								tp.Conditions[0],
    60  							},
    61  						},
    62  					)
    63  				}
    64  			}
    65  		case *DataSourceSpecConfigurationTimeTrigger:
    66  			if tp != nil {
    67  				if len(tp.Conditions) > 0 {
    68  					filters = append(filters,
    69  						&datapb.Filter{
    70  							Key: &datapb.PropertyKey{
    71  								Name: "vegaprotocol.builtin.timetrigger",
    72  								Type: datapb.PropertyKey_TYPE_TIMESTAMP,
    73  							},
    74  							Conditions: tp.Conditions,
    75  						},
    76  					)
    77  				}
    78  			}
    79  		}
    80  	}
    81  
    82  	return filters
    83  }
    84  
    85  func NewDataSourceDefinitionWith(dst isDataSourceDefinition_SourceType) *DataSourceDefinition {
    86  	return &DataSourceDefinition{SourceType: dst}
    87  }
    88  
    89  func NewDataSourceDefinition(tp DataSourceContentType) *DataSourceDefinition {
    90  	ds := &DataSourceDefinition{}
    91  
    92  	switch tp {
    93  	case DataSourceContentTypeOracle:
    94  		ds.SourceType = &DataSourceDefinition_External{
    95  			External: &DataSourceDefinitionExternal{
    96  				SourceType: &DataSourceDefinitionExternal_Oracle{
    97  					Oracle: &DataSourceSpecConfiguration{
    98  						Signers: []*datapb.Signer{},
    99  						Filters: []*datapb.Filter{},
   100  					},
   101  				},
   102  			},
   103  		}
   104  
   105  	case DataSourceContentTypeEthOracle:
   106  		ds.SourceType = &DataSourceDefinition_External{
   107  			External: &DataSourceDefinitionExternal{
   108  				SourceType: &DataSourceDefinitionExternal_EthOracle{
   109  					EthOracle: &EthCallSpec{
   110  						Abi:         "",
   111  						Args:        []*structpb.Value{},
   112  						Trigger:     &EthCallTrigger{},
   113  						Normalisers: []*Normaliser{},
   114  						Filters:     []*datapb.Filter{},
   115  					},
   116  				},
   117  			},
   118  		}
   119  
   120  	case DataSourceContentTypeInternalTimeTermination:
   121  		ds.SourceType = &DataSourceDefinition_Internal{
   122  			Internal: &DataSourceDefinitionInternal{
   123  				SourceType: &DataSourceDefinitionInternal_Time{
   124  					Time: &DataSourceSpecConfigurationTime{
   125  						Conditions: []*datapb.Condition{},
   126  					},
   127  				},
   128  			},
   129  		}
   130  
   131  	case DataSourceContentTypeInternalTimeTriggerTermination:
   132  		ds.SourceType = &DataSourceDefinition_Internal{
   133  			Internal: &DataSourceDefinitionInternal{
   134  				SourceType: &DataSourceDefinitionInternal_TimeTrigger{
   135  					TimeTrigger: &DataSourceSpecConfigurationTimeTrigger{
   136  						Conditions: []*datapb.Condition{},
   137  						Triggers:   []*datapb.InternalTimeTrigger{},
   138  					},
   139  				},
   140  			},
   141  		}
   142  	}
   143  
   144  	return ds
   145  }
   146  
   147  // SetOracleConfig sets a given oracle config in the receiver.
   148  // If the receiver is not external oracle type of data source - it is not changed.
   149  // This method does not care about object previous contents - use with caution (currenty needed only for testing purposes).
   150  func (s *DataSourceDefinition) SetOracleConfig(oc isDataSourceDefinitionExternal_SourceType) *DataSourceDefinition {
   151  	if oc != nil {
   152  		cnt := NewDataSourceDefinitionWith(
   153  			&DataSourceDefinition_External{
   154  				External: &DataSourceDefinitionExternal{
   155  					SourceType: oc,
   156  				},
   157  			}).Content()
   158  		if s.SourceType != nil {
   159  			switch te := s.SourceType.(type) {
   160  			case *DataSourceDefinition_External:
   161  				if te.External != nil {
   162  					if te.External.SourceType != nil {
   163  						switch te.External.SourceType.(type) {
   164  						case *DataSourceDefinitionExternal_Oracle:
   165  							switch tp := cnt.(type) {
   166  							case *DataSourceSpecConfiguration:
   167  								ds := &DataSourceDefinition{
   168  									SourceType: &DataSourceDefinition_External{
   169  										External: &DataSourceDefinitionExternal{
   170  											SourceType: &DataSourceDefinitionExternal_Oracle{
   171  												Oracle: tp,
   172  											},
   173  										},
   174  									},
   175  								}
   176  								*s = *ds
   177  							}
   178  
   179  						case *DataSourceDefinitionExternal_EthOracle:
   180  							switch tp := cnt.(type) {
   181  							case *EthCallSpec:
   182  								ds := &DataSourceDefinition{
   183  									SourceType: &DataSourceDefinition_External{
   184  										External: &DataSourceDefinitionExternal{
   185  											SourceType: &DataSourceDefinitionExternal_EthOracle{
   186  												EthOracle: tp,
   187  											},
   188  										},
   189  									},
   190  								}
   191  								*s = *ds
   192  							}
   193  						}
   194  					}
   195  				}
   196  			}
   197  		}
   198  	}
   199  	return s
   200  }
   201  
   202  // SetTimeTriggerConditionConfig sets a condition to the time triggered receiver.
   203  // If the receiver is not a time triggered data source - it does not set anything to it.
   204  // This method does not care about object previous contents - use with caution (currenty needed only for testing purposes).
   205  func (s *DataSourceDefinition) SetTimeTriggerConditionConfig(c []*datapb.Condition) *DataSourceDefinition {
   206  	if c != nil {
   207  		cnt := s.Content()
   208  		if cnt != nil {
   209  			switch cnt.(type) {
   210  			// For the case the vegapb.DataSourceDefinitionInternal is not nill
   211  			// and its embedded object is of type vegapb.DataSourceDefinitionInternal_Time
   212  			case *DataSourceSpecConfigurationTime:
   213  				// Set the new condition only in this case
   214  				cond := []*datapb.Condition{}
   215  				if len(c) > 0 {
   216  					cond = append(cond, c[0])
   217  				}
   218  				ds := &DataSourceDefinition{
   219  					SourceType: &DataSourceDefinition_Internal{
   220  						Internal: &DataSourceDefinitionInternal{
   221  							SourceType: &DataSourceDefinitionInternal_Time{
   222  								Time: &DataSourceSpecConfigurationTime{
   223  									Conditions: cond,
   224  								},
   225  							},
   226  						},
   227  					},
   228  				}
   229  				*s = *ds
   230  
   231  			case *DataSourceSpecConfigurationTimeTrigger:
   232  				// Set the new condition only in this case
   233  				ds := &DataSourceDefinition{
   234  					SourceType: &DataSourceDefinition_Internal{
   235  						Internal: &DataSourceDefinitionInternal{
   236  							SourceType: &DataSourceDefinitionInternal_TimeTrigger{
   237  								TimeTrigger: &DataSourceSpecConfigurationTimeTrigger{
   238  									Conditions: c,
   239  								},
   240  							},
   241  						},
   242  					},
   243  				}
   244  				*s = *ds
   245  			}
   246  		}
   247  	}
   248  
   249  	return s
   250  }
   251  
   252  func (s *DataSourceDefinition) Content() interface{} {
   253  	if s != nil {
   254  		if s.SourceType != nil {
   255  			switch tp := s.SourceType.(type) {
   256  			case *DataSourceDefinition_External:
   257  				if tp.External != nil {
   258  					if tp.External.SourceType != nil {
   259  						switch extTp := tp.External.SourceType.(type) {
   260  						case *DataSourceDefinitionExternal_Oracle:
   261  							if extTp.Oracle != nil {
   262  								return extTp.Oracle
   263  							}
   264  
   265  						case *DataSourceDefinitionExternal_EthOracle:
   266  							if extTp.EthOracle != nil {
   267  								return extTp.EthOracle
   268  							}
   269  						}
   270  					}
   271  				}
   272  
   273  			case *DataSourceDefinition_Internal:
   274  				if tp.Internal != nil {
   275  					if tp.Internal.SourceType != nil {
   276  						switch intTp := tp.Internal.SourceType.(type) {
   277  						case *DataSourceDefinitionInternal_Time:
   278  							if intTp.Time != nil {
   279  								return intTp.Time
   280  							}
   281  
   282  						case *DataSourceDefinitionInternal_TimeTrigger:
   283  							if intTp.TimeTrigger != nil {
   284  								return intTp.TimeTrigger
   285  							}
   286  						}
   287  					}
   288  				}
   289  			}
   290  		}
   291  	}
   292  
   293  	return nil
   294  }