diff --git a/web/config/dev.exs b/web/config/dev.exs index 57bb9e14e..ff994c164 100644 --- a/web/config/dev.exs +++ b/web/config/dev.exs @@ -110,3 +110,5 @@ config :zk_arcade, :nft_contract_address, "0xdbC43Ba45381e02825b14322cDdd15eC4B3 config :zk_arcade, :ip_info_api_key, "" config :zk_arcade, :ipgeolocation_api_key, "" + +config :zk_arcade, :eth_usd_price_fallback, System.get_env("ETH_PRICE_USD") diff --git a/web/config/runtime.exs b/web/config/runtime.exs index 108621e16..49e5c827f 100644 --- a/web/config/runtime.exs +++ b/web/config/runtime.exs @@ -110,6 +110,8 @@ if config_env() == :prod do config :zk_arcade, :ip_info_api_key, System.get_env("IP_INFO_API_KEY") config :zk_arcade, :ipgeolocation_api_key, System.get_env("IPGEOLOCATION_API_KEY") + config :zk_arcade, :eth_usd_price_fallback, String.to_float(System.get_env("ETH_PRICE_USD")) + newrelic_license_key = System.get_env("NEWRELIC_KEY") newrelic_app_name = System.get_env("NEWRELIC_APP_NAME") diff --git a/web/lib/zk_arcade/eth_price.ex b/web/lib/zk_arcade/eth_price.ex index 62f954836..93502853b 100644 --- a/web/lib/zk_arcade/eth_price.ex +++ b/web/lib/zk_arcade/eth_price.ex @@ -45,8 +45,23 @@ defmodule ZkArcade.EthPrice do case fetch_from_cryptoprices() do {:ok, price} -> {:ok, price} - {:error, reason} -> - {:error, reason} + {:error, _} -> + # As a last option, try to get from environment variable + + Logger.error("Failed to get the initial ETH price from coingecko and cryptoprices, using environment set value as fallback") + + case Application.get_env(:zk_arcade, :eth_usd_price_fallback) do + nil -> + {:error, "ETH_PRICE_USD environment variable is not set"} + price_str -> + case Float.parse(price_str) do + {price, ""} when is_float(price) and price > 0 -> + Logger.info("Successfully fetched ETH price from environment variable: #{price}") + {:ok, price} + _ -> + {:error, "Invalid ETH_PRICE_USD environment variable format"} + end + end end end end