Skip to content

Commit

Permalink
feat: clean up explorer url building
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Nardi <[email protected]>
  • Loading branch information
nicole-obrien and MarkNerdi committed May 15, 2024
1 parent c22c3ec commit d93f8d8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 38 deletions.
40 changes: 28 additions & 12 deletions packages/shared/src/lib/core/network/apis/explorer.api.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { DEFAULT_APPLICATION_JSON_REQUEST_OPTIONS } from '@core/utils'
import { DEFAULT_APPLICATION_JSON_REQUEST_OPTIONS, buildUrl } from '@core/utils'
import {} from '../enums'
import { DEFAULT_EXPLORER_API_BASE_URL } from '../constants/default-explorer-api-base-url.constant'
import { ExplorerApiEndpoint } from '../enums'
import { IExplorerApiNetworks } from '../interfaces'
import { getExplorerApiNetworkName } from '../utils'
import { IExplorerApiNetwork, IExplorerApiNetworks } from '../interfaces'
import { NetworkId } from '../types'
import { SupportedStardustNetworkId } from '../constants'

const ExplorerNetworkId = {
[SupportedStardustNetworkId.Iota]: 'mainnet',
[SupportedStardustNetworkId.Shimmer]: 'shimmer',
[SupportedStardustNetworkId.IotaTestnet]: 'iota-testnet',
[SupportedStardustNetworkId.Testnet]: 'shimmer-testnet',
}

export class ExplorerApi {
static async makeRequest<T>(endpoint: ExplorerApiEndpoint, queryParams?: string): Promise<T> {
static async makeRequest<T>(endpoint: ExplorerApiEndpoint): Promise<T> {
try {
const response = await fetch(
`${DEFAULT_EXPLORER_API_BASE_URL}${endpoint}?${queryParams ?? ''}`,
DEFAULT_APPLICATION_JSON_REQUEST_OPTIONS
)
const requestUrl = buildUrl({
base: DEFAULT_EXPLORER_API_BASE_URL,
pathname: endpoint,
})

if (!requestUrl) {
throw new Error('Invalid request URL')
}

const response = await fetch(requestUrl.href, DEFAULT_APPLICATION_JSON_REQUEST_OPTIONS)
const data = await response.json()
return <T>data
} catch (err) {
Expand All @@ -21,10 +34,13 @@ export class ExplorerApi {
}
}

static async getNetworkInfo(networkId: NetworkId): Promise<IExplorerApiNetwork | undefined> {
const networksInfo = await ExplorerApi.makeRequest<IExplorerApiNetworks>(ExplorerApiEndpoint.Networks)
const networkInfo = networksInfo?.networks.find((network) => network.network === ExplorerNetworkId[networkId])
return networkInfo
}

static async getCirculatingSupply(networkId: NetworkId): Promise<number> {
const networkName = getExplorerApiNetworkName(networkId)
const networksInfo = await this.makeRequest<IExplorerApiNetworks>(ExplorerApiEndpoint.Networks, networkId)
const networkInfo = networksInfo?.networks.find((network) => network.network === networkName)
return networkInfo?.circulatingSupply ?? 0
return (await ExplorerApi.getNetworkInfo(networkId))?.circulatingSupply ?? 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { NetworkId } from '../types'
import { SupportedNetworkId } from './supported-network-id.constant'

export const DEFAULT_EXPLORER_URLS: Readonly<{ [key in NetworkId]?: string }> = {
[SupportedNetworkId.Iota]: 'https://explorer.iota.org',
[SupportedNetworkId.Shimmer]: 'https://explorer.shimmer.network',
[SupportedNetworkId.IotaTestnet]: 'https://explorer.iota.org',
[SupportedNetworkId.Testnet]: 'https://explorer.shimmer.network',
[SupportedNetworkId.Iota]: 'https://explorer.iota.org/mainnet/',
[SupportedNetworkId.Shimmer]: 'https://explorer.shimmer.network/shimmer/',
[SupportedNetworkId.IotaTestnet]: 'https://explorer.iota.org/iota-testnet/',
[SupportedNetworkId.Testnet]: 'https://explorer.shimmer.network/testnet/',
[SupportedNetworkId.IotaEvm]: 'https://explorer.evm.iota.org',
[SupportedNetworkId.ShimmerEvm]: 'https://explorer.evm.shimmer.network',
[SupportedNetworkId.IotaTestnetEvm]: 'https://explorer.evm.testnet.iota.org',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ExplorerEndpoint } from '../enums'
import { NetworkId } from '../types'
import { getExplorerApiNetworkName } from './getExplorerApiNetworkName'
import { isStardustNetwork } from './isStardustNetwork'
import { isEvmNetwork } from './isEvmNetwork'

Expand All @@ -20,7 +19,7 @@ const EVM_EXPLORER_ENDPOINTS: Readonly<{ [key in ExplorerEndpoint]?: string }> =

export function getExplorerEndpoint(networkId: NetworkId, explorerEndpoint: ExplorerEndpoint): string | undefined {
if (isStardustNetwork(networkId)) {
return `${getExplorerApiNetworkName(networkId)}/${STARDUST_EXPLORER_ENDPOINTS[explorerEndpoint]}`
return STARDUST_EXPLORER_ENDPOINTS[explorerEndpoint]
} else if (isEvmNetwork(networkId)) {
return EVM_EXPLORER_ENDPOINTS[explorerEndpoint]
}
Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/lib/core/network/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export * from './getDefaultStardustNetwork'
export * from './getEvmTransactionOptions'
export * from './getExplorerEndpoint'
export * from './getExplorerUrl'
export * from './getExplorerApiNetworkName'
export * from './getNetworkIdFromOnboardingNetworkType'
export * from './getOnboardingNetworkTypeFromNetworkId'
export * from './getSplitNetworkId'
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/lib/core/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function stripTrailingSlash(str: string): string {
return str ? str.replace(/\/+$/, '') : ''
}

export function addTrailingSlash(str: string): string {
return stripTrailingSlash(str) + '/'
}

/**
* Strip spaces from the text
* @param str The text to strip the values from
Expand Down
5 changes: 3 additions & 2 deletions packages/shared/src/lib/core/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stripSpaces, stripTrailingSlash } from './string'
import { addTrailingSlash, stripSpaces, stripTrailingSlash } from './string'
import { QueryParameters } from './types'

export function cleanUrl(
Expand Down Expand Up @@ -32,7 +32,8 @@ type UrlParams = {

export function buildUrl(urlParams: UrlParams): URL | undefined {
try {
const url = new URL(urlParams.pathname ?? '', urlParams.base)
// URL parser requires a slash at the end of the base URL to parse the URL correctly
const url = new URL(urlParams.pathname ?? '', addTrailingSlash(urlParams.base))
for (const key of Object.keys(urlParams.query ?? {})) {
const value = urlParams.query?.[key]
if (!value) continue
Expand Down

0 comments on commit d93f8d8

Please sign in to comment.