Skip to content

Commit

Permalink
use CoinGecko instead of CoinMarketcap for exchange rates
Browse files Browse the repository at this point in the history
CoinMarketcap deprecated and took offline its v1 API. They have
v2 API which has free plan and requires API keys. But as part of
#234 we alresy
implemented fetching from CoinGecko.

Now, coin id should be set for CoinGecko for the required coin.
```
config :explorer, Explorer.ExchangeRates.Source.CoinGecko,
  coin_id: System.get_env("COIN_GECKO_ID", "poa-network")
```
Or the `COIN_GECKO_ID` env var should be set. For example, COIN_GECKO_ID=poa-network
  • Loading branch information
ayrat555 committed Aug 21, 2019
1 parent e9d7b7c commit ae89658
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 40 deletions.
2 changes: 2 additions & 0 deletions apps/explorer/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: true
config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap,
pages: String.to_integer(System.get_env("COINMARKETCAP_PAGES") || "10")

config :explorer, Explorer.ExchangeRates.Source.CoinGecko, coin_id: System.get_env("COIN_GECKO_ID", "poa-network")

balances_update_interval =
if System.get_env("ADDRESS_WITH_BALANCES_UPDATE_INTERVAL") do
case Integer.parse(System.get_env("ADDRESS_WITH_BALANCES_UPDATE_INTERVAL")) do
Expand Down
2 changes: 1 addition & 1 deletion apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,7 @@ defmodule Explorer.Chain do
end

defp supply_module do
Application.get_env(:explorer, :supply, Explorer.Chain.Supply.CoinMarketCap)
Application.get_env(:explorer, :supply, Explorer.Chain.Supply.ExchangeRate)
end

@doc """
Expand Down
22 changes: 0 additions & 22 deletions apps/explorer/lib/explorer/chain/supply/coin_market_cap.ex

This file was deleted.

2 changes: 1 addition & 1 deletion apps/explorer/lib/explorer/exchange_rates/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule Explorer.ExchangeRates.Source do

@spec exchange_rates_source() :: module()
defp exchange_rates_source do
config(:source) || Explorer.ExchangeRates.Source.CoinMarketCap
config(:source) || Explorer.ExchangeRates.Source.CoinGecko
end

@spec config(atom()) :: term
Expand Down
38 changes: 22 additions & 16 deletions apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,45 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do
{:ok, price} = get_btc_price()
btc_price = to_decimal(price)

for item <- decode_json(data),
not is_nil(item["total_supply"]) and not is_nil(item["current_price"]) do
{:ok, last_updated, 0} = DateTime.from_iso8601(item["last_updated"])
json_data = decode_json(data)

current_price = to_decimal(item["current_price"])
market_data = json_data["market_data"]
{:ok, last_updated, 0} = DateTime.from_iso8601(market_data["last_updated"])

id = item["id"]
btc_value = if id != "btc", do: Decimal.div(current_price, btc_price), else: 1
current_price = to_decimal(market_data["current_price"]["usd"])

id = json_data["id"]
btc_value = if id != "btc", do: Decimal.div(current_price, btc_price), else: 1

[
%Token{
available_supply: to_decimal(item["total_supply"]),
total_supply: to_decimal(item["total_supply"]),
available_supply: to_decimal(market_data["circulating_supply"]),
total_supply: to_decimal(market_data["total_supply"]),
btc_value: btc_value,
id: id,
id: json_data["id"],
last_updated: last_updated,
market_cap_usd: to_decimal(item["market_cap"]),
name: item["name"],
symbol: item["symbol"],
market_cap_usd: to_decimal(market_data["market_cap"]["usd"]),
name: json_data["name"],
symbol: String.upcase(json_data["symbol"]),
usd_value: current_price,
volume_24h_usd: to_decimal(item["total_volume"])
volume_24h_usd: to_decimal(market_data["total_volume"]["usd"])
}
end
]
end

@impl Source
def source_url(currency \\ "usd") do
"#{base_url()}/coins/markets?vs_currency=#{currency}"
def source_url do
"#{base_url()}/coins/#{coin_id()}"
end

defp base_url do
config(:base_url) || "https://api.coingecko.com/api/v3"
end

defp coin_id do
Application.get_env(:explorer, __MODULE__)[:coin_id]
end

defp get_btc_price(currency \\ "usd") do
url = "#{base_url()}/exchange_rates"

Expand Down

0 comments on commit ae89658

Please sign in to comment.