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. 14
      apps/explorer/lib/explorer/chain.ex
  3. 10
      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
### 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
- [#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

@ -5654,16 +5654,22 @@ defmodule Explorer.Chain do
@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
if address_hash !== %{} do
address_hash
|> CurrentTokenBalance.last_token_balance(token_contract_address_hash)
|> Repo.one() || Decimal.new(0)
|> CurrentTokenBalance.last_token_balance(token_contract_address_hash) || Decimal.new(0)
else
Decimal.new(0)
end
end
# @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
if address_hash !== %{} do
address_hash
|> CurrentTokenBalance.last_token_balance_1155(token_contract_address_hash, token_id)
|> 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
@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.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}
@default_paging_options %PagingOptions{page_size: 50}
@ -188,18 +188,23 @@ 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.
"""
def last_token_balance(address_hash, token_contract_address_hash) do
query =
from(
tb in __MODULE__,
where: tb.token_contract_address_hash == ^token_contract_address_hash,
where: tb.address_hash == ^address_hash,
select: tb.value
)
query
|> Repo.one()
end
@doc """
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
query =
from(
ctb in __MODULE__,
where: ctb.token_contract_address_hash == ^token_contract_address_hash,
@ -207,6 +212,9 @@ defmodule Explorer.Chain.Address.CurrentTokenBalance do
where: ctb.token_id == ^token_id,
select: ctb.value
)
query
|> Repo.one()
end
@doc """

Loading…
Cancel
Save