Add EXCHANGE_RATES_COINMARKETCAP_COIN_ID env var

pull/7962/head
Viktor Baranov 1 year ago
parent 070bd3b9d3
commit e04aea9834
  1. 1
      CHANGELOG.md
  2. 16
      apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex
  3. 32
      apps/explorer/test/explorer/exchange_rates/source/coin_gecko_test.exs
  4. 37
      apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs
  5. 3
      config/runtime.exs
  6. 1
      docker-compose/envs/common-blockscout.env
  7. 3
      docker/Makefile

@ -5,6 +5,7 @@
### Features
- [#7771](https://github.com/blockscout/blockscout/pull/7771) - CSV export: speed up
- [#7962](https://github.com/blockscout/blockscout/pull/7962) - Allow indicate CMC id of the coin through env var
- [#7946](https://github.com/blockscout/blockscout/pull/7946) - API v2 rate limit: Put token to cookies & change /api/v2/key method
- [#7888](https://github.com/blockscout/blockscout/pull/7888) - Add token balances info to watchlist address response
- [#7898](https://github.com/blockscout/blockscout/pull/7898) - Add possibility to add extra headers with JSON RPC URL

@ -55,8 +55,18 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do
def source_url do
coin = Explorer.coin()
symbol = if coin, do: String.upcase(Explorer.coin()), else: nil
coin_id = coin_id()
if symbol, do: "#{api_quotes_latest_url()}?symbol=#{symbol}&CMC_PRO_API_KEY=#{api_key()}", else: nil
cond do
coin_id ->
"#{api_quotes_latest_url()}?id=#{coin_id}&CMC_PRO_API_KEY=#{api_key()}"
symbol ->
"#{api_quotes_latest_url()}?symbol=#{symbol}&CMC_PRO_API_KEY=#{api_key()}"
true ->
nil
end
end
@impl Source
@ -84,6 +94,10 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do
config(:api_key)
end
defp coin_id do
config(:coin_id)
end
defp get_token_properties(market_data) do
token_values_list =
market_data

@ -58,6 +58,38 @@ defmodule Explorer.ExchangeRates.Source.CoinGeckoTest do
]
"""
describe "source_url/1" do
setup do
bypass = Bypass.open()
Application.put_env(:explorer, CoinGecko, base_url: "https://api.coingecko.com/api/v3")
{:ok, bypass: bypass}
end
test "composes cg :coins_list URL" do
assert "https://api.coingecko.com/api/v3/coins/list?include_platform=true" == CoinGecko.source_url(:coins_list)
end
test "composes cg url to list of contract address hashes" do
assert "https://api.coingecko.com/api/v3/simple/token_price/ethereum?vs_currencies=usd&include_market_cap=true&contract_addresses=0xdAC17F958D2ee523a2206206994597C13D831ec7" ==
CoinGecko.source_url(["0xdAC17F958D2ee523a2206206994597C13D831ec7"])
end
test "composes cg url by contract address hash" do
assert "https://api.coingecko.com/api/v3/coins/ethereum/contract/0xdAC17F958D2ee523a2206206994597C13D831ec7" ==
CoinGecko.source_url("0xdAC17F958D2ee523a2206206994597C13D831ec7")
end
test "composes cg url by contract address hash with custom coin_id" do
Application.put_env(:explorer, CoinGecko, platform: "poa-network")
assert "https://api.coingecko.com/api/v3/coins/poa-network/contract/0xdAC17F958D2ee523a2206206994597C13D831ec7" ==
CoinGecko.source_url("0xdAC17F958D2ee523a2206206994597C13D831ec7")
Application.put_env(:explorer, CoinGecko, platform: nil)
end
end
describe "format_data/1" do
setup do
bypass = Bypass.open()

@ -0,0 +1,37 @@
defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do
use ExUnit.Case
alias Explorer.ExchangeRates.Source.CoinMarketCap
describe "source_url/0" do
test "returns default cmc source url" do
assert "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=ETH&CMC_PRO_API_KEY=" ==
CoinMarketCap.source_url()
end
test "returns cmc source url with not default symbol" do
Application.put_env(:explorer, :coin, "ETC")
assert "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=ETC&CMC_PRO_API_KEY=" ==
CoinMarketCap.source_url()
Application.put_env(:explorer, :coin, "ETH")
end
test "returns cmc source url with id" do
Application.put_env(:explorer, CoinMarketCap, coin_id: 100_500)
assert "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?id=100500&CMC_PRO_API_KEY=" ==
CoinMarketCap.source_url()
Application.put_env(:explorer, CoinMarketCap, coin_id: nil)
end
end
describe "source_url/1" do
test "returns cmc source url for symbol" do
assert "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=ETH&CMC_PRO_API_KEY=" ==
CoinMarketCap.source_url("ETH")
end
end
end

@ -269,7 +269,8 @@ config :explorer, Explorer.ExchangeRates,
config :explorer, Explorer.ExchangeRates.Source, source: ConfigHelper.exchange_rates_source()
config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap,
api_key: System.get_env("EXCHANGE_RATES_COINMARKETCAP_API_KEY")
api_key: System.get_env("EXCHANGE_RATES_COINMARKETCAP_API_KEY"),
coin_id: System.get_env("EXCHANGE_RATES_COINMARKETCAP_COIN_ID")
config :explorer, Explorer.ExchangeRates.Source.CoinGecko,
platform: System.get_env("EXCHANGE_RATES_COINGECKO_PLATFORM_ID"),

@ -35,6 +35,7 @@ EXCHANGE_RATES_COIN=
# EXCHANGE_RATES_COINGECKO_COIN_ID=
# EXCHANGE_RATES_COINGECKO_API_KEY=
# EXCHANGE_RATES_COINMARKETCAP_API_KEY=
# EXCHANGE_RATES_COINMARKETCAP_COIN_ID=
POOL_SIZE=90
# EXCHANGE_RATES_COINGECKO_PLATFORM_ID=
# TOKEN_EXCHANGE_RATE_INTERVAL=

@ -240,6 +240,9 @@ endif
ifdef EXCHANGE_RATES_COINMARKETCAP_API_KEY
BLOCKSCOUT_CONTAINER_PARAMS += -e 'EXCHANGE_RATES_COINMARKETCAP_API_KEY=$(EXCHANGE_RATES_COINMARKETCAP_API_KEY)'
endif
ifdef EXCHANGE_RATES_COINMARKETCAP_COIN_ID
BLOCKSCOUT_CONTAINER_PARAMS += -e 'EXCHANGE_RATES_COINMARKETCAP_COIN_ID=$(EXCHANGE_RATES_COINMARKETCAP_COIN_ID)'
endif
ifdef DISABLE_EXCHANGE_RATES
BLOCKSCOUT_CONTAINER_PARAMS += -e 'DISABLE_EXCHANGE_RATES=$(DISABLE_EXCHANGE_RATES)'
endif

Loading…
Cancel
Save