Exemplo n.º 1
0
        internal SingleQueryClause(QuantifierType quantifier
                                   , bool hasTop
                                   , int top
                                   , bool hasWildcard
                                   , ResultColumns results
                                   , IFromSource from
                                   , Predicate where
                                   , GroupBy groupBy
                                   , Predicate having
                                   , Comments comments)
        {
            _quantifier   = quantifier;
            _hasTop       = hasTop;
            this.Top      = top;
            _hasWildcard  = hasWildcard;
            this.Results  = results;
            _from         = from;
            _where        = where;
            this.GroupBy  = groupBy;
            _having       = having;
            this.Comments = comments;
            //this.IsSubQuery = true;

            this.SetParent(from);
            this.SetParent(where);
            this.SetParent(having);
        }
Exemplo n.º 2
0
 internal CommaJoinSource(IFromSource left
                          , IFromSource right
                          , Comments comments)
 {
     this.Comments = comments;
     this.Left     = left;
     this.Right    = right;
 }
Exemplo n.º 3
0
 public BracketedSource(IFromSource operand
                        , bool hasAs
                        , Identifier aliasName)
 {
     this.Comments  = new Comments(2);
     this.Operand   = operand;
     this.HasAs     = hasAs;
     this.AliasName = aliasName;
 }
Exemplo n.º 4
0
 public async Task <decimal?> EstimateSwapPaymentFeeAsync(
     IFromSource from,
     decimal amount,
     CancellationToken cancellationToken = default)
 {
     return(await EstimateFeeAsync(
                type : BlockchainTransactionType.SwapPayment,
                cancellationToken : cancellationToken)
            .ConfigureAwait(false));
 }
Exemplo n.º 5
0
 internal BracketedSource(IFromSource operand
                          , bool hasAs
                          , Identifier aliasName
                          , Comments comments)
 {
     this.Comments = comments;
     this.Operand  = operand;
     _hasAs        = hasAs;
     _aliasName    = aliasName;
 }
Exemplo n.º 6
0
        public Task <MaxAmountEstimation> EstimateMaxSwapPaymentAmountAsync(
            IFromSource from,
            bool reserve = false,
            CancellationToken cancellationToken = default)
        {
            var fromAddress = (from as FromAddress)?.Address;

            return(EstimateMaxAmountToSendAsync(
                       from: fromAddress,
                       type: BlockchainTransactionType.SwapPayment,
                       reserve: reserve,
                       cancellationToken: cancellationToken));
        }
Exemplo n.º 7
0
 public static SingleQueryClause WrapInSelectStar(IFromSource from)
 {
     return(new SingleQueryClause(QuantifierType.None
                                  , false
                                  , 0
                                  , true
                                  , new ResultColumns()
                                  , from
                                  , null
                                  , new GroupBy()
                                  , null
                                  , new Comments(3)));
 }
Exemplo n.º 8
0
        public async Task <decimal?> EstimateSwapPaymentFeeAsync(
            IFromSource from,
            decimal amount,
            CancellationToken cancellationToken = default)
        {
            var fromAddress = (from as FromAddress)?.Address;

            return(await EstimateFeeAsync(
                       from : fromAddress,
                       to : null,
                       type : BlockchainTransactionType.SwapPayment,
                       cancellationToken : cancellationToken)
                   .ConfigureAwait(false));
        }
Exemplo n.º 9
0
 internal JoinSource(IFromSource left
                     , JoinOperator operater
                     , IFromSource right
                     , Predicate constraint
                     , UnqualifiedColumnNames usingConstraint
                     , Comments comments)
 {
     this.Comments        = comments;
     this.Left            = left;
     this.Operator        = operater;
     this.Right           = right;
     this.Constraint      = constraint;
     this.UsingConstraint = usingConstraint;
 }
Exemplo n.º 10
0
 internal SingleQuery(QuantifierType quantifier
                      , bool hasTop
                      , int top
                      , bool hasWildcard
                      , ResultColumns results
                      , IFromSource from
                      , Predicate where
                      , GroupBy groupBy
                      , Predicate having
                      , OrderBy orderBy
                      , ILimitClause limit
                      , Comments comments)
     : base(quantifier, hasTop, top, hasWildcard, results, from, where, groupBy, having, comments)
 {
     this.OrderBy = orderBy;
     this.Limit   = limit;
     //this.IsSubQuery = true;
 }
