public FundService(
     IOptions <AppSettings> appSettings,
     IInvictusClient invictusClient,
     IEthplorerClient ethplorerClient,
     ICoinGeckoClient coinGeckoClient,
     IGraphClient graphClient,
     ICurrencyConverter currencyConverter,
     IFundPerformanceRepository fundPerformanceRepository,
     ITransactionRepository transactionRepository,
     IOperationRepository operationRepository,
     IHttpContextAccessor httpContextAccessor,
     IScopedCancellationToken scopedCancellationToken)
     : base(appSettings, currencyConverter, transactionRepository, operationRepository, httpContextAccessor, scopedCancellationToken)
 {
     this.invictusClient            = invictusClient;
     this.ethplorerClient           = ethplorerClient;
     this.coinGeckoClient           = coinGeckoClient;
     this.graphClient               = graphClient;
     this.fundPerformanceRepository = fundPerformanceRepository;
 }
Beispiel #2
0
        private async Task SyncPerformanceAsync(
            ICoinGeckoClient coinGeckoClient,
            IInvictusClient invictusClient,
            IFundPerformanceRepository repository,
            IFundSettings fund,
            DateTimeOffset startDate,
            DateTimeOffset endDate,
            CancellationToken cancellationToken)
        {
            var start = new DateTimeOffset(startDate.Date, TimeSpan.Zero);

            while (!cancellationToken.IsCancellationRequested)
            {
                if (start >= DateTimeOffset.UtcNow)
                {
                    break;
                }

                var end = start.AddDays(MaxDays).Round();

                Console.WriteLine($"[{fund.ContractAddress}] Processing Batch: {start} -> {end}");

                var invictusNavs = await invictusClient
                                   .ListPerformanceAsync(fund.Symbol, start, end)
                                   .ToListAsync(cancellationToken);

                if (invictusNavs.Any())
                {
                    var marketPrices = fund.Tradable
                        ? await coinGeckoClient
                                       .ListCoinPerformanceAsync(fund.CoinGeckoId, start.AddDays(-1), end.AddDays(1))
                                       .ToListAsync(cancellationToken)
                        : new List <CoinGeckoCoinPerformance>();

                    foreach (var nav in invictusNavs)
                    {
                        var closestPrice = marketPrices
                                           .OrderBy(i => Math.Abs(i.Date.ToUnixTimeSeconds() - new DateTimeOffset(nav.Date, TimeSpan.Zero).ToUnixTimeSeconds()))
                                           .FirstOrDefault();

                        var perf = new DataFundPerformance()
                        {
                            Address   = fund.ContractAddress,
                            Date      = nav.Date,
                            Nav       = nav.NetAssetValuePerToken,
                            Price     = closestPrice?.Price ?? -1,
                            MarketCap = closestPrice?.MarketCap ?? -1,
                            Volume    = closestPrice?.Volume ?? -1
                        };

                        await repository.UploadItemsAsync(perf);
                    }
                }

                Console.WriteLine($"[{fund.ContractAddress}] Finished Batch: {start} -> {end}");

                if (end >= endDate)
                {
                    break;
                }
                else
                {
                    start = end;
                }
            }
        }