From 0c8fd8a850e3537843d306d34cabf6f18efc5281 Mon Sep 17 00:00:00 2001 From: brdy <41711440+BrodyHughes@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:18:05 -0500 Subject: [PATCH] add configureSDK function with options (#90) * add configureSDK function with options * also swaps * Update sdk/src/slippage.ts remove unnecessary comment Co-authored-by: Bruno Barbieri <1247834+brunobar79@users.noreply.github.com> --------- Co-authored-by: Bruno Barbieri <1247834+brunobar79@users.noreply.github.com> --- sdk/src/quotes.ts | 23 ++++++++++++++++++----- sdk/src/slippage.ts | 4 ++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sdk/src/quotes.ts b/sdk/src/quotes.ts index b0ebcaf..b1ec6b5 100644 --- a/sdk/src/quotes.ts +++ b/sdk/src/quotes.ts @@ -30,6 +30,19 @@ import { signPermit } from './utils/permit'; import { getReferrerCode } from './utils/referrer'; import { sanityCheckAddress } from './utils/sanity_check'; + +/** + * Configure SDK for mocking or fallback to API_BASE_URL + * + */ +export let sdkConfig = { + apiBaseUrl: API_BASE_URL +}; + +export function configureSDK(options: { apiBaseUrl?: string }) { + sdkConfig = { ...sdkConfig, ...options }; +} + /** * Function to get the rainbow router contract address based on the chainId * @@ -97,7 +110,7 @@ const buildRainbowQuoteUrl = ({ ? { feePercentageBasisPoints: String(feePercentageBasisPoints) } : {}), }); - return `${API_BASE_URL}/v1/quote?` + searchParams.toString(); + return `${sdkConfig.apiBaseUrl}/v1/quote?` + searchParams.toString(); }; /** @@ -151,7 +164,7 @@ export const buildRainbowCrosschainQuoteUrl = ({ ? { feePercentageBasisPoints: String(feePercentageBasisPoints) } : {}), }); - return `${API_BASE_URL}/v1/quote?bridgeVersion=4&` + searchParams.toString(); + return `${sdkConfig.apiBaseUrl}/v1/quote?bridgeVersion=4&` + searchParams.toString(); }; /** @@ -192,7 +205,7 @@ export const buildRainbowClaimBridgeQuoteUrl = ({ source: Source.CrosschainAggregatorRelay.toString(), toChainId: String(toChainId), }); - return `${API_BASE_URL}/v1/quote?bridgeVersion=4&` + searchParams.toString(); + return `${sdkConfig.apiBaseUrl}/v1/quote?bridgeVersion=4&` + searchParams.toString(); }; /** @@ -207,7 +220,7 @@ export const getMinRefuelAmount = async (params: { toChainId: ChainId; }) => { const { chainId, toChainId } = params; - const url = `${API_BASE_URL}/v1/chains`; + const url = `${sdkConfig.apiBaseUrl}/v1/chains`; const response = await fetch(url); const chainsData = (await response.json()) as SocketChainsData; @@ -680,4 +693,4 @@ export const getCrosschainQuoteExecutionDetails = ( value, }, }; -}; +}; \ No newline at end of file diff --git a/sdk/src/slippage.ts b/sdk/src/slippage.ts index 55524db..a0ec997 100644 --- a/sdk/src/slippage.ts +++ b/sdk/src/slippage.ts @@ -6,7 +6,7 @@ import { SlippageError, SlippageParams, } from './types'; -import { API_BASE_URL } from './utils/constants'; +import { sdkConfig } from './quotes'; /** * Function to get a slippage formatted quote url to use with backend @@ -43,7 +43,7 @@ const buildRainbowSlippageUrl = ({ ? { sellAmount: String(sellAmount) } : { buyAmount: String(buyAmount) }), }); - return `${API_BASE_URL}/v1/slippage?` + searchParams.toString(); + return `${sdkConfig.apiBaseUrl}/v1/slippage?` + searchParams.toString(); }; /**