示例#1
0
        private async Task <bool> SaveTransactionHistory(TxDirectionType direction, TxHistory history)
        {
            await _txHistoryRepository.InsertOrReplaceAsync(direction, history);

            return(false);
        }
示例#2
0
        private async Task <int> QueryAndProcessPayments(string address, PaymentContext context)
        {
            int count    = 0;
            var payments = await _horizonService.GetPayments(address, StellarSdkConstants.OrderAsc, context.Cursor);

            if (payments == null)
            {
                await _log.WriteWarningAsync(nameof(TransactionHistoryService), nameof(QueryAndProcessPayments),
                                             $"Address not found: {address}");

                context.Cursor = null;
                return(count);
            }

            context.Cursor = null;
            foreach (var payment in payments.Embedded.Records)
            {
                try
                {
                    context.Cursor = payment.PagingToken;
                    count++;

                    // create_account, payment or account_merge
                    if (payment.TypeI == (int)OperationType.OperationTypeEnum.CREATE_ACCOUNT ||
                        payment.TypeI == (int)OperationType.OperationTypeEnum.PAYMENT && Core.Domain.Asset.Stellar.TypeName.Equals(payment.AssetType, StringComparison.OrdinalIgnoreCase) ||
                        payment.TypeI == (int)OperationType.OperationTypeEnum.ACCOUNT_MERGE)
                    {
                        if (context.Transaction == null || !context.Transaction.Hash.Equals(payment.TransactionHash, StringComparison.OrdinalIgnoreCase))
                        {
                            var tx = await _horizonService.GetTransactionDetails(payment.TransactionHash);

                            context.Transaction  = tx ?? throw new BusinessException($"Transaction not found. hash={payment.TransactionHash}");
                            context.AccountMerge = 0;
                        }

                        var history = new TxHistory
                        {
                            AssetId   = Core.Domain.Asset.Stellar.Id,
                            Hash      = payment.TransactionHash,
                            PaymentId = payment.Id,
                            CreatedAt = payment.CreatedAt,
                            Memo      = GetMemo(context.Transaction)
                        };

                        // create_account
                        if (payment.TypeI == (int)OperationType.OperationTypeEnum.CREATE_ACCOUNT)
                        {
                            history.FromAddress = payment.Funder;
                            history.ToAddress   = payment.Account;
                            history.PaymentType = PaymentType.CreateAccount;

                            decimal amount = Decimal.Parse(payment.StartingBalance);
                            history.Amount = Convert.ToInt64(amount * One.Value);
                        }
                        // payment
                        else if (payment.TypeI == (int)OperationType.OperationTypeEnum.PAYMENT)
                        {
                            history.FromAddress = payment.From;
                            history.ToAddress   = payment.To;
                            history.PaymentType = PaymentType.Payment;

                            decimal amount = Decimal.Parse(payment.Amount);
                            history.Amount = Convert.ToInt64(amount * One.Value);
                        }
                        // account_merge
                        else if (payment.TypeI == (int)OperationType.OperationTypeEnum.ACCOUNT_MERGE)
                        {
                            history.FromAddress = payment.Account;
                            history.ToAddress   = payment.Into;
                            history.PaymentType = PaymentType.AccountMerge;

                            var resultXdrBase64 = context.Transaction.ResultXdr;
                            history.Amount = _horizonService.GetAccountMergeAmount(resultXdrBase64, context.AccountMerge);
                            context.AccountMerge++;
                        }
                        else
                        {
                            throw new BusinessException($"Invalid payment type. type=${payment.TypeI}");
                        }

                        history.OperationId = await _txBroadcastRepository.GetOperationId(payment.TransactionHash);

                        if (address.Equals(history.ToAddress, StringComparison.OrdinalIgnoreCase))
                        {
                            await _txHistoryRepository.InsertOrReplaceAsync(context.TableId, TxDirectionType.Incoming, history);

                            context.Sequence++;
                        }
                        if (address.Equals(history.FromAddress, StringComparison.OrdinalIgnoreCase))
                        {
                            await _txHistoryRepository.InsertOrReplaceAsync(context.TableId, TxDirectionType.Outgoing, history);

                            context.Sequence++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new BusinessException($"Failed to process payment of transaction. payment={payment?.Id}, hash={context?.Transaction?.Hash}", ex);
                }
            }
            return(count);
        }