Skip to content

Commit

Permalink
modify: token allowance image url.
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-zhaolei committed Dec 26, 2024
1 parent 4189889 commit 2eda2ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
17 changes: 12 additions & 5 deletions src/CAServer.Application/Tokens/Cache/TokenCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class TokenCacheProvider : ITokenCacheProvider, ISingletonDependency
private readonly IDistributedCache<GetTokenInfoDto> _tokenInfoCache;
private readonly ILogger<TokenCacheProvider> _logger;

public TokenCacheProvider(IContractProvider contractProvider, IDistributedCache<GetTokenInfoDto> tokenInfoCache, ILogger<TokenCacheProvider> logger)
public TokenCacheProvider(IContractProvider contractProvider, IDistributedCache<GetTokenInfoDto> tokenInfoCache,
ILogger<TokenCacheProvider> logger)
{
_contractProvider = contractProvider;
_tokenInfoCache = tokenInfoCache;
Expand Down Expand Up @@ -56,10 +57,15 @@ public async Task<GetTokenInfoDto> GetTokenInfoAsync(string chainId, string symb
Issuer = output.Issuer.ToBase58(),
IsBurnable = output.IsBurnable,
IssueChainId = output.IssueChainId,
Expires = output.ExternalInfo?.Value.TryGetValue("__seed_exp_time", out _) == true ?
output.ExternalInfo?.Value["__seed_exp_time"] : "",
SeedOwnedSymbol = output.ExternalInfo?.Value.TryGetValue("__seed_owned_symbol", out _) == true ?
output.ExternalInfo?.Value?["__seed_owned_symbol"] : ""
Expires = output.ExternalInfo?.Value.TryGetValue("__seed_exp_time", out _) == true
? output.ExternalInfo?.Value["__seed_exp_time"]
: "",
SeedOwnedSymbol = output.ExternalInfo?.Value.TryGetValue("__seed_owned_symbol", out _) == true
? output.ExternalInfo?.Value?["__seed_owned_symbol"]
: "",
ImageUrl = output.ExternalInfo?.Value.TryGetValue("__ft_image_uri", out _) == true
? output.ExternalInfo?.Value?["__ft_image_uri"]
: ""
}
: new GetTokenInfoDto();
await _tokenInfoCache.SetAsync(cacheKey, tokenInfoCache, new DistributedCacheEntryOptions
Expand All @@ -68,6 +74,7 @@ public async Task<GetTokenInfoDto> GetTokenInfoAsync(string chainId, string symb
});
return tokenInfoCache;
}

return tokenInfoCache;
}
catch (Exception e)
Expand Down
25 changes: 18 additions & 7 deletions src/CAServer.Application/Tokens/TokenAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ public async Task<List<GetTokenListDto>> GetTokenListAsync(GetTokenListRequestDt
var tokenInfoList = GetTokenInfoList(userTokensDto, indexerToken.TokenInfo);

// Check and adjust SkipCount and MaxResultCount
var skipCount = input.SkipCount < TokensConstants.SkipCountDefault ? TokensConstants.SkipCountDefault : input.SkipCount;
var maxResultCount = input.MaxResultCount <= TokensConstants.MaxResultCountInvalid ? TokensConstants.MaxResultCountDefault : input.MaxResultCount;
var skipCount = input.SkipCount < TokensConstants.SkipCountDefault
? TokensConstants.SkipCountDefault
: input.SkipCount;
var maxResultCount = input.MaxResultCount <= TokensConstants.MaxResultCountInvalid
? TokensConstants.MaxResultCountDefault
: input.MaxResultCount;

return tokenInfoList.Skip(skipCount).Take(maxResultCount).ToList();
}
Expand Down Expand Up @@ -305,12 +309,15 @@ public async Task<GetTokenAllowancesDto> GetTokenAllowancesAsync(GetAssetsBase i
SymbolApproveList = t.Items.Select(s => new SymbolApprove()
{
Symbol = s.Symbol,
Amount = allowanceMap.TryGetValue(GetKey(s.Symbol, t.Key.Spender, t.Key.ChainId), out long value) ? value : 0,
Amount = allowanceMap.TryGetValue(GetKey(s.Symbol, t.Key.Spender, t.Key.ChainId), out long value)
? value
: 0,
Decimals = tokenInfoDtos.FirstOrDefault(i => i.Symbol == s.Symbol.Replace("-*", "-1")) == null
? 0
: tokenInfoDtos.First(i => i.Symbol == s.Symbol.Replace("-*", "-1")).Decimals,
UpdateTime = s.UpdateTime,
ImageUrl = _assetsLibraryProvider.buildSymbolImageUrl(s.Symbol),
ImageUrl = _assetsLibraryProvider.buildSymbolImageUrl(s.Symbol,
tokenInfoDtos.FirstOrDefault(i => i.Symbol == s.Symbol)?.ImageUrl),
}).ToList()
}).ToList();