Exemplo n.º 11
0
        // 抽出元の別名を取得する
        // 別名のないサブクエリには別名を付加する
        private List <Table> GetAliasNamesFromSource(IFromSource fromSource)
        {
            var ret = new List <Table>();

            if (fromSource.Type == FromSourceType.Table)
            {
                ret.Add((Table)fromSource);
            }
            else if (fromSource.Type == FromSourceType.AliasedQuery)
            {
                var aliasedQuery = (AliasedQuery)fromSource;
                if (string.IsNullOrEmpty(aliasedQuery.AliasName))
                {
                    // サブクエリの別名を変更すると、そのサブクエリのSELECT句を参照する
                    // 式も変更する必要がある. SqlAccessorではサブクエリに別名を付加する
                    // ことを強制しているので、実装を保留する.
                    throw new NotImplementedException("Can't deal with sub query that has no alias name");
                }
                ret.Add(new Table(aliasedQuery.AliasName));
            }
            else if (fromSource.Type == FromSourceType.Join)
            {
                ret.AddRange(this.GetAliasNamesFromSource(((JoinSource)fromSource).Left));
                ret.AddRange(this.GetAliasNamesFromSource(((JoinSource)fromSource).Right));
            }
            else if (fromSource.Type == FromSourceType.CommaJoin)
            {
                ret.AddRange(this.GetAliasNamesFromSource(((CommaJoinSource)fromSource).Left));
                ret.AddRange(this.GetAliasNamesFromSource(((CommaJoinSource)fromSource).Right));
            }
            else if (fromSource.Type == FromSourceType.Bracketed)
            {
                var bracketedSource = (BracketedSource)fromSource;
                if (!string.IsNullOrEmpty(bracketedSource.AliasName))
                {
                    ret.Add(new Table(bracketedSource.AliasName));
                }
                else
                {
                    ret.AddRange(this.GetAliasNamesFromSource(((BracketedSource)fromSource).Operand));
                }
            }
            return(ret);
        }
Exemplo n.º 12
0
 // 引数で指定したFROM句に変更対象のテーブル名/別名があればTrueを返す
 private bool FindTableAliasName(IFromSource fromSource, Identifier tableAliasName)
 {
     if (fromSource == null)
     {
         return(false);
     }
     else if (fromSource.Type == FromSourceType.Table)
     {
         var table = ((Table)fromSource);
         return(table.GetAliasOrTableName() == tableAliasName);
     }
     else if (fromSource.Type == FromSourceType.AliasedQuery)
     {
         return(false);
     }
     else if (fromSource.Type == FromSourceType.Join)
     {
         var join = ((JoinSource)fromSource);
         return(this.FindTableAliasName(join.Left, tableAliasName) ||
                this.FindTableAliasName(join.Right, tableAliasName));
     }
     else if (fromSource.Type == FromSourceType.Bracketed)
     {
         return(this.FindTableAliasName(((BracketedSource)fromSource).Operand, tableAliasName));
     }
     else if (fromSource.Type == FromSourceType.CommaJoin)
     {
         var commaJoin = ((CommaJoinSource)fromSource);
         return(this.FindTableAliasName(commaJoin.Left, tableAliasName) ||
                this.FindTableAliasName(commaJoin.Right, tableAliasName));
     }
     else
     {
         throw new InvalidEnumArgumentException("Undefined FromSourceType is used");
     }
 }
