use coinmarketcap for total_supply by default

pull/1803/head
Ayrat Badykov 6 years ago
parent 0ed2ad0150
commit 3c90107c92
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 2
      apps/explorer/lib/explorer/chain.ex
  2. 22
      apps/explorer/lib/explorer/chain/supply/coin_market_cap.ex
  3. 1
      apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex
  4. 1
      apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex
  5. 1
      apps/explorer/lib/explorer/exchange_rates/source/transaction_and_log.ex
  6. 15
      apps/explorer/lib/explorer/exchange_rates/token.ex
  7. 2
      apps/explorer/test/explorer/chain_test.exs
  8. 1
      apps/explorer/test/support/fakes/one_coin_source.ex

@ -2374,7 +2374,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,

@ -3273,6 +3273,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
@ -3281,6 +3282,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

@ -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