Skip to content

Commit

Permalink
feat: load rpc additional methods by cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Wsteth committed May 18, 2024
1 parent dc55b39 commit 5d9c53d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/chopsticks/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import type { MiddlewareFunction } from 'yargs'

import { Blockchain, connectParachains, connectVertical, environment } from '@acala-network/chopsticks-core'
import { configSchema, fetchConfig, getYargsOptions } from './schema/index.js'
import { loadMethodsByScripts } from './rpc/rpc-methods.js'
import { pluginExtendCli } from './plugins/index.js'
import { setupWithServer } from './index.js'

dotenvConfig()

const processArgv: MiddlewareFunction<{ config?: string; port?: number }> = async (argv) => {
const processArgv: MiddlewareFunction<{ config?: string; port?: number,rpcMethods?: string }> = async (argv) => {
if (argv.rpcMethods) {
await loadMethodsByScripts(argv.rpcMethods);
}
if (argv.config) {
Object.assign(argv, _.defaults(argv, await fetchConfig(argv.config)))
}
Expand Down Expand Up @@ -84,6 +88,7 @@ const commands = yargs(hideBin(process.argv))
.alias('endpoint', 'e')
.alias('port', 'p')
.alias('block', 'b')
.alias('rpc-methods', 'r')
.alias('import-storage', 's')
.alias('wasm-override', 'w')
.usage('Usage: $0 <command> [options]')
Expand Down
5 changes: 3 additions & 2 deletions packages/chopsticks/src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@acala-network/chopsticks-core'

import { loadRpcPlugin, rpcPluginMethods } from '../plugins/index.js'
import { rpcExtensionMethods } from './rpc-methods.js'

const rpcLogger = defaultLogger.child({ name: 'rpc' })

Expand All @@ -16,12 +17,12 @@ const allHandlers: Handlers = {
rpc_methods: async () =>
Promise.resolve({
version: 1,
methods: [...Object.keys(allHandlers), ...rpcPluginMethods].sort(),
methods: [...Object.keys(allHandlers), ...Object.keys(rpcExtensionMethods), ...rpcPluginMethods].sort(),
}),
}

const getHandler = async (method: string) => {
const handler = allHandlers[method]
const handler = allHandlers[method] || rpcExtensionMethods[method]
if (!handler) {
// no handler for this method, check if it's a plugin
return loadRpcPlugin(method)
Expand Down
17 changes: 17 additions & 0 deletions packages/chopsticks/src/rpc/rpc-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Handlers, isUrl } from '@acala-network/chopsticks-core'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import axios from 'axios'

// This is a global value that store the loaded methods by cli
export let rpcExtensionMethods: Handlers = {}

// Use cli to load rpc methods of external scripts
export const loadMethodsByScripts = async (path: string) => {
try {
const scriptContent = isUrl(path) ? await axios.get(path).then((x) => x.data) : readFileSync(resolve(path), 'utf8')
rpcExtensionMethods = new Function(scriptContent)()
} catch (error) {
console.log('Failed to load rpc extension methods')
}
}

0 comments on commit 5d9c53d

Please sign in to comment.