Exemplo n.º 13
0
        public static async Task <decimal> GetAmountReservedForSwapsAsync(
            IFromSource from,
            IAccount account,
            CurrencyConfig currency)
        {
            var swaps = await account
                        .GetSwapsAsync()
                        .ConfigureAwait(false);

            var reservedAmount = 0m;

            foreach (var swap in swaps)
            {
                if (!swap.IsActive ||
                    swap.SoldCurrency != currency.Name ||
                    swap.StateFlags.HasFlag(SwapStateFlags.IsPaymentBroadcast))
                {
                    continue;
                }

                if (from is FromAddress fromAddress && fromAddress.Address == swap.FromAddress)
                {
                    reservedAmount += (swap.Symbol.IsBaseCurrency(currency.Name) ? swap.Qty : swap.Qty * swap.Price) + swap.MakerNetworkFee;
                }
Exemplo n.º 14
0
        public static Task <SwapParams> EstimateSwapParamsAsync(
            IFromSource from,
            decimal fromAmount,
            string redeemFromAddress,
            CurrencyConfig fromCurrency,
            CurrencyConfig toCurrency,
            IAccount account,
            IAtomexClient atomexClient,
            ISymbolsProvider symbolsProvider,
            ICurrencyQuotesProvider quotesProvider,
            CancellationToken cancellationToken = default)
        {
            return(Task.Run(async() =>
            {
                if (fromCurrency == null)
                {
                    return null;
                }

                if (toCurrency == null)
                {
                    return null;
                }

                var redeemFromWalletAddress = redeemFromAddress != null
                    ? await account
                                              .GetAddressAsync(toCurrency.Name, redeemFromAddress, cancellationToken)
                                              .ConfigureAwait(false)
                    : null;

                // estimate redeem fee
                var estimatedRedeemFee = await toCurrency
                                         .GetEstimatedRedeemFeeAsync(redeemFromWalletAddress, withRewardForRedeem: false)
                                         .ConfigureAwait(false);

                // estimate reward for redeem
                var rewardForRedeem = await RewardForRedeemHelper.EstimateAsync(
                    account: account,
                    quotesProvider: quotesProvider,
                    feeCurrencyQuotesProvider: symbol => atomexClient?.GetOrderBook(symbol)?.TopOfBook(),
                    redeemableCurrency: toCurrency,
                    redeemFromAddress: redeemFromWalletAddress,
                    cancellationToken: cancellationToken);

                // get amount reserved for active swaps
                var reservedForSwapsAmount = await GetAmountReservedForSwapsAsync(
                    from: from,
                    account: account,
                    currency: fromCurrency)
                                             .ConfigureAwait(false);

                // estimate maker network fee
                var estimatedMakerNetworkFee = await EstimateMakerNetworkFeeAsync(
                    fromCurrency: fromCurrency,
                    toCurrency: toCurrency,
                    account: account,
                    atomexClient: atomexClient,
                    symbolsProvider: symbolsProvider,
                    cancellationToken: cancellationToken)
                                               .ConfigureAwait(false);

                var fromCurrencyAccount = account
                                          .GetCurrencyAccount(fromCurrency.Name) as IEstimatable;

                // estimate payment fee
                var estimatedPaymentFee = await fromCurrencyAccount
                                          .EstimateSwapPaymentFeeAsync(
                    from: from,
                    amount: fromAmount,
                    cancellationToken: cancellationToken)
                                          .ConfigureAwait(false);

                // estimate max amount and max fee
                var maxAmountEstimation = await fromCurrencyAccount
                                          .EstimateMaxSwapPaymentAmountAsync(
                    from: from,
                    reserve: true,
                    cancellationToken: cancellationToken)
                                          .ConfigureAwait(false);

                if (maxAmountEstimation.Error != null)
                {
                    return new SwapParams
                    {
                        Amount = 0m,
                        PaymentFee = estimatedPaymentFee.Value,
                        RedeemFee = estimatedRedeemFee,
                        RewardForRedeem = rewardForRedeem,
                        MakerNetworkFee = estimatedMakerNetworkFee,
                        ReservedForSwaps = reservedForSwapsAmount,
                        Error = maxAmountEstimation.Error
                    };
                }

                var maxNetAmount = Math.Max(maxAmountEstimation.Amount - reservedForSwapsAmount - estimatedMakerNetworkFee, 0m);

                if (maxNetAmount == 0m) // insufficient funds
                {
                    return new SwapParams
                    {
                        Amount = 0m,
                        PaymentFee = maxAmountEstimation.Fee,
                        RedeemFee = estimatedRedeemFee,
                        RewardForRedeem = rewardForRedeem,
                        MakerNetworkFee = estimatedMakerNetworkFee,
                        ReservedForSwaps = reservedForSwapsAmount,
                        Error = new Error(
                            code: Errors.InsufficientFunds,
                            description: Resources.InsufficientFundsToCoverMakerNetworkFee,
                            details: string.Format(Resources.InsufficientFundsToCoverMakerNetworkFeeDetails,
                                                   estimatedMakerNetworkFee,                             // required
                                                   fromCurrency.Name,                                    // currency code
                                                   maxAmountEstimation.Amount - reservedForSwapsAmount)) // available
                    };
                }

                if (fromAmount > maxNetAmount) // amount greater than max net amount => use max amount params
                {
                    return new SwapParams
                    {
                        Amount = Math.Max(maxNetAmount, 0m),
                        PaymentFee = maxAmountEstimation.Fee,
                        RedeemFee = estimatedRedeemFee,
                        RewardForRedeem = rewardForRedeem,
                        MakerNetworkFee = estimatedMakerNetworkFee,
                        ReservedForSwaps = reservedForSwapsAmount,
                        Error = new Error(
                            code: Errors.InsufficientFunds,
                            description: Resources.InsufficientFunds,
                            details: string.Format(Resources.InsufficientFundsToSendAmountDetails,
                                                   fromAmount,        // required
                                                   fromCurrency.Name, // currency code
                                                   maxNetAmount))     // available
                    };
                }

                return new SwapParams
                {
                    Amount = fromAmount,
                    PaymentFee = estimatedPaymentFee.Value,
                    RedeemFee = estimatedRedeemFee,
                    RewardForRedeem = rewardForRedeem,
                    MakerNetworkFee = estimatedMakerNetworkFee,
                    ReservedForSwaps = reservedForSwapsAmount,
                    Error = null
                };
            }, cancellationToken));
        }
Exemplo n.º 15
0
 public MagazineStateImporter(IMapperToDestinationState <MagazineState, MagazineStateSource> mapperToMagazineState,
                              IFromSource <MagazineStateSource, ParserInput> fromSource)
 {
     _mapperToMagazineState = mapperToMagazineState;
     _fromSource            = fromSource;
 }