From f0d0bc964631759b9cfb2b3080a07039dcad3944 Mon Sep 17 00:00:00 2001 From: Nikita Klimin Date: Fri, 26 Apr 2024 09:11:15 +0300 Subject: [PATCH] feat: display chain ID for EVM, SS58 prefix for Substrate and chain name instead of internal identifier --- .github/workflows/{test.yml => test.yaml} | 25 ++++++------ .gitignore | 5 +++ package.json | 2 +- src/api/gateways.ts | 3 ++ src/commands/gateways/ls.ts | 49 +++++++++++++++-------- 5 files changed, 55 insertions(+), 29 deletions(-) rename .github/workflows/{test.yml => test.yaml} (57%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yaml similarity index 57% rename from .github/workflows/test.yml rename to .github/workflows/test.yaml index 41c1421..1d96a67 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yaml @@ -24,19 +24,20 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Get yarn cache directory path - id: yarn-cache-dir-path - run: echo "::set-output name=dir::$(yarn cache dir)" - - - uses: actions/cache@v3 - id: yarn-cache - env: - cache-name: rest-api + - name: Install node + uses: actions/setup-node@v4 with: - path: ${{ steps.yarn-cache-dir-path.outputs.dir }} - key: ${{ runner.os }}-${{ env.cache-name }}-yarn-${{ hashFiles('yarn.lock') }} - restore-keys: | - ${{ runner.os }}-${{ env.cache-name }}-yarn- + node-version: 18 + + - name: Install Yarn + run: corepack enable + + # Yarn dependencies cannot be cached until yarn is installed + # WORKAROUND: https://github.com/actions/setup-node/issues/531 + - name: Extract cached dependencies + uses: actions/setup-node@v4 + with: + cache: yarn - name: Install run: yarn install diff --git a/.gitignore b/.gitignore index 896263d..35fb3e7 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,8 @@ dist/ # Oclif oclif.manifest.json +# Yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz diff --git a/package.json b/package.json index 6abfeff..c67e101 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "sqd": "./bin/run.js", "bl": "node ./lib/blessed.js", "lint": "eslint --fix src/**/*", - "test:unit": "NODE_ENV=test jest --bail --testRegex=.unit.spec.ts$", + "test:unit": "NODE_ENV=test jest --bail --testRegex=.unit.spec.ts\\$", "tsc": "tsc --noEmit", "pkg:build": "./bin/pkg-build.sh", "pkg:compress": "./bin/pkg-compress.sh", diff --git a/src/api/gateways.ts b/src/api/gateways.ts index 590068b..37e9995 100644 --- a/src/api/gateways.ts +++ b/src/api/gateways.ts @@ -8,6 +8,9 @@ export type Provider = { export type Gateway = { network: string; + chainName: string; + chainId?: number; + chainSS58Prefix?: number; providers: Provider[]; }; diff --git a/src/commands/gateways/ls.ts b/src/commands/gateways/ls.ts index 58b3fde..f1cd978 100644 --- a/src/commands/gateways/ls.ts +++ b/src/commands/gateways/ls.ts @@ -12,21 +12,28 @@ export default class Ls extends CliCommand { static flags = { type: Flags.string({ char: 't', - description: 'Filter gateways by network type', + description: 'Filter by network type', options: ['evm', 'substrate'], helpValue: '', required: false, }), - search: Flags.string({ - char: 's', - description: 'Search gateways', + name: Flags.string({ + char: 'n', + description: 'Filter by network name', + helpValue: '', + required: false, + }), + chain: Flags.string({ + char: 'c', + description: 'Filter by chain ID or SS58 prefix', + helpValue: '', required: false, }), }; async run(): Promise { const { - flags: { type, search }, + flags: { type, name, chain: chainId }, } = await this.parse(Ls); const [evm, substrate] = await Promise.all([ @@ -34,42 +41,51 @@ export default class Ls extends CliCommand { !type || type === 'substrate' ? getSubstrateGateways() : [], ]); - const maxNameLength = maxBy([...evm, ...substrate], (g) => g.network.length)?.network.length; + const maxNameLength = maxBy([...evm, ...substrate], (g) => g.chainName.length)?.chainName.length; switch (type) { case 'evm': - this.processGateways(evm, { search, summary: 'EVM', maxNameLength }); + this.processGateways(evm, { type, name, chainId, summary: 'EVM', maxNameLength }); break; case 'substrate': - this.processGateways(substrate, { search, summary: 'Substrate', maxNameLength }); + this.processGateways(substrate, { type, name, chainId, summary: 'Substrate', maxNameLength }); break; default: - this.processGateways(evm, { search, summary: 'EVM', maxNameLength }); + this.processGateways(evm, { type: 'evm', name, chainId, summary: 'EVM', maxNameLength }); this.log(); - this.processGateways(substrate, { search, summary: 'Substrate', maxNameLength }); + this.processGateways(substrate, { type: 'substrate', name, chainId, summary: 'Substrate', maxNameLength }); } } processGateways( gateways: Gateway[], - { summary, search, maxNameLength }: { search?: string; summary?: string; maxNameLength?: number }, + { + type, + name, + chainId, + summary, + maxNameLength, + }: { type: 'evm' | 'substrate'; name?: string; chainId?: string; summary?: string; maxNameLength?: number }, ) { if (summary) { this.log(chalk.bold(summary)); } - gateways = search - ? gateways.filter((g) => g.network.toLocaleLowerCase().includes(search.toLocaleLowerCase())) + gateways = name ? gateways.filter((g) => g.network.match(new RegExp(name, 'i'))) : gateways; + + gateways = chainId + ? gateways.filter((g) => (type === 'evm' ? String(g.chainId) === chainId : String(g.chainSS58Prefix) === chainId)) : gateways; if (!gateways.length) { return this.log('No gateways found'); } + const headRow = ['Network', type === 'evm' ? 'Chain ID' : 'SS58 prefix', 'Gateway URL']; const table = new Table({ wordWrap: true, colWidths: [maxNameLength ? maxNameLength + 2 : null], - head: ['Name', 'Release', 'Gateway URL'], + head: headRow, wrapOnWordBoundary: false, style: { @@ -79,8 +95,9 @@ export default class Ls extends CliCommand { }, }); - gateways.map(({ network, providers }) => { - table.push([network, chalk.dim(providers[0].release), providers[0].dataSourceUrl]); + gateways.map(({ chainName, chainId, chainSS58Prefix, providers }) => { + const row = [chainName, chalk.dim(chainId || chainSS58Prefix || '-'), providers[0].dataSourceUrl]; + table.push(row); }); this.log(table.toString());