Skip to content

Commit

Permalink
chain filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
bergarces committed Aug 31, 2023
1 parent 8b9a0c3 commit 0d9890b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const supportedProtocols: Record<
new ExampleProductAdapter({
metadata: {},
chainId: Chain.Ethereum,
provider: provider,
provider,
}),
],
},
Expand Down
60 changes: 40 additions & 20 deletions src/scripts/newAdapterCommand.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Command } from 'commander'
import { promises as fs } from 'fs'
import { partition } from 'lodash'
import * as path from 'path'
import { parse, visit, types, print } from 'recast'
import { adapterTemplate } from './templates/adapter'
import { Chain } from '../core/constants/chains'
import { camelCase, kebabCase, pascalCase } from '../core/utils/caseConversion'
import { partition } from 'lodash'
import { filterMap } from '../core/utils/filters'
import { adapterTemplate } from './templates/adapter'
import { writeCodeFile } from './writeCodeFile'

import n = types.namedTypes
import b = types.builders
import { writeCodeFile } from './writeCodeFile'

export function newAdapterCommand(program: Command) {
program
Expand All @@ -21,10 +23,21 @@ export function newAdapterCommand(program: Command) {
'Ethereum',
)
.showHelpAfterError()
.action(async (protocol: string, product: string, chains: string) => {
// TODO: Validate that chains exist
.action(async (protocol: string, product: string, chainsInput: string) => {
const chains = filterMap(chainsInput.split(','), (chainInput) => {
const chain = Object.keys(Chain).find((chainKey) => {
return chainKey.toLowerCase() === chainInput.toLowerCase()
})

if (!chain) {
console.warn(`Cannot find corresponding chain for ${chainInput}`)
}

return chain
}) as (keyof typeof Chain)[]

await buildAdapterFromTemplate(protocol, product)
await exportAdapter(protocol, product, chains.split(','))
await exportAdapter(protocol, product, chains)
})
}

Expand Down Expand Up @@ -69,7 +82,7 @@ async function buildAdapterFromTemplate(protocol: string, product: string) {
async function exportAdapter(
protocol: string,
product: string,
chains: string[],
chains: (keyof typeof Chain)[],
) {
const adaptersFile = path.resolve('./src/adapters/index.ts')
const contents = await fs.readFile(adaptersFile, 'utf-8')
Expand Down Expand Up @@ -171,7 +184,7 @@ function addAdapterEntries(
supportedProtocolsDeclaratorNode: n.VariableDeclarator,
protocol: string,
product: string,
chains: string[],
chains: (keyof typeof Chain)[],
) {
const supportedProtocolsObjectNode = supportedProtocolsDeclaratorNode.init
if (!n.ObjectExpression.check(supportedProtocolsObjectNode)) {
Expand Down Expand Up @@ -296,23 +309,30 @@ function buildChainEntry(chain: string) {
new <ProductName>Adapter({
metadata: {},
chainId: Chain.<ChainName>,
provider: provider,
provider,
}),
*/
function buildAdapterEntry(chain: string, product: string) {
const params = [b.identifier('provider')]

const metadataProperty = b.objectProperty(
b.identifier('metadata'),
b.objectExpression([]),
)

const chainIdProperty = b.objectProperty(
b.identifier('chainId'),
b.memberExpression(b.identifier('Chain'), b.identifier(pascalCase(chain))),
)

const providerProperty = b.objectProperty(
b.identifier('provider'),
b.identifier('provider'),
)
providerProperty.shorthand = true

const body = b.newExpression(b.identifier(`${pascalCase(product)}Adapter`), [
b.objectExpression([
b.objectProperty(b.identifier('metadata'), b.objectExpression([])),
b.objectProperty(
b.identifier('chainId'),
b.memberExpression(
b.identifier('Chain'),
b.identifier(pascalCase(chain)),
),
),
b.objectProperty(b.identifier('provider'), b.identifier('provider')),
]),
b.objectExpression([metadataProperty, chainIdProperty, providerProperty]),
])

return b.arrowFunctionExpression(params, body)
Expand Down

0 comments on commit 0d9890b

Please sign in to comment.