public async Task <IActionResult> Transactions(string asset, string address, int offset = 0, int limit = 10) { var user = await GetUser(required : true); // get wallet address var wallet = _walletProvider.GetChain(asset); var addrs = wallet.GetAddresses(user.Id); WalletAddr addr = null; if (addrs.Any()) { addr = addrs.First(); } else { addr = wallet.NewAddress(user.Id); } var chainAssetSettings = _walletProvider.ChainAssetSettings(asset); var incommingTxs = wallet.GetAddrTransactions(addr.Address); if (incommingTxs != null) { incommingTxs = incommingTxs.Where(t => t.Direction == WalletDirection.Incomming).OrderByDescending(t => t.ChainTx.Date); } else { incommingTxs = new List <WalletTx>(); } var model = new UserTransactionsViewModel { User = user, Asset = asset, DepositAddress = addr.Address, ChainAssetSettings = _walletProvider.ChainAssetSettings(asset), AssetSettings = _settings.Assets[asset], Wallet = wallet, TransactionsIncomming = incommingTxs.Skip(offset).Take(limit), TxsIncommingOffset = offset, TxsIncommingLimit = limit, TxsIncommingCount = incommingTxs.Count(), }; return(View(model)); }
public async Task <IActionResult> Deposit(string asset) { var user = await GetUser(required : true); if (_walletProvider.IsFiat(asset)) { return(RedirectToAction("DepositFiat", new { asset = asset })); } var wallet = _walletProvider.GetChain(asset); var addrs = wallet.GetAddresses(user.Id); WalletAddr addr = null; if (addrs.Any()) { addr = addrs.First(); } else { if (!wallet.HasTag(user.Id)) { wallet.NewTag(user.Id); wallet.Save(); } addr = wallet.NewAddress(user.Id); wallet.Save(); } var model = new DepositViewModel { User = user, Asset = asset, DepositAddress = addr.Address, }; return(View(model)); }
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>(); var incommingTxs = wallet.GetAddrTransactions(addr.Address); 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); } } } var unackedTxs = wallet.GetAddrUnacknowledgedTransactions(addr.Address); 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 IEnumerable <WalletTx> justAckedTxs = unackedTxs; if (unackedTxs.Any()) { justAckedTxs = new List <WalletTx>(unackedTxs); // wallet.Save will kill unackedTxs because they are no longer unacked wallet.AcknowledgeTransactions(unackedTxs); wallet.Save(); } else if (newlySeenTxs.Any()) { wallet.Save(); } // register new deposits with the exchange backend foreach (var tx in justAckedTxs) { 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 = justAckedTxs, NewDeposits = newDeposits }); }