public TransactionViewModel(Wallet wallet, WalletTransaction transaction, BlockIdentity transactionLocation) { _transaction = transaction; _location = transactionLocation; var groupedOutputs = _transaction.NonChangeOutputs.Select(o => { var destination = wallet.OutputDestination(o); return(new GroupedOutput(o.Amount, destination)); }).Aggregate(new List <GroupedOutput>(), (items, next) => { var item = items.Find(a => a.Destination == next.Destination); if (item == null) { items.Add(next); } else { item.Amount += next.Amount; } return(items); }); Depth = BlockChain.Depth(wallet.ChainTip.Height, transactionLocation); Inputs = _transaction.Inputs.Select(input => new Input(-input.Amount, wallet.AccountName(input.PreviousAccount))).ToArray(); Outputs = _transaction.Outputs.Select(output => new Output(output.Amount, wallet.OutputDestination(output))).ToArray(); GroupedOutputs = groupedOutputs; }
private void OnWalletChangesProcessed(object sender, Wallet.ChangesProcessedEventArgs e) { var wallet = (Wallet)sender; var currentHeight = e.NewChainTip?.Height ?? SyncedBlockHeight; // TODO: The OverviewViewModel should probably connect to this event. This could be // done after the wallet is synced. var overviewViewModel = ViewModelLocator.OverviewViewModel as OverviewViewModel; if (overviewViewModel != null) { var movedTxViewModels = overviewViewModel.RecentTransactions .Where(txvm => e.MovedTransactions.ContainsKey(txvm.TxHash)) .Select(txvm => Tuple.Create(txvm, e.MovedTransactions[txvm.TxHash])); var newTxViewModels = e.AddedTransactions.Select(tx => new TransactionViewModel(wallet, tx.Item1, tx.Item2)).ToList(); foreach (var movedTx in movedTxViewModels) { var txvm = movedTx.Item1; var location = movedTx.Item2; txvm.Location = location; txvm.Depth = BlockChain.Depth(currentHeight, location); } App.Current.Dispatcher.Invoke(() => { foreach (var txvm in newTxViewModels) { overviewViewModel.RecentTransactions.Insert(0, txvm); } while (overviewViewModel.RecentTransactions.Count > 10) { overviewViewModel.RecentTransactions.RemoveAt(10); } }); } // TODO: same.. in fact these tx viewmodels should be reused so changes don't need to be recalculated. // It would be a good idea for this synchronzier viewmodel to manage these and hand them out to other // viewmodels for sorting and organization. var transactionHistoryViewModel = ViewModelLocator.TransactionHistoryViewModel as TransactionHistoryViewModel; if (transactionHistoryViewModel != null) { foreach (var tx in transactionHistoryViewModel.Transactions) { var txvm = tx.Transaction; BlockIdentity newLocation; if (e.MovedTransactions.TryGetValue(txvm.TxHash, out newLocation)) { txvm.Location = newLocation; } txvm.Depth = BlockChain.Depth(currentHeight, txvm.Location); } transactionHistoryViewModel.AppendNewTransactions(wallet, e.AddedTransactions); } foreach (var modifiedAccount in e.ModifiedAccountProperties) { var accountNumber = checked ((int)modifiedAccount.Key.AccountNumber); var accountProperties = modifiedAccount.Value; if (accountNumber < Accounts.Count) { Accounts[accountNumber].AccountProperties = accountProperties; } } // TODO: this would be better if all new accounts were a field in the event message. var newAccounts = e.ModifiedAccountProperties. Where(kvp => kvp.Key.AccountNumber >= Accounts.Count && kvp.Key.AccountNumber != Wallet.ImportedAccountNumber). OrderBy(kvp => kvp.Key.AccountNumber); foreach (var modifiedAccount in newAccounts) { var accountNumber = checked ((int)modifiedAccount.Key.AccountNumber); var accountProperties = modifiedAccount.Value; // TODO: This is very inefficient because it recalculates balances of every account, for each new account. var accountBalance = wallet.CalculateBalances(2)[accountNumber]; var accountViewModel = new AccountViewModel(modifiedAccount.Key, accountProperties, accountBalance); App.Current.Dispatcher.Invoke(() => Accounts.Add(accountViewModel)); } if (e.NewChainTip != null) { SyncedBlockHeight = e.NewChainTip.Value.Height; var oldRetarget = BlocksUntilTicketPriceRetarget; BlocksUntilTicketPriceRetarget = BlockChain.BlocksUntilTicketPriceRetarget(SyncedBlockHeight, wallet.ActiveChain); if (BlocksUntilTicketPriceRetarget > oldRetarget) { Task.Run(async() => { var sdiff = await WalletRpcClient.TicketPriceAsync(); await App.Current.Dispatcher.InvokeAsync(() => TicketPrice = sdiff); }).ContinueWith(t => { if (t.Exception != null) { MessageBox.Show(t.Exception.InnerException.Message, "Error querying ticket price"); } }); } } if (e.AddedTransactions.Count != 0 || e.RemovedTransactions.Count != 0 || e.NewChainTip != null) { TotalBalance = wallet.TotalBalance; TransactionCount += e.AddedTransactions.Count - e.RemovedTransactions.Count; var balances = wallet.CalculateBalances(2); // TODO: don't hardcode confs for (var i = 0; i < balances.Length; i++) { Accounts[i].Balances = balances[i]; } } }