Expand All @@ -325,7 +332,9 @@ public async Task<GetTokenAllowancesDto> GetTokenAllowancesAsync(GetAssetsBase i
}

tokenAllowanceList.Sort((t1, t2) =>
(t1.Name.IsNullOrWhiteSpace() ? CommonConstant.UpperZ : t1.Name).CompareTo(t2.Name.IsNullOrWhiteSpace() ? CommonConstant.UpperZ : t2.Name));
(t1.Name.IsNullOrWhiteSpace() ? CommonConstant.UpperZ : t1.Name).CompareTo(t2.Name.IsNullOrWhiteSpace()
? CommonConstant.UpperZ
: t2.Name));

return new GetTokenAllowancesDto
{
Expand All @@ -341,7 +350,8 @@ private async Task<Dictionary<string, long>> GetAllowanceList(List<GetAllowanceD
var tasks = dtoList.Select(dto => Task.Run(() => GetAllowanceTask(dto)));
GetAllowanceDTO[] result = await Task.WhenAll(tasks);
stopwatch.Stop();
_logger.LogDebug("GetAllowanceList dtoList count = {0}, spendTime = {1} ms dtoList = {2}", dtoList.Count, stopwatch.ElapsedMilliseconds,JsonConvert.SerializeObject(result));
_logger.LogDebug("GetAllowanceList dtoList count = {0}, spendTime = {1} ms dtoList = {2}", dtoList.Count,
stopwatch.ElapsedMilliseconds, JsonConvert.SerializeObject(result));

return result.ToDictionary(
dto => GetKey(dto.Symbol, dto.Spender, dto.ChainId),
Expand All @@ -351,7 +361,8 @@ private async Task<Dictionary<string, long>> GetAllowanceList(List<GetAllowanceD

private async Task<GetAllowanceDTO> GetAllowanceTask(GetAllowanceDTO dto)
{
GetAllowanceOutput output = await _contractProvider.GetAllowanceAsync(dto.Symbol, dto.Owner, dto.Spender, dto.ChainId);
GetAllowanceOutput output =
await _contractProvider.GetAllowanceAsync(dto.Symbol, dto.Owner, dto.Spender, dto.ChainId);
dto.Allowance = output.Allowance;
_logger.LogDebug("GetAllowanceTask dto = {0}", JsonConvert.SerializeObject(dto));
return dto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ private async Task<TransferLimitDto> GeneratorTransferLimitAsync(IndexerSearchTo
Restricted = true
};

transferLimit.ImageUrl = _assetsLibraryProvider.buildSymbolImageUrl(token.TokenInfo.Symbol);
transferLimit.ImageUrl =
_assetsLibraryProvider.buildSymbolImageUrl(token.TokenInfo.Symbol, token.TokenInfo.ImageUrl);

if (_securityOptions.TokenTransferLimitDict[token.ChainId].SingleTransferLimit
.TryGetValue(token.TokenInfo.Symbol, out var singleLimit))
Expand Down

0 comments on commit 2eda2ae

Please sign in to comment.