code.vegaprotocol.io/vega@v0.79.0/datanode/entities/trade.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 entities 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "time" 22 23 "code.vegaprotocol.io/vega/libs/num" 24 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 25 "code.vegaprotocol.io/vega/protos/vega" 26 27 "github.com/shopspring/decimal" 28 ) 29 30 type _Trade struct{} 31 32 type TradeID = ID[_Trade] 33 34 type Trade struct { 35 SyntheticTime time.Time 36 TxHash TxHash 37 VegaTime time.Time 38 SeqNum uint64 39 ID TradeID 40 MarketID MarketID 41 Price decimal.Decimal 42 Size uint64 43 Buyer PartyID 44 Seller PartyID 45 Aggressor Side 46 BuyOrder OrderID 47 SellOrder OrderID 48 Type TradeType 49 BuyerMakerFee decimal.Decimal 50 BuyerInfrastructureFee decimal.Decimal 51 BuyerLiquidityFee decimal.Decimal 52 BuyerBuyBackFee decimal.Decimal 53 BuyerTreasuryFee decimal.Decimal 54 BuyerHighVolumeMakerFee decimal.Decimal 55 SellerMakerFee decimal.Decimal 56 SellerInfrastructureFee decimal.Decimal 57 SellerLiquidityFee decimal.Decimal 58 SellerBuyBackFee decimal.Decimal 59 SellerTreasuryFee decimal.Decimal 60 SellerHighVolumeMakerFee decimal.Decimal 61 62 BuyerMakerFeeReferralDiscount decimal.Decimal 63 BuyerMakerFeeVolumeDiscount decimal.Decimal 64 BuyerInfrastructureFeeReferralDiscount decimal.Decimal 65 BuyerInfrastructureFeeVolumeDiscount decimal.Decimal 66 BuyerLiquidityFeeReferralDiscount decimal.Decimal 67 BuyerLiquidityFeeVolumeDiscount decimal.Decimal 68 SellerMakerFeeReferralDiscount decimal.Decimal 69 SellerMakerFeeVolumeDiscount decimal.Decimal 70 SellerInfrastructureFeeReferralDiscount decimal.Decimal 71 SellerInfrastructureFeeVolumeDiscount decimal.Decimal 72 SellerLiquidityFeeReferralDiscount decimal.Decimal 73 SellerLiquidityFeeVolumeDiscount decimal.Decimal 74 75 BuyerAuctionBatch uint64 76 SellerAuctionBatch uint64 77 } 78 79 func (t Trade) ToProto() *vega.Trade { 80 return &vega.Trade{ 81 Id: t.ID.String(), 82 MarketId: t.MarketID.String(), 83 Price: t.Price.String(), 84 Size: t.Size, 85 Buyer: t.Buyer.String(), 86 Seller: t.Seller.String(), 87 Aggressor: t.Aggressor, 88 BuyOrder: t.BuyOrder.String(), 89 SellOrder: t.SellOrder.String(), 90 Timestamp: t.VegaTime.UnixNano(), 91 Type: t.Type, 92 BuyerFee: &vega.Fee{ 93 MakerFee: t.BuyerMakerFee.String(), 94 InfrastructureFee: t.BuyerInfrastructureFee.String(), 95 LiquidityFee: t.BuyerLiquidityFee.String(), 96 MakerFeeReferrerDiscount: t.BuyerMakerFeeReferralDiscount.String(), 97 MakerFeeVolumeDiscount: t.BuyerMakerFeeVolumeDiscount.String(), 98 InfrastructureFeeReferrerDiscount: t.BuyerInfrastructureFeeReferralDiscount.String(), 99 InfrastructureFeeVolumeDiscount: t.BuyerInfrastructureFeeVolumeDiscount.String(), 100 LiquidityFeeReferrerDiscount: t.BuyerLiquidityFeeReferralDiscount.String(), 101 LiquidityFeeVolumeDiscount: t.BuyerLiquidityFeeVolumeDiscount.String(), 102 TreasuryFee: t.BuyerTreasuryFee.String(), 103 BuyBackFee: t.BuyerBuyBackFee.String(), 104 HighVolumeMakerFee: t.BuyerHighVolumeMakerFee.String(), 105 }, 106 SellerFee: &vega.Fee{ 107 MakerFee: t.SellerMakerFee.String(), 108 InfrastructureFee: t.SellerInfrastructureFee.String(), 109 LiquidityFee: t.SellerLiquidityFee.String(), 110 MakerFeeReferrerDiscount: t.SellerMakerFeeReferralDiscount.String(), 111 MakerFeeVolumeDiscount: t.SellerMakerFeeVolumeDiscount.String(), 112 InfrastructureFeeReferrerDiscount: t.SellerInfrastructureFeeReferralDiscount.String(), 113 InfrastructureFeeVolumeDiscount: t.SellerInfrastructureFeeVolumeDiscount.String(), 114 LiquidityFeeReferrerDiscount: t.SellerLiquidityFeeReferralDiscount.String(), 115 LiquidityFeeVolumeDiscount: t.SellerLiquidityFeeVolumeDiscount.String(), 116 TreasuryFee: t.SellerTreasuryFee.String(), 117 BuyBackFee: t.SellerBuyBackFee.String(), 118 HighVolumeMakerFee: t.SellerHighVolumeMakerFee.String(), 119 }, 120 BuyerAuctionBatch: t.BuyerAuctionBatch, 121 SellerAuctionBatch: t.SellerAuctionBatch, 122 } 123 } 124 125 func (t Trade) Cursor() *Cursor { 126 return NewCursor(TradeCursor{SyntheticTime: t.SyntheticTime}.String()) 127 } 128 129 func (t Trade) ToProtoEdge(_ ...any) (*v2.TradeEdge, error) { 130 return &v2.TradeEdge{ 131 Node: t.ToProto(), 132 Cursor: t.Cursor().Encode(), 133 }, nil 134 } 135 136 func TradeFromProto(t *vega.Trade, txHash TxHash, vegaTime time.Time, sequenceNumber uint64) (*Trade, error) { 137 syntheticTime := vegaTime.Add(time.Duration(sequenceNumber) * time.Microsecond) 138 139 price, err := decimal.NewFromString(t.Price) 140 if err != nil { 141 return nil, fmt.Errorf("failed to decode price:%w", err) 142 } 143 144 buyerMakerFee := decimal.Zero 145 buyerHighMakerFee := decimal.Zero 146 buyerInfraFee := decimal.Zero 147 buyerLiquidityFee := decimal.Zero 148 buyerBuyBackFee := decimal.Zero 149 buyerTreasuryFee := decimal.Zero 150 151 buyerMakerFeeReferrerDiscount := decimal.Zero 152 buyerMakerFeeVolumeDiscount := decimal.Zero 153 buyerInfraFeeReferrerDiscount := decimal.Zero 154 buyerInfraFeeVolumeDiscount := decimal.Zero 155 buyerLiquidityFeeReferrerDiscount := decimal.Zero 156 buyerLiquidityFeeVolumeDiscount := decimal.Zero 157 158 if t.BuyerFee != nil { 159 buyerMakerFee, err = decimal.NewFromString(t.BuyerFee.MakerFee) 160 if err != nil { 161 return nil, fmt.Errorf("failed to decode buyer maker fee:%w", err) 162 } 163 if len(t.BuyerFee.MakerFeeReferrerDiscount) > 0 { 164 buyerMakerFeeReferrerDiscount, err = decimal.NewFromString(t.BuyerFee.MakerFeeReferrerDiscount) 165 if err != nil { 166 return nil, fmt.Errorf("failed to decode buyer maker fee referrer discount:%w", err) 167 } 168 } 169 if len(t.BuyerFee.MakerFeeVolumeDiscount) > 0 { 170 buyerMakerFeeVolumeDiscount, err = decimal.NewFromString(t.BuyerFee.MakerFeeVolumeDiscount) 171 if err != nil { 172 return nil, fmt.Errorf("failed to decode buyer maker fee volume discount:%w", err) 173 } 174 } 175 buyerInfraFee, err = decimal.NewFromString(t.BuyerFee.InfrastructureFee) 176 if err != nil { 177 return nil, fmt.Errorf("failed to decode buyer infrastructure fee:%w", err) 178 } 179 if len(t.BuyerFee.BuyBackFee) > 0 { 180 buyerBuyBackFee, err = decimal.NewFromString(t.BuyerFee.BuyBackFee) 181 if err != nil { 182 buyerBuyBackFee = num.DecimalZero() 183 } 184 } 185 if len(t.BuyerFee.TreasuryFee) > 0 { 186 buyerTreasuryFee, err = decimal.NewFromString(t.BuyerFee.TreasuryFee) 187 if err != nil { 188 buyerTreasuryFee = num.DecimalZero() 189 } 190 } 191 if len(t.BuyerFee.HighVolumeMakerFee) > 0 { 192 buyerHighMakerFee, err = decimal.NewFromString(t.BuyerFee.HighVolumeMakerFee) 193 if err != nil { 194 buyerHighMakerFee = num.DecimalZero() 195 } 196 } 197 if len(t.BuyerFee.InfrastructureFeeReferrerDiscount) > 0 { 198 buyerInfraFeeReferrerDiscount, err = decimal.NewFromString(t.BuyerFee.InfrastructureFeeReferrerDiscount) 199 if err != nil { 200 return nil, fmt.Errorf("failed to decode buyer infrastructure fee referrer discount:%w", err) 201 } 202 } 203 if len(t.BuyerFee.InfrastructureFeeVolumeDiscount) > 0 { 204 buyerInfraFeeVolumeDiscount, err = decimal.NewFromString(t.BuyerFee.InfrastructureFeeVolumeDiscount) 205 if err != nil { 206 return nil, fmt.Errorf("failed to decode buyer infrastructure fee volume discount:%w", err) 207 } 208 } 209 buyerLiquidityFee, err = decimal.NewFromString(t.BuyerFee.LiquidityFee) 210 if err != nil { 211 return nil, fmt.Errorf("failed to decode buyer liquidity fee:%w", err) 212 } 213 if len(t.BuyerFee.LiquidityFeeReferrerDiscount) > 0 { 214 buyerLiquidityFeeReferrerDiscount, err = decimal.NewFromString(t.BuyerFee.LiquidityFeeReferrerDiscount) 215 if err != nil { 216 return nil, fmt.Errorf("failed to decode buyer liquidity fee referrer discount:%w", err) 217 } 218 } 219 if len(t.BuyerFee.LiquidityFeeVolumeDiscount) > 0 { 220 buyerLiquidityFeeVolumeDiscount, err = decimal.NewFromString(t.BuyerFee.LiquidityFeeVolumeDiscount) 221 if err != nil { 222 return nil, fmt.Errorf("failed to decode buyer liquidity fee volume discount:%w", err) 223 } 224 } 225 } 226 227 sellerMakerFee := decimal.Zero 228 sellerHighMakerFee := decimal.Zero 229 sellerInfraFee := decimal.Zero 230 sellerLiquidityFee := decimal.Zero 231 sellerBuyBackFee := decimal.Zero 232 sellerTreasuryFee := decimal.Zero 233 234 sellerMakerFeeReferrerDiscount := decimal.Zero 235 sellerMakerFeeVolumeDiscount := decimal.Zero 236 sellerInfraFeeReferrerDiscount := decimal.Zero 237 sellerInfraFeeVolumeDiscount := decimal.Zero 238 sellerLiquidityFeeReferrerDiscount := decimal.Zero 239 sellerLiquidityFeeVolumeDiscount := decimal.Zero 240 241 if t.SellerFee != nil { 242 sellerMakerFee, err = decimal.NewFromString(t.SellerFee.MakerFee) 243 if err != nil { 244 return nil, fmt.Errorf("failed to decode seller maker fee:%w", err) 245 } 246 if len(t.SellerFee.MakerFeeReferrerDiscount) > 0 { 247 sellerMakerFeeReferrerDiscount, err = decimal.NewFromString(t.SellerFee.MakerFeeReferrerDiscount) 248 if err != nil { 249 return nil, fmt.Errorf("failed to decode seller maker fee referrer discount:%w", err) 250 } 251 } 252 if len(t.SellerFee.MakerFeeVolumeDiscount) > 0 { 253 sellerMakerFeeVolumeDiscount, err = decimal.NewFromString(t.SellerFee.MakerFeeVolumeDiscount) 254 if err != nil { 255 return nil, fmt.Errorf("failed to decode seller maker fee volume discount:%w", err) 256 } 257 } 258 sellerInfraFee, err = decimal.NewFromString(t.SellerFee.InfrastructureFee) 259 if err != nil { 260 return nil, fmt.Errorf("failed to decode seller infrastructure fee:%w", err) 261 } 262 if len(t.SellerFee.BuyBackFee) > 0 { 263 sellerBuyBackFee, err = decimal.NewFromString(t.SellerFee.BuyBackFee) 264 if err != nil { 265 sellerBuyBackFee = num.DecimalZero() 266 } 267 } 268 if len(t.SellerFee.TreasuryFee) > 0 { 269 sellerTreasuryFee, err = decimal.NewFromString(t.SellerFee.TreasuryFee) 270 if err != nil { 271 sellerTreasuryFee = num.DecimalZero() 272 } 273 } 274 if len(t.SellerFee.HighVolumeMakerFee) > 0 { 275 sellerHighMakerFee, err = decimal.NewFromString(t.SellerFee.HighVolumeMakerFee) 276 if err != nil { 277 sellerHighMakerFee = num.DecimalZero() 278 } 279 } 280 if len(t.SellerFee.InfrastructureFeeReferrerDiscount) > 0 { 281 sellerInfraFeeReferrerDiscount, err = decimal.NewFromString(t.SellerFee.InfrastructureFeeReferrerDiscount) 282 if err != nil { 283 return nil, fmt.Errorf("failed to decode seller infrastructure fee referrer discount:%w", err) 284 } 285 } 286 if len(t.SellerFee.InfrastructureFeeVolumeDiscount) > 0 { 287 sellerInfraFeeVolumeDiscount, err = decimal.NewFromString(t.SellerFee.InfrastructureFeeVolumeDiscount) 288 if err != nil { 289 return nil, fmt.Errorf("failed to decode seller infrastructure fee volume discount:%w", err) 290 } 291 } 292 sellerLiquidityFee, err = decimal.NewFromString(t.SellerFee.LiquidityFee) 293 if err != nil { 294 return nil, fmt.Errorf("failed to decode seller liquidity fee:%w", err) 295 } 296 if len(t.SellerFee.LiquidityFeeReferrerDiscount) > 0 { 297 sellerLiquidityFeeReferrerDiscount, err = decimal.NewFromString(t.SellerFee.LiquidityFeeReferrerDiscount) 298 if err != nil { 299 return nil, fmt.Errorf("failed to decode seller liquidity fee referrer discount:%w", err) 300 } 301 } 302 if len(t.SellerFee.LiquidityFeeVolumeDiscount) > 0 { 303 sellerLiquidityFeeVolumeDiscount, err = decimal.NewFromString(t.SellerFee.LiquidityFeeVolumeDiscount) 304 if err != nil { 305 return nil, fmt.Errorf("failed to decode seller liquidity fee volume discount:%w", err) 306 } 307 } 308 } 309 310 trade := Trade{ 311 SyntheticTime: syntheticTime, 312 TxHash: txHash, 313 VegaTime: vegaTime, 314 SeqNum: sequenceNumber, 315 ID: TradeID(t.Id), 316 MarketID: MarketID(t.MarketId), 317 Price: price, 318 Size: t.Size, 319 Buyer: PartyID(t.Buyer), 320 Seller: PartyID(t.Seller), 321 Aggressor: t.Aggressor, 322 BuyOrder: OrderID(t.BuyOrder), 323 SellOrder: OrderID(t.SellOrder), 324 Type: t.Type, 325 BuyerMakerFee: buyerMakerFee, 326 BuyerInfrastructureFee: buyerInfraFee, 327 BuyerLiquidityFee: buyerLiquidityFee, 328 BuyerBuyBackFee: buyerBuyBackFee, 329 BuyerTreasuryFee: buyerTreasuryFee, 330 BuyerHighVolumeMakerFee: buyerHighMakerFee, 331 BuyerMakerFeeReferralDiscount: buyerMakerFeeReferrerDiscount, 332 BuyerMakerFeeVolumeDiscount: buyerMakerFeeVolumeDiscount, 333 BuyerInfrastructureFeeReferralDiscount: buyerInfraFeeReferrerDiscount, 334 BuyerInfrastructureFeeVolumeDiscount: buyerInfraFeeVolumeDiscount, 335 BuyerLiquidityFeeReferralDiscount: buyerLiquidityFeeReferrerDiscount, 336 BuyerLiquidityFeeVolumeDiscount: buyerLiquidityFeeVolumeDiscount, 337 SellerMakerFee: sellerMakerFee, 338 SellerHighVolumeMakerFee: sellerHighMakerFee, 339 SellerInfrastructureFee: sellerInfraFee, 340 SellerLiquidityFee: sellerLiquidityFee, 341 SellerBuyBackFee: sellerBuyBackFee, 342 SellerTreasuryFee: sellerTreasuryFee, 343 SellerMakerFeeReferralDiscount: sellerMakerFeeReferrerDiscount, 344 SellerMakerFeeVolumeDiscount: sellerMakerFeeVolumeDiscount, 345 SellerInfrastructureFeeReferralDiscount: sellerInfraFeeReferrerDiscount, 346 SellerInfrastructureFeeVolumeDiscount: sellerInfraFeeVolumeDiscount, 347 SellerLiquidityFeeReferralDiscount: sellerLiquidityFeeReferrerDiscount, 348 SellerLiquidityFeeVolumeDiscount: sellerLiquidityFeeVolumeDiscount, 349 BuyerAuctionBatch: t.BuyerAuctionBatch, 350 SellerAuctionBatch: t.SellerAuctionBatch, 351 } 352 return &trade, nil 353 } 354 355 type TradeCursor struct { 356 SyntheticTime time.Time `json:"synthetic_time"` 357 } 358 359 func (c TradeCursor) String() string { 360 bs, err := json.Marshal(c) 361 if err != nil { 362 panic(fmt.Errorf("could not marshal Trade cursor: %w", err)) 363 } 364 return string(bs) 365 } 366 367 func (c *TradeCursor) Parse(cursorString string) error { 368 if cursorString == "" { 369 return nil 370 } 371 return json.Unmarshal([]byte(cursorString), c) 372 }