code.vegaprotocol.io/vega@v0.79.0/datanode/entities/trade_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 entities_test
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/protos/vega"
    24  
    25  	"github.com/shopspring/decimal"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestProtoFromTrade(t *testing.T) {
    30  	vegaTime := time.Now()
    31  	priceString := "1000035452"
    32  	price, _ := decimal.NewFromString(priceString)
    33  
    34  	idString := "BC2001BDDAC588F8AAAE0D9BEC3D6881A447B888447E5D0A9DE92D149BA4E877"
    35  	marketIDString := "8cc0e020c0bc2f9eba77749d81ecec8283283b85941722c2cb88318aaf8b8cd8"
    36  	size := uint64(5)
    37  	buyerIDString := "2e4f34a38204a2a155be678e670903ed8df96e813700729deacd3daf7e55039e"
    38  	sellerIDString := "8b6be1a03cc4d529f682887a78b66e6879d17f81e2b37356ca0acbc5d5886eb8"
    39  	buyOrderIDString := "CF951606211775C43449807FE15F908704A85C514D65D549D67BBD6B5EEF66BB"
    40  	sellOrderIDString := "6A94947F724CDB7851BEE793ACA6888F68ABBF8D49DFD0F778424A7CE42E7B7D"
    41  
    42  	trade := entities.Trade{
    43  		VegaTime:                vegaTime,
    44  		ID:                      entities.TradeID(idString),
    45  		MarketID:                entities.MarketID(marketIDString),
    46  		Price:                   price,
    47  		Size:                    size,
    48  		Buyer:                   entities.PartyID(buyerIDString),
    49  		Seller:                  entities.PartyID(sellerIDString),
    50  		Aggressor:               entities.SideBuy,
    51  		BuyOrder:                entities.OrderID(buyOrderIDString),
    52  		SellOrder:               entities.OrderID(sellOrderIDString),
    53  		Type:                    entities.TradeTypeNetworkCloseOutGood,
    54  		BuyerMakerFee:           decimal.NewFromInt(2),
    55  		BuyerInfrastructureFee:  decimal.NewFromInt(3),
    56  		BuyerLiquidityFee:       decimal.NewFromInt(4),
    57  		SellerMakerFee:          decimal.NewFromInt(1),
    58  		SellerInfrastructureFee: decimal.NewFromInt(10),
    59  		SellerLiquidityFee:      decimal.NewFromInt(100),
    60  		BuyerAuctionBatch:       3,
    61  		SellerAuctionBatch:      4,
    62  	}
    63  
    64  	p := trade.ToProto()
    65  
    66  	assert.Equal(t, vegaTime.UnixNano(), p.Timestamp)
    67  	assert.Equal(t, idString, p.Id)
    68  	assert.Equal(t, marketIDString, p.MarketId)
    69  	assert.Equal(t, priceString, p.Price)
    70  	assert.Equal(t, size, p.Size)
    71  	assert.Equal(t, buyerIDString, p.Buyer)
    72  	assert.Equal(t, sellerIDString, p.Seller)
    73  	assert.Equal(t, vega.Side_SIDE_BUY, p.Aggressor)
    74  	assert.Equal(t, buyOrderIDString, p.BuyOrder)
    75  	assert.Equal(t, sellOrderIDString, p.SellOrder)
    76  	assert.Equal(t, vega.Trade_TYPE_NETWORK_CLOSE_OUT_GOOD, p.Type)
    77  	assert.Equal(t, "2", p.BuyerFee.MakerFee)
    78  	assert.Equal(t, "3", p.BuyerFee.InfrastructureFee)
    79  	assert.Equal(t, "4", p.BuyerFee.LiquidityFee)
    80  	assert.Equal(t, "1", p.SellerFee.MakerFee)
    81  	assert.Equal(t, "10", p.SellerFee.InfrastructureFee)
    82  	assert.Equal(t, "100", p.SellerFee.LiquidityFee)
    83  	assert.Equal(t, uint64(3), p.BuyerAuctionBatch)
    84  	assert.Equal(t, uint64(4), p.SellerAuctionBatch)
    85  }
    86  
    87  func TestTradeFromProto(t *testing.T) {
    88  	tradeEventProto := vega.Trade{
    89  		Id:        "521127F24B1FA40311BA2FB3F6977310346346604B275DB7B767B04240A5A5C3",
    90  		MarketId:  "8cc0e020c0bc2f9eba77749d81ecec8283283b85941722c2cb88318aaf8b8cd8",
    91  		Price:     "1000097674",
    92  		Size:      1,
    93  		Buyer:     "b4376d805a888548baabfae74ef6f4fa4680dc9718bab355fa7191715de4fafe",
    94  		Seller:    "539e8c7c8c15044a6b37a8bf4d7d988588b2f63ed48666b342bc530c8312e002",
    95  		Aggressor: vega.Side_SIDE_SELL,
    96  		BuyOrder:  "0976E6CFE1513C46D5EC8877EFB51E6F12EB24709131D08EF358310FA4409158",
    97  		SellOrder: "459B8150105322406C1CEABF596E0E13ED113A98C1E290E2144D7A6236EDC6C2",
    98  		Timestamp: 1644573750767832307,
    99  		Type:      vega.Trade_TYPE_DEFAULT,
   100  		BuyerFee: &vega.Fee{
   101  			MakerFee:          "4000142",
   102  			InfrastructureFee: "10000036",
   103  			LiquidityFee:      "10000355",
   104  		},
   105  		SellerFee:          nil,
   106  		BuyerAuctionBatch:  3,
   107  		SellerAuctionBatch: 0,
   108  	}
   109  
   110  	testVegaTime := time.Now()
   111  	trade, err := entities.TradeFromProto(&tradeEventProto, generateTxHash(), testVegaTime, 5)
   112  	if err != nil {
   113  		t.Fatalf("failed to convert proto to trade:%s", err)
   114  	}
   115  
   116  	assert.Equal(t, testVegaTime.Add(5*time.Microsecond), trade.SyntheticTime)
   117  	assert.Equal(t, testVegaTime, trade.VegaTime)
   118  	assert.Equal(t, uint64(5), trade.SeqNum)
   119  
   120  	assert.Equal(t, tradeEventProto.Id, trade.ID.String())
   121  	assert.Equal(t, tradeEventProto.MarketId, trade.MarketID.String())
   122  	price, _ := decimal.NewFromString(tradeEventProto.Price)
   123  	assert.Equal(t, price, trade.Price)
   124  	size := tradeEventProto.Size
   125  	assert.Equal(t, size, trade.Size)
   126  	assert.Equal(t, tradeEventProto.Buyer, trade.Buyer.String())
   127  	assert.Equal(t, tradeEventProto.Seller, trade.Seller.String())
   128  	assert.Equal(t, entities.SideSell, trade.Aggressor)
   129  	assert.Equal(t, tradeEventProto.BuyOrder, trade.BuyOrder.String())
   130  	assert.Equal(t, tradeEventProto.SellOrder, trade.SellOrder.String())
   131  	assert.Equal(t, entities.TradeTypeDefault, trade.Type)
   132  
   133  	buyerMakerFee, _ := decimal.NewFromString(tradeEventProto.BuyerFee.MakerFee)
   134  	buyerLiquidityFee, _ := decimal.NewFromString(tradeEventProto.BuyerFee.LiquidityFee)
   135  	buyerInfraFee, _ := decimal.NewFromString(tradeEventProto.BuyerFee.InfrastructureFee)
   136  	assert.Equal(t, buyerMakerFee, trade.BuyerMakerFee)
   137  	assert.Equal(t, buyerLiquidityFee, trade.BuyerLiquidityFee)
   138  	assert.Equal(t, buyerInfraFee, trade.BuyerInfrastructureFee)
   139  	assert.Equal(t, decimal.Zero, trade.SellerMakerFee)
   140  	assert.Equal(t, decimal.Zero, trade.SellerLiquidityFee)
   141  	assert.Equal(t, decimal.Zero, trade.SellerInfrastructureFee)
   142  	assert.Equal(t, tradeEventProto.BuyerAuctionBatch, trade.BuyerAuctionBatch)
   143  	assert.Equal(t, tradeEventProto.SellerAuctionBatch, trade.SellerAuctionBatch)
   144  }