Skip to content

Commit

Permalink
Merge pull request blockscout#8765 from blockscout/vb-fix-tvl-update
Browse files Browse the repository at this point in the history
Fix for tvl update in market history when row already exists
  • Loading branch information
vbaranov authored Nov 1, 2023
2 parents 9bc9e50 + 64309ed commit 1e832d9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixes

- [#8765](https://github.com/blockscout/blockscout/pull/8765) - Fix for tvl update in market history when row already exists
- [#8759](https://github.com/blockscout/blockscout/pull/8759) - Gnosis safe proxy via singleton input
- [#8752](https://github.com/blockscout/blockscout/pull/8752) - Add `TOKEN_INSTANCE_OWNER_MIGRATION_ENABLED` env
- [#8724](https://github.com/blockscout/blockscout/pull/8724) - Fix flaky account notifier test
Expand Down
70 changes: 69 additions & 1 deletion apps/explorer/lib/explorer/market/market.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Explorer.Market do
alias Explorer.Market.{MarketHistory, MarketHistoryCache}
alias Explorer.{ExchangeRates, Repo}

import Ecto.Query, only: [from: 2]

@doc """
Retrieves the history for the recent specified amount of days.
Expand Down Expand Up @@ -67,7 +69,73 @@ defmodule Explorer.Market do
# Enforce MarketHistory ShareLocks order (see docs: sharelocks.md)
|> Enum.sort_by(& &1.date)

Repo.insert_all(MarketHistory, records_without_zeroes, on_conflict: :nothing, conflict_target: [:date])
Repo.insert_all(MarketHistory, records_without_zeroes,
on_conflict: market_history_on_conflict(),
conflict_target: [:date]
)
end

defp market_history_on_conflict do
from(
market_history in MarketHistory,
update: [
set: [
opening_price:
fragment(
"""
CASE WHEN (? IS NULL OR ? = 0) AND EXCLUDED.opening_price IS NOT NULL AND EXCLUDED.opening_price > 0
THEN EXCLUDED.opening_price
ELSE ?
END
""",
market_history.opening_price,
market_history.opening_price,
market_history.opening_price
),
closing_price:
fragment(
"""
CASE WHEN (? IS NULL OR ? = 0) AND EXCLUDED.closing_price IS NOT NULL AND EXCLUDED.closing_price > 0
THEN EXCLUDED.closing_price
ELSE ?
END
""",
market_history.closing_price,
market_history.closing_price,
market_history.closing_price
),
market_cap:
fragment(
"""
CASE WHEN (? IS NULL OR ? = 0) AND EXCLUDED.market_cap IS NOT NULL AND EXCLUDED.market_cap > 0
THEN EXCLUDED.market_cap
ELSE ?
END
""",
market_history.market_cap,
market_history.market_cap,
market_history.market_cap
),
tvl:
fragment(
"""
CASE WHEN (? IS NULL OR ? = 0) AND EXCLUDED.tvl IS NOT NULL AND EXCLUDED.tvl > 0
THEN EXCLUDED.tvl
ELSE ?
END
""",
market_history.tvl,
market_history.tvl,
market_history.tvl
)
]
],
where:
is_nil(market_history.tvl) or market_history.tvl == 0 or is_nil(market_history.market_cap) or
market_history.market_cap == 0 or is_nil(market_history.opening_price) or
market_history.opening_price == 0 or is_nil(market_history.closing_price) or
market_history.closing_price == 0
)
end

@spec get_exchange_rate(String.t()) :: Token.t() | nil
Expand Down

0 comments on commit 1e832d9

Please sign in to comment.