Skip to content

Commit 6730fd3

Browse files
committed
Refactor setModuleDefaults to improve chains validation and add default fetch handling
1 parent 7fb3d12 commit 6730fd3

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

adapters/utils/runAdapter.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ function genUID(length: number = 10): string {
2727
const adapterRunResponseCache = {} as any
2828

2929
export async function setModuleDefaults(module: SimpleAdapter) {
30-
const { chains, fetch, start, runAtCurrTime } = module
31-
const rootConfig: any = { fetch }
30+
const { chains = [], fetch, start, runAtCurrTime } = module
31+
const rootConfig: any = {}
3232

33+
if (fetch) rootConfig.fetch = fetch
3334
if (start) rootConfig.start = start
3435
if (runAtCurrTime) rootConfig.runAtCurrTime = runAtCurrTime
3536

@@ -39,26 +40,27 @@ export async function setModuleDefaults(module: SimpleAdapter) {
3940
module.adapter = adapterObject
4041

4142
if (!module.version) module.version = 1 // default to version 1
43+
module.runAtCurrTime = runAtCurrTime ?? Object.values(adapterObject).some((c: BaseAdapterChainConfig) => c.runAtCurrTime)
4244

43-
if (Array.isArray(chains)) {
44-
if (typeof fetch !== 'function') throw new Error('If chains field is passed, fetch function must be provided')
45-
for (const cConfig of chains) {
46-
47-
if (typeof cConfig === 'string') {
48-
setChainConfig(cConfig, rootConfig)
49-
} else if (Array.isArray(cConfig)) {
50-
const [chain, chainConfig] = cConfig
51-
if (typeof chain !== 'string' || typeof chainConfig !== 'object')
52-
throw new Error(`Invalid chain config: ${cConfig}`)
53-
setChainConfig(chain, { ...rootConfig, ...chainConfig })
54-
} else {
45+
if (!Array.isArray(chains))
46+
throw new Error(`Chains should be an array, got ${typeof chains} instead`)
47+
48+
Object.keys(adapterObject).filter(chain => !chains.includes(chain)).forEach(chain => chains.push(chain))
49+
50+
for (const cConfig of chains) {
51+
52+
if (typeof cConfig === 'string') {
53+
setChainConfig(cConfig, rootConfig)
54+
} else if (Array.isArray(cConfig)) {
55+
const [chain, chainConfig] = cConfig
56+
if (typeof chain !== 'string' || typeof chainConfig !== 'object')
5557
throw new Error(`Invalid chain config: ${cConfig}`)
56-
}
58+
setChainConfig(chain, { ...rootConfig, ...chainConfig })
59+
} else {
60+
throw new Error(`Invalid chain config: ${cConfig}`)
5761
}
5862
}
5963

60-
module.runAtCurrTime = runAtCurrTime ?? Object.values(adapterObject).some((c: BaseAdapterChainConfig) => c.runAtCurrTime)
61-
6264
// check if chain already has a given field before setting it, so we dont end up overwriting it with defaults
6365
function setChainConfig(chain: string, config: BaseAdapterChainConfig) {
6466
if (!adapterObject[chain]) adapterObject[chain] = {}

0 commit comments

Comments
 (0)