Filter empty values in token update

pull/9371/head
Viktor Baranov 9 months ago
parent e69c87e754
commit 59cec00a41
  1. 1
      CHANGELOG.md
  2. 8
      apps/explorer/lib/explorer/chain.ex
  3. 66
      apps/explorer/lib/explorer/chain/bridged_token.ex

@ -13,6 +13,7 @@
### Fixes
- [#9377](https://github.com/blockscout/blockscout/pull/9377) - Speed up account abstraction proxy
- [#9371](https://github.com/blockscout/blockscout/pull/9371) - Filter empty values before token update
- [#9356](https://github.com/blockscout/blockscout/pull/9356) - Remove ERC-1155 logs params from coin balances params
- [#9346](https://github.com/blockscout/blockscout/pull/9346) - Process integer balance in genesis.json
- [#9317](https://github.com/blockscout/blockscout/pull/9317) - Include null gas price txs in fee calculations

@ -3718,8 +3718,12 @@ defmodule Explorer.Chain do
"""
@spec update_token(Token.t(), map()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
def update_token(%Token{contract_address_hash: address_hash} = token, params \\ %{}) do
token_changeset = Token.changeset(token, Map.put(params, :updated_at, DateTime.utc_now()))
address_name_changeset = Address.Name.changeset(%Address.Name{}, Map.put(params, :address_hash, address_hash))
filtered_params = for({key, value} <- params, value !== "" && !is_nil(value), do: {key, value}) |> Enum.into(%{})
token_changeset = Token.changeset(token, Map.put(filtered_params, :updated_at, DateTime.utc_now()))
address_name_changeset =
Address.Name.changeset(%Address.Name{}, Map.put(filtered_params, :address_hash, address_hash))
stale_error_field = :contract_address_hash
stale_error_message = "is up to date"

@ -31,6 +31,18 @@ defmodule Explorer.Chain.BridgedToken do
require Logger
@default_paging_options %PagingOptions{page_size: 50}
# keccak 256 from name()
@name_signature "0x06fdde03"
# 95d89b41 = keccak256(symbol())
@symbol_signature "0x95d89b41"
# keccak 256 from decimals()
@decimals_signature "0x313ce567"
# keccak 256 from totalSupply()
@total_supply_signature "0x18160ddd"
# keccak 256 from token0()
@token0_signature "0x0dfe1681"
# keccak 256 from token1()
@token1_signature "0xd21220a7"
@derive {Poison.Encoder,
except: [
@ -557,24 +569,12 @@ defmodule Explorer.Chain.BridgedToken do
end
defp sushiswap_custom_metadata(foreign_token_address_hash, eth_call_foreign_json_rpc_named_arguments) do
# keccak 256 from token0()
token0_signature = "0x0dfe1681"
# keccak 256 from token1()
token1_signature = "0xd21220a7"
# keccak 256 from name()
name_signature = "0x06fdde03"
# keccak 256 from symbol()
symbol_signature = "0x95d89b41"
with {:ok, "0x" <> token0_encoded} <-
token0_signature
@token0_signature
|> Contract.eth_call_request(foreign_token_address_hash, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
{:ok, "0x" <> token1_encoded} <-
token1_signature
@token1_signature
|> Contract.eth_call_request(foreign_token_address_hash, 2, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
token0_hash <- parse_contract_response(token0_encoded, :address),
@ -584,19 +584,19 @@ defmodule Explorer.Chain.BridgedToken do
token0_hash_str <- "0x" <> Base.encode16(token0_hash, case: :lower),
token1_hash_str <- "0x" <> Base.encode16(token1_hash, case: :lower),
{:ok, "0x" <> token0_name_encoded} <-
name_signature
@name_signature
|> Contract.eth_call_request(token0_hash_str, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
{:ok, "0x" <> token1_name_encoded} <-
name_signature
@name_signature
|> Contract.eth_call_request(token1_hash_str, 2, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
{:ok, "0x" <> token0_symbol_encoded} <-
symbol_signature
@symbol_signature
|> Contract.eth_call_request(token0_hash_str, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
{:ok, "0x" <> token1_symbol_encoded} <-
symbol_signature
@symbol_signature
|> Contract.eth_call_request(token1_hash_str, 2, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments) do
token0_name = parse_contract_response(token0_name_encoded, :string, {:bytes, 32})
@ -650,21 +650,12 @@ defmodule Explorer.Chain.BridgedToken do
# keccak 256 from getReserves()
get_reserves_signature = "0x0902f1ac"
# keccak 256 from token0()
token0_signature = "0x0dfe1681"
# keccak 256 from token1()
token1_signature = "0xd21220a7"
# keccak 256 from totalSupply()
total_supply_signature = "0x18160ddd"
with {:ok, "0x" <> get_reserves_encoded} <-
get_reserves_signature
|> Contract.eth_call_request(foreign_token_address_hash, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
{:ok, "0x" <> home_token_total_supply_encoded} <-
total_supply_signature
@total_supply_signature
|> Contract.eth_call_request(home_token_contract_address_hash, 1, nil, nil)
|> json_rpc(json_rpc_named_arguments),
[reserve0, reserve1, _] <-
@ -672,7 +663,7 @@ defmodule Explorer.Chain.BridgedToken do
{:ok, token0_cap_usd} <-
get_lp_token_cap(
home_token_total_supply_encoded,
token0_signature,
@token0_signature,
reserve0,
foreign_token_address_hash,
eth_call_foreign_json_rpc_named_arguments
@ -680,7 +671,7 @@ defmodule Explorer.Chain.BridgedToken do
{:ok, token1_cap_usd} <-
get_lp_token_cap(
home_token_total_supply_encoded,
token1_signature,
@token1_signature,
reserve1,
foreign_token_address_hash,
eth_call_foreign_json_rpc_named_arguments
@ -700,12 +691,6 @@ defmodule Explorer.Chain.BridgedToken do
foreign_token_address_hash,
eth_call_foreign_json_rpc_named_arguments
) do
# keccak 256 from decimals()
decimals_signature = "0x313ce567"
# keccak 256 from totalSupply()
total_supply_signature = "0x18160ddd"
home_token_total_supply =
home_token_total_supply_encoded
|> parse_contract_response({:uint, 256})
@ -719,11 +704,11 @@ defmodule Explorer.Chain.BridgedToken do
false <- is_nil(token_hash),
token_hash_str <- "0x" <> Base.encode16(token_hash, case: :lower),
{:ok, "0x" <> token_decimals_encoded} <-
decimals_signature
@decimals_signature
|> Contract.eth_call_request(token_hash_str, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments),
{:ok, "0x" <> foreign_token_total_supply_encoded} <-
total_supply_signature
@total_supply_signature
|> Contract.eth_call_request(foreign_token_address_hash, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments) do
token_decimals = parse_contract_response(token_decimals_encoded, {:uint, 256})
@ -862,10 +847,7 @@ defmodule Explorer.Chain.BridgedToken do
balancer_token_hash = "0x" <> balancer_token_hash_without_0x
# 95d89b41 = keccak256(symbol())
symbol_signature = "0x95d89b41"
case symbol_signature
case @symbol_signature
|> Contract.eth_call_request(balancer_token_hash, 1, nil, nil)
|> json_rpc(eth_call_foreign_json_rpc_named_arguments) do
{:ok, "0x" <> symbol_encoded} ->

Loading…
Cancel
Save