code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/oracles_resolver.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 gql 17 18 import ( 19 "context" 20 "errors" 21 "time" 22 23 "code.vegaprotocol.io/vega/libs/ptr" 24 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 25 vegapb "code.vegaprotocol.io/vega/protos/vega" 26 v1 "code.vegaprotocol.io/vega/protos/vega/data/v1" 27 ) 28 29 type oracleSpecResolver VegaResolverRoot 30 31 func (o *oracleSpecResolver) DataSourceSpec(_ context.Context, obj *vegapb.OracleSpec) (extDss *ExternalDataSourceSpec, _ error) { 32 extDss = &ExternalDataSourceSpec{Spec: &DataSourceSpec{Data: &vegapb.DataSourceDefinition{}}} 33 if obj.ExternalDataSourceSpec != nil { 34 extDss.Spec = resolveDataSourceSpec(obj.ExternalDataSourceSpec.Spec) 35 } 36 return 37 } 38 39 func (o *oracleSpecResolver) DataConnection(ctx context.Context, obj *vegapb.OracleSpec, pagination *v2.Pagination) (*v2.OracleDataConnection, error) { 40 var specID *string 41 if ed := obj.ExternalDataSourceSpec; ed != nil && ed.Spec != nil && ed.Spec.Id != "" { 42 specID = &ed.Spec.Id 43 } 44 45 req := v2.ListOracleDataRequest{ 46 OracleSpecId: specID, 47 Pagination: pagination, 48 } 49 50 resp, err := o.tradingDataClientV2.ListOracleData(ctx, &req) 51 if err != nil { 52 return nil, err 53 } 54 55 return resp.OracleData, nil 56 } 57 58 type oracleDataResolver VegaResolverRoot 59 60 func (o *oracleDataResolver) ExternalData(_ context.Context, obj *vegapb.OracleData) (ed *ExternalData, _ error) { 61 ed = &ExternalData{ 62 Data: &Data{}, 63 } 64 65 oed := obj.ExternalData 66 if oed == nil || oed.Data == nil { 67 return 68 } 69 70 ed.Data.Signers = resolveSigners(oed.Data.Signers) 71 ed.Data.Data = oed.Data.Data 72 ed.Data.MatchedSpecIds = oed.Data.MatchedSpecIds 73 ed.Data.BroadcastAt = oed.Data.BroadcastAt 74 75 return 76 } 77 78 func resolveTrigger(obj any) (trigger TriggerKind) { 79 if obj != nil { 80 switch trig := obj.(type) { 81 case *vegapb.EthCallTrigger_TimeTrigger: 82 if trig.TimeTrigger != nil { 83 var ( 84 init *int64 85 until *int64 86 ) 87 if trig.TimeTrigger.GetInitial() != 0 { 88 init = ptr.From(time.Unix(int64(trig.TimeTrigger.GetInitial()), 0).UnixNano()) 89 } 90 every := int(trig.TimeTrigger.GetEvery()) 91 if trig.TimeTrigger.GetUntil() != 0 { 92 until = ptr.From(time.Unix(int64(trig.TimeTrigger.GetUntil()), 0).UnixNano()) 93 } 94 trigger = &EthTimeTrigger{ 95 Initial: init, 96 Every: &every, 97 Until: until, 98 } 99 } 100 } 101 } 102 103 return 104 } 105 106 func resolveSigners(obj []*v1.Signer) (signers []*Signer) { 107 for i := range obj { 108 signers = append(signers, &Signer{Signer: resolveSigner(obj[i].Signer)}) 109 } 110 return 111 } 112 113 func resolveSigner(obj any) (signer SignerKind) { 114 switch sig := obj.(type) { 115 case *v1.Signer_PubKey: 116 signer = &PubKey{Key: &sig.PubKey.Key} 117 case *v1.Signer_EthAddress: 118 signer = ÐAddress{Address: &sig.EthAddress.Address} 119 } 120 return 121 } 122 123 func resolveFilters(obj []*v1.Filter) (filters []*Filter, e error) { 124 filters = []*Filter{} 125 if obj != nil { 126 for _, f := range obj { 127 if f != nil { 128 filter, err := resolveFilter(f) 129 if err != nil { 130 e = err 131 return 132 } 133 filters = append(filters, filter) 134 } 135 } 136 return 137 } 138 139 return 140 } 141 142 func resolveFilter(obj *v1.Filter) (filter *Filter, e error) { 143 filter = &Filter{ 144 Key: &PropertyKey{}, 145 Conditions: []*Condition{}, 146 } 147 148 if obj.Key != nil { 149 filter.Key = &PropertyKey{ 150 Name: &obj.Key.Name, 151 Type: obj.Key.Type, 152 } 153 154 if obj.Key.NumberDecimalPlaces != nil { 155 indp := new(int) 156 *indp = int(*obj.Key.NumberDecimalPlaces) 157 filter.Key.NumberDecimalPlaces = indp 158 } 159 } else { 160 e = errors.New("Property key is empty") 161 return 162 } 163 164 if obj.Conditions != nil || len(obj.Conditions) > 0 { 165 filter.Conditions = resolveConditions(obj.Conditions) 166 } else { 167 e = errors.New("Conditions list is empty") 168 return 169 } 170 171 return 172 } 173 174 func resolveConditions(obj []*v1.Condition) (conditions []*Condition) { 175 conditions = []*Condition{} 176 for _, c := range obj { 177 conditions = append( 178 conditions, 179 &Condition{ 180 Operator: c.Operator, 181 Value: &c.Value, 182 }, 183 ) 184 } 185 return 186 } 187 188 func resolveNormalisers(obj []*vegapb.Normaliser) (normalisers []*Normaliser) { 189 if obj != nil { 190 for _, n := range obj { 191 normalisers = append(normalisers, resolveNormaliser(n)) 192 } 193 return 194 } 195 196 normalisers = []*Normaliser{} 197 return 198 } 199 200 func resolveNormaliser(obj any) (normaliser *Normaliser) { 201 normaliser = &Normaliser{} 202 203 // if obj != nil { 204 switch norm := obj.(type) { 205 case *vegapb.Normaliser: 206 normaliser = &Normaliser{ 207 Name: norm.Name, 208 Expression: norm.Expression, 209 } 210 // return 211 } 212 // } 213 214 return 215 } 216 217 func resolveDataSourceDefinition(d *vegapb.DataSourceDefinition) (ds *vegapb.DataSourceDefinition) { 218 ds = &vegapb.DataSourceDefinition{} 219 if d == nil || d.SourceType == nil { 220 return ds 221 } 222 data := d.Content() 223 if data != nil { 224 switch tp := data.(type) { 225 case *vegapb.DataSourceSpecConfiguration: 226 ds.SourceType = &vegapb.DataSourceDefinition_External{ 227 External: &vegapb.DataSourceDefinitionExternal{ 228 SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{ 229 Oracle: tp, 230 }, 231 }, 232 } 233 234 case *vegapb.EthCallSpec: 235 ds.SourceType = &vegapb.DataSourceDefinition_External{ 236 External: &vegapb.DataSourceDefinitionExternal{ 237 SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{ 238 EthOracle: tp, 239 }, 240 }, 241 } 242 243 case *vegapb.DataSourceSpecConfigurationTime: 244 ds.SourceType = &vegapb.DataSourceDefinition_Internal{ 245 Internal: &vegapb.DataSourceDefinitionInternal{ 246 SourceType: &vegapb.DataSourceDefinitionInternal_Time{ 247 Time: tp, 248 }, 249 }, 250 } 251 case *vegapb.DataSourceSpecConfigurationTimeTrigger: 252 ds.SourceType = &vegapb.DataSourceDefinition_Internal{ 253 Internal: &vegapb.DataSourceDefinitionInternal{ 254 SourceType: &vegapb.DataSourceDefinitionInternal_TimeTrigger{ 255 TimeTrigger: tp, 256 }, 257 }, 258 } 259 } 260 } 261 262 return ds 263 } 264 265 func resolveDataSourceSpec(d *vegapb.DataSourceSpec) (ds *DataSourceSpec) { 266 ds = &DataSourceSpec{ 267 Data: &vegapb.DataSourceDefinition{}, 268 } 269 if d == nil { 270 return 271 } 272 273 ds.ID = d.GetId() 274 ds.CreatedAt = d.CreatedAt 275 if d.UpdatedAt != 0 { 276 ds.UpdatedAt = ptr.From(d.UpdatedAt) 277 } 278 279 switch d.Status { 280 case vegapb.DataSourceSpec_STATUS_ACTIVE: 281 ds.Status = DataSourceSpecStatusStatusActive 282 case vegapb.DataSourceSpec_STATUS_DEACTIVATED: 283 ds.Status = DataSourceSpecStatusStatusDeactivated 284 } 285 286 if d.Data != nil { 287 ds.Data = resolveDataSourceDefinition(d.Data) 288 } 289 290 return 291 }