import { isTestnet } from "./common"; import type { AssetInfo } from "../types"; export function transformTokenIconUrl(icon: string, documentUrl: string): string { if (icon && typeof icon === 'string') { if (icon.startsWith('http')) { return icon.replace('http://', 'https://'); } if (icon.startsWith('ipfs://')) { return icon; } const url = new URL(documentUrl); return `${url.origin}${icon}`; } return ""; } export function parseTokenDataUrl(data?: string): string | null { if (!data) { return null; } return translateIfIpfsUrl(data); } function translateIfIpfsUrl(url: string, gateway = "https://ipfs.nebula.markets/"): string { // if ipfs gateway is implemented properly, you will be able to trim // the ipfs:// from the url and append the url to the gateway path // see https://docs.ipfs.tech/concepts/ipfs-gateway/#path for more info if (url?.startsWith("ipfs://")) { return gateway + url.substring(7); } return url; } export function getAssetMetadata(token: string): Promise { return performGet(`${getAssetsEndpoint()}/metadata/${token}`, 'json'); } export function getAssetFileUrl(token:string, asset: string): string { return `${getAssetsEndpoint()}/assets/${token}/${asset}`; } function getAssetsEndpoint(): string { return `https://${isTestnet() ? 'testapi' : 'api'}.otoplo.com`; } async function performGet(url: string, responseType?: 'json' | 'raw'): Promise { try { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to perform GET. status: ${response.status}`); } if (responseType === 'raw') { const arrayBuffer = await response.arrayBuffer(); return new Uint8Array(arrayBuffer); } else { return await response.json(); } } catch (e) { throw new Error(`Unexpected Error: ${e}`); } }