Skip to content

Commit

Permalink
fix: use only unique providers
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Jan 15, 2024
1 parent 45ff26d commit 5bff9f3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const config = (() => {
apiUrl: 'https://li.quest/v1',
rpcUrls: {},
chains: [],
providers: [],
preloadChains: true,
}
let _loading: Promise<void> | undefined
Expand All @@ -19,14 +20,22 @@ export const config = (() => {
return _config
},
set(options: SDKConfig) {
Object.assign(_config, options)
if (options.chains) {
this.setChains(options.chains)
const { chains, providers, ...otherOptions } = options
Object.assign(_config, otherOptions)
if (chains) {
this.setChains(chains)
}
if (providers) {
this.setProviders(providers)
}
return _config
},
setProviders(providers: SDKProvider[]) {
_config.providers = providers
const providerMap = new Map(
_config.providers.map((provider) => [provider.type, provider])
)
providers.forEach((provider) => providerMap.set(provider.type, provider))
_config.providers = Array.from(providerMap.values())
},
setChains(chains: ExtendedChain[]) {
hydrateRPCUrls(this.get(), chains)
Expand Down
2 changes: 1 addition & 1 deletion src/core/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const executeSteps = async (

const provider = config
.get()
.providers?.find((provider) => provider.isAddress(fromAddress))
.providers.find((provider) => provider.isAddress(fromAddress))

if (!provider) {
throw new Error('SDK Execution Provider not found.')
Expand Down
2 changes: 1 addition & 1 deletion src/core/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { config } from '../config.js'
export const getProvider = <T>(type: ChainType): T => {
const provider = config
.get()
.providers?.find((provider) => provider.type === type) as T
.providers.find((provider) => provider.type === type) as T
if (!provider) {
throw new Error(`${type} provider not found.`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const getTokenBalancesByChain = async (
const baseTokenAddress = tokensByChain[chainId][0].address
const provider = config
.get()
.providers?.find((provider) => provider.isAddress(baseTokenAddress))
.providers.find((provider) => provider.isAddress(baseTokenAddress))
if (!provider) {
throw new Error('SDK Token Provider not found.')
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface SDKBaseConfig {
apiUrl: string
integrator: string
userId?: string
providers?: SDKProvider[]
providers: SDKProvider[]
routeOptions?: RouteOptions
rpcUrls: Partial<Record<ChainId, string[]>>
chains: ExtendedChain[]
Expand Down

0 comments on commit 5bff9f3

Please sign in to comment.