import { useAsync , UseAsyncReturn } from "react-async-hook" ; import { Connection , PublicKey } from "@solana/web3.js" ; type SolletInfo = { blockchain : string ; erc20Contract : string ; name : string ; splMint : PublicKey ; ticker : string ; } ; export function useSolletInfo ( mint ) { return useAsync ( async ( ) => { return fetchSolletInfo ( mint ) ; } , [ mint ] ) ; } export async function fetchSolletInfo ( mint ) { let info = _SOLLET_INFO_CACHE . get ( mint . toString ( ) ) ; if ( info !== undefined ) { return info ; } const infoRaw = await swapApiRequest ( "GET" , `coins/sol/${ mint . toString ( ) }` ) ; info = { ... infoRaw , splMint : new PublicKey ( infoRaw . splMint ) } ; _SOLLET_INFO_CACHE . set ( mint . toString ( ) , info ! ) ; return info ! ; } export async function requestWormholeSwapMarketIfNeeded ( connection , solletMint , wormholeMint , swapMarket , solletInfo ) { const cached = _SWAP_MARKET_EXISTS_CACHE . get ( swapMarket . toString ( ) ) ; if ( cached !== undefined ) { return cached ; } const acc = await connection . getAccountInfo ( swapMarket ) ; if ( acc === null ) { _SWAP_MARKET_EXISTS_CACHE . set ( swapMarket . toString ( ) , false ) ; const resource = `wormhole/pool/${ solletInfo . ticker }/${ swapMarket . toString ( ) }/${ solletMint . toString ( ) }/${ wormholeMint . toString ( ) }` ; swapApiRequest ( "POST" , resource ) . catch ( console . error ) ; return false ; } else { _SWAP_MARKET_EXISTS_CACHE . set ( swapMarket . toString ( ) , true ) ; return true ; } } export async function swapApiRequest ( method , path , body ? ) { let headers = { } ; let params = { headers , method } ; if ( method === "GET" ) { params . cache = "no-cache" ; } else if ( body ) { headers [ "Content-Type" ] = "application/json" ; params . body = JSON . stringify ( body ) ; } let resp = await fetch ( `https://swap.sollet.io/api/${ path }` , params ) ; return await handleSwapApiResponse ( resp ) ; } async function handleSwapApiResponse ( resp ) { let json = await resp . json ( ) ; if ( ! json . success ) { throw new SwapApiError ( json . error , resp . status ) ; } return json . result ; } export class SwapApiError extends Error { readonly name : string ; readonly status : number ; constructor ( msg , status ) { super ( msg ) ; this . name = "SwapApiError" ; this . status = status ; } } const _SOLLET_INFO_CACHE = new Map < string , SolletInfo > ( ) ; const _SWAP_MARKET_EXISTS_CACHE = new Map < string , boolean > ( ) ;