-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add chain export and import, also add ChainRegistry domain
- Loading branch information
Showing
26 changed files
with
424 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "test", | ||
"chainId": "super-1", | ||
"contractsPath": "./contracts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { BaseCommand } from '../../../lib/base'; | ||
import { DefaultChainsRelativePath } from '../../../config'; | ||
import path from 'node:path'; | ||
import { bold, green } from '../../../utils/style'; | ||
import { Args } from '@oclif/core'; | ||
import { BuiltInChains } from '../../../services/BuiltInChains'; | ||
import { ChainRegistry } from '../../../domain/ChainRegistry'; | ||
import { CosmosChain } from 'src/types/CosmosSchema'; | ||
|
||
export default class ConfigChainsExport extends BaseCommand<typeof ConfigChainsExport> { | ||
static summary = `Exports a built-in chain registry file to ${bold( | ||
path.join('{project-root}', DefaultChainsRelativePath, './{chain-id}.json') | ||
)}.`; | ||
|
||
static args = { | ||
chain: Args.string({ name: 'chain', required: true, options: BuiltInChains.getChainIds() }), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const chainRegistry = await ChainRegistry.init(); | ||
|
||
await chainRegistry.writeChainFile(BuiltInChains.getChainById(this.args.chain) as CosmosChain); | ||
|
||
this.log(`✅ ${green('Exported chain to')} ${bold(path.join(chainRegistry.path, `./${this.args.chain}.json`))}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { BaseCommand } from '../../../lib/base'; | ||
import { DefaultChainsRelativePath } from '../../../config'; | ||
import path from 'node:path'; | ||
import { bold, green, red } from '../../../utils/style'; | ||
import { Args } from '@oclif/core'; | ||
import fs from 'node:fs/promises'; | ||
import { CosmosChain } from '../../../types/CosmosSchema'; | ||
import { ChainRegistry } from '../../../domain/ChainRegistry'; | ||
|
||
export default class ConfigChainsImport extends BaseCommand<typeof ConfigChainsImport> { | ||
static summary = `Import a chain registry file and save it to ${bold( | ||
path.join('{project-root}', DefaultChainsRelativePath, './{chain-id}.json') | ||
)}.`; | ||
|
||
static args = { | ||
file: Args.string({ name: 'file', required: false }), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
// to do add pipe support | ||
// if (this.args.file && this.argv.length === 1) { | ||
// this.error(`❌ ${red('Please specify only one file to import')}`); | ||
// } else if (!this.args.file) { | ||
// this.error(`❌ ${red('Please specify the file to import as an argument, or pass the chain info in a pipe')}`); | ||
// } | ||
|
||
const chainInfo: CosmosChain = JSON.parse(await fs.readFile(this.args.file as string, 'utf-8')); | ||
|
||
const chainRegistry = await ChainRegistry.init(); | ||
|
||
await chainRegistry.writeChainFile(chainInfo); | ||
|
||
this.log(`✅ ${green('Imported chain')} ${bold(chainInfo.chain_id)}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Command } from '@oclif/core'; | ||
import Help from '../../../plugins/help-plugin/help'; | ||
|
||
export default class ConfigChains extends Command { | ||
static summary = 'Display help for the contracts command.'; | ||
|
||
public async run(): Promise<void> { | ||
const help = new Help(this.config, { all: true }); | ||
await help.showCommandHelp(ConfigChains); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { BaseCommand } from '../../../lib/base'; | ||
import { DefaultConfigFileName } from '../../../config'; | ||
import { bold, green, red } from '../../../utils/style'; | ||
import { ChainRegistry } from '../../../domain/ChainRegistry'; | ||
import { ConfigFile } from '../../../domain/ConfigFile'; | ||
import { Flags } from '@oclif/core'; | ||
|
||
export default class ConfigChainsUse extends BaseCommand<typeof ConfigChainsUse> { | ||
static summary = `Switches the current chain in use and updates the ${bold(DefaultConfigFileName)} config file with his information.`; | ||
static flags = { | ||
chain: Flags.string({ required: true }), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const configFile = await ConfigFile.open(); | ||
const chainRegistry = await ChainRegistry.init(); | ||
|
||
if (!chainRegistry.getChainById(this.flags.chain)) { | ||
throw new Error(`❌ ${red('Chain id')} ${bold(this.flags.chain)} ${red('not found')}`); | ||
} | ||
|
||
configFile.update({ chainId: this.flags.chain }, true); | ||
|
||
this.log(`✅ ${green('Switched chain to')} ${bold(this.flags.chain)}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
import { Flags } from '@oclif/core'; | ||
import { Chain } from '../../services/Chain'; | ||
import { BuiltInChains } from '../../services/BuiltInChains'; | ||
import { BaseCommand } from '../../lib/base'; | ||
import { showPrompt } from '../../actions/Prompt'; | ||
import { ChainPrompt } from '../../domain/Chain'; | ||
import { ChainPrompt } from '../../services/Prompts'; | ||
import { ConfigFile } from '../../domain/ConfigFile'; | ||
import { bold, green, red } from '../../utils/style'; | ||
import { DefaultConfigFileName } from '../../config'; | ||
|
||
export default class ConfigInit extends BaseCommand<typeof ConfigInit> { | ||
static summary = 'Initializes a config file for the current project.'; | ||
static flags = { | ||
chain: Flags.string({ options: Chain.getChainIds() }), | ||
chain: Flags.string({ options: BuiltInChains.getChainIds() }), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
// Get flags | ||
const { flags } = await this.parse(ConfigInit); | ||
let chain = flags.chain; | ||
|
||
// If chain flag is not set, prompt user input in list | ||
if (await ConfigFile.exists()) { | ||
this.error(`❌ ${red('The file')} ${bold(DefaultConfigFileName)} ${red('already exists in this repository')}`); | ||
} | ||
|
||
// If chain flag is not set, prompt user | ||
if (!chain) { | ||
const response = await showPrompt(ChainPrompt); | ||
|
||
chain = response?.chain; | ||
chain = response.chain as string; | ||
} | ||
|
||
const configFile = ConfigFile.init({ | ||
const configFile = await ConfigFile.init({ | ||
name: 'test', | ||
chainId: chain || '', | ||
chainId: chain, | ||
}); | ||
|
||
if (await ConfigFile.exists()) { | ||
throw new Error('the file modulor.json already exists in this repository'); | ||
} | ||
|
||
await configFile.write(); | ||
|
||
this.log('Config file created: modulor.json'); | ||
this.log(`✅ ${green('Config file')} ${bold(DefaultConfigFileName)} ${green('created')}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { BaseCommand } from '../../lib/base'; | ||
import { ConfigFile } from '../../domain/ConfigFile'; | ||
|
||
export default class ConfigShow extends BaseCommand<typeof ConfigShow> { | ||
static summary = 'Shows the config file for the current project.'; | ||
|
||
public async run(): Promise<void> { | ||
this.log('commands/config/show.ts'); | ||
const configFile = await ConfigFile.open(); | ||
|
||
this.log(await configFile.formattedStatus()); | ||
|
||
if (this.jsonEnabled()) { | ||
this.logJson(await configFile.dataWithContracts()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const DefaultChainId = 'constantine-1'; | ||
export const DefaultConfigFileName = 'modulor.json'; | ||
export const DefaultContractsRelativePath = './contracts'; | ||
export const DefaultChainsRelativePath = './.modulor/chains'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getWokspaceRoot } from '../utils/paths'; | ||
import path from 'node:path'; | ||
import { DefaultChainsRelativePath } from '../config'; | ||
import { CosmosChain } from '../types/CosmosSchema'; | ||
import fs from 'node:fs/promises'; | ||
import { BuiltInChains } from '../services/BuiltInChains'; | ||
|
||
export class ChainRegistry { | ||
private _data: CosmosChain[]; | ||
private _path: string; | ||
|
||
constructor(data: CosmosChain[], path: string) { | ||
this._data = data; | ||
this._path = path; | ||
} | ||
|
||
get data(): CosmosChain[] { | ||
return this._data; | ||
} | ||
|
||
get path(): string { | ||
return this._path; | ||
} | ||
|
||
static async init(chainsDirectory?: string): Promise<ChainRegistry> { | ||
const directoryPath = chainsDirectory || path.join(await getWokspaceRoot(), DefaultChainsRelativePath); | ||
|
||
let filesList = await fs.readdir(directoryPath); | ||
filesList = filesList.filter(item => path.extname(item) === '.json'); | ||
|
||
const filesRead = await Promise.all<string>(filesList.map(item => fs.readFile(path.join(directoryPath, item), 'utf8'))); | ||
|
||
// List of built-in chains that could be added to final result | ||
const builtInToAdd = { ...BuiltInChains.chainMap }; | ||
|
||
// Parse file contents, and check if they override built-in chain info | ||
const parsedList: CosmosChain[] = []; | ||
for (const file of filesRead) { | ||
const parsed: CosmosChain = JSON.parse(file); | ||
if (BuiltInChains.getChainIds().includes(parsed.chain_id)) { | ||
delete builtInToAdd[parsed.chain_id]; | ||
} | ||
|
||
parsedList.push(parsed); | ||
} | ||
|
||
// Create chain registry with the parsed chains and the built-in chains pending to add | ||
return new ChainRegistry([...parsedList, ...Object.values(builtInToAdd)], directoryPath); | ||
} | ||
|
||
getChainById(chainId: string): CosmosChain | undefined { | ||
return this._data.find(item => item.chain_id === chainId); | ||
} | ||
|
||
async writeChainFile(chain: CosmosChain): Promise<void> { | ||
const json = JSON.stringify(chain, null, 2); | ||
|
||
fs.writeFile(path.join(this._path, `./${chain.chain_id}.json`), json); | ||
this._data.push(chain); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.