示例#1
0
        private async Task <EstimationResult> Estimation(RuntimeConfig rcfg, BigInteger inputAmount, TradableCurrency?ethereumToken, FiatCurrency fiatCurrency, string ethAddress, bool reversed, BigInteger withdrawalLimitMin, BigInteger withdrawalLimitMax)
        {
            var allowed = false;

            var centsPerAsset                = 0L;
            var centsPerGold                 = 0L;
            var resultGoldAmount             = BigInteger.Zero;
            var resultCurrencyAmount         = BigInteger.Zero;
            var resultCurrencyAmountMinusFee = BigInteger.Zero;

            object viewAmount         = null;
            var    viewAmountCurrency = "";
            object viewFee            = null;
            var    viewFeeCurrency    = "";

            var limitsData = (EstimateLimitsView)null;

            // default estimation: GOLD to specified currency
            if (!reversed)
            {
                // fiat
                if (ethereumToken == null)
                {
                    var res = await CoreLogic.Finance.Estimation.SellGoldFiat(
                        services : HttpContext.RequestServices,
                        fiatCurrency : fiatCurrency,
                        goldAmount : inputAmount
                        );

                    allowed              = res.Allowed;
                    centsPerGold         = res.CentsPerGoldRate;
                    resultGoldAmount     = res.ResultGoldAmount;
                    resultCurrencyAmount = res.ResultCentsAmount;

                    var mntBalance = ethAddress != null ? await EthereumObserver.GetAddressMntBalance(ethAddress) : BigInteger.Zero;

                    var fee = CoreLogic.Finance.Estimation.SellingFeeForFiat(res.ResultCentsAmount, mntBalance);
                    resultCurrencyAmountMinusFee = res.ResultCentsAmount - fee;

                    viewAmount         = (long)resultCurrencyAmountMinusFee / 100d;
                    viewAmountCurrency = fiatCurrency.ToString().ToUpper();
                    viewFee            = fee / 100d;
                    viewFeeCurrency    = fiatCurrency.ToString().ToUpper();

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = (long)withdrawalLimitMin / 100d,
                        Max      = (long)withdrawalLimitMax / 100d,
                        Cur      = (long)resultCurrencyAmountMinusFee / 100d,
                    };
                }
                // cryptoasset
                else
                {
                    var res = await CoreLogic.Finance.Estimation.SellGoldCrypto(
                        services : HttpContext.RequestServices,
                        ethereumToken : ethereumToken.Value,
                        fiatCurrency : fiatCurrency,
                        goldAmount : inputAmount
                        );

                    allowed              = res.Allowed;
                    centsPerGold         = res.CentsPerGoldRate;
                    centsPerAsset        = res.CentsPerAssetRate;
                    resultGoldAmount     = res.ResultGoldAmount;
                    resultCurrencyAmount = res.ResultAssetAmount;

                    var fee = CoreLogic.Finance.Estimation.SellingFeeForCrypto(ethereumToken.Value, res.ResultAssetAmount);
                    resultCurrencyAmountMinusFee = res.ResultAssetAmount - fee;

                    viewAmount         = resultCurrencyAmountMinusFee.ToString();
                    viewAmountCurrency = ethereumToken.Value.ToString().ToUpper();
                    viewFee            = fee.ToString();
                    viewFeeCurrency    = ethereumToken.Value.ToString().ToUpper();

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = withdrawalLimitMin.ToString(),
                        Max      = withdrawalLimitMax.ToString(),
                        Cur      = resultCurrencyAmountMinusFee.ToString(),
                    };
                }
            }
            // reversed estimation: specified currency to GOLD
            else
            {
                // fiat
                if (ethereumToken == null)
                {
                    var mntBalance = ethAddress != null ? await EthereumObserver.GetAddressMntBalance(ethAddress) : BigInteger.Zero;

                    var fee = CoreLogic.Finance.Estimation.SellingFeeForFiat((long)inputAmount, mntBalance);
                    var res = await CoreLogic.Finance.Estimation.SellGoldFiatRev(
                        services : HttpContext.RequestServices,
                        fiatCurrency : fiatCurrency,
                        requiredFiatAmountWithFeeCents : (long)inputAmount + fee
                        );

                    allowed                      = res.Allowed;
                    centsPerGold                 = res.CentsPerGoldRate;
                    resultGoldAmount             = res.ResultGoldAmount;
                    resultCurrencyAmount         = res.ResultCentsAmount;
                    resultCurrencyAmountMinusFee = inputAmount;

                    viewAmount         = res.ResultGoldAmount.ToString();
                    viewAmountCurrency = "GOLD";
                    viewFee            = fee.ToString();
                    viewFeeCurrency    = fiatCurrency.ToString().ToUpper();

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = (long)withdrawalLimitMin / 100d,
                        Max      = (long)withdrawalLimitMax / 100d,
                        Cur      = (long)resultCurrencyAmountMinusFee / 100d,
                    };
                }
                // cryptoasset
                else
                {
                    var fee = CoreLogic.Finance.Estimation.SellingFeeForCrypto(ethereumToken.Value, inputAmount);

                    var res = await CoreLogic.Finance.Estimation.SellGoldCryptoRev(
                        services : HttpContext.RequestServices,
                        ethereumToken : ethereumToken.Value,
                        fiatCurrency : fiatCurrency,
                        requiredCryptoAmountWithFee : inputAmount + fee
                        );

                    allowed                      = res.Allowed;
                    centsPerGold                 = res.CentsPerGoldRate;
                    centsPerAsset                = res.CentsPerAssetRate;
                    resultGoldAmount             = res.ResultGoldAmount;
                    resultCurrencyAmount         = res.ResultAssetAmount;
                    resultCurrencyAmountMinusFee = inputAmount;

                    viewAmount         = res.ResultGoldAmount.ToString();
                    viewAmountCurrency = "GOLD";
                    viewFee            = fee.ToString();
                    viewFeeCurrency    = ethereumToken.Value.ToString().ToUpper();

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = withdrawalLimitMin.ToString(),
                        Max      = withdrawalLimitMax.ToString(),
                        Cur      = resultCurrencyAmountMinusFee.ToString(),
                    };
                }
            }

            var limitExceeded = resultCurrencyAmountMinusFee <withdrawalLimitMin || resultCurrencyAmountMinusFee> withdrawalLimitMax;

            return(new EstimationResult()
            {
                TradingAllowed = allowed,
                IsLimitExceeded = limitExceeded,
                View = new EstimateView()
                {
                    Amount = viewAmount,
                    AmountCurrency = viewAmountCurrency,
                    Fee = viewFee,
                    FeeCurrency = viewFeeCurrency,
                    Limits = limitsData,
                },
                CentsPerAssetRate = centsPerAsset,
                CentsPerGoldRate = centsPerGold,
                ResultGoldAmount = resultGoldAmount,
                ResultCurrencyAmount = resultCurrencyAmount,
            });
        }