Ejemplo n.º 1
0
        protected override async Task DoWork()
        {
            //DbContext.DetachEverything();
            var tokenList = await DbContext.Tokens.Where(x => x.IsEnabled && !x.IsDeleted).ToListAsync();

            var thisDayStart = DayStartOf(DateTime.Now);

            foreach (var token in tokenList)
            {
                var lastStat = await DbContext.TokenStatistics.LastOrDefaultAsync(x => x.TokenId == token.Id);

                var add = lastStat == null || lastStat.Date != thisDayStart;

                var price = await EthereumObserver.GetTokenPrice(token.EtheramaContractAddress);

                var buyCount = await EthereumObserver.GetBuyCount(token.EtheramaContractAddress);

                var sellCount = await EthereumObserver.GetSellCount(token.EtheramaContractAddress);

                var bonusPerShare = await EthereumObserver.GetBonusPerShare(token.EtheramaContractAddress);

                var volumeEth = await EthereumObserver.GetVolumeEth(token.EtheramaContractAddress);

                var volumeToken = await EthereumObserver.GetVolumeToken(token.EtheramaContractAddress);

                var blockNum = await EthereumObserver.GetLogsLatestBlockNumber();

                if (add)
                {
                    var tokenStat = new TokenStatistics {
                        Date        = thisDayStart,
                        PriceEth    = price,
                        BuyCount    = buyCount,
                        SellCount   = sellCount,
                        ShareReward = bonusPerShare,
                        VolumeEth   = volumeEth,
                        VolumeToken = volumeToken,
                        BlockNum    = blockNum,
                        TokenId     = token.Id
                    };
                    await DbContext.TokenStatistics.AddAsync(tokenStat);
                }
                else
                {
                    lastStat.PriceEth    = price;
                    lastStat.BuyCount    = buyCount;
                    lastStat.SellCount   = sellCount;
                    lastStat.ShareReward = bonusPerShare;
                    lastStat.VolumeEth   = volumeEth;
                    lastStat.VolumeToken = volumeToken;
                    lastStat.BlockNum    = blockNum;
                }
            }

            await DbContext.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        protected override async Task DoWork()
        {
            //DbContext.DetachEverything();
            var tokenList = await DbContext.Tokens.Where(x => x.IsEnabled && !x.IsDeleted).ToListAsync();

            foreach (var token in tokenList)
            {
                token.CurrentPriceEth = await EthereumObserver.GetTokenPrice(token.EtheramaContractAddress);

                token.TimeUpdated = DateTime.Now;
            }

            await DbContext.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        protected override async Task DoWork()
        {
            var gasPrice = await EthereumObserver.GetCurrentGasPrice();

            await EthereumWriter.UpdateMaxGaxPrice(gasPrice);
        }
Ejemplo n.º 4
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,
            });
        }