-
Notifications
You must be signed in to change notification settings - Fork 7
/
Info.cs
108 lines (90 loc) · 2.96 KB
/
Info.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
#region license
// Please read and agree to license.md contents before using this SDK.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace mcxNOW
{
public class Info
{
/**
* Amount of base currency in users account
*/
public decimal base_bal { get; set; }
/**
* Amount of info currency in users account
*/
public decimal cur_bal { get; set; }
/**
* Users buy and sell orders
*/
public List<Order> orders { get; set; }
}
public class Order
{
/**
* Id of order
*/
public string id { get; set; }
/**
* Was executed 1 yes 0 no
*/
public bool e { get; set; }
/**
* Time order was placed
*/
public DateTime t { get; set; }
/**
* Textual info about order
*/
public string i { get; set; }
private OrderInfo orderInfo = null;
public OrderInfo GetOrderInfo()
{
if (orderInfo == null)
{
orderInfo = OrderInfo.FromOrder(this);
}
return orderInfo;
}
}
public class OrderInfo
{
public enum Types {
Buy,
Sell
};
public Types Type { get; private set; }
public Currency Currency { get; private set; }
public decimal Amount { get; private set; }
public decimal Price { get; private set; }
public DateTime DateTime { get; private set; }
public static OrderInfo FromOrder(Order order)
{
// "Buy (Devcoin) with (0.01)BTC for (0.0000001)BTC each"
// "Sell (20.0)(MNC) for (0.002928)BTC each"
Regex buyRegex = new Regex(@"^Buy (?<currency>[A-Za-z]+) with (?<amount>[0-9]+(\.[0-9]+)?)BTC for (?<price>[0-9]+(\.[0-9]+)?)BTC each$", RegexOptions.Singleline);
Regex sellRegex = new Regex(@"^Sell (?<amount>[0-9]+(\.[0-9]+)?)(?<currency>[A-Za-z]{2,3}) for (?<price>[0-9]+(\.[0-9]+)?)BTC each$", RegexOptions.Singleline);
Match match = buyRegex.Match(order.i);
if (!match.Success)
{
match = sellRegex.Match(order.i);
}
if (!match.Success)
{
throw new ArgumentException();
}
return new OrderInfo()
{
Type = match.Groups["type"].Value == "Buy" ? Types.Buy : Types.Sell,
Currency = Currency.FromString(match.Groups["currency"].Value),
Amount = Convert.ToDecimal(match.Groups["amount"].Value, System.Globalization.NumberFormatInfo.InvariantInfo),
Price = Convert.ToDecimal(match.Groups["price"].Value, System.Globalization.NumberFormatInfo.InvariantInfo),
};
}
}
}