-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matcher.cs
154 lines (133 loc) · 5.69 KB
/
Matcher.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using DictionaryObject = System.Collections.Generic.Dictionary<string, object>;
namespace WavesCS.Core
{
public class Matcher
{
private readonly string _host;
public string MatcherKey { get; }
private string GetMatcherKey()
{
return Http.GetString(_host + "/matcher");
}
public Matcher(string host = "https://matcher.waves.exchange", string matcherKey = null)
{
_host = host;
MatcherKey = matcherKey ?? GetMatcherKey();
}
public string PlaceOrder(PrivateKeyAccount sender, Order order)
{
order.Sign(sender);
var json = order.GetJson();
return Http.Post($"{_host}/matcher/orderbook", json);
}
public Dictionary<Asset, decimal> GetTradableBalance(string address, Asset amountAsset, Asset priceAsset)
{
var url = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/tradableBalance/{address}";
var response = Http.GetObject(url);
return new Dictionary<Asset, decimal>
{
{amountAsset, amountAsset.LongToAmount(response.GetLong(amountAsset.Id))},
{priceAsset, priceAsset.LongToAmount(response.GetLong(priceAsset.Id))},
};
}
public OrderBook GetOrderBook(Asset amountAsset, Asset priceAsset)
{
string path = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}";
var json = Http.GetObject(path);
return OrderBook.CreateFromJson(json, amountAsset, priceAsset);
}
public Order[] GetOrders(PrivateKeyAccount account, Asset amountAsset, Asset priceAsset)
{
string path = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/publicKey/{account.PublicKey.ToBase58()}";
var headers = GetProtectionHeaders(account);
var response = Http.GetObjectsWithHeaders(path, headers);
return response.Select(j => Order.CreateFromJson(j, amountAsset, priceAsset)).ToArray();
}
public string CancelOrder(PrivateKeyAccount account,
Asset amountAsset, Asset priceAsset, string orderId)
{
var request = MakeOrderCancelRequest(account, orderId);
var url = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/cancel";
return Http.Post(url, request);
}
public string CancelAll(PrivateKeyAccount account)
{
var request = MakeCancelAllRequest(account);
var url = $"{_host}/matcher/orderbook/cancel";
return Http.Post(url, request);
}
public string DeleteOrder(PrivateKeyAccount account,
Asset amountAsset, Asset priceAsset, string orderId)
{
var request = MakeOrderCancelRequest(account, orderId);
var url = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/delete";
return Http.Post(url, request);
}
public long GetLastPrice(Asset amountAsset, Asset priceAsset)
{
var path = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/status";
var json = Http.GetObject(path);
return (long)json["lastPrice"];
}
public long GetBidPrice(Asset amountAsset, Asset priceAsset)
{
var path = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/status";
var json = Http.GetObject(path);
return (long)json["bid"];
}
public long GetAskPrice(Asset amountAsset, Asset priceAsset)
{
var path = $"{_host}/matcher/orderbook/{amountAsset.Id}/{priceAsset.Id}/status";
var json = Http.GetObject(path);
return (long)json["ask"];
}
private static NameValueCollection GetProtectionHeaders(PrivateKeyAccount account)
{
long timestamp = Utils.CurrentTimestamp();
var stream = new MemoryStream(40);
var writer = new BinaryWriter(stream);
writer.Write(account.PublicKey);
writer.WriteLong(timestamp);
var signature = account.Sign(stream);
return new NameValueCollection
{
{"Timestamp", Convert.ToString(timestamp) },
{"Signature", signature.ToBase58() }
};
}
public static DictionaryObject MakeOrderCancelRequest(PrivateKeyAccount sender, string orderId)
{
var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
writer.Write(sender.PublicKey);
writer.Write(Base58.Decode(orderId));
var signature = sender.Sign(stream);
return new DictionaryObject
{
{"sender", sender.PublicKey.ToBase58()},
{"orderId", orderId},
{"signature", signature.ToBase58()}
};
}
public static DictionaryObject MakeCancelAllRequest(PrivateKeyAccount sender)
{
long timestamp = Utils.CurrentTimestamp();
var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
writer.Write(sender.PublicKey);
writer.WriteLong(timestamp);
var signature = sender.Sign(stream);
return new DictionaryObject
{
{"sender", sender.PublicKey.ToBase58()},
{"timestamp", timestamp},
{"signature", signature.ToBase58()}
};
}
}
}