-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrderBook.cs
54 lines (48 loc) · 1.71 KB
/
OrderBook.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using DictionaryObject = System.Collections.Generic.Dictionary<string, object>;
namespace WavesCS.Core
{
public class OrderBook
{
public DateTime Timestamp { get; }
public Asset AmountAsset { get; }
public Asset PriceAsset { get; }
public Item[] Bids { get; }
public Item[] Asks { get; }
public class Item
{
public decimal Price { get; }
public decimal Amount { get; }
public Item(decimal price, decimal amount)
{
Price = price;
Amount = amount;
}
}
public OrderBook(DateTime timestamp, Asset amountAsset, Asset priceAsset, Item[] bids, Item[] asks)
{
Timestamp = timestamp;
AmountAsset = amountAsset;
PriceAsset = priceAsset;
Bids = bids;
Asks = asks;
}
public static OrderBook CreateFromJson(DictionaryObject json, Asset amountAsset, Asset priceAsset)
{
return new OrderBook(
json.GetDate("timestamp"),
amountAsset,
priceAsset,
json.GetObjects("bids").Select(o => ParseItem(amountAsset, priceAsset, o)).ToArray(),
json.GetObjects("asks").Select(o => ParseItem(amountAsset, priceAsset, o)).ToArray());
}
private static Item ParseItem(Asset amountAsset, Asset priceAsset, DictionaryObject o)
{
return new Item(
Asset.LongToPrice(amountAsset, priceAsset, o.GetLong("price")),
amountAsset.LongToAmount(o.GetLong("amount")));
}
}
}