Exemplo n.º 1
0
        public async Task <ActionResult <PaginationResponse <WalletBalanceContract> > > Get(int take, string continuation)
        {
            if (take <= 0)
            {
                return(BadRequest(BlockchainErrorResponse.Create("'take' must be grater than zero")));
            }

            if (!Validators.ValidateAzureContinuation(continuation))
            {
                return(BadRequest(BlockchainErrorResponse.Create("'continuation' must be null or valid Azure continuation token")));
            }

            IEnumerable <DepositWalletBalanceEntity> balances;

            if (_api.CanGetBalances)
            {
                // to be able to enroll fake transfers properly
                // we keep block number increased by one order of magnitude
                var lastConfirmedBlock = await _api.GetLastConfirmedBlockNumberAsync() * 10;

                var depositWallets = await _depositWallets.GetWalletsAsync(take, continuation);

                var addresses = depositWallets.items.Select(w => w.Address).ToArray();

                continuation = depositWallets.continuation;
                balances     = (await _api.GetBalancesAsync(addresses, async assetId => await _assets.GetAsync(assetId)))
                               .Select(b => new DepositWalletBalanceEntity(b.Address, b.AssetId)
                {
                    Amount      = b.Amount,
                    BlockNumber = lastConfirmedBlock
                });
            }
            else
            {
                (balances, continuation) =
                    await _depositWallets.GetBalanceAsync(take, continuation);
            }

            var result = new List <WalletBalanceContract>();

            foreach (var balance in balances)
            {
                // blockchain may return unknown assets,
                // filter out such items

                var accuracy = (await _assets.GetAsync(balance.AssetId))?.Accuracy;
                if (accuracy == null)
                {
                    continue;
                }

                result.Add(new WalletBalanceContract
                {
                    Address = balance.Address,
                    AssetId = balance.AssetId,
                    Balance = Conversions.CoinsToContract(balance.Amount, accuracy.Value),
                    Block   = balance.BlockNumber
                });
            }

            return(PaginationResponse.From(continuation, result));
        }