-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConsoleTasks.cs
372 lines (340 loc) · 17.2 KB
/
ConsoleTasks.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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 static class ConsoleTasks
{
public static async Task CreateRoles(IServiceProvider serviceProvider)
{
// create roles if they dont exist
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] roleNames = { Utils.AdminRole, Utils.FinanceRole, Utils.EmailConfirmedRole };
foreach (var roleName in roleNames)
{
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
Console.WriteLine($"Creating {roleName}");
await roleManager.CreateAsync(new IdentityRole(roleName));
}
else
Console.WriteLine($"{roleName} exists");
}
}
public static async Task GiveUserRole(IServiceProvider serviceProvider, string email, string role)
{
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var _role = await roleManager.FindByNameAsync(role);
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var user = await userManager.FindByEmailAsync(email);
if (!await userManager.IsInRoleAsync(user, _role.Name))
await userManager.AddToRoleAsync(user, _role.Name);
}
public static async Task<bool> ChangeUserEmail(IServiceProvider serviceProvider, string oldEmail, string newEmail)
{
Console.WriteLine($"Changing {oldEmail} to {newEmail}...");
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var user = await userManager.FindByEmailAsync(oldEmail);
if (user == null)
{
Console.WriteLine($"Could not find user {oldEmail}");
return false;
}
// update user name
user.UserName = newEmail;
user.Email = newEmail;
// change email
var result = await userManager.UpdateAsync(user);
if (result.Succeeded)
{
Console.WriteLine($"Success");
return true;
}
Console.WriteLine($"Could not change email to {newEmail}");
return false;
}
public static async Task<WalletError> ConsolidateWallet(IServiceProvider serviceProvider, string asset, IEnumerable<string> userEmails, bool allUsers)
{
// check for conflicting options
if (userEmails.Count() > 0 && allUsers)
{
Console.WriteLine("ERROR: use either a list of emails *OR* the all users flag");
return WalletError.Cancelled;
}
// get the user manager
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
// get the wallet
asset = asset.ToUpper();
var walletProvider = serviceProvider.GetRequiredService<IWalletProvider>();
var wallet = walletProvider.GetChain(asset);
var assetSettings = walletProvider.ChainAssetSettings(asset);
// ensure consolidate tag exists
if (!wallet.HasTag(walletProvider.ConsolidatedFundsTag()))
{
wallet.NewTag(walletProvider.ConsolidatedFundsTag());
wallet.Save();
}
// get user ids list
var userIds = new List<string>();
Console.WriteLine("Consolidating asset '{0}' to tag '{1}'", asset, walletProvider.ConsolidatedFundsTag());
if (allUsers)
{
Console.WriteLine(" from all users");
foreach (var user in userManager.Users)
userIds.Add(user.Id);
}
else
foreach (var email in userEmails)
{
var emailTrimmed = email.Trim();
var user = await userManager.FindByEmailAsync(emailTrimmed);
if (user != null)
{
Console.WriteLine(" from user: {0} - {1}", emailTrimmed, user.Id);
userIds.Add(user.Id);
}
}
Console.WriteLine();
var dbtx = wallet.BeginDbTransaction();
// update wallet from the blockchain
Console.WriteLine("Updating txs from blockchain..");
wallet.UpdateFromBlockchain(dbtx);
Console.WriteLine("Saving wallet..");
wallet.Save();
Console.WriteLine();
// Get amount that will be consolidated
var minConfs = assetSettings.MinConf;
var balance = wallet.GetBalance(userIds, minConfs);
Console.WriteLine($"Users available balance: {balance} ({wallet.AmountToString(balance)} {wallet.Type()}, {minConfs} Confirmations)");
Console.WriteLine("Do you want to continue? (y/N)");
string input = Console.ReadLine();
if (input.Count() < 1 || input[0] != 'y')
return WalletError.Cancelled;
// Create and broadcast transactions
Console.WriteLine("Creating consolidations txs..");
IEnumerable<WalletTx> wtxs;
var res = wallet.Consolidate(userIds, walletProvider.ConsolidatedFundsTag(), assetSettings.FeeMax, assetSettings.FeeUnit, out wtxs, minConfs);
Console.WriteLine(res);
foreach (var tx in wtxs)
Console.WriteLine(tx.ChainTx.TxId);
Console.WriteLine("Saving wallet..");
wallet.Save();
dbtx.Commit();
return res;
}
static async Task ProcessFiat(IServiceProvider serviceProvider, bool isDeposit, string asset, string depositCode, long date, decimal amount, string bankMetadata)
{
// deposit code to int
var depositCodeInt = long.Parse(depositCode);
// convert amount to int
asset = asset.ToUpper();
var walletProvider = serviceProvider.GetRequiredService<IWalletProvider>();
var wallet = walletProvider.GetFiat(asset);
var amountInt = wallet.StringToAmount(amount.ToString());
// process deposit
FiatWalletTx tx;
if (isDeposit)
tx = wallet.UpdateDeposit(depositCode, date, amountInt, bankMetadata);
else
tx = wallet.UpdateWithdrawal(depositCode, date, amountInt, bankMetadata);
System.Diagnostics.Debug.Assert(tx != null);
// get user
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var user = await userManager.FindByIdAsync(tx.Tag.Tag);
var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
// test backend connection (get user balance)
var settings = serviceProvider.GetRequiredService<IOptions<ExchangeSettings>>();
var via = new ViaJsonRpc(settings.Value.AccessHttpUrl); //TODO: move this to a ViaRpcProvider in /Services (like IWalletProvider)
var balance = via.BalanceQuery(user.Exchange.Id, asset);
Console.WriteLine($"Before - available {asset} balance: {balance.Available}");
// save wallet
wallet.Save();
Console.WriteLine($"Saved {asset} wallet");
if (isDeposit)
{
// register new deposits with the exchange backend
var source = new Dictionary<string, object>();
source["bankMetadata"] = bankMetadata;
var amountStr = amount.ToString();
via.BalanceUpdateQuery(user.Exchange.Id, asset, "deposit", depositCodeInt, amountStr, source);
Console.WriteLine($"Updated exchange backend");
}
balance = via.BalanceQuery(user.Exchange.Id, asset);
Console.WriteLine($"After - available {asset} balance: {balance.Available}");
// send email
var emailSender = serviceProvider.GetRequiredService<IEmailSender>();
if (isDeposit)
await emailSender.SendEmailFiatDepositConfirmedAsync(user.Email, asset, wallet.AmountToString(tx.Amount), tx.DepositCode);
else
await emailSender.SendEmailFiatWithdrawalConfirmedAsync(user.Email, asset, wallet.AmountToString(tx.Amount), tx.DepositCode);
Console.WriteLine($"Sent email to {user.Email}");
}
public static async Task ProcessFiatDeposit(IServiceProvider serviceProvider, string asset, string depositCode, long date, decimal amount, string bankMetadata)
{
await ProcessFiat(serviceProvider, true, asset, depositCode, date, amount, bankMetadata);
}
public static async Task ProcessFiatWithdrawal(IServiceProvider serviceProvider, string asset, string depositCode, long date, decimal amount, string bankMetadata)
{
await ProcessFiat(serviceProvider, false, asset, depositCode, date, amount, bankMetadata);
}
public static async Task CompletedFiatWithdrawForBrokerOrder(IServiceProvider serviceProvider, string token)
{
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var walletProvider = serviceProvider.GetRequiredService<IWalletProvider>();
var broker = serviceProvider.GetRequiredService<IBroker>();
var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
var apiSettings = serviceProvider.GetRequiredService<IOptions<ApiSettings>>().Value;
// find broker order
var order = context.BrokerOrders.SingleOrDefault(o => o.Token == token);
if (order == null)
{
Console.WriteLine($"ERROR: could not find order ({token})");
return;
}
// check BrokerOrderFiatWithdrawal does not exist
var bow = context.BrokerOrderFiatWithdrawals.SingleOrDefault(b => b.BrokerOrderId == order.Id);
if (bow != null)
{
Console.WriteLine($"ERROR: BrokerOrderFiatWithdrawal object already exists for this order");
return;
}
// check receive asset is fiat
if (!walletProvider.IsFiat(order.AssetReceive))
{
Console.WriteLine($"ERROR: order.AssetReceive ({order.AssetReceive}) is not fiat");
return;
}
// get fiat wallet
var wallet = walletProvider.GetFiat(order.AssetReceive);
if (wallet == null)
{
Console.WriteLine($"ERROR: could not get wallet ({order.AssetReceive})");
return;
}
// get broker user
var brokerUser = userManager.FindByNameAsync(apiSettings.Broker.BrokerTag).GetAwaiter().GetResult();
if (brokerUser == null)
{
Console.WriteLine("Failed to find broker user");
return;
}
// create BrokerOrderFiatWithdrawal and pending wallet withdrawal
if (!broker.FiatWithdrawToCustomer(brokerUser, order))
{
Console.WriteLine("Failed call to FiatWithdrawToCustomer");
return;
}
// find created BrokerOrderFiatWithdrawal
bow = context.BrokerOrderFiatWithdrawals.SingleOrDefault(b => b.BrokerOrderId == order.Id);
if (bow == null)
{
Console.WriteLine($"ERROR: BrokerOrderFiatWithdrawal object does not exist (we should have just created it)");
return;
}
// update newly created withdrawal and set to processed
var amountInt = wallet.AmountToLong(order.AmountReceive);
var fiatTx = wallet.UpdateWithdrawal(bow.DepositCode, DateTimeOffset.UtcNow.ToUnixTimeSeconds(), amountInt, "");
if (fiatTx == null)
{
Console.WriteLine("Failed call to wallet.UpdateWithdrawal");
return;
}
wallet.Save();
}
public static void ProcessChainWithdrawal(IServiceProvider serviceProvider, string asset, string spendCode)
{
// get wallet
asset = asset.ToUpper();
var walletProvider = serviceProvider.GetRequiredService<IWalletProvider>();
var wallet = walletProvider.GetChain(asset);
var assetSettings = walletProvider.ChainAssetSettings(asset);
// process withdrawal
Console.WriteLine($"Actioning pending spend: {spendCode}, asset: {asset}");
var err = wallet.PendingSpendAction(spendCode, assetSettings.FeeMax, assetSettings.FeeUnit, out IEnumerable<WalletTx> wtxs);
Console.WriteLine($"Result: {err}");
// save wallet
wallet.Save();
Console.WriteLine($"Saved {asset} wallet");
if (err == WalletError.Success)
{
foreach (var wtx in wtxs)
{
// get user
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var tags = wallet.GetTagsFor(wtx);
System.Diagnostics.Debug.Assert(tags.Count() == 1);
var user = userManager.FindByIdAsync(tags.First().Tag).GetAwaiter().GetResult();
System.Diagnostics.Debug.Assert(user != null);
// send email
var emailSender = serviceProvider.GetRequiredService<IEmailSender>();
emailSender.SendEmailChainWithdrawalConfirmedAsync(user.Email, asset, wallet.AmountToString(wtx.AmountInputs() - wtx.ChainTx.Fee), wtx.ChainTx.TxId).GetAwaiter().GetResult();
Console.WriteLine($"Sent email to {user.Email}");
}
}
}
public static void ShowPendingChainWithdrawals(IServiceProvider serviceProvider, string asset)
{
// get wallet
asset = asset.ToUpper();
var walletProvider = serviceProvider.GetRequiredService<IWalletProvider>();
var wallet = walletProvider.GetChain(asset);
// show pending spends
var spends = wallet.PendingSpendsGet(null, new PendingSpendState[]{ PendingSpendState.Pending, PendingSpendState.Error });
foreach (var spend in spends)
Console.WriteLine($"SpendCode: {spend.SpendCode}, Date: {spend.Date}, Amount: {spend.Amount}, To: {spend.To}, State: {spend.State}");
}
public static void CheckChainDeposits(IServiceProvider serviceProvider, string asset)
{
// get exchange settings
var settings = serviceProvider.GetRequiredService<IOptions<ExchangeSettings>>().Value;
var apiSettings = serviceProvider.GetRequiredService<IOptions<ApiSettings>>().Value;
// get wallet
asset = asset.ToUpper();
var walletProvider = serviceProvider.GetRequiredService<IWalletProvider>();
var wallet = walletProvider.GetChain(asset);
var assetSettings = walletProvider.ChainAssetSettings(asset);
// update wallet
var dbtx = wallet.BeginDbTransaction();
wallet.UpdateFromBlockchain(dbtx);
wallet.Save();
dbtx.Commit();
// get the user manager & email sender
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
var emailSender = serviceProvider.GetRequiredService<IEmailSender>();
// update for each user
foreach (var user in userManager.Users.ToList())
{
// skip broker user because his chain deposits are handled in the Broker class
if (user.UserName == apiSettings.Broker.BrokerTag)
continue;
var addrs = wallet.GetAddresses(user.Id);
if (addrs != null && addrs.Any())
{
var addr = addrs.First();
var addrTxs = Utils.CheckAddressIncommingTxsAndUpdateWalletAndExchangeBalance(emailSender, settings, asset, wallet, assetSettings, user, addr).GetAwaiter().GetResult();
foreach (var tx in addrTxs.NewlySeenTxs)
Console.WriteLine($"{user.Email}: new tx: {tx}");
foreach (var tx in addrTxs.JustAckedTxs)
Console.WriteLine($"{user.Email}: confirmed tx: {tx}");
}
}
}
}
}