Skip to content

Commit

Permalink
fix: improve chains config
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Nov 20, 2023
1 parent 7618476 commit 5c62794
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 70 deletions.
60 changes: 40 additions & 20 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,65 @@ export const config = (() => {
integrator: 'lifi-sdk',
apiUrl: 'https://li.quest/v1',
rpcUrls: {},
chains: [],
preloadChains: true,
}
let _chains: ExtendedChain[]
let _loading: Promise<void> | undefined
return {
get chains() {
return _chains
},
set chains(chains: ExtendedChain[]) {
_chains = chains
_loading = undefined
},
set loading(loading: Promise<void>) {
_loading = loading
},
async getRPCUrls() {
get() {
return _config
},
set(options: SDKOptions) {
Object.assign(_config, options)
if (options.chains) {
this.setChains(options.chains)
}
return _config
},
setProviders(providers: SDKProvider[]) {
_config.providers = providers
},
setChains(chains: ExtendedChain[]) {
hydrateRPCUrls(this.get(), chains)
_config.chains = chains
_loading = undefined
},
async getChains() {
if (_loading) {
await _loading
}
return _config.rpcUrls
return _config.chains
},
async getChainById(chainId: ChainId) {
if (_loading) {
await _loading
}
const chain = this.chains?.find((chain) => chain.id === chainId)
const chain = _config.chains?.find((chain) => chain.id === chainId)
if (!chain) {
throw new Error(`ChainId ${chainId} not found`)
}
return chain
},
get() {
return _config
},
set(options: SDKOptions) {
Object.assign(_config, options)
return _config
},
setProviders(providers: SDKProvider[]) {
_config.providers = providers
async getRPCUrls() {
if (_loading) {
await _loading
}
return _config.rpcUrls
},
}
})()

export const hydrateRPCUrls = (config: SDKConfig, chains: ExtendedChain[]) => {
for (const chain of chains) {
const chainId = chain.id as ChainId
// set RPCs if they were not configured by the user before
if (!config.rpcUrls[chainId]?.length) {
config.rpcUrls[chainId] = chain.metamask.rpcUrls
} else {
config.rpcUrls[chainId]?.push(...chain.metamask.rpcUrls)
}
}
}
3 changes: 0 additions & 3 deletions src/core/EVM/EVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export function EVM(options?: EVMProviderOptions): EVMProvider {
get type() {
return ChainType.EVM
},
get multicall() {
return _options.multicall
},
get multisig() {
return _options.multisig
},
Expand Down
4 changes: 1 addition & 3 deletions src/core/EVM/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { ChainType, type ChainId, type Token } from '@lifi/types'
import { ChainType, type Token } from '@lifi/types'
import type { Hash, WalletClient } from 'viem'
import type { SwitchChainHook } from '../types.js'
import { type SDKProvider } from '../types.js'

export interface EVMProviderOptions {
getWalletClient?: () => Promise<WalletClient>
switchChain?: SwitchChainHook
multicall?: Partial<Record<ChainId, string>>
multisig?: MultisigConfig
}

export interface EVMProvider extends SDKProvider {
setOptions(options: EVMProviderOptions): void
multicall?: Partial<Record<ChainId, string>>
multisig?: MultisigConfig
}

Expand Down
11 changes: 3 additions & 8 deletions src/core/EVM/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ChainType, type ChainId } from '@lifi/types'
import { type ChainId } from '@lifi/types'
import type { PublicClient, Transaction } from 'viem'
import { config } from '../../config.js'
import { median } from '../../utils/median.js'
import { getProvider } from '../provider.js'
import type { EVMProvider } from './types.js'

export const getMaxPriorityFeePerGas = async (
client: PublicClient
Expand Down Expand Up @@ -39,9 +37,6 @@ export const getMaxPriorityFeePerGas = async (
export const getMulticallAddress = async (
chainId: ChainId
): Promise<string | undefined> => {
const provider = getProvider<EVMProvider>(ChainType.EVM)
return (
provider.multicall?.[chainId] ??
config.chains.find((chain) => chain.id === chainId)?.multicallAddress
)
const chains = await config.getChains()
return chains.find((chain) => chain.id === chainId)?.multicallAddress
}
40 changes: 10 additions & 30 deletions src/createConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import type { ChainId } from '@lifi/types'
import { ChainType } from '@lifi/types'
import { config } from './config.js'
import { isEVM } from './core/EVM/types.js'
import { checkPackageUpdates } from './helpers.js'
import { getChains } from './services/api.js'
import type { SDKOptions } from './types/index.js'
Expand All @@ -13,41 +12,22 @@ function createBaseConfig(options: SDKOptions) {
'Integrator not found. Please see documentation https://docs.li.fi/integrate-li.fi-js-sdk/set-up-the-sdk'
)
}
config.set(options)
const _config = config.set(options)
checkPackageUpdates(name, version, options.disableVersionCheck)
return _config
}

export async function createChainsConfig() {
const _config = config.get()
const chainTypes = _config.providers?.map((provider) => provider.type)
config.loading = getChains({ chainTypes: chainTypes })
.then((chains) => {
config.chains = chains
const evmProvider = _config.providers?.find(isEVM)
const multicallAddresses: Partial<Record<ChainId, string>> = {}
for (const chain of chains) {
const chainId = chain.id as ChainId

// set RPCs if they were not configured by the user before
if (!_config.rpcUrls[chainId]?.length) {
_config.rpcUrls[chainId] = chain.metamask.rpcUrls
}

// set multicall addresses if they exist and were not configured by the user before
if (chain.multicallAddress && !evmProvider?.multicall?.[chainId]) {
multicallAddresses[chainId] = chain.multicallAddress
}
}
evmProvider?.setOptions({
multicall: multicallAddresses,
})
})
config.loading = getChains({ chainTypes: Object.values(ChainType) })
.then((chains) => config.setChains(chains))
.catch()
await config.loading
}

export function createConfig(options: SDKOptions) {
createBaseConfig(options)
createChainsConfig()
return config
const _config = createBaseConfig(options)
if (_config.preloadChains) {
createChainsConfig()
}
return _config
}
14 changes: 8 additions & 6 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { ChainId, RouteOptions } from '@lifi/types'
import type { ChainId, ExtendedChain, RouteOptions } from '@lifi/types'
import type { SDKProvider } from '../core/types.js'

export interface SDKConfig {
integrator: string
apiUrl: string
apiKey?: string
apiUrl: string
integrator: string
userId?: string
disableVersionCheck?: boolean
widgetVersion?: string
routeOptions?: RouteOptions
providers?: SDKProvider[]
routeOptions?: RouteOptions
rpcUrls: Partial<Record<ChainId, string[]>>
chains: ExtendedChain[]
disableVersionCheck?: boolean
widgetVersion?: string
preloadChains: boolean
}

export interface SDKOptions extends Partial<Omit<SDKConfig, 'integrator'>> {
Expand Down

0 comments on commit 5c62794

Please sign in to comment.