Bridged tokens price from address hash refactoring

pull/3707/head
Viktor Baranov 4 years ago committed by Victor Baranov
parent 66719fb215
commit d3a54d821b
  1. 43
      apps/explorer/lib/explorer/chain/cache/token_exchange_rate.ex
  2. 23
      apps/explorer/lib/explorer/chain/supply/token_bridge.ex
  3. 8
      apps/explorer/lib/explorer/counters/bridge.ex
  4. 9
      apps/explorer/lib/explorer/market/market.ex

@ -55,14 +55,32 @@ defmodule Explorer.Chain.Cache.TokenExchangeRate do
"token_symbol_exchange_rate_#{symbol_or_address_hash_str}"
end
def fetch(token_hash, symbol_or_address_hash_str) do
if cache_expired?(symbol_or_address_hash_str) || value_is_empty?(symbol_or_address_hash_str) do
def fetch(token_hash, address_hash_str) do
if cache_expired?(address_hash_str) || value_is_empty?(address_hash_str) do
Task.start_link(fn ->
update_cache(token_hash, symbol_or_address_hash_str)
update_cache_by_address_hash_str(token_hash, address_hash_str)
end)
end
cached_value = fetch_from_cache(cache_key(symbol_or_address_hash_str))
cached_value = fetch_from_cache(cache_key(address_hash_str))
if is_nil(cached_value) || Decimal.cmp(cached_value, 0) == :eq do
fetch_from_db(token_hash)
else
cached_value
end
end
# fetching by symbol is not recommended to use because of possible collisions
# fetch() should be used instead
def fetch_by_symbol(token_hash, symbol) do
if cache_expired?(symbol) || value_is_empty?(symbol) do
Task.start_link(fn ->
update_cache_by_symbol(token_hash, symbol)
end)
end
cached_value = fetch_from_cache(cache_key(symbol))
if is_nil(cached_value) || Decimal.cmp(cached_value, 0) == :eq do
fetch_from_db(token_hash)
@ -89,13 +107,22 @@ defmodule Explorer.Chain.Cache.TokenExchangeRate do
is_nil(value) || value == 0
end
defp update_cache(token_hash, symbol_or_address_hash_str) do
put_into_cache("#{cache_key(symbol_or_address_hash_str)}_#{@last_update_key}", current_time())
defp update_cache_by_symbol(token_hash, symbol) do
put_into_cache("#{cache_key(symbol)}_#{@last_update_key}", current_time())
exchange_rate = fetch_token_exchange_rate(symbol)
put_into_db(token_hash, exchange_rate)
put_into_cache(cache_key(symbol), exchange_rate)
end
defp update_cache_by_address_hash_str(token_hash, address_hash_str) do
put_into_cache("#{cache_key(address_hash_str)}_#{@last_update_key}", current_time())
exchange_rate = fetch_token_exchange_rate(symbol_or_address_hash_str)
exchange_rate = fetch_token_exchange_rate_by_address(address_hash_str)
put_into_db(token_hash, exchange_rate)
put_into_cache(cache_key(symbol_or_address_hash_str), exchange_rate)
put_into_cache(cache_key(address_hash_str), exchange_rate)
end
def fetch_token_exchange_rate(symbol) do

@ -194,13 +194,18 @@ defmodule Explorer.Chain.Supply.TokenBridge do
omni_bridge_market_cap
end
def get_current_price_for_bridged_token(_token_hash, symbol) when is_nil(symbol), do: nil
def get_current_price_for_bridged_token(token_hash, _symbol) when is_nil(token_hash), do: nil
def get_current_price_for_bridged_token(_token_hash, foreign_token_contract_address_hash)
when is_nil(foreign_token_contract_address_hash),
do: nil
def get_current_price_for_bridged_token(token_hash, symbol) do
bridged_token_symbol_for_price_fetching = bridged_token_symbol_mapping_to_get_price(symbol)
def get_current_price_for_bridged_token(token_hash, _foreign_token_contract_address_hash) when is_nil(token_hash),
do: nil
TokenExchangeRateCache.fetch(token_hash, bridged_token_symbol_for_price_fetching)
def get_current_price_for_bridged_token(token_hash, foreign_token_contract_address_hash) do
foreign_token_contract_address_hash_str =
"0x" <> Base.encode16(foreign_token_contract_address_hash.bytes, case: :lower)
TokenExchangeRateCache.fetch(token_hash, foreign_token_contract_address_hash_str)
end
def get_bridged_mainnet_tokens_list do
@ -296,12 +301,4 @@ defmodule Explorer.Chain.Supply.TokenBridge do
omni_bridge_market_cap
end
defp bridged_token_symbol_mapping_to_get_price(symbol) do
case symbol do
"POA20" -> "POA"
"yDAI+yUSDC+yUSDT+yTUSD" -> "yCurve"
symbol -> symbol
end
end
end

@ -166,9 +166,11 @@ defmodule Explorer.Counters.Bridge do
bridged_mainnet_tokens_list = TokenBridge.get_bridged_mainnet_tokens_list()
bridged_mainnet_tokens_list
|> Enum.each(fn {bridged_token_hash, bridged_token_symbol, _} ->
bridged_token_price = TokenBridge.get_current_price_for_bridged_token(bridged_token_hash, bridged_token_symbol)
cache_key = TokenExchangeRate.cache_key(bridged_token_symbol)
|> Enum.each(fn {bridged_token_hash, _bridged_token_symbol, _custom_cap, foreign_token_contract_address_hash} ->
bridged_token_price =
TokenBridge.get_current_price_for_bridged_token(bridged_token_hash, foreign_token_contract_address_hash)
cache_key = TokenExchangeRate.cache_key(foreign_token_contract_address_hash)
TokenExchangeRate.put_into_cache(cache_key, bridged_token_price)
end)

@ -62,8 +62,11 @@ defmodule Explorer.Market do
matches_known_address ->
fetch_token_usd_value(matches_known_address, symbol)
mainnet_bridged_token?(token) ->
TokenBridge.get_current_price_for_bridged_token(token.contract_address_hash, symbol)
bridged_token = mainnet_bridged_token?(token) ->
TokenBridge.get_current_price_for_bridged_token(
token.contract_address_hash,
bridged_token.foreign_token_contract_address_hash
)
true ->
nil
@ -90,7 +93,7 @@ defmodule Explorer.Market do
if bridged_token do
if bridged_token.foreign_chain_id do
if Decimal.cmp(bridged_token.foreign_chain_id, Decimal.new(1)) == :eq, do: true, else: false
if Decimal.cmp(bridged_token.foreign_chain_id, Decimal.new(1)) == :eq, do: bridged_token, else: false
else
false
end

Loading…
Cancel
Save