From a04e7105fa0fe063274ec0b0f6db27878f88c86a Mon Sep 17 00:00:00 2001 From: Crusher Date: Sat, 5 Mar 2022 18:27:20 +0100 Subject: [PATCH 1/3] More accurate TIF filtering based on snapshot timestamp instead of current time. --- Solnet.Mango/Models/Matching/LeafNode.cs | 7 ++-- Solnet.Mango/Models/Matching/OrderBookSide.cs | 40 +++++++++---------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/Solnet.Mango/Models/Matching/LeafNode.cs b/Solnet.Mango/Models/Matching/LeafNode.cs index 57fa612..3294cbf 100644 --- a/Solnet.Mango/Models/Matching/LeafNode.cs +++ b/Solnet.Mango/Models/Matching/LeafNode.cs @@ -170,14 +170,13 @@ internal static class ExtraLayout /// /// This is checked by the order's value. /// If it is equal to the order never expires, - /// otherwise the order expires after plus seconds. + /// otherwise the order expires at plus seconds. /// /// true if it is valid, otherwise false. - public bool IsValid() + public bool IsValid(ulong timestamp) { var expiry = Timestamp + TimeInForce; - var now = (ulong) DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; - return TimeInForce == 0 || now <= expiry; + return TimeInForce == 0 || timestamp < expiry; } /// diff --git a/Solnet.Mango/Models/Matching/OrderBookSide.cs b/Solnet.Mango/Models/Matching/OrderBookSide.cs index 17bb09d..0631721 100644 --- a/Solnet.Mango/Models/Matching/OrderBookSide.cs +++ b/Solnet.Mango/Models/Matching/OrderBookSide.cs @@ -106,35 +106,33 @@ internal static class Layout /// A list of open orders on the book side. public List GetOrders(bool includeExpired = false) { - bool isBids = Metadata.DataType == DataType.Bids; - _orders = new List(); - foreach (var node in Nodes) + var leafNodes = Nodes.Where(n => n is LeafNode).Cast().ToList(); + ulong timestamp = leafNodes.Select(n => n.Timestamp).Max(); + + foreach (LeafNode leafNode in leafNodes) { - if (node is LeafNode leafNode) + var valid = leafNode.IsValid(timestamp); + if (valid || includeExpired) { - var valid = leafNode.IsValid(); - if (valid || includeExpired) + _orders.Add(new OpenOrder { - _orders.Add(new OpenOrder - { - RawPrice = leafNode.Price, - RawQuantity = leafNode.Quantity, - ClientOrderId = leafNode.ClientOrderId, - Owner = leafNode.Owner, - OrderIndex = leafNode.OwnerSlot, - OrderId = new BigInteger(leafNode.Key), - Timestamp = leafNode.Timestamp, - ExpiryTimestamp = leafNode.TimeInForce != 0 ? leafNode.Timestamp + leafNode.TimeInForce : ulong.MaxValue, - TimeInForce = leafNode.TimeInForce, - OrderType = leafNode.OrderType, - }); - } + RawPrice = leafNode.Price, + RawQuantity = leafNode.Quantity, + ClientOrderId = leafNode.ClientOrderId, + Owner = leafNode.Owner, + OrderIndex = leafNode.OwnerSlot, + OrderId = new BigInteger(leafNode.Key), + Timestamp = leafNode.Timestamp, + ExpiryTimestamp = leafNode.TimeInForce != 0 ? leafNode.Timestamp + leafNode.TimeInForce : ulong.MaxValue, + TimeInForce = leafNode.TimeInForce, + OrderType = leafNode.OrderType, + }); } } - if (!isBids) + if (Metadata.DataType != DataType.Bids) { _orders.Sort(Comparer.Create((order, order1) => order.RawPrice.CompareTo(order1.RawPrice))); } From aed2786d0a54b382c11520ff2e457a942783d972 Mon Sep 17 00:00:00 2001 From: Crusher Date: Sat, 5 Mar 2022 22:12:59 +0100 Subject: [PATCH 2/3] Fixed tests and bumped version --- SharedBuildProperties.props | 2 +- Solnet.Mango.Test/MangoClientTest.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SharedBuildProperties.props b/SharedBuildProperties.props index 5e1da79..c3fc128 100644 --- a/SharedBuildProperties.props +++ b/SharedBuildProperties.props @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> Solnet.Mango - 6.0.3.3 + 6.0.3.4 Copyright 2021 © blockmountain blockmountain blockmountain diff --git a/Solnet.Mango.Test/MangoClientTest.cs b/Solnet.Mango.Test/MangoClientTest.cs index b6e4064..9fffbc0 100644 --- a/Solnet.Mango.Test/MangoClientTest.cs +++ b/Solnet.Mango.Test/MangoClientTest.cs @@ -582,7 +582,7 @@ public void GetOrderBookSideWithExpired() Assert.IsTrue(obs.ParsedResult.Metadata.IsInitialized); Assert.AreEqual(DataType.Bids, obs.ParsedResult.Metadata.DataType); var bids = obs.ParsedResult.GetOrders(); - Assert.AreEqual(11, bids.Count); // there's 11 orders without expiry + Assert.AreEqual(14, bids.Count); // there's 14 orders without expiry var bidsWithExpired = obs.ParsedResult.GetOrders(true); Assert.AreEqual(16, bidsWithExpired.Count); // there's 16 orders total including expired } From 911b4a94b5a893cb0307a03d44d5cb223bab85b3 Mon Sep 17 00:00:00 2001 From: Crusher Date: Sun, 6 Mar 2022 22:51:19 +0100 Subject: [PATCH 3/3] Updated PerpMarket public keys --- Solnet.Mango.Examples/SubscribeOrderBook.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Solnet.Mango.Examples/SubscribeOrderBook.cs b/Solnet.Mango.Examples/SubscribeOrderBook.cs index 089ebad..38026bc 100644 --- a/Solnet.Mango.Examples/SubscribeOrderBook.cs +++ b/Solnet.Mango.Examples/SubscribeOrderBook.cs @@ -16,9 +16,9 @@ public class SubscribeOrderBook : IRunnableExample private readonly IMangoClient _mangoClient; private static Dictionary Markets = new Dictionary() { - { "SOL-PERP", "CqxX2QupYiYafBSbA519j4vRVxxecidbh2zwX66Lmqem" }, - { "BTC-PERP", "4nfmQP3KmUqEJ6qJLsS3offKgE96YUB4Rp7UQvm2Fbi9" }, - { "MNGO-PERP", "2TgaaVoHgnSeEtXvWTx13zQeTf4hYWAMEiMQdcG6EwHi" } + { "SOL-PERP", "2TgaaVoHgnSeEtXvWTx13zQeTf4hYWAMEiMQdcG6EwHi" }, + { "BTC-PERP", "DtEcjPLyD4YtTBB4q8xwFZ9q49W89xZCZtJyrGebi5t8" }, + { "MNGO-PERP", "4nfmQP3KmUqEJ6qJLsS3offKgE96YUB4Rp7UQvm2Fbi9" } }; private Dictionary> allAskOrders;