code.vegaprotocol.io/vega@v0.79.0/core/datasource/external/signedoracle/signedoracle.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 signedoracle
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"code.vegaprotocol.io/vega/core/datasource/common"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    26  )
    27  
    28  // SpecConfiguration is used only by Oracles without a type wrapper at the moment.
    29  type SpecConfiguration struct {
    30  	Signers []*common.Signer
    31  	Filters []*common.SpecFilter
    32  }
    33  
    34  // IntoProto tries to build the proto object from SpecConfiguration.
    35  func (s *SpecConfiguration) IntoProto() *vegapb.DataSourceSpecConfiguration {
    36  	signers := []*datapb.Signer{}
    37  	filters := []*datapb.Filter{}
    38  
    39  	dsc := &vegapb.DataSourceSpecConfiguration{}
    40  	if s != nil {
    41  		if s.Signers != nil {
    42  			signers = common.SignersIntoProto(s.Signers)
    43  		}
    44  
    45  		if s.Filters != nil {
    46  			filters = common.SpecFilters(s.Filters).IntoProto()
    47  		}
    48  
    49  		dsc = &vegapb.DataSourceSpecConfiguration{
    50  			// SignersIntoProto returns a list of signers after checking the list length.
    51  			Signers: signers,
    52  			Filters: filters,
    53  		}
    54  	}
    55  
    56  	return dsc
    57  }
    58  
    59  func (s SpecConfiguration) ToDefinitionProto() (*vegapb.DataSourceDefinition, error) {
    60  	return &vegapb.DataSourceDefinition{
    61  		SourceType: &vegapb.DataSourceDefinition_External{
    62  			External: &vegapb.DataSourceDefinitionExternal{
    63  				SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
    64  					Oracle: s.IntoProto(),
    65  				},
    66  			},
    67  		},
    68  	}, nil
    69  }
    70  
    71  // String returns the content of DataSourceSpecConfiguration as a string.
    72  func (s SpecConfiguration) String() string {
    73  	signers := ""
    74  	for i, signer := range s.Signers {
    75  		if i == 0 {
    76  			signers = signer.String()
    77  		} else {
    78  			signers = signers + fmt.Sprintf(", %s", signer.String())
    79  		}
    80  	}
    81  
    82  	filters := ""
    83  	for i, filter := range s.Filters {
    84  		if i == 0 {
    85  			filters = filter.String()
    86  		} else {
    87  			filters = filters + fmt.Sprintf(", %s", filter.String())
    88  		}
    89  	}
    90  	return fmt.Sprintf(
    91  		"signers(%v) filters(%v)",
    92  		signers,
    93  		filters,
    94  	)
    95  }
    96  
    97  func (s SpecConfiguration) DeepClone() common.DataSourceType {
    98  	return SpecConfiguration{
    99  		Signers: s.Signers,
   100  		Filters: common.DeepCloneSpecFilters(s.Filters),
   101  	}
   102  }
   103  
   104  func (s SpecConfiguration) GetFilters() []*common.SpecFilter {
   105  	return s.Filters
   106  }
   107  
   108  // SpecConfigurationFromProto tries to build the SpecConfiguration object
   109  // from the given proto object.
   110  func SpecConfigurationFromProto(protoConfig *vegapb.DataSourceSpecConfiguration) SpecConfiguration {
   111  	if protoConfig == nil {
   112  		return SpecConfiguration{}
   113  	}
   114  
   115  	return SpecConfiguration{
   116  		Filters: common.SpecFiltersFromProto(protoConfig.Filters),
   117  		Signers: common.SignersFromProto(protoConfig.Signers),
   118  	}
   119  }