Merge pull request #5019 from blockscout/vb-fetch_last_token_balance-error-fix

Fix fetch_last_token_balance function termination
pull/5020/head
Victor Baranov 3 years ago committed by GitHub
commit 33d636d1a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 18
      apps/explorer/lib/explorer/chain.ex
  3. 36
      apps/explorer/lib/explorer/chain/address/current_token_balance.ex

@ -8,6 +8,7 @@
- [#4931](https://github.com/blockscout/blockscout/pull/4931) - Web3 modal with Wallet Connect for Write contract page and Staking Dapp - [#4931](https://github.com/blockscout/blockscout/pull/4931) - Web3 modal with Wallet Connect for Write contract page and Staking Dapp
### Fixes ### Fixes
- [#5019](https://github.com/blockscout/blockscout/pull/5019) - Fix fetch_last_token_balance function termination
- [#5011](https://github.com/blockscout/blockscout/pull/5011) - Fix `0x0` implementation address - [#5011](https://github.com/blockscout/blockscout/pull/5011) - Fix `0x0` implementation address
- [#5008](https://github.com/blockscout/blockscout/pull/5008) - Extend decimals cap in format_according_to_decimals up to 24 - [#5008](https://github.com/blockscout/blockscout/pull/5008) - Extend decimals cap in format_according_to_decimals up to 24
- [#5005](https://github.com/blockscout/blockscout/pull/5005) - Fix falsy appearance `Connection Lost` warning on reload/switch page - [#5005](https://github.com/blockscout/blockscout/pull/5005) - Fix falsy appearance `Connection Lost` warning on reload/switch page

@ -5654,16 +5654,22 @@ defmodule Explorer.Chain do
@spec fetch_last_token_balance(Hash.Address.t(), Hash.Address.t()) :: Decimal.t() @spec fetch_last_token_balance(Hash.Address.t(), Hash.Address.t()) :: Decimal.t()
def fetch_last_token_balance(address_hash, token_contract_address_hash) do def fetch_last_token_balance(address_hash, token_contract_address_hash) do
address_hash if address_hash !== %{} do
|> CurrentTokenBalance.last_token_balance(token_contract_address_hash) address_hash
|> Repo.one() || Decimal.new(0) |> CurrentTokenBalance.last_token_balance(token_contract_address_hash) || Decimal.new(0)
else
Decimal.new(0)
end
end end
# @spec fetch_last_token_balance_1155(Hash.Address.t(), Hash.Address.t()) :: Decimal.t() # @spec fetch_last_token_balance_1155(Hash.Address.t(), Hash.Address.t()) :: Decimal.t()
def fetch_last_token_balance_1155(address_hash, token_contract_address_hash, token_id) do def fetch_last_token_balance_1155(address_hash, token_contract_address_hash, token_id) do
address_hash if address_hash !== %{} do
|> CurrentTokenBalance.last_token_balance_1155(token_contract_address_hash, token_id) address_hash
|> Repo.one() || Decimal.new(0) |> CurrentTokenBalance.last_token_balance_1155(token_contract_address_hash, token_id) || Decimal.new(0)
else
Decimal.new(0)
end
end end
@spec address_to_coin_balances(Hash.Address.t(), [paging_options]) :: [] @spec address_to_coin_balances(Hash.Address.t(), [paging_options]) :: []

@ -11,7 +11,7 @@ defmodule Explorer.Chain.Address.CurrentTokenBalance do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query, only: [from: 2, limit: 2, offset: 2, order_by: 3, preload: 2] import Ecto.Query, only: [from: 2, limit: 2, offset: 2, order_by: 3, preload: 2]
alias Explorer.{Chain, PagingOptions} alias Explorer.{Chain, PagingOptions, Repo}
alias Explorer.Chain.{Address, Block, BridgedToken, Hash, Token} alias Explorer.Chain.{Address, Block, BridgedToken, Hash, Token}
@default_paging_options %PagingOptions{page_size: 50} @default_paging_options %PagingOptions{page_size: 50}
@ -188,25 +188,33 @@ defmodule Explorer.Chain.Address.CurrentTokenBalance do
Builds an `t:Ecto.Query.t/0` to fetch the current balance of the given address for the given token. Builds an `t:Ecto.Query.t/0` to fetch the current balance of the given address for the given token.
""" """
def last_token_balance(address_hash, token_contract_address_hash) do def last_token_balance(address_hash, token_contract_address_hash) do
from( query =
tb in __MODULE__, from(
where: tb.token_contract_address_hash == ^token_contract_address_hash, tb in __MODULE__,
where: tb.address_hash == ^address_hash, where: tb.token_contract_address_hash == ^token_contract_address_hash,
select: tb.value where: tb.address_hash == ^address_hash,
) select: tb.value
)
query
|> Repo.one()
end end
@doc """ @doc """
Builds an `t:Ecto.Query.t/0` to fetch the current balance of the given address for the given token and token_id Builds an `t:Ecto.Query.t/0` to fetch the current balance of the given address for the given token and token_id
""" """
def last_token_balance_1155(address_hash, token_contract_address_hash, token_id) do def last_token_balance_1155(address_hash, token_contract_address_hash, token_id) do
from( query =
ctb in __MODULE__, from(
where: ctb.token_contract_address_hash == ^token_contract_address_hash, ctb in __MODULE__,
where: ctb.address_hash == ^address_hash, where: ctb.token_contract_address_hash == ^token_contract_address_hash,
where: ctb.token_id == ^token_id, where: ctb.address_hash == ^address_hash,
select: ctb.value where: ctb.token_id == ^token_id,
) select: ctb.value
)
query
|> Repo.one()
end end
@doc """ @doc """

Loading…
Cancel
Save