Exemplo n.º 1
0
        private async Task <EstimationResult> Estimation(
            RuntimeConfig rcfg,
            BigInteger inputAmount,
            TradableCurrency?ethereumToken,
            FiatCurrency fiatCurrency,
            bool reversed,
            double discount,
            BigInteger depositLimitMin,
            BigInteger depositLimitMax
            )
        {
            bool allowed = false;

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

            object viewAmount         = null;
            var    viewAmountCurrency = "";

            var limitsData = (EstimateLimitsView)null;

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

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

                    viewAmount         = resultGoldAmount.ToString();
                    viewAmountCurrency = "GOLD";

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = (long)depositLimitMin / 100d,
                        Max      = (long)depositLimitMax / 100d,
                        Cur      = (long)resultCurrencyAmount / 100d,
                    };
                }

                // cryptoasset
                else
                {
                    var res = await CoreLogic.Finance.Estimation.BuyGoldCrypto(
                        services : HttpContext.RequestServices,
                        ethereumToken : ethereumToken.Value,
                        fiatCurrency : fiatCurrency,
                        cryptoAmount : inputAmount,
                        discount : discount
                        );

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

                    viewAmount         = resultGoldAmount.ToString();
                    viewAmountCurrency = "GOLD";

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = depositLimitMin.ToString(),
                        Max      = depositLimitMax.ToString(),
                        Cur      = resultCurrencyAmount.ToString(),
                    };
                }
            }
            // reversed estimation: GOLD to specified currency
            else
            {
                // fiat
                if (ethereumToken == null)
                {
                    var res = await CoreLogic.Finance.Estimation.BuyGoldFiatRev(
                        services : HttpContext.RequestServices,
                        fiatCurrency : fiatCurrency,
                        requiredGoldAmount : inputAmount,
                        discount : discount
                        );

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

                    viewAmount         = (long)resultCurrencyAmount / 100d;
                    viewAmountCurrency = fiatCurrency.ToString().ToUpper();

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = (long)depositLimitMin / 100d,
                        Max      = (long)depositLimitMax / 100d,
                        Cur      = (long)resultCurrencyAmount / 100d,
                    };
                }

                // cryptoasset
                else
                {
                    var res = await CoreLogic.Finance.Estimation.BuyGoldCryptoRev(
                        services : HttpContext.RequestServices,
                        ethereumToken : ethereumToken.Value,
                        fiatCurrency : fiatCurrency,
                        requiredGoldAmount : inputAmount,
                        discount : discount
                        );

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

                    viewAmount         = resultCurrencyAmount.ToString();
                    viewAmountCurrency = ethereumToken.Value.ToString().ToUpper();

                    limitsData = new EstimateLimitsView()
                    {
                        Currency = fiatCurrency.ToString().ToUpper(),
                        Min      = depositLimitMin.ToString(),
                        Max      = depositLimitMax.ToString(),
                        Cur      = resultCurrencyAmount.ToString(),
                    };
                }
            }

            var limitExceeded = resultCurrencyAmount <depositLimitMin || resultCurrencyAmount> depositLimitMax;

            return(new EstimationResult()
            {
                TradingAllowed = allowed,
                IsLimitExceeded = limitExceeded,
                View = new EstimateView()
                {
                    Amount = viewAmount,
                    AmountCurrency = viewAmountCurrency,
                    Limits = limitsData,
                },
                CentsPerAssetRate = centsPerAsset,
                CentsPerGoldRate = centsPerGold,
                ResultCurrencyAmount = resultCurrencyAmount,
                ResultGoldAmount = resultGoldAmount,
            });
        }
Exemplo n.º 2
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,
            });
        }