コード例 #1
0
        public List <BalanceViewModel> GetHolders(string symbol)
        {
            var accountQuery = new AccountQueries(_context);
            var tokenQuery   = new TokenQueries(_context);

            var token = tokenQuery.QueryToken(symbol);

            var balances = new List <BalanceViewModel>();

            if (token == null)
            {
                return(balances);
            }

            if ((token.Flags & TokenFlags.Fungible) != 0)
            {
                foreach (var account in accountQuery.QueryRichList(30, symbol))
                {
                    var accountTokenBalanceList = accountQuery.QueryAccountTokenBalanceList(account.Address, symbol);

                    foreach (var balance in accountTokenBalanceList)
                    {
                        var existingEntry = balances.SingleOrDefault(p => p.Address.Equals(account.Address));
                        if (existingEntry != null)
                        {
                            existingEntry.Balance += UnitConversion.ToDecimal(balance.Amount, (int)token.Decimals);
                        }
                        else
                        {
                            var vm = new BalanceViewModel
                            {
                                ChainName = balance.Chain,
                                Balance   = UnitConversion.ToDecimal(balance.Amount, (int)token.Decimals),
                                Token     = TokenViewModel.FromToken(token, AppSettings.MockLogoUrl),
                                Address   = account.Address
                            };
                            balances.Add(vm);
                        }
                    }
                }
            }
            else
            {
                var nftList = tokenQuery.QueryAllNonFungibleTokens(symbol);
                foreach (var nonFungibleToken in nftList)
                {
                    var vm = new BalanceViewModel
                    {
                        ChainName = nonFungibleToken.Chain,
                        Token     = TokenViewModel.FromToken(token, AppSettings.MockLogoUrl),
                        Address   = nonFungibleToken.AccountAddress
                    };
                    vm.Balance = nftList.Count(p => p.AccountAddress.Equals(vm.Address));
                    balances.Add(vm);
                }
            }

            return(balances);
        }