code.vegaprotocol.io/vega@v0.79.0/core/types/liquidity.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 types 17 18 import ( 19 "errors" 20 "fmt" 21 22 "code.vegaprotocol.io/vega/libs/num" 23 "code.vegaprotocol.io/vega/libs/ptr" 24 "code.vegaprotocol.io/vega/libs/stringer" 25 proto "code.vegaprotocol.io/vega/protos/vega" 26 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 27 ) 28 29 type LiquidityFeeMethod = proto.LiquidityFeeSettings_Method 30 31 const ( 32 LiquidityFeeMethodUnspecified LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_UNSPECIFIED 33 LiquidityFeeMethodMarginalCost LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_MARGINAL_COST 34 LiquidityFeeMethodWeightedAverage LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_WEIGHTED_AVERAGE 35 LiquidityFeeMethodConstant LiquidityFeeMethod = proto.LiquidityFeeSettings_METHOD_CONSTANT 36 ) 37 38 type LiquidityProvisionStatus = proto.LiquidityProvision_Status 39 40 const ( 41 // LiquidityProvisionUnspecified The default value. 42 LiquidityProvisionUnspecified LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_UNSPECIFIED 43 // LiquidityProvisionStatusActive The liquidity provision is active. 44 LiquidityProvisionStatusActive LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_ACTIVE 45 // LiquidityProvisionStatusStopped The liquidity provision was stopped by the network. 46 LiquidityProvisionStatusStopped LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_STOPPED 47 // LiquidityProvisionStatusCancelled The liquidity provision was cancelled by the liquidity provider. 48 LiquidityProvisionStatusCancelled LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_CANCELLED 49 // LiquidityProvisionStatusRejected The liquidity provision was invalid and got rejected. 50 LiquidityProvisionStatusRejected LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_REJECTED 51 // LiquidityProvisionStatusUndeployed The liquidity provision is valid and accepted by network, but orders aren't deployed. 52 LiquidityProvisionStatusUndeployed LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_UNDEPLOYED 53 // LiquidityProvisionStatusPending The liquidity provision is valid and accepted by network 54 // but have never been deployed. I when it's possible to deploy them for the first time 55 // margin check fails, then they will be cancelled without any penalties. 56 LiquidityProvisionStatusPending LiquidityProvisionStatus = proto.LiquidityProvision_STATUS_PENDING 57 ) 58 59 type LiquiditySLAParams struct { 60 PriceRange num.Decimal 61 CommitmentMinTimeFraction num.Decimal 62 PerformanceHysteresisEpochs uint64 63 SlaCompetitionFactor num.Decimal 64 } 65 66 func (l LiquiditySLAParams) IntoProto() *proto.LiquiditySLAParameters { 67 return &proto.LiquiditySLAParameters{ 68 PriceRange: l.PriceRange.String(), 69 CommitmentMinTimeFraction: l.CommitmentMinTimeFraction.String(), 70 PerformanceHysteresisEpochs: l.PerformanceHysteresisEpochs, 71 SlaCompetitionFactor: l.SlaCompetitionFactor.String(), 72 } 73 } 74 75 func LiquiditySLAParamsFromProto(l *proto.LiquiditySLAParameters) *LiquiditySLAParams { 76 if l == nil { 77 return nil 78 } 79 return &LiquiditySLAParams{ 80 PriceRange: num.MustDecimalFromString(l.PriceRange), 81 CommitmentMinTimeFraction: num.MustDecimalFromString(l.CommitmentMinTimeFraction), 82 PerformanceHysteresisEpochs: l.PerformanceHysteresisEpochs, 83 SlaCompetitionFactor: num.MustDecimalFromString(l.SlaCompetitionFactor), 84 } 85 } 86 87 func (l LiquiditySLAParams) String() string { 88 return fmt.Sprintf( 89 "priceRange(%s) commitmentMinTimeFraction(%s) performanceHysteresisEpochs(%v) slaCompetitionFactor(%s)", 90 l.PriceRange.String(), 91 l.CommitmentMinTimeFraction.String(), 92 l.PerformanceHysteresisEpochs, 93 l.SlaCompetitionFactor.String(), 94 ) 95 } 96 97 func (l LiquiditySLAParams) DeepClone() *LiquiditySLAParams { 98 return &LiquiditySLAParams{ 99 PriceRange: l.PriceRange, 100 CommitmentMinTimeFraction: l.CommitmentMinTimeFraction, 101 PerformanceHysteresisEpochs: l.PerformanceHysteresisEpochs, 102 SlaCompetitionFactor: l.SlaCompetitionFactor, 103 } 104 } 105 106 type TargetStakeParameters struct { 107 TimeWindow int64 108 ScalingFactor num.Decimal 109 } 110 111 func (t TargetStakeParameters) IntoProto() *proto.TargetStakeParameters { 112 sf, _ := t.ScalingFactor.Float64() 113 return &proto.TargetStakeParameters{ 114 TimeWindow: t.TimeWindow, 115 ScalingFactor: sf, 116 } 117 } 118 119 func TargetStakeParametersFromProto(p *proto.TargetStakeParameters) *TargetStakeParameters { 120 return &TargetStakeParameters{ 121 TimeWindow: p.TimeWindow, 122 ScalingFactor: num.DecimalFromFloat(p.ScalingFactor), 123 } 124 } 125 126 func (t TargetStakeParameters) String() string { 127 return fmt.Sprintf( 128 "timeWindows(%v) scalingFactor(%s)", 129 t.TimeWindow, 130 t.ScalingFactor.String(), 131 ) 132 } 133 134 func (t TargetStakeParameters) DeepClone() *TargetStakeParameters { 135 return &TargetStakeParameters{ 136 TimeWindow: t.TimeWindow, 137 ScalingFactor: t.ScalingFactor, 138 } 139 } 140 141 type LiquidityProvisionSubmission struct { 142 // Market identifier for the order, required field 143 MarketID string 144 // Specified as a unitless number that represents the amount of settlement asset of the market 145 CommitmentAmount *num.Uint 146 // Nominated liquidity fee factor, which is an input to the calculation of taker fees on the market, as per setting fees and rewarding liquidity providers 147 Fee num.Decimal 148 // A reference to be added to every order created out of this liquidityProvisionSubmission 149 Reference string 150 } 151 152 func (l LiquidityProvisionSubmission) IntoProto() *commandspb.LiquidityProvisionSubmission { 153 return &commandspb.LiquidityProvisionSubmission{ 154 MarketId: l.MarketID, 155 CommitmentAmount: num.UintToString(l.CommitmentAmount), 156 Fee: l.Fee.String(), 157 Reference: l.Reference, 158 } 159 } 160 161 func LiquidityProvisionSubmissionFromProto(p *commandspb.LiquidityProvisionSubmission) (*LiquidityProvisionSubmission, error) { 162 fee, err := num.DecimalFromString(p.Fee) 163 if err != nil { 164 return nil, err 165 } 166 167 commitmentAmount := num.UintZero() 168 if len(p.CommitmentAmount) > 0 { 169 var overflowed bool 170 commitmentAmount, overflowed = num.UintFromString(p.CommitmentAmount, 10) 171 if overflowed { 172 return nil, errors.New("invalid commitment amount") 173 } 174 } 175 176 l := LiquidityProvisionSubmission{ 177 Fee: fee, 178 MarketID: p.MarketId, 179 CommitmentAmount: commitmentAmount, 180 Reference: p.Reference, 181 } 182 183 return &l, nil 184 } 185 186 func (l LiquidityProvisionSubmission) String() string { 187 return fmt.Sprintf( 188 "marketID(%s) reference(%s) commitmentAmount(%s) fee(%s)", 189 l.MarketID, 190 l.Reference, 191 stringer.PtrToString(l.CommitmentAmount), 192 l.Fee.String(), 193 ) 194 } 195 196 type LiquidityProvision struct { 197 // Unique identifier 198 ID string 199 // Unique party identifier for the creator of the provision 200 Party string 201 // Timestamp for when the order was created at, in nanoseconds since the epoch 202 // - See [`VegaTimeResponse`](#api.VegaTimeResponse).`timestamp` 203 CreatedAt int64 204 // Timestamp for when the order was updated at, in nanoseconds since the epoch 205 // - See [`VegaTimeResponse`](#api.VegaTimeResponse).`timestamp` 206 UpdatedAt int64 207 // Market identifier for the order, required field 208 MarketID string 209 // Specified as a unitless number that represents the amount of settlement asset of the market 210 CommitmentAmount *num.Uint 211 // Nominated liquidity fee factor, which is an input to the calculation of taker fees on the market, as per seeting fees and rewarding liquidity providers 212 Fee num.Decimal 213 // Version of this liquidity provision 214 Version uint64 215 // Status of this liquidity provision 216 Status LiquidityProvisionStatus 217 // A reference shared between this liquidity provision and all it's orders 218 Reference string 219 } 220 221 func (l LiquidityProvision) String() string { 222 return fmt.Sprintf( 223 "ID(%s) marketID(%s) party(%s) status(%s) reference(%s) commitmentAmount(%s) fee(%s) version(%v) createdAt(%v) updatedAt(%v)", 224 l.ID, 225 l.MarketID, 226 l.Party, 227 l.Status.String(), 228 l.Reference, 229 stringer.PtrToString(l.CommitmentAmount), 230 l.Fee.String(), 231 l.Version, 232 l.CreatedAt, 233 l.UpdatedAt, 234 ) 235 } 236 237 func (l LiquidityProvision) IntoProto() *proto.LiquidityProvision { 238 lp := &proto.LiquidityProvision{ 239 Id: l.ID, 240 PartyId: l.Party, 241 CreatedAt: l.CreatedAt, 242 UpdatedAt: l.UpdatedAt, 243 MarketId: l.MarketID, 244 CommitmentAmount: num.UintToString(l.CommitmentAmount), 245 Fee: l.Fee.String(), 246 Version: l.Version, 247 Status: l.Status, 248 Reference: l.Reference, 249 } 250 251 return lp 252 } 253 254 func LiquidityProvisionFromProto(p *proto.LiquidityProvision) (*LiquidityProvision, error) { 255 fee, _ := num.DecimalFromString(p.Fee) 256 commitmentAmount := num.UintZero() 257 if len(p.CommitmentAmount) > 0 { 258 var overflowed bool 259 commitmentAmount, overflowed = num.UintFromString(p.CommitmentAmount, 10) 260 if overflowed { 261 return nil, errors.New("invalid commitment amount") 262 } 263 } 264 l := LiquidityProvision{ 265 CommitmentAmount: commitmentAmount, 266 CreatedAt: p.CreatedAt, 267 ID: p.Id, 268 MarketID: p.MarketId, 269 Party: p.PartyId, 270 Fee: fee, 271 Reference: p.Reference, 272 Status: p.Status, 273 UpdatedAt: p.UpdatedAt, 274 Version: p.Version, 275 } 276 277 return &l, nil 278 } 279 280 type LiquidityMonitoringParameters struct { 281 // Specifies parameters related to target stake calculation 282 TargetStakeParameters *TargetStakeParameters 283 } 284 285 func (l LiquidityMonitoringParameters) IntoProto() *proto.LiquidityMonitoringParameters { 286 var params *proto.TargetStakeParameters 287 if l.TargetStakeParameters != nil { 288 params = l.TargetStakeParameters.IntoProto() 289 } 290 return &proto.LiquidityMonitoringParameters{ 291 TargetStakeParameters: params, 292 } 293 } 294 295 func (l LiquidityMonitoringParameters) DeepClone() *LiquidityMonitoringParameters { 296 var params *TargetStakeParameters 297 if l.TargetStakeParameters != nil { 298 params = l.TargetStakeParameters.DeepClone() 299 } 300 return &LiquidityMonitoringParameters{ 301 TargetStakeParameters: params, 302 } 303 } 304 305 func (l LiquidityMonitoringParameters) String() string { 306 return fmt.Sprintf( 307 "auctionExtension(%v)", 308 stringer.PtrToString(l.TargetStakeParameters), 309 ) 310 } 311 312 func LiquidityMonitoringParametersFromProto(p *proto.LiquidityMonitoringParameters) (*LiquidityMonitoringParameters, error) { 313 if p == nil { 314 return nil, nil 315 } 316 var params *TargetStakeParameters 317 if p.TargetStakeParameters != nil { 318 params = TargetStakeParametersFromProto(p.TargetStakeParameters) 319 } 320 321 return &LiquidityMonitoringParameters{ 322 TargetStakeParameters: params, 323 }, nil 324 } 325 326 type LiquidityProvisionAmendment struct { 327 // Market identifier for the order, required field 328 MarketID string 329 // Specified as a unitless number that represents the amount of settlement asset of the market 330 CommitmentAmount *num.Uint 331 // Nominated liquidity fee factor, which is an input to the calculation of taker fees on the market, as per setting fees and rewarding liquidity providers 332 Fee num.Decimal 333 // A reference to be added to every order created out of this liquidityProvisionAmendment 334 Reference string 335 } 336 337 func LiquidityProvisionAmendmentFromProto(p *commandspb.LiquidityProvisionAmendment) (*LiquidityProvisionAmendment, error) { 338 fee, err := num.DecimalFromString(p.Fee) 339 if err != nil { 340 return nil, err 341 } 342 343 commitmentAmount := num.UintZero() 344 if len(p.CommitmentAmount) > 0 { 345 var overflowed bool 346 commitmentAmount, overflowed = num.UintFromString(p.CommitmentAmount, 10) 347 if overflowed { 348 return nil, errors.New("invalid commitment amount") 349 } 350 } 351 352 return &LiquidityProvisionAmendment{ 353 Fee: fee, 354 MarketID: p.MarketId, 355 CommitmentAmount: commitmentAmount, 356 Reference: p.Reference, 357 }, nil 358 } 359 360 func (a LiquidityProvisionAmendment) IntoProto() *commandspb.LiquidityProvisionAmendment { 361 return &commandspb.LiquidityProvisionAmendment{ 362 MarketId: a.MarketID, 363 CommitmentAmount: num.UintToString(a.CommitmentAmount), 364 Fee: a.Fee.String(), 365 Reference: a.Reference, 366 } 367 } 368 369 func (a LiquidityProvisionAmendment) String() string { 370 return fmt.Sprintf( 371 "marketID(%s) reference(%s) commitmentAmount(%s) fee(%s)", 372 a.MarketID, 373 a.Reference, 374 stringer.PtrToString(a.CommitmentAmount), 375 a.Fee.String(), 376 ) 377 } 378 379 func (a LiquidityProvisionAmendment) GetMarketID() string { 380 return a.MarketID 381 } 382 383 type LiquidityProvisionCancellation struct { 384 // Market identifier for the order, required field 385 MarketID string 386 } 387 388 func LiquidityProvisionCancellationFromProto(p *commandspb.LiquidityProvisionCancellation) (*LiquidityProvisionCancellation, error) { 389 l := LiquidityProvisionCancellation{ 390 MarketID: p.MarketId, 391 } 392 393 return &l, nil 394 } 395 396 func (l LiquidityProvisionCancellation) IntoProto() *commandspb.LiquidityProvisionCancellation { 397 return &commandspb.LiquidityProvisionCancellation{ 398 MarketId: l.MarketID, 399 } 400 } 401 402 func (l LiquidityProvisionCancellation) String() string { 403 return fmt.Sprintf("marketID(%s)", l.MarketID) 404 } 405 406 func (l LiquidityProvisionCancellation) GetMarketID() string { 407 return l.MarketID 408 } 409 410 type LiquidityFeeSettings struct { 411 Method LiquidityFeeMethod 412 FeeConstant num.Decimal 413 } 414 415 func (l *LiquidityFeeSettings) IntoProto() *proto.LiquidityFeeSettings { 416 if l == nil { 417 return nil 418 } 419 420 r := &proto.LiquidityFeeSettings{ 421 Method: l.Method, 422 } 423 424 if l.Method == LiquidityFeeMethodConstant { 425 r.FeeConstant = ptr.From(l.FeeConstant.String()) 426 } 427 428 return r 429 } 430 431 func LiquidityFeeSettingsFromProto(l *proto.LiquidityFeeSettings) *LiquidityFeeSettings { 432 if l == nil { 433 return nil 434 } 435 436 fc := num.DecimalZero() 437 if l.Method == LiquidityFeeMethodConstant { 438 fc, _ = num.DecimalFromString(*l.FeeConstant) 439 } 440 441 return &LiquidityFeeSettings{ 442 Method: l.Method, 443 FeeConstant: fc, 444 } 445 } 446 447 func (l *LiquidityFeeSettings) DeepClone() *LiquidityFeeSettings { 448 if l == nil { 449 return nil 450 } 451 return &LiquidityFeeSettings{ 452 Method: l.Method, 453 FeeConstant: l.FeeConstant, 454 } 455 } 456 457 func (l LiquidityFeeSettings) String() string { 458 return fmt.Sprintf( 459 "method(%s) feeConstant(%s)", 460 l.Method.String(), 461 l.FeeConstant.String(), 462 ) 463 }