Zerion API proxy (#9896)

* Zerion API proxy

* Add envs to common-blockscout.env

* Change proxy endpoint path
pull/9905/head
Victor Baranov 7 months ago committed by GitHub
parent 58c1915959
commit ac2ff4638e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      apps/block_scout_web/lib/block_scout_web/api_router.ex
  2. 8
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/proxy/noves_fi_controller.ex
  3. 24
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/proxy/zerion_controller.ex
  4. 10
      apps/explorer/lib/explorer/third_party_integrations/noves_fi.ex
  5. 45
      apps/explorer/lib/explorer/third_party_integrations/zerion.ex
  6. 4
      config/runtime.exs
  7. 1
      cspell.json
  8. 6
      docker-compose/envs/common-blockscout.env

@ -316,6 +316,10 @@ defmodule BlockScoutWeb.ApiRouter do
get("/bundles", V2.Proxy.AccountAbstractionController, :bundles) get("/bundles", V2.Proxy.AccountAbstractionController, :bundles)
get("/operations", V2.Proxy.AccountAbstractionController, :operations) get("/operations", V2.Proxy.AccountAbstractionController, :operations)
end end
scope "/zerion" do
get("/wallets/:address_hash_param/portfolio", V2.Proxy.ZerionController, :wallet_portfolio)
end
end end
scope "/blobs" do scope "/blobs" do

@ -17,7 +17,7 @@ defmodule BlockScoutWeb.API.V2.Proxy.NovesFiController do
api?: true api?: true
), ),
url = NovesFi.tx_url(transaction_hash_string), url = NovesFi.tx_url(transaction_hash_string),
{response, status} <- NovesFi.noves_fi_api_request(url, conn), {response, status} <- NovesFi.api_request(url, conn),
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do {:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do
conn conn
|> put_status(status) |> put_status(status)
@ -26,13 +26,13 @@ defmodule BlockScoutWeb.API.V2.Proxy.NovesFiController do
end end
@doc """ @doc """
Function to handle GET requests to `/api/v2/proxy/noves-fi/transactions/:transaction_hash_param/transaction` endpoint. Function to handle GET requests to `/api/v2/proxy/noves-fi/addresses/:address_hash_param/transactions` endpoint.
""" """
@spec address_transactions(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()} @spec address_transactions(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()}
def address_transactions(conn, %{"address_hash_param" => address_hash_string} = params) do def address_transactions(conn, %{"address_hash_param" => address_hash_string} = params) do
with {:ok, _address_hash, _address} <- AddressController.validate_address(address_hash_string, params), with {:ok, _address_hash, _address} <- AddressController.validate_address(address_hash_string, params),
url = NovesFi.address_txs_url(address_hash_string), url = NovesFi.address_txs_url(address_hash_string),
{response, status} <- NovesFi.noves_fi_api_request(url, conn), {response, status} <- NovesFi.api_request(url, conn),
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do {:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do
conn conn
|> put_status(status) |> put_status(status)
@ -47,7 +47,7 @@ defmodule BlockScoutWeb.API.V2.Proxy.NovesFiController do
def describe_transactions(conn, _) do def describe_transactions(conn, _) do
url = NovesFi.describe_txs_url() url = NovesFi.describe_txs_url()
with {response, status} <- NovesFi.noves_fi_api_request(url, conn, :post_transactions), with {response, status} <- NovesFi.api_request(url, conn, :post_transactions),
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do {:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do
conn conn
|> put_status(status) |> put_status(status)

@ -0,0 +1,24 @@
defmodule BlockScoutWeb.API.V2.Proxy.ZerionController do
use BlockScoutWeb, :controller
alias BlockScoutWeb.API.V2.AddressController
alias Explorer.ThirdPartyIntegrations.Zerion
action_fallback(BlockScoutWeb.API.V2.FallbackController)
@doc """
Function to handle GET requests to `/api/v2/proxy/zerion/wallet-portfolio/:address_hash_param` endpoint.
"""
@spec wallet_portfolio(Plug.Conn.t(), map()) :: Plug.Conn.t() | {atom(), any()}
def wallet_portfolio(conn, %{"address_hash_param" => address_hash_string} = params) do
with {:ok, _address_hash, _address} <- AddressController.validate_address(address_hash_string, params),
url = Zerion.wallet_portfolio_url(address_hash_string),
{response, status} <- Zerion.api_request(url, conn),
{:is_empty_response, false} <- {:is_empty_response, is_nil(response)} do
conn
|> put_status(status)
|> json(response)
end
end
end

@ -9,12 +9,12 @@ defmodule Explorer.ThirdPartyIntegrations.NovesFi do
@recv_timeout 60_000 @recv_timeout 60_000
@doc """ @doc """
Proxy request to noves.fi API endpoints Proxy request to Noves.fi API endpoints
""" """
@spec noves_fi_api_request(String.t(), Plug.Conn.t(), :get | :post_transactions) :: {any(), integer()} @spec api_request(String.t(), Plug.Conn.t(), :get | :post_transactions) :: {any(), integer()}
def noves_fi_api_request(url, conn, method \\ :get) def api_request(url, conn, method \\ :get)
def noves_fi_api_request(url, conn, :post_transactions) do def api_request(url, conn, :post_transactions) do
headers = [{"apiKey", api_key()}, {"Content-Type", "application/json"}, {"accept", "text/plain"}] headers = [{"apiKey", api_key()}, {"Content-Type", "application/json"}, {"accept", "text/plain"}]
hashes = hashes =
@ -38,7 +38,7 @@ defmodule Explorer.ThirdPartyIntegrations.NovesFi do
end end
end end
def noves_fi_api_request(url, conn, :get) do def api_request(url, conn, :get) do
headers = [{"apiKey", api_key()}] headers = [{"apiKey", api_key()}]
url_with_params = url <> "?" <> conn.query_string url_with_params = url <> "?" <> conn.query_string

@ -0,0 +1,45 @@
defmodule Explorer.ThirdPartyIntegrations.Zerion do
@moduledoc """
Module for Zerion API integration https://developers.zerion.io/reference
"""
alias Explorer.Helper
alias Explorer.Utility.Microservice
@recv_timeout 60_000
@doc """
Proxy request to Zerion API endpoints
"""
@spec api_request(String.t(), Plug.Conn.t(), :get | :post_transactions) :: {any(), integer()}
def api_request(url, conn, method \\ :get)
def api_request(url, _conn, :get) do
auth_token = Base.encode64("#{api_key()}:")
headers = [{"Authorization", "Basic #{auth_token}"}]
case HTTPoison.get(url, headers, recv_timeout: @recv_timeout) do
{:ok, %HTTPoison.Response{status_code: status, body: body}} ->
{Helper.decode_json(body), status}
_ ->
{nil, 500}
end
end
@doc """
Zerion /wallets/{accountAddress}/portfolio endpoint
"""
@spec wallet_portfolio_url(String.t()) :: String.t()
def wallet_portfolio_url(address_hash_string) do
"#{base_url()}/wallets/#{address_hash_string}/portfolio"
end
defp base_url do
Microservice.base_url(__MODULE__)
end
defp api_key do
Application.get_env(:explorer, __MODULE__)[:api_key]
end
end

@ -446,6 +446,10 @@ config :explorer, Explorer.ThirdPartyIntegrations.NovesFi,
chain_name: System.get_env("NOVES_FI_CHAIN_NAME"), chain_name: System.get_env("NOVES_FI_CHAIN_NAME"),
api_key: System.get_env("NOVES_FI_API_TOKEN") api_key: System.get_env("NOVES_FI_API_TOKEN")
config :explorer, Explorer.ThirdPartyIntegrations.Zerion,
service_url: System.get_env("ZERION_BASE_API_URL", "https://api.zerion.io/v1"),
api_key: System.get_env("ZERION_API_TOKEN")
enabled? = ConfigHelper.parse_bool_env_var("MICROSERVICE_SC_VERIFIER_ENABLED", "true") enabled? = ConfigHelper.parse_bool_env_var("MICROSERVICE_SC_VERIFIER_ENABLED", "true")
# or "eth_bytecode_db" # or "eth_bytecode_db"
type = System.get_env("MICROSERVICE_SC_VERIFIER_TYPE", "sc_verifier") type = System.get_env("MICROSERVICE_SC_VERIFIER_TYPE", "sc_verifier")

@ -596,6 +596,7 @@
"yellowgreen", "yellowgreen",
"zaphod", "zaphod",
"zeppelinos", "zeppelinos",
"zerion",
"zetachain", "zetachain",
"zftv", "zftv",
"ziczr", "ziczr",

@ -317,8 +317,8 @@ MICROSERVICE_SIG_PROVIDER_ENABLED=true
MICROSERVICE_SIG_PROVIDER_URL=http://sig-provider:8050/ MICROSERVICE_SIG_PROVIDER_URL=http://sig-provider:8050/
# MICROSERVICE_BENS_URL= # MICROSERVICE_BENS_URL=
# MICROSERVICE_BENS_ENABLED= # MICROSERVICE_BENS_ENABLED=
#MICROSERVICE_ACCOUNT_ABSTRACTION_ENABLED=true # MICROSERVICE_ACCOUNT_ABSTRACTION_ENABLED=true
#MICROSERVICE_ACCOUNT_ABSTRACTION_URL= # MICROSERVICE_ACCOUNT_ABSTRACTION_URL=
# MICROSERVICE_METADATA_URL= # MICROSERVICE_METADATA_URL=
# MICROSERVICE_METADATA_ENABLED= # MICROSERVICE_METADATA_ENABLED=
DECODE_NOT_A_CONTRACT_CALLS=true DECODE_NOT_A_CONTRACT_CALLS=true
@ -362,6 +362,8 @@ TENDERLY_CHAIN_PATH=
# NOVES_FI_BASE_API_URL= # NOVES_FI_BASE_API_URL=
# NOVES_FI_CHAIN_NAME= # NOVES_FI_CHAIN_NAME=
# NOVES_FI_API_TOKEN= # NOVES_FI_API_TOKEN=
# ZERION_BASE_API_URL=
# ZERION_API_TOKEN=
# BRIDGED_TOKENS_ENABLED= # BRIDGED_TOKENS_ENABLED=
# BRIDGED_TOKENS_ETH_OMNI_BRIDGE_MEDIATOR= # BRIDGED_TOKENS_ETH_OMNI_BRIDGE_MEDIATOR=
# BRIDGED_TOKENS_BSC_OMNI_BRIDGE_MEDIATOR= # BRIDGED_TOKENS_BSC_OMNI_BRIDGE_MEDIATOR=

Loading…
Cancel
Save