-
Notifications
You must be signed in to change notification settings - Fork 6
/
Utils.cs
247 lines (230 loc) · 10.4 KB
/
Utils.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Numerics;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using viafront3.Models;
using viafront3.Models.ApiViewModels;
using viafront3.Services;
using viafront3.Data;
using xchwallet;
using via_jsonrpc;
namespace viafront3
{
public class NzDateTimeConverter : System.ComponentModel.TypeConverter
{
// Overrides the CanConvertFrom method of TypeConverter.
// The ITypeDescriptorContext interface provides the context for the
// conversion. Typically, this interface is used at design time to
// provide information about the design-time container.
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
// Overrides the ConvertFrom method of TypeConverter.
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
if (DateTime.TryParse(((string)value), new System.Globalization.CultureInfo("nz-NZ") /*or use culture*/, System.Globalization.DateTimeStyles.None, out DateTime date))
return date;
}
return base.ConvertFrom(context, culture, value);
}
}
public static class Utils
{
public const string AdminRole = "admin";
public const string FinanceRole = "finance";
public const string AdminOrFinanceRole = AdminRole + "," + FinanceRole;
public const string EmailConfirmedRole = "emailconfirmed";
public struct AddressIncommingTxs
{
public IEnumerable<WalletTx> IncommingTxs;
public List<WalletTx> NewlySeenTxs;
public IEnumerable<WalletTx> JustAckedTxs;
public BigInteger NewDeposits;
}
public static async Task<AddressIncommingTxs> CheckAddressIncommingTxsAndUpdateWalletAndExchangeBalance(IEmailSender emailSender, ExchangeSettings settings, string asset, IWallet wallet, ChainAssetSettings chainAssetSettings, ApplicationUser user, WalletAddr addr)
{
// create and test backend connection
var via = new ViaJsonRpc(settings.AccessHttpUrl); //TODO: move this to a ViaRpcProvider in /Services (like IWalletProvider)
via.BalanceQuery(1);
// get wallet transactions
var newlySeenTxs = new List<WalletTx>();
IEnumerable<WalletTx> incommingTxs = wallet.GetAddrTransactions(addr.Address).ToList();
if (incommingTxs != null)
incommingTxs = incommingTxs.Where(t => t.Direction == WalletDirection.Incomming);
else
incommingTxs = new List<WalletTx>();
foreach (var tx in incommingTxs)
if (tx.State == WalletTxState.None)
{
// send email: deposit detected
wallet.SeenTransaction(tx);
newlySeenTxs.Add(tx);
if (!string.IsNullOrEmpty(user.Email))
await emailSender.SendEmailChainDepositDetectedAsync(user.Email, asset, wallet.AmountToString(tx.AmountOutputs()), tx.ChainTx.TxId);
}
IEnumerable<WalletTx> unackedTxs = wallet.GetAddrUnacknowledgedTransactions(addr.Address).ToList();
if (unackedTxs != null)
unackedTxs = unackedTxs.Where(t => t.Direction == WalletDirection.Incomming && t.ChainTx.Confirmations >= chainAssetSettings.MinConf);
else
unackedTxs = new List<WalletTx>();
BigInteger newDeposits = 0;
foreach (var tx in unackedTxs)
{
newDeposits += tx.AmountOutputs();
// send email: deposit confirmed
await emailSender.SendEmailChainDepositConfirmedAsync(user.Email, asset, wallet.AmountToString(tx.AmountOutputs()), tx.ChainTx.TxId);
}
// ack txs and save wallet
if (unackedTxs.Any())
{
wallet.AcknowledgeTransactions(unackedTxs);
wallet.Save();
}
else if (newlySeenTxs.Any())
wallet.Save();
// register new deposits with the exchange backend
foreach (var tx in unackedTxs)
{
var amount = wallet.AmountToString(tx.AmountOutputs());
var source = new Dictionary<string, object>();
source["txid"] = tx.ChainTx.TxId;
var businessId = tx.Id;
via.BalanceUpdateQuery(user.Exchange.Id, asset, "deposit", businessId, amount, source);
}
return new AddressIncommingTxs { IncommingTxs=incommingTxs, NewlySeenTxs=newlySeenTxs, JustAckedTxs=unackedTxs, NewDeposits=newDeposits };
}
public static string CreateToken(int chars = 16)
{
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var rnd = new RNGCryptoServiceProvider();
var tokenBytes = new byte[chars];
rnd.GetBytes(tokenBytes);
var token =
Enumerable
.Range(0, chars)
.Select(i => ALPHABET[tokenBytes[i] % ALPHABET.Length])
.ToArray();
return new String(token);
}
public static int GetDecimalPlaces(decimal n)
{
n = Math.Abs(n); //make sure it is positive.
n -= (int)n; //remove the integer part of the number.
var decimalPlaces = 0;
while (n > 0)
{
decimalPlaces++;
n *= 10;
n -= (int)n;
}
return decimalPlaces;
}
public static Models.ApiKey CreateApiKey(ApplicationUser user, int accountRequestId, int apiKeyRequestId, string deviceName)
{
var key = Utils.CreateToken();
var secret = Utils.CreateToken(32);
return new Models.ApiKey
{
ApplicationUserId = user.Id,
AccountCreationRequestId = accountRequestId,
ApiKeyCreationRequestId = apiKeyRequestId,
Name = deviceName,
Key = key,
Secret = secret,
Nonce = 0
};
}
public static (bool success, string error) ValidateOrderParams(ExchangeSettings settings, ApiOrderCreateMarket model, string price, bool marketOrder=false)
{
// check market exists
if (model.Market == null || !settings.Markets.ContainsKey(model.Market))
return (false, "Market does not exist");
// check amount exists
if (model.Amount == null)
return (false, "Amount not present");
// initialize amount vars for further validation
var amount = decimal.Parse(model.Amount);
var amountInterval = decimal.Parse(settings.Markets[model.Market].AmountInterval);
// check amount is greater then amountInterval
if (amount < amountInterval)
return (false, $"Amount is less then {amountInterval}");
// check amonut is a multiple of the amount interval
if ((amount / amountInterval) % 1 != 0)
return (false, $"Amount is not a multiple of {amountInterval}");
if (!marketOrder)
{
// check price exists
if (price == null)
return (false, "Price not present");
// initialize price vars for further validation
var priceDec = decimal.Parse(price);
var priceInterval = decimal.Parse(settings.Markets[model.Market].PriceInterval);
// check price is greater then priceInterval
if (priceDec < priceInterval)
return (false, $"Price is less then {priceInterval}");
// check price is a multiple of the price interval
if ((priceDec / priceInterval) % 1 != 0)
return (false, $"Price is not a multiple of {priceInterval}");
}
return (true, null);
}
public static (OrderSide side, string error) GetOrderSide(String side)
{
var res = OrderSide.Bid;
if (side == "buy")
res = OrderSide.Bid;
else if (side == "sell")
res = OrderSide.Ask;
else
return (res, $"Invalid side '{side}'");
return (res, null);
}
public static bool ValidateBankAccount(string account)
{
account = account.Replace("-", "");
account = Regex.Replace(account, @"\s+", "");
// bank account digits 2 + 4 + 7 + 2/3, 15-16 digits
if (account.Count() != 15 && account.Count() != 16)
return false;
foreach (var ch in account)
if (!Char.IsDigit(ch))
return false;
return true;
}
public static long IntPow(long x, long exp)
{
if (exp == 0)
return 1;
if (exp % 2 == 0)
{
var val = IntPow(x, exp/2);
return val * val;
}
return x * IntPow(x, exp - 1);
}
public static Dictionary<string, Balance> GetUsedBalances(ExchangeSettings settings, ViaJsonRpc via, Exchange xch)
{
var balances = via.BalanceQuery(xch.Id);
foreach (var key in balances.Keys.ToList())
if (!settings.Assets.ContainsKey(key))
balances.Remove(key);
return balances;
}
}
}