private void UpdateBalanceIndex(string fromAddress, string toAddress, string tokenSymbol, long amount) { if (tokenSymbol == null) { return; } //Create sender index CreateIndexIfNotExist(fromAddress, tokenSymbol); //Create recipient index CreateIndexIfNotExist(toAddress, tokenSymbol); //Update sender balance if (fromAddress != null) { BalanceIndexItem index = database.GetBalanceIndex(fromAddress, tokenSymbol); index.Balance = index.Balance - amount; database.SaveBalanceIndex(index); IndexBalanceCache.AddOrUpdate(index); } //Update recipient balance if (toAddress != null) { BalanceIndexItem index = database.GetBalanceIndex(toAddress, tokenSymbol); index.Balance = index.Balance + amount; database.SaveBalanceIndex(index); IndexBalanceCache.AddOrUpdate(index); } }
public static void AddOrUpdate(BalanceIndexItem item) { if (cache == null) { cache = new List <BalanceIndexItem>(); cache.Add(item); } else { var find = cache.Where(x => x.Address == item.Address && x.TokenSymbol == item.TokenSymbol) .FirstOrDefault(); if (find == null) { cache.Add(item); } else { cache.Remove(find); cache.Add(item); } } }
private void CreateIndexIfNotExist(string address, string tokenSymbol) { if (address != null) { var index = database.GetBalanceIndex(address, tokenSymbol); if (index == null) { index = new BalanceIndexItem(); index.TokenSymbol = tokenSymbol; index.Balance = 0; index.Address = address; database.SaveBalanceIndex(index); IndexBalanceCache.AddOrUpdate(index); } } }