Ejemplo n.º 1
0
        private async Task <BitcoinApiResult <ApiTransaction[]> > FetchTransactions(DbWallet wallet)
        {
            var result = new List <ApiTransaction>();
            var step   = 10;

            var accounts = await BitcoinClient.MakeRequest <Dictionary <string, string> >("listaccounts", ToRequestParams(wallet));

            if (!string.IsNullOrEmpty(accounts.Error))
            {
                return(new BitcoinApiResult <ApiTransaction[]>
                {
                    Error = accounts.Error,
                    Id = accounts.Id,
                    Result = new ApiTransaction[0],
                });
            }

            using (await Locker.Lock(wallet.Id))
            {
                foreach (var account in accounts.Result)
                {
                    for (int i = 0; ; i += step)
                    {
                        var transactionsResult = await BitcoinClient.MakeRequest <ApiTransaction[]>("listtransactions", ToRequestParams(wallet), account.Key, step, i);

                        if (!string.IsNullOrEmpty(transactionsResult.Error))
                        {
                            return(transactionsResult);
                        }

                        if (transactionsResult.Result.Length == 0)
                        {
                            break;
                        }

                        result.AddRange(transactionsResult.Result);
                    }
                }

                return(new BitcoinApiResult <ApiTransaction[]>
                {
                    Result = result.ToArray(),
                });
            }
        }
Ejemplo n.º 2
0
 private (string bitcoindAddress, string user, string password) ToRequestParams(DbWallet wallet)
 {
     return
         (
         wallet.BitcoindServerAddress,
         wallet.BitcoindUser,
         wallet.BitcoindPassword
         );
 }
Ejemplo n.º 3
0
        private async Task <(bool success, string errorMessage)> AppendTransactions(List <Transaction> result, DbWallet wallet)
        {
            var transactionsApiResult = await FetchTransactions(wallet);

            if (!string.IsNullOrEmpty(transactionsApiResult.Error))
            {
                return(false, transactionsApiResult.Error);
            }

            var transactionsFromApi = transactionsApiResult.Result.ToDictionary(x => GetKey(x.TxId, IsSend(x.Category)), x => x, StringComparer.InvariantCulture);
            var transactionsFromDb  = DataAccessService.GetTransactions(wallet.Id).ToDictionary(x => GetKey(x.TxId, x.Category), x => x, StringComparer.InvariantCulture);

            var transactionsToInsertOrUpdate = new List <DbTransactionParam>(transactionsFromApi.Count);

            foreach (var transaction in transactionsFromApi)
            {
                AppendTransaction(transaction, result, transactionsToInsertOrUpdate, transactionsFromDb);
            }

            if (transactionsToInsertOrUpdate.Count > 0)
            {
                if (!DataAccessService.InsertOrUpdateTransactions(wallet.Id, transactionsToInsertOrUpdate.ToArray()))
                {
                    return(success : false, errorMessage : "Please try again later.");
                }
            }

            return(true, string.Empty);

            string GetKey(string txId, bool send)
            {
                return($"{txId}_{(send ? 1 : 0)}");
            }
        }