|
| 1 | +using System; |
| 2 | +using DigitexWire; |
| 3 | +using Google.Protobuf; |
| 4 | + |
| 5 | +namespace DigitexConnector.Extentions |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Class with methods of conversion. |
| 9 | + /// </summary> |
| 10 | + public static class DWConverter |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Create message for send to exchange. |
| 14 | + /// </summary> |
| 15 | + /// <param name="oneOfContent"><see cref="Message"/></param> |
| 16 | + /// <param name="marketId">Id of current market.</param> |
| 17 | + /// <param name="clientId">Id of message to send.</param> |
| 18 | + /// <param name="traderId">Identificator of trader (ain't necessary in stock version of connector - set to 0)</param> |
| 19 | + /// <returns>Message to send. <see cref="Message"/></returns> |
| 20 | + static public Message BuildMessage(IMessage oneOfContent, uint marketId, Guid clientId, uint traderId) |
| 21 | + { |
| 22 | + Message masterMessage = new Message(); |
| 23 | + //switch (slaveMessage.GetType().ToString()) |
| 24 | + if (oneOfContent.GetType() == typeof(PlaceOrderMessage)) |
| 25 | + { masterMessage.PlaceOrderMsg = ((PlaceOrderMessage)oneOfContent).Clone(); } |
| 26 | + else if (oneOfContent.GetType() == typeof(CancelOrderMessage)) |
| 27 | + { masterMessage.CancelOrderMsg = ((CancelOrderMessage)oneOfContent).Clone(); } |
| 28 | + else if (oneOfContent.GetType() == typeof(CancelAllOrdersMessage)) |
| 29 | + { masterMessage.CancelAllOrdersMsg = ((CancelAllOrdersMessage)oneOfContent).Clone(); } |
| 30 | + else if (oneOfContent.GetType() == typeof(OrderStatusMessage)) |
| 31 | + { masterMessage.OrderStatusMsg = ((OrderStatusMessage)oneOfContent).Clone(); } |
| 32 | + else if (oneOfContent.GetType() == typeof(OrderFilledMessage)) |
| 33 | + { masterMessage.OrderFilledMsg = ((OrderFilledMessage)oneOfContent).Clone(); } |
| 34 | + else if (oneOfContent.GetType() == typeof(ChangeLeverageAllMessage)) |
| 35 | + { masterMessage.ChangeLeverageAllMsg = ((ChangeLeverageAllMessage)oneOfContent).Clone(); } |
| 36 | + else if (oneOfContent.GetType() == typeof(GetTraderStatusMessage)) |
| 37 | + { masterMessage.GetTraderStatusMsg = ((GetTraderStatusMessage)oneOfContent).Clone(); } |
| 38 | + else if (oneOfContent.GetType() == typeof(OrderBookRequestMessage)) |
| 39 | + { masterMessage.OrderBookRequestMsg = ((OrderBookRequestMessage)oneOfContent).Clone(); } |
| 40 | + else if (oneOfContent.GetType() == typeof(GetMarketStateMessage)) |
| 41 | + { masterMessage.GetMarketStateMsg = ((GetMarketStateMessage)oneOfContent).Clone(); } |
| 42 | + else |
| 43 | + { throw new FormatException($"Message Type \"{oneOfContent.GetType()}\" does not exist"); } |
| 44 | + |
| 45 | + masterMessage.Timestamp = DateTime.Now.Ticks; |
| 46 | + masterMessage.Serial = 0; |
| 47 | + masterMessage.MarketId = marketId; |
| 48 | + masterMessage.TraderId = traderId; |
| 49 | + masterMessage.ClientId = ByteString.CopyFrom(clientId.ToByteArray()); |
| 50 | + return masterMessage; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Convert System.Decimal type to DigitexWire.Decimal. |
| 55 | + /// </summary> |
| 56 | + /// <param name="decValue">Original value.</param> |
| 57 | + /// <returns>Value converted to DigitexWire.Decimal. <see cref="DigitexWire.Decimal"/></returns> |
| 58 | + static public DigitexWire.Decimal ToProtoDecimal(decimal decValue) |
| 59 | + { |
| 60 | + uint scale = 0; |
| 61 | + decimal reminder = decValue % 1; |
| 62 | + long value64 = (long)(decValue - reminder); |
| 63 | + while (reminder != 0) |
| 64 | + { |
| 65 | + reminder *= 10; |
| 66 | + value64 *= 10; |
| 67 | + scale += 1; |
| 68 | + value64 += (long)(reminder - (reminder % 1)); |
| 69 | + reminder %= 1; |
| 70 | + } |
| 71 | + DigitexWire.Decimal value = new DigitexWire.Decimal(); |
| 72 | + value.Value64 = value64; |
| 73 | + value.Scale = scale; |
| 74 | + return value; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Convert DigitexWire.Decimal to System.Decimal. |
| 79 | + /// </summary> |
| 80 | + /// <param name="value">Original DigitexWire.Decimal value. <see cref="DigitexWire.Decimal"/></param> |
| 81 | + /// <returns>Value converted to System.Decimal.</returns> |
| 82 | + static public decimal FromProtoDecimal(DigitexWire.Decimal value) |
| 83 | + { |
| 84 | + return value == null ? 0 : (decimal)(value.Value64 * Math.Pow(10, -value.Scale)); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Convert Google.Protobuf.ByteString guid, using in DigitexWire, to System.Guid. |
| 89 | + /// </summary> |
| 90 | + /// <param name="guid">Original Google.Protobuf.ByteString value.</param> |
| 91 | + /// <returns>Value converted to System.Guid.</returns> |
| 92 | + static public Guid FromProtoUuid(ByteString uuid) |
| 93 | + { |
| 94 | + if (uuid.IsEmpty) |
| 95 | + { |
| 96 | + return Guid.Empty; |
| 97 | + } |
| 98 | + byte[] bytes = uuid.ToByteArray(); |
| 99 | + byte[] newBytes = new byte[] |
| 100 | + { |
| 101 | + bytes[3], |
| 102 | + bytes[2], |
| 103 | + bytes[1], |
| 104 | + bytes[0], |
| 105 | + bytes[5], |
| 106 | + bytes[4], |
| 107 | + bytes[7], |
| 108 | + bytes[6], |
| 109 | + bytes[8], |
| 110 | + bytes[9], |
| 111 | + bytes[10], |
| 112 | + bytes[11], |
| 113 | + bytes[12], |
| 114 | + bytes[13], |
| 115 | + bytes[14], |
| 116 | + bytes[15], |
| 117 | + }; |
| 118 | + return new Guid(newBytes); |
| 119 | + } |
| 120 | + |
| 121 | + static public Guid FromOpenAPIUUID(string uuid) |
| 122 | + { |
| 123 | + char[] charArray = uuid.ToCharArray(); |
| 124 | + char[] resultArray = new char[] |
| 125 | + { |
| 126 | + charArray[6], |
| 127 | + charArray[7], |
| 128 | + charArray[4], |
| 129 | + charArray[5], |
| 130 | + charArray[2], |
| 131 | + charArray[3], |
| 132 | + charArray[0], |
| 133 | + charArray[1], |
| 134 | + charArray[8], |
| 135 | + charArray[11], |
| 136 | + charArray[12], |
| 137 | + charArray[9], |
| 138 | + charArray[10], |
| 139 | + charArray[13], |
| 140 | + charArray[16], |
| 141 | + charArray[17], |
| 142 | + charArray[14], |
| 143 | + charArray[15], |
| 144 | + charArray[18], |
| 145 | + charArray[19], |
| 146 | + charArray[20], |
| 147 | + charArray[21], |
| 148 | + charArray[22], |
| 149 | + charArray[23], |
| 150 | + charArray[24], |
| 151 | + charArray[25], |
| 152 | + charArray[26], |
| 153 | + charArray[27], |
| 154 | + charArray[28], |
| 155 | + charArray[29], |
| 156 | + charArray[30], |
| 157 | + charArray[31], |
| 158 | + charArray[32], |
| 159 | + charArray[33], |
| 160 | + charArray[34], |
| 161 | + charArray[35] |
| 162 | + }; |
| 163 | + string guid = new string(charArray); |
| 164 | + return new Guid(guid); |
| 165 | + } |
| 166 | + |
| 167 | + static public Guid GuidToUuid(Guid guid) |
| 168 | + { |
| 169 | + byte[] bytes = guid.ToByteArray(); |
| 170 | + byte[] newBytes = new byte[] |
| 171 | + { |
| 172 | + bytes[3], |
| 173 | + bytes[2], |
| 174 | + bytes[1], |
| 175 | + bytes[0], |
| 176 | + bytes[5], |
| 177 | + bytes[4], |
| 178 | + bytes[7], |
| 179 | + bytes[6], |
| 180 | + bytes[8], |
| 181 | + bytes[9], |
| 182 | + bytes[10], |
| 183 | + bytes[11], |
| 184 | + bytes[12], |
| 185 | + bytes[13], |
| 186 | + bytes[14], |
| 187 | + bytes[15], |
| 188 | + }; |
| 189 | + return new Guid(newBytes); |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary> |
| 193 | + /// Convert long Timestamp value to System.DateTime./> |
| 194 | + /// </summary> |
| 195 | + /// <param name="time">Original long Timestamp value.</param> |
| 196 | + /// <returns>Value converted to System.DateTime.</returns> |
| 197 | + static public DateTime FromLongDateTime(long time) |
| 198 | + { |
| 199 | + return (new DateTime(1970, 1, 1) + TimeSpan.FromMilliseconds(time / 1000)).ToLocalTime(); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments