From 0acd9ac9634e3040726c35569a47fe6a44e4a782 Mon Sep 17 00:00:00 2001 From: Antonio Ventilii <169057656+AntonioVentilii-DFINITY@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:23:12 +0100 Subject: [PATCH] refactor(frontend): Blockstream API URL is hard-coded (#3266) # Motivation The Blockstream API URL can be hard-coded since it is constant throughout the envs. --- .env.example | 1 - .env.test | 1 - .github/workflows/deploy-to-environment.yml | 2 -- src/frontend/src/env/rest/blockstream.env.ts | 7 +++++++ src/frontend/src/lib/rest/blockstream.rest.ts | 4 ++-- 5 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 src/frontend/src/env/rest/blockstream.env.ts diff --git a/.env.example b/.env.example index 7eaeacc6b2..7a3b65d627 100644 --- a/.env.example +++ b/.env.example @@ -6,7 +6,6 @@ VITE_ALCHEMY_API_KEY= VITE_WALLET_CONNECT_PROJECT_ID= VITE_OISY_URL=https://oisy.com VITE_COINGECKO_API_KEY= -VITE_BLOCKSTREAM_API_URL=https://blockstream.info/api VITE_EXCHANGE_DISABLED=true VITE_JUNO_SATELLITE_ID= VITE_JUNO_ORBITER_ID= diff --git a/.env.test b/.env.test index 4c63006977..8f7414a2de 100644 --- a/.env.test +++ b/.env.test @@ -5,6 +5,5 @@ VITE_INFURA_API_KEY= VITE_ALCHEMY_API_KEY= VITE_WALLET_CONNECT_PROJECT_ID= VITE_OISY_URL=https://oisy.com -VITE_BLOCKSTREAM_API_URL=https://blockstream.info/api VITE_JUNO_SATELLITE_ID= VITE_JUNO_ORBITER_ID= diff --git a/.github/workflows/deploy-to-environment.yml b/.github/workflows/deploy-to-environment.yml index b400ec2bd7..0c705fd3f6 100644 --- a/.github/workflows/deploy-to-environment.yml +++ b/.github/workflows/deploy-to-environment.yml @@ -79,7 +79,6 @@ jobs: echo "VITE_AUTH_ALTERNATIVE_ORIGINS=${{ secrets.VITE_AUTH_ALTERNATIVE_ORIGINS_STAGING }}" >> $GITHUB_ENV echo "VITE_AUTH_DERIVATION_ORIGIN=${{ secrets.VITE_AUTH_DERIVATION_ORIGIN_STAGING }}" >> $GITHUB_ENV echo "VITE_BTC_TO_CKBTC_EXCHANGE_ENABLED=${{ secrets.VITE_BTC_TO_CKBTC_EXCHANGE_ENABLED_STAGING }}" >> $GITHUB_ENV - echo "VITE_BLOCKSTREAM_API_URL=https://blockstream.info/api" >> $GITHUB_ENV echo "VITE_ONRAMPER_API_KEY_DEV=${{ secrets.VITE_ONRAMPER_API_KEY_DEV_STAGING }}" >> $GITHUB_ENV echo "VITE_ONRAMPER_API_KEY_PROD=${{ secrets.VITE_ONRAMPER_API_KEY_PROD_STAGING }}" >> $GITHUB_ENV { @@ -102,7 +101,6 @@ jobs: echo "VITE_AUTH_ALTERNATIVE_ORIGINS=${{ secrets.VITE_AUTH_ALTERNATIVE_ORIGINS_BETA }}" >> $GITHUB_ENV echo "VITE_AUTH_DERIVATION_ORIGIN=${{ secrets.VITE_AUTH_DERIVATION_ORIGIN_BETA }}" >> $GITHUB_ENV echo "VITE_BTC_TO_CKBTC_EXCHANGE_ENABLED=${{ secrets.VITE_BTC_TO_CKBTC_EXCHANGE_ENABLED_BETA }}" >> $GITHUB_ENV - echo "VITE_BLOCKSTREAM_API_URL=https://blockstream.info/api" >> $GITHUB_ENV echo "VITE_ONRAMPER_API_KEY_DEV=${{ secrets.VITE_ONRAMPER_API_KEY_DEV_BETA }}" >> $GITHUB_ENV echo "VITE_ONRAMPER_API_KEY_PROD=${{ secrets.VITE_ONRAMPER_API_KEY_PROD_BETA }}" >> $GITHUB_ENV { diff --git a/src/frontend/src/env/rest/blockstream.env.ts b/src/frontend/src/env/rest/blockstream.env.ts new file mode 100644 index 0000000000..cb68f806a4 --- /dev/null +++ b/src/frontend/src/env/rest/blockstream.env.ts @@ -0,0 +1,7 @@ +import { UrlSchema } from '$lib/validation/url.validation'; +import { safeParse } from '$lib/validation/utils.validation'; + +export const BLOCKSTREAM_API_URL = safeParse({ + schema: UrlSchema, + value: 'https://blockstream.info/api' +}); diff --git a/src/frontend/src/lib/rest/blockstream.rest.ts b/src/frontend/src/lib/rest/blockstream.rest.ts index ecc4b53f1b..9bc6927a83 100644 --- a/src/frontend/src/lib/rest/blockstream.rest.ts +++ b/src/frontend/src/lib/rest/blockstream.rest.ts @@ -1,4 +1,4 @@ -const API_URL = import.meta.env.VITE_BLOCKSTREAM_API_URL; +import { BLOCKSTREAM_API_URL } from '$env/rest/blockstream.env'; /** * Get latest mined BTC block height. Used for calculating the number of confirmations for a BTC transaction. @@ -13,7 +13,7 @@ export const btcLatestBlockHeight = (): Promise => }); const fetchBlockstreamApi = async ({ endpointPath }: { endpointPath: string }): Promise => { - const response = await fetch(`${API_URL}/${endpointPath}`); + const response = await fetch(`${BLOCKSTREAM_API_URL}/${endpointPath}`); if (!response.ok) { throw new Error('Blockstream API response not ok.');