code.vegaprotocol.io/vega@v0.79.0/core/events/market_event_test.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 events_test 17 18 import ( 19 "context" 20 "testing" 21 22 "code.vegaprotocol.io/vega/core/datasource" 23 dstypes "code.vegaprotocol.io/vega/core/datasource/common" 24 "code.vegaprotocol.io/vega/core/datasource/external/signedoracle" 25 "code.vegaprotocol.io/vega/core/events" 26 "code.vegaprotocol.io/vega/core/types" 27 "code.vegaprotocol.io/vega/libs/num" 28 "code.vegaprotocol.io/vega/protos/vega" 29 vegapb "code.vegaprotocol.io/vega/protos/vega" 30 datapb "code.vegaprotocol.io/vega/protos/vega/data/v1" 31 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func changeOracleSpec(spec *datasource.Spec) { 37 spec.ID = "Changed" 38 spec.CreatedAt = 999 39 spec.UpdatedAt = 999 40 41 filters := []*dstypes.SpecFilter{ 42 { 43 Key: &dstypes.SpecPropertyKey{ 44 Name: "Changed", 45 Type: datapb.PropertyKey_TYPE_UNSPECIFIED, 46 }, 47 Conditions: []*dstypes.SpecCondition{ 48 { 49 Operator: datapb.Condition_OPERATOR_UNSPECIFIED, 50 Value: "Changed", 51 }, 52 }, 53 }, 54 } 55 56 spec.Data.SetOracleConfig( 57 &signedoracle.SpecConfiguration{ 58 Signers: []*dstypes.Signer{dstypes.CreateSignerFromString("Changed", dstypes.SignerTypePubKey)}, 59 Filters: filters, 60 }, 61 ) 62 63 spec.Status = vegapb.DataSourceSpec_STATUS_UNSPECIFIED 64 } 65 66 func assertSpecsNotEqual(t *testing.T, spec1 *datasource.Spec, spec2 *datasource.Spec) { 67 t.Helper() 68 assert.NotEqual(t, spec1.ID, spec2.ID) 69 assert.NotEqual(t, spec1.CreatedAt, spec2.CreatedAt) 70 assert.NotEqual(t, spec1.UpdatedAt, spec2.UpdatedAt) 71 assert.NotEqual(t, spec1.Data.GetSigners()[0], spec2.Data.GetSigners()[0]) 72 assert.NotEqual(t, spec1.Data.GetFilters()[0].Key.Name, spec2.Data.GetFilters()[0].Key.Name) 73 assert.NotEqual(t, spec1.Data.GetFilters()[0].Key.Type, spec2.Data.GetFilters()[0].Key.Type) 74 assert.NotEqual(t, spec1.Data.GetFilters()[0].Conditions[0].Operator, spec2.Data.GetFilters()[0].Conditions[0].Operator) 75 assert.NotEqual(t, spec1.Data.GetFilters()[0].Conditions[0].Value, spec2.Data.GetFilters()[0].Conditions[0].Value) 76 assert.NotEqual(t, spec1.Status, spec2.Status) 77 } 78 79 func TestMarketDeepClone(t *testing.T) { 80 ctx := context.Background() 81 pubKeys := []*dstypes.Signer{ 82 dstypes.CreateSignerFromString("PubKey", dstypes.SignerTypePubKey), 83 } 84 85 pme := vegapb.Market{ 86 Id: "Id", 87 TradableInstrument: &vegapb.TradableInstrument{ 88 Instrument: &vegapb.Instrument{ 89 Id: "Id", 90 Code: "Code", 91 Name: "Name", 92 Metadata: &vegapb.InstrumentMetadata{ 93 Tags: []string{"Tag1", "Tag2"}, 94 }, 95 Product: &vegapb.Instrument_Future{ 96 Future: &vegapb.Future{ 97 SettlementAsset: "Asset", 98 QuoteName: "QuoteName", 99 DataSourceSpecForSettlementData: &vegapb.DataSourceSpec{ 100 Id: "Id", 101 CreatedAt: 1000, 102 UpdatedAt: 2000, 103 Data: &vegapb.DataSourceDefinition{ 104 SourceType: &vegapb.DataSourceDefinition_External{ 105 External: &vegapb.DataSourceDefinitionExternal{ 106 SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{ 107 Oracle: &vegapb.DataSourceSpecConfiguration{ 108 Signers: dstypes.SignersIntoProto(pubKeys), 109 Filters: []*datapb.Filter{ 110 { 111 Key: &datapb.PropertyKey{ 112 Name: "Name", 113 Type: datapb.PropertyKey_TYPE_DECIMAL, 114 }, 115 Conditions: []*datapb.Condition{ 116 { 117 Operator: datapb.Condition_OPERATOR_EQUALS, 118 Value: "Value", 119 }, 120 }, 121 }, 122 }, 123 }, 124 }, 125 }, 126 }, 127 }, 128 Status: vegapb.DataSourceSpec_STATUS_ACTIVE, 129 }, 130 DataSourceSpecForTradingTermination: &vegapb.DataSourceSpec{ 131 Id: "Id2", 132 CreatedAt: 1000, 133 UpdatedAt: 2000, 134 Data: &vegapb.DataSourceDefinition{ 135 SourceType: &vegapb.DataSourceDefinition_External{ 136 External: &vegapb.DataSourceDefinitionExternal{ 137 SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{ 138 Oracle: &vegapb.DataSourceSpecConfiguration{ 139 Signers: dstypes.SignersIntoProto(pubKeys), 140 Filters: []*datapb.Filter{ 141 { 142 Key: &datapb.PropertyKey{ 143 Name: "Name", 144 Type: datapb.PropertyKey_TYPE_BOOLEAN, 145 }, 146 Conditions: []*datapb.Condition{ 147 { 148 Operator: datapb.Condition_OPERATOR_EQUALS, 149 Value: "Value", 150 }, 151 }, 152 }, 153 }, 154 }, 155 }, 156 }, 157 }, 158 }, 159 Status: vegapb.DataSourceSpec_STATUS_ACTIVE, 160 }, 161 162 DataSourceSpecBinding: &vegapb.DataSourceSpecToFutureBinding{ 163 SettlementDataProperty: "SettlementData", 164 TradingTerminationProperty: "trading.terminated", 165 }, 166 }, 167 }, 168 }, 169 MarginCalculator: &vegapb.MarginCalculator{ 170 ScalingFactors: &vegapb.ScalingFactors{ 171 SearchLevel: 123.45, 172 InitialMargin: 234.56, 173 CollateralRelease: 345.67, 174 }, 175 }, 176 RiskModel: &vegapb.TradableInstrument_SimpleRiskModel{ 177 SimpleRiskModel: &vegapb.SimpleRiskModel{ 178 Params: &vegapb.SimpleModelParams{ 179 FactorLong: 123.45, 180 FactorShort: 234.56, 181 MaxMoveUp: 345.67, 182 MinMoveDown: 456.78, 183 ProbabilityOfTrading: 567.89, 184 }, 185 }, 186 }, 187 }, 188 DecimalPlaces: 5, 189 Fees: &vegapb.Fees{ 190 Factors: &vegapb.FeeFactors{ 191 MakerFee: "0.1", 192 InfrastructureFee: "0.2", 193 LiquidityFee: "0.3", 194 }, 195 LiquidityFeeSettings: &vega.LiquidityFeeSettings{ 196 Method: vega.LiquidityFeeSettings_METHOD_MARGINAL_COST, 197 }, 198 }, 199 OpeningAuction: &vegapb.AuctionDuration{ 200 Duration: 1000, 201 Volume: 2000, 202 }, 203 PriceMonitoringSettings: &vegapb.PriceMonitoringSettings{ 204 Parameters: &vegapb.PriceMonitoringParameters{ 205 Triggers: []*vegapb.PriceMonitoringTrigger{ 206 { 207 Horizon: 1000, 208 Probability: "123.45", 209 AuctionExtension: 2000, 210 }, 211 }, 212 }, 213 }, 214 LiquidityMonitoringParameters: &vegapb.LiquidityMonitoringParameters{ 215 TargetStakeParameters: &vegapb.TargetStakeParameters{ 216 TimeWindow: 1000, 217 ScalingFactor: 2.0, 218 }, 219 }, 220 LiquiditySlaParams: &vegapb.LiquiditySLAParameters{ 221 PriceRange: "0.95", 222 CommitmentMinTimeFraction: "0.5", 223 PerformanceHysteresisEpochs: 4, 224 SlaCompetitionFactor: "0.5", 225 }, 226 TradingMode: vegapb.Market_TRADING_MODE_CONTINUOUS, 227 State: vegapb.Market_STATE_ACTIVE, 228 MarketTimestamps: &vegapb.MarketTimestamps{ 229 Proposed: 1000, 230 Pending: 2000, 231 Open: 3000, 232 Close: 4000, 233 }, 234 } 235 236 me, err := types.MarketFromProto(&pme) 237 require.NoError(t, err) 238 marketEvent := events.NewMarketCreatedEvent(ctx, *me) 239 mktProto := marketEvent.Market() 240 me2, err := types.MarketFromProto(&mktProto) 241 require.NoError(t, err) 242 243 // Change the original and check we are not updating the wrapped event 244 me.ID = "Changed" 245 me.TradableInstrument.Instrument.ID = "Changed" 246 me.TradableInstrument.Instrument.Code = "Changed" 247 me.TradableInstrument.Instrument.Name = "Changed" 248 me.TradableInstrument.Instrument.Metadata.Tags[0] = "Changed1" 249 me.TradableInstrument.Instrument.Metadata.Tags[1] = "Changed2" 250 future := me.TradableInstrument.Instrument.Product.(*types.InstrumentFuture) 251 future.Future.SettlementAsset = "Changed" 252 future.Future.QuoteName = "Changed" 253 changeOracleSpec(future.Future.DataSourceSpecForSettlementData) 254 changeOracleSpec(future.Future.DataSourceSpecForTradingTermination) 255 future.Future.DataSourceSpecBinding.SettlementDataProperty = "Changed" 256 future.Future.DataSourceSpecBinding.TradingTerminationProperty = "Changed" 257 258 me.TradableInstrument.MarginCalculator.ScalingFactors.SearchLevel = num.DecimalFromFloat(99.9) 259 me.TradableInstrument.MarginCalculator.ScalingFactors.InitialMargin = num.DecimalFromFloat(99.9) 260 me.TradableInstrument.MarginCalculator.ScalingFactors.CollateralRelease = num.DecimalFromFloat(99.9) 261 262 risk := me.TradableInstrument.RiskModel.(*types.TradableInstrumentSimpleRiskModel) 263 risk.SimpleRiskModel.Params.FactorLong = num.DecimalFromFloat(99.9) 264 risk.SimpleRiskModel.Params.FactorShort = num.DecimalFromFloat(99.9) 265 risk.SimpleRiskModel.Params.MaxMoveUp = num.DecimalFromFloat(99.9) 266 risk.SimpleRiskModel.Params.MinMoveDown = num.DecimalFromFloat(99.9) 267 risk.SimpleRiskModel.Params.ProbabilityOfTrading = num.DecimalFromFloat(99.9) 268 269 me.DecimalPlaces = 999 270 me.Fees.Factors.MakerFee = num.DecimalFromFloat(1999.) 271 me.Fees.Factors.InfrastructureFee = num.DecimalFromFloat(1999.) 272 me.Fees.Factors.LiquidityFee = num.DecimalFromFloat(1999.) 273 274 me.OpeningAuction.Duration = 999 275 me.OpeningAuction.Volume = 999 276 277 me.PriceMonitoringSettings.Parameters.Triggers[0].Horizon = 999 278 me.PriceMonitoringSettings.Parameters.Triggers[0].Probability = num.DecimalFromFloat(99.9) 279 me.PriceMonitoringSettings.Parameters.Triggers[0].AuctionExtension = 999 280 281 me.LiquidityMonitoringParameters.TargetStakeParameters.TimeWindow = 999 282 me.LiquidityMonitoringParameters.TargetStakeParameters.ScalingFactor = num.DecimalFromFloat(99.9) 283 284 me.TradingMode = vegapb.Market_TRADING_MODE_UNSPECIFIED 285 me.State = vegapb.Market_STATE_UNSPECIFIED 286 me.MarketTimestamps.Proposed = 999 287 me.MarketTimestamps.Pending = 999 288 me.MarketTimestamps.Open = 999 289 me.MarketTimestamps.Close = 999 290 291 assert.NotEqual(t, me.ID, me2.ID) 292 293 assert.NotEqual(t, me.TradableInstrument.Instrument.ID, me2.TradableInstrument.Instrument.ID) 294 assert.NotEqual(t, me.TradableInstrument.Instrument.Code, me2.TradableInstrument.Instrument.Code) 295 assert.NotEqual(t, me.TradableInstrument.Instrument.Name, me2.TradableInstrument.Instrument.Name) 296 assert.NotEqual(t, me.TradableInstrument.Instrument.Metadata.Tags[0], me2.TradableInstrument.Instrument.Metadata.Tags[0]) 297 assert.NotEqual(t, me.TradableInstrument.Instrument.Metadata.Tags[1], me2.TradableInstrument.Instrument.Metadata.Tags[1]) 298 299 future2 := me2.TradableInstrument.Instrument.Product.(*types.InstrumentFuture) 300 301 assert.NotEqual(t, future.Future.SettlementAsset, future2.Future.SettlementAsset) 302 assert.NotEqual(t, future.Future.QuoteName, future2.Future.QuoteName) 303 assertSpecsNotEqual(t, future.Future.DataSourceSpecForSettlementData, future2.Future.DataSourceSpecForSettlementData) 304 assertSpecsNotEqual(t, future.Future.DataSourceSpecForTradingTermination, future2.Future.DataSourceSpecForTradingTermination) 305 assert.NotEqual(t, future.Future.DataSourceSpecBinding.TradingTerminationProperty, future2.Future.DataSourceSpecBinding.TradingTerminationProperty) 306 assert.NotEqual(t, future.Future.DataSourceSpecBinding.SettlementDataProperty, future2.Future.DataSourceSpecBinding.SettlementDataProperty) 307 308 assert.NotEqual(t, me.TradableInstrument.MarginCalculator.ScalingFactors.SearchLevel, me2.TradableInstrument.MarginCalculator.ScalingFactors.SearchLevel) 309 assert.NotEqual(t, me.TradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, me2.TradableInstrument.MarginCalculator.ScalingFactors.InitialMargin) 310 assert.NotEqual(t, me.TradableInstrument.MarginCalculator.ScalingFactors.CollateralRelease, me2.TradableInstrument.MarginCalculator.ScalingFactors.CollateralRelease) 311 312 risk2 := me2.TradableInstrument.RiskModel.(*types.TradableInstrumentSimpleRiskModel) 313 assert.NotEqual(t, risk.SimpleRiskModel.Params.FactorLong, risk2.SimpleRiskModel.Params.FactorLong) 314 assert.NotEqual(t, risk.SimpleRiskModel.Params.FactorShort, risk2.SimpleRiskModel.Params.FactorShort) 315 assert.NotEqual(t, risk.SimpleRiskModel.Params.MaxMoveUp, risk2.SimpleRiskModel.Params.MaxMoveUp) 316 assert.NotEqual(t, risk.SimpleRiskModel.Params.MinMoveDown, risk2.SimpleRiskModel.Params.MinMoveDown) 317 assert.NotEqual(t, risk.SimpleRiskModel.Params.ProbabilityOfTrading, risk2.SimpleRiskModel.Params.ProbabilityOfTrading) 318 319 assert.NotEqual(t, me.DecimalPlaces, me2.DecimalPlaces) 320 assert.NotEqual(t, me.Fees.Factors.MakerFee, me2.Fees.Factors.MakerFee) 321 assert.NotEqual(t, me.Fees.Factors.InfrastructureFee, me2.Fees.Factors.InfrastructureFee) 322 assert.NotEqual(t, me.Fees.Factors.LiquidityFee, me2.Fees.Factors.LiquidityFee) 323 assert.NotEqual(t, me.OpeningAuction.Duration, me2.OpeningAuction.Duration) 324 assert.NotEqual(t, me.OpeningAuction.Volume, me2.OpeningAuction.Volume) 325 326 assert.NotEqual(t, me.PriceMonitoringSettings.Parameters.Triggers[0].Horizon, me2.PriceMonitoringSettings.Parameters.Triggers[0].Horizon) 327 assert.NotEqual(t, me.PriceMonitoringSettings.Parameters.Triggers[0].Probability, me2.PriceMonitoringSettings.Parameters.Triggers[0].Probability) 328 assert.NotEqual(t, me.PriceMonitoringSettings.Parameters.Triggers[0].AuctionExtension, me2.PriceMonitoringSettings.Parameters.Triggers[0].AuctionExtension) 329 assert.NotEqual(t, me.LiquidityMonitoringParameters.TargetStakeParameters.TimeWindow, me2.LiquidityMonitoringParameters.TargetStakeParameters.TimeWindow) 330 assert.NotEqual(t, me.LiquidityMonitoringParameters.TargetStakeParameters.ScalingFactor, me2.LiquidityMonitoringParameters.TargetStakeParameters.ScalingFactor) 331 assert.NotEqual(t, me.TradingMode, me2.TradingMode) 332 assert.NotEqual(t, me.State, me2.State) 333 assert.NotEqual(t, me.MarketTimestamps.Proposed, me2.MarketTimestamps.Proposed) 334 assert.NotEqual(t, me.MarketTimestamps.Pending, me2.MarketTimestamps.Pending) 335 assert.NotEqual(t, me.MarketTimestamps.Open, me2.MarketTimestamps.Open) 336 assert.NotEqual(t, me.MarketTimestamps.Close, me2.MarketTimestamps.Close) 337 }