public static async Task UpdateAccount(string address) { var acttoupd = (from a in Globals.DbContext.Accounts where a.Address == address select a).First(); try { var actinf = await Globals.API.Accounts_GetAccount(acttoupd.Address); if (actinf == null || actinf.account == null || !actinf.success) { return; } acttoupd.Balance = LiskAPI.LSKLongToDecimal(actinf.account.balance); acttoupd.LastUpdate = DateTime.UtcNow; var dg = await Globals.API.Delegates_Get(acttoupd.PublicKey); if (dg != null && dg.success && dg.@delegate != null && !string.IsNullOrEmpty([email protected])) { acttoupd.FriendlyName = [email protected]; } await Globals.DbContext.SaveChangesAsync(); Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("Accounts"); Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("TotalBalance"); } catch (Exception crap) { Console.WriteLine("AccountsViewModel.UpdateAccount threw an error: " + crap.Message); } }
public static async Task UpdateAccounts() { var cdt = DateTime.UtcNow.AddMinutes(-2); var actstoupd = (from a in Globals.DbContext.Accounts where a.LastUpdate < cdt select a).Take(40); foreach (var a in actstoupd) { try { var actinf = await Globals.API.Accounts_GetAccount(a.Address); if (actinf == null || actinf.account == null || !actinf.success) { continue; } a.Balance = LiskAPI.LSKLongToDecimal(actinf.account.balance); a.LastUpdate = DateTime.UtcNow; var dg = await Globals.API.Delegates_Get(a.PublicKey); if (dg != null && dg.success && dg.@delegate != null && !string.IsNullOrEmpty([email protected])) { a.FriendlyName = [email protected]; } await Globals.DbContext.SaveChangesAsync(); Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("Accounts"); Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("TotalBalance"); } catch (Exception crap) { Console.WriteLine("AccountsViewModel.UpdateAccounts threw an error: " + crap.Message); } } }
private async void ModernButton_Click_1(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(AccountSecretTextBox.Text.Trim())) { return; } var sec = AccountSecretTextBox.Text; var issecvalid = await AppHelpers.IsSecretValid(sec); if (!issecvalid) { var nd = new NoticeDialog("Invalid Account Secret", "Invalid account secret.\r\nPlease correct and try again."); nd.ShowDialog(); return; } AccountSecretTextBox.Text = ""; var act = await Globals.API.Accounts_Open(sec); var fn = AccountFriendlyNameTextBox.Text.Trim(); AccountFriendlyNameTextBox.Text = ""; if (string.IsNullOrEmpty(fn)) { var dg = await Globals.API.Delegates_Get(act.account.publicKey); if (dg != null && dg.success && dg.@delegate != null && !string.IsNullOrEmpty([email protected])) { fn = [email protected]; } } var hasrecord = (from a in Globals.AppViewModel.AccountsViewModel.Accounts where a.Address == act.account.address || a.FriendlyName == fn select a).Any(); if (hasrecord) { var nd = new NoticeDialog("Account Record Exists", "A record for this account secret or friendly name already exists.\r\nPlease use a different secret or friendly name and try again"); nd.ShowDialog(); return; } using (var avm = new AuthViewModel { ActionDescription = "Add / Import new account " + fn }) { var rmpw = new AuthRequestDialog(avm); rmpw.ShowDialog(); if (!avm.Accepted) { return; } if (rmpw.DialogResult == null || rmpw.DialogResult == false || string.IsNullOrEmpty(avm.Password)) { return; } var ssece = AppHelpers.EncryptString(sec, avm.Password); var ni = new Account { Address = act.account.address, PublicKey = act.account.publicKey, FriendlyName = fn, SecretHash = ssece, Balance = LiskAPI.LSKLongToDecimal(act.account.balance) }; await AccountsViewModel.AddAccountAsync(ni); NavigationCommands.BrowseBack.Execute(null, null); } }
private async void BulkImportButton_OnClick(object sender, RoutedEventArgs e) { var fd = new OpenFileDialog(); fd.Multiselect = false; fd.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"; var showDialog = fd.ShowDialog(); var res = showDialog != null && (bool)showDialog; if (!res || string.IsNullOrEmpty(fd.FileName) || !File.Exists(fd.FileName)) { return; } string[] secrets; try { secrets = File.ReadAllText(fd.FileName).Split(','); } catch (Exception) { var nd = new NoticeDialog("Bulk Account Import", "Error: CSV file could not be parsed.\r\nPlease correct the file and try again."); nd.ShowDialog(); return; } using (var avm = new AuthViewModel { ActionDescription = "Bulk Import Accounts " }) { var rmpw = new AuthRequestDialog(avm); rmpw.ShowDialog(); if (!avm.Accepted) { return; } if (rmpw.DialogResult == null || rmpw.DialogResult == false || string.IsNullOrEmpty(avm.Password)) { return; } var i = 0; var ii = 1; var pd = new ProcessingDialog("Importing Accounts", "Please wait while your accounts are imported. This may take some time depending on the number of accounts."); pd.Show(); foreach (var sec in secrets) { ii++; pd.ProgressTextBlock.Text = ii + " of " + secrets.Length; var act = await Globals.API.Accounts_Open(sec); if (!act.success) { continue; } var hasrecord = (from a in Globals.AppViewModel.AccountsViewModel.Accounts where a.Address == act.account.address select a).Any(); if (hasrecord) { continue; } var ssece = AppHelpers.EncryptString(sec, avm.Password); var bal = LiskAPI.LSKLongToDecimal(act.account.balance); var ni = new Account { Address = act.account.address, PublicKey = act.account.publicKey, FriendlyName = act.account.address, SecretHash = ssece, Balance = bal, LastUpdate = DateTime.UtcNow }; try { await AccountsViewModel.AddAccountAsync(ni); i++; } catch (Exception crap) { Console.WriteLine("Error saving imported account " + act.account.address + " | " + crap.Message); } } pd.Hide(); var nd = new NoticeDialog("Bulk Account Import", "Bulk account import complete.\r\n" + i + " of " + secrets.Length + " accounts imported."); nd.ShowDialog(); NavigationCommands.BrowseBack.Execute(null, null); } }
internal async Task UpdateTransactionsAsync() { try { var cdt = DateTime.UtcNow.AddMinutes(-2); var accounts = (from a in Globals.DbContext.Accounts where a.LastTransactionsUpdate < cdt select a.Address).Take(20); if (!accounts.Any()) { return; } foreach (var a in accounts) { //TODO: the code in this loop should be put into a private method since it is duplicated do { // just wait } while (Globals.API == null); var stx = await Globals.API.Transactions_GetList("", a, "", 50, 0, "t_timestamp:desc"); Thread.Sleep(300); var rtx = await Globals.API.Transactions_GetList("", "", a, 50, 0, "t_timestamp:desc"); var trans = new List <Transaction_Object>(); if (stx != null && stx.transactions.Any()) { trans.AddRange(stx.transactions); } if (rtx != null && rtx.transactions.Any()) { trans.AddRange(rtx.transactions); } var newtrans = from t in trans select t; foreach (var t in newtrans) { Globals.AppViewModel.TransactionsViewModel.Transactions = new ObservableCollection <Transaction>(Globals.DbContext.Transactions); if ((from c in Globals.DbContext.Transactions where c.Id == t.id select c).Any()) { continue; } var ni = new Transaction { Id = t.id, Created = AppHelpers.TimestampToDateTime(t.timestamp), Amount = LiskAPI.LSKLongToDecimal(t.amount), Block = (await Globals.API.Blocks_GetHeight()).height - long.Parse(t.confirmations), Receiver = t.recipientId, Sender = t.senderId, TType = int.Parse(t.type), Fee = LiskAPI.LSKLongToDecimal(t.fee) }; if (string.IsNullOrEmpty(ni.Receiver)) { ni.Receiver = ""; } try { Globals.AppViewModel.TransactionsViewModel.Transactions = new ObservableCollection <Transaction>(Globals.DbContext.Transactions); if ((from r in Globals.DbContext.Transactions where r.Id == ni.Id select r).Any()) { continue; } await AddTransactionAsync(ni, false); } catch (Exception crap) { Console.WriteLine("TransactionsViewModel.UpdateTransactions Error saving transaction: " + crap.Message); } } var act = (from c in Globals.DbContext.Accounts where c.Address == a select c).First(); act.LastTransactionsUpdate = DateTime.UtcNow; await Globals.DbContext.SaveChangesAsync(); } } catch (Exception crap) { Console.WriteLine("TransactionsViewModel.UpdateTransactions threw an exception: " + crap.Message + " | " + crap.Source); } try { Globals.AppViewModel.TransactionsViewModel.Transactions = new ObservableCollection <Transaction>(Globals.DbContext.Transactions); Globals.AppViewModel.TransactionsViewModel.RecentTransactions = new ObservableCollection <Transaction>((from t in Globals.DbContext.Transactions orderby t.Created descending select t).Take(20)); Globals.AppViewModel.TransactionsViewModel.RaisePropertyChanged("Transactions"); Globals.AppViewModel.TransactionsViewModel.RaisePropertyChanged("RecentTransactions"); } catch (Exception crap) { // the async thread is probably not caught up Console.WriteLine("TransactionsViewModel.UpdateTransactions threw an exception: " + crap.Message); } }