Merge pull request #1803 from poanetwork/ab-fetch-total-supply-from-coinmarketcap

use coinmarketcap for total_supply by default
pull/1806/head
Victor Baranov 6 years ago committed by GitHub
commit 46f36319bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 1
      apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs
  3. 1
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs
  4. 2
      apps/block_scout_web/test/block_scout_web/views/address_view_test.exs
  5. 2
      apps/explorer/lib/explorer/chain.ex
  6. 22
      apps/explorer/lib/explorer/chain/supply/coin_market_cap.ex
  7. 1
      apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex
  8. 1
      apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex
  9. 1
      apps/explorer/lib/explorer/exchange_rates/source/transaction_and_log.ex
  10. 15
      apps/explorer/lib/explorer/exchange_rates/token.ex
  11. 2
      apps/explorer/test/explorer/chain_test.exs
  12. 1
      apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs
  13. 1
      apps/explorer/test/explorer/exchange_rates/source/coin_gecko_test.exs
  14. 1
      apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs
  15. 1
      apps/explorer/test/support/fakes/one_coin_source.ex

@ -22,6 +22,7 @@
- [#1790](https://github.com/poanetwork/blockscout/pull/1790) - fix constructor arguments verification
- [#1793](https://github.com/poanetwork/blockscout/pull/1793) - fix top nav autocomplete
- [#1795](https://github.com/poanetwork/blockscout/pull/1795) - fix line numbers for decompiled contracts
- [#1803](https://github.com/poanetwork/blockscout/pull/1803) - use coinmarketcap for total_supply by default
- [#1802](https://github.com/poanetwork/blockscout/pull/1802) - make coinmarketcap's number of pages configurable
- [#1799](https://github.com/poanetwork/blockscout/pull/1799) - Use eth_getUncleByBlockHashAndIndex for uncle block fetching

@ -21,6 +21,7 @@ defmodule BlockScoutWeb.ExchangeRateChannelTest do
token = %Token{
available_supply: Decimal.new("1000000.0"),
total_supply: Decimal.new("1000000.0"),
btc_value: Decimal.new("1.000"),
id: "test",
last_updated: DateTime.utc_now(),

@ -122,6 +122,7 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do
eth = %Token{
available_supply: Decimal.new("1000000.0"),
total_supply: Decimal.new("1000000.0"),
btc_value: Decimal.new("1.000"),
id: "test",
last_updated: DateTime.utc_now(),

@ -134,7 +134,9 @@ defmodule BlockScoutWeb.AddressViewTest do
end
test "balance_percentage/1" do
Application.put_env(:explorer, :supply, Explorer.Chain.Supply.ProofOfAuthority)
address = insert(:address, fetched_coin_balance: 2_524_608_000_000_000_000_000_000)
assert "1.0000% Market Cap" = AddressView.balance_percentage(address)
end

@ -2373,7 +2373,7 @@ defmodule Explorer.Chain do
end
defp supply_module do
Application.get_env(:explorer, :supply, Explorer.Chain.Supply.ProofOfAuthority)
Application.get_env(:explorer, :supply, Explorer.Chain.Supply.CoinMarketCap)
end
@doc """

@ -0,0 +1,22 @@
defmodule Explorer.Chain.Supply.CoinMarketCap do
@moduledoc """
Defines the supply API for calculating supply for coins from coinmarketcap.
"""
use Explorer.Chain.Supply
alias Explorer.ExchangeRates.Token
alias Explorer.Market
def circulating do
exchange_rate().available_supply
end
def total do
exchange_rate().total_supply
end
def exchange_rate do
Market.get_exchange_rate(Explorer.coin()) || Token.null()
end
end

@ -26,6 +26,7 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do
%Token{
available_supply: to_decimal(item["total_supply"]),
total_supply: to_decimal(item["total_supply"]),
btc_value: btc_value,
id: id,
last_updated: last_updated,

@ -17,6 +17,7 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do
%Token{
available_supply: to_decimal(item["available_supply"]),
total_supply: to_decimal(item["total_supply"]),
btc_value: to_decimal(item["price_btc"]),
id: item["id"],
last_updated: last_updated,

@ -26,6 +26,7 @@ defmodule Explorer.ExchangeRates.Source.TransactionAndLog do
defp build_struct(original_token) do
%Token{
available_supply: to_decimal(Chain.circulating_supply()),
total_supply: 0,
btc_value: original_token.btc_value,
id: original_token.id,
last_updated: original_token.last_updated,

@ -7,6 +7,7 @@ defmodule Explorer.ExchangeRates.Token do
Represents an exchange rate for a given token.
* `:available_supply` - Available supply of a token
* `:total_supply` - Max Supply
* `:btc_value` - The Bitcoin value of the currency
* `:id` - ID of a currency
* `:last_updated` - Timestamp of when the value was last updated
@ -18,6 +19,7 @@ defmodule Explorer.ExchangeRates.Token do
"""
@type t :: %__MODULE__{
available_supply: Decimal.t(),
total_supply: Decimal.t(),
btc_value: Decimal.t(),
id: String.t(),
last_updated: DateTime.t(),
@ -28,8 +30,8 @@ defmodule Explorer.ExchangeRates.Token do
volume_24h_usd: Decimal.t()
}
@enforce_keys ~w(available_supply btc_value id last_updated market_cap_usd name symbol usd_value volume_24h_usd)a
defstruct ~w(available_supply btc_value id last_updated market_cap_usd name symbol usd_value volume_24h_usd)a
@enforce_keys ~w(available_supply total_supply btc_value id last_updated market_cap_usd name symbol usd_value volume_24h_usd)a
defstruct ~w(available_supply total_supply btc_value id last_updated market_cap_usd name symbol usd_value volume_24h_usd)a
def null,
do: %__MODULE__{
@ -37,6 +39,7 @@ defmodule Explorer.ExchangeRates.Token do
id: nil,
name: nil,
available_supply: nil,
total_supply: nil,
usd_value: nil,
volume_24h_usd: nil,
market_cap_usd: nil,
@ -51,6 +54,7 @@ defmodule Explorer.ExchangeRates.Token do
id: id,
name: name,
available_supply: available_supply,
total_supply: total_supply,
usd_value: usd_value,
volume_24h_usd: volume_24h_usd,
market_cap_usd: market_cap_usd,
@ -58,17 +62,20 @@ defmodule Explorer.ExchangeRates.Token do
last_updated: last_updated
}) do
# symbol is first because it is the key used for lookup in `Explorer.ExchangeRates`'s ETS table
{symbol, id, name, available_supply, usd_value, volume_24h_usd, market_cap_usd, btc_value, last_updated}
{symbol, id, name, available_supply, total_supply, usd_value, volume_24h_usd, market_cap_usd, btc_value,
last_updated}
end
def from_tuple(
{symbol, id, name, available_supply, usd_value, volume_24h_usd, market_cap_usd, btc_value, last_updated}
{symbol, id, name, available_supply, total_supply, usd_value, volume_24h_usd, market_cap_usd, btc_value,
last_updated}
) do
%__MODULE__{
symbol: symbol,
id: id,
name: name,
available_supply: available_supply,
total_supply: total_supply,
usd_value: usd_value,
volume_24h_usd: volume_24h_usd,
market_cap_usd: market_cap_usd,

@ -3276,6 +3276,7 @@ defmodule Explorer.ChainTest do
end
test "total_supply/0" do
Application.put_env(:explorer, :supply, Explorer.Chain.Supply.ProofOfAuthority)
height = 2_000_000
insert(:block, number: height)
expected = ProofOfAuthority.initial_supply() + height
@ -3284,6 +3285,7 @@ defmodule Explorer.ChainTest do
end
test "circulating_supply/0" do
Application.put_env(:explorer, :supply, Explorer.Chain.Supply.ProofOfAuthority)
assert Chain.circulating_supply() == ProofOfAuthority.circulating()
end

@ -68,6 +68,7 @@ defmodule Explorer.ExchangeRatesTest do
test "with successful fetch" do
expected_token = %Token{
available_supply: Decimal.new("1000000.0"),
total_supply: Decimal.new("1000000.0"),
btc_value: Decimal.new("1.000"),
id: "test_id",
last_updated: DateTime.utc_now(),

@ -64,6 +64,7 @@ defmodule Explorer.ExchangeRates.Source.CoinGeckoTest do
expected = [
%Token{
available_supply: Decimal.new("252193195"),
total_supply: Decimal.new("252193195"),
btc_value: Decimal.new("0.00001753101509231471092879666458"),
id: "poa-network",
last_updated: expected_date,

@ -33,6 +33,7 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do
expected = [
%Token{
available_supply: Decimal.new("203981804.0"),
total_supply: Decimal.new("254473964.0"),
btc_value: Decimal.new("0.00007032"),
id: "poa-network",
last_updated: expected_date,

@ -10,6 +10,7 @@ defmodule Explorer.ExchangeRates.Source.OneCoinSource do
def format_data(_) do
pseudo_token = %Token{
available_supply: Decimal.new(10_000_000),
total_supply: Decimal.new(10_000_000_000),
btc_value: Decimal.new(1),
id: "",
last_updated: Timex.now(),

Loading…
Cancel
Save