From 10a014ff94dca7bf740f4d8830f758975c9f537a Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Tue, 27 Aug 2024 14:09:06 +0200 Subject: [PATCH 1/3] include base url and signal as optional params in api --- packages/sdk/src/api.ts | 84 ++++++++++++++++++++++++--------------- packages/sdk/src/types.ts | 5 +++ 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/packages/sdk/src/api.ts b/packages/sdk/src/api.ts index 485ea53..e62e6ea 100644 --- a/packages/sdk/src/api.ts +++ b/packages/sdk/src/api.ts @@ -4,6 +4,7 @@ import type { ChainID, ContractSolutionOptions, FailedSolution, + FetchOptions, FungibleToken, FungibleTokenBalance, Solution, @@ -21,9 +22,12 @@ export function setBaseUrl(url: string): void { BASE_URL = url; } -export async function getSupportedChains(): Promise { - const url = new URL("/networks", BASE_URL); - const response = await fetch(url).then( +export async function getSupportedChains({ + baseUrl, + signal, +}: FetchOptions = {}): Promise { + const url = new URL("/networks", baseUrl || BASE_URL); + const response = await fetch(url, { signal }).then( (response) => response.json() as unknown as { data: Chain[] }, ); @@ -32,18 +36,25 @@ export async function getSupportedChains(): Promise { export async function getChainTokens( chainID: ChainID, + { baseUrl, signal }: FetchOptions = {}, ): Promise { - const url = new URL(`/networks/${chainID}/assets/fungible`, BASE_URL); - const response = await fetch(url).then( + const url = new URL( + `/networks/${chainID}/assets/fungible`, + baseUrl || BASE_URL, + ); + const response = await fetch(url, { signal }).then( (response) => response.json() as unknown as { data: FungibleToken[] }, ); return response.data; } -export async function getFungibleTokens(): Promise { - const url = new URL("/assets/fungible", BASE_URL); - const response = await fetch(url).then( +export async function getFungibleTokens({ + baseUrl, + signal, +}: FetchOptions = {}): Promise { + const url = new URL("/assets/fungible", baseUrl || BASE_URL); + const response = await fetch(url, { signal }).then( (response) => response.json() as unknown as { data: FungibleToken[] }, ); @@ -52,9 +63,10 @@ export async function getFungibleTokens(): Promise { export async function getFungibleToken( token: TokenSymbol, + { baseUrl, signal }: FetchOptions = {}, ): Promise { - const url = new URL(`/assets/fungible/${token}`, BASE_URL); - return await fetch(url).then( + const url = new URL(`/assets/fungible/${token}`, baseUrl || BASE_URL); + return await fetch(url, { signal }).then( (response) => response.json() as unknown as FungibleToken, ); } @@ -62,12 +74,13 @@ export async function getFungibleToken( export async function getUserFungibleTokens( address: Address, token: TokenSymbol, + { baseUrl, signal }: FetchOptions = {}, ): Promise { const url = new URL( `/accounts/${address}/assets/fungible/${token}`, - BASE_URL, + baseUrl || BASE_URL, ); - const response = await fetch(url).then( + const response = await fetch(url, { signal }).then( (response) => response.json() as unknown as { data: FungibleTokenBalance[] }, ); @@ -75,15 +88,18 @@ export async function getUserFungibleTokens( return response.data; } -export async function getSolution({ - account, - destinationChain, - token, - amount, - threshold, - whitelistedSourceChains, -}: SolutionOptions): Promise { - const url = new URL("/solutions/aggregation", BASE_URL); +export async function getSolution( + { + account, + destinationChain, + token, + amount, + threshold, + whitelistedSourceChains, + }: SolutionOptions, + { baseUrl, signal }: FetchOptions = {}, +): Promise { + const url = new URL("/solutions/aggregation", baseUrl || BASE_URL); url.searchParams.set("account", account); url.searchParams.set("destination", String(destinationChain)); @@ -97,7 +113,7 @@ export async function getSolution({ whitelistedSourceChains.join(","), ); - const response = await fetch(url).then( + const response = await fetch(url, { signal }).then( (response) => response.json() as unknown as { data: Solution[] } | FailedSolution, ); @@ -106,18 +122,22 @@ export async function getSolution({ return response.data; } -export async function getContractSolution({ - account, - destinationChain, - token, - amount, - contractCall, - threshold, - whitelistedSourceChains, -}: ContractSolutionOptions): Promise { - const url = new URL("/solutions/aggregation", BASE_URL); +export async function getContractSolution( + { + account, + destinationChain, + token, + amount, + contractCall, + threshold, + whitelistedSourceChains, + }: ContractSolutionOptions, + { baseUrl, signal }: FetchOptions = {}, +): Promise { + const url = new URL("/solutions/aggregation", baseUrl || BASE_URL); const response = await fetch(url, { + signal, method: "POST", body: JSON.stringify({ account, diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 93f3ecd..8431f4a 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -88,3 +88,8 @@ export interface Transaction { to: Address; value: string; } + +export interface FetchOptions { + signal?: AbortSignal; + baseUrl?: string; +} From 03252f509e71805a87f71f773f9638727afef645 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Tue, 27 Aug 2024 14:22:20 +0200 Subject: [PATCH 2/3] implement fetch options on class calls --- packages/sdk/src/index.ts | 53 ++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 0554b19..df5525a 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -13,6 +13,7 @@ import type { Address, Chain, ContractSolutionOptions, + FetchOptions, FungibleToken, FungibleTokenBalance, SolutionOptions, @@ -31,26 +32,41 @@ class Sprinter { #tokens?: FungibleToken[]; #chains?: Chain[]; - constructor(provider: EIP1193Provider) { + #fetchOptions: Omit; + + constructor( + provider: EIP1193Provider, + fetchOptions: Omit = {}, + ) { this.#provider = provider; + this.#fetchOptions = fetchOptions; } - public async getAvailableTokens(): Promise { - if (!this.#tokens) this.#tokens = await getFungibleTokens(); + public async getAvailableTokens( + options: FetchOptions = {}, + ): Promise { + if (!this.#tokens) + this.#tokens = await getFungibleTokens(this.makeFetchOptions(options)); return this.#tokens; } - public async getAvailableChains(): Promise { - if (!this.#chains) this.#chains = await getSupportedChains(); + public async getAvailableChains( + options: FetchOptions = {}, + ): Promise { + if (!this.#chains) + this.#chains = await getSupportedChains(this.makeFetchOptions(options)); return this.#chains; } - public async getUserBalances(tokens?: FungibleToken[]): Promise<{ + public async getUserBalances( + tokens?: FungibleToken[], + options: FetchOptions = {}, + ): Promise<{ [sybol: TokenSymbol]: { balances: FungibleTokenBalance[]; total: string }; }> { const account = await this.getAccount(); - const tokenList = tokens || (await this.getAvailableTokens()); + const tokenList = tokens || (await this.getAvailableTokens(options)); const balances = await Promise.all( tokenList.map((token) => @@ -83,14 +99,17 @@ class Sprinter { public async getSolution( settings: Omit, targetAccount?: Address, + options?: FetchOptions, ): Promise; public async getSolution( settings: Omit, targetAccount?: Address, + options?: FetchOptions, ): Promise; public async getSolution( settings: unknown, targetAccount?: Address, + options?: FetchOptions, ): Promise { const account = targetAccount || (await this.getAccount()); @@ -98,11 +117,17 @@ class Sprinter { throw new Error("Missing settings object"); if ("contractCall" in settings) - return await getContractSolution({ - ...settings, - account, - }); - return await getSolution({ ...settings, account }); + return await getContractSolution( + { + ...settings, + account, + }, + this.makeFetchOptions(options || {}), + ); + return await getSolution( + { ...settings, account }, + this.makeFetchOptions(options || {}), + ); } private isContractSolutionOptions( @@ -126,6 +151,10 @@ class Sprinter { return account; } + + private makeFetchOptions(options: FetchOptions): FetchOptions { + return { ...this.#fetchOptions, ...options }; + } } export { Sprinter, setBaseUrl, BASE_URL, EIP1193Provider }; From d451d395e3d5ef9b8194acf5a24926b0e7fb23d9 Mon Sep 17 00:00:00 2001 From: BeroBurny Date: Tue, 27 Aug 2024 14:22:37 +0200 Subject: [PATCH 3/3] remove unused code --- packages/sdk/src/index.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index df5525a..7e6c8fd 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -130,17 +130,6 @@ class Sprinter { ); } - private isContractSolutionOptions( - settings: unknown, - ): settings is Omit { - return ( - settings != undefined && - typeof settings === "object" && - "contractCall" in settings && - typeof settings.contractCall === "object" - ); - } - private async getAccount(): Promise
{ const [account] = (await this.#provider.request({ method: "eth_requestAccounts",