Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display chain ID (for EVM), SS58 prefix (for Substrate) and chain name instead of internal identifier #85

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions .github/workflows/test.yml → .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ dist/
# Oclif
oclif.manifest.json

# Yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/api/gateways.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export type Provider = {

export type Gateway = {
network: string;
chainName: string;
chainId?: number;
chainSS58Prefix?: number;
providers: Provider[];
};

Expand Down
49 changes: 33 additions & 16 deletions src/commands/gateways/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,80 @@ 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: '<evm|substrate>',
required: false,
}),
search: Flags.string({
char: 's',
description: 'Search gateways',
name: Flags.string({
char: 'n',
description: 'Filter by network name',
helpValue: '<regex>',
required: false,
}),
chain: Flags.string({
char: 'c',
description: 'Filter by chain ID or SS58 prefix',
helpValue: '<number>',
required: false,
}),
};

async run(): Promise<void> {
const {
flags: { type, search },
flags: { type, name, chain: chainId },
} = await this.parse(Ls);

const [evm, substrate] = await Promise.all([
!type || type === 'evm' ? getEvmGateways() : [],
!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: {
Expand All @@ -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());
Expand Down
Loading