Skip to content
Open
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
10 changes: 5 additions & 5 deletions modules/cosmos/configs/devnet.module.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"account": "ethm1dakgyqjulg29m5fmv992g2y66m9g2mjn6hahwg"
},
"evm": {
"v1": {
"v2": {
"accounts": [
{
"address": "0x6F6C82025CFA145DD13B614AA4289AD6CA856E53",
Expand All @@ -41,7 +41,7 @@
"params": {
"evmDenom": "axrp",
"extraEips": [
"ethereum_3855"
"3855"
],
"allowUnprotectedTxs": false,
"evmChannels": [],
Expand All @@ -63,7 +63,7 @@
]
}
},
"v2": {
"v1": {
"accounts": [{
"address": "",
"code": "",
Expand All @@ -89,7 +89,7 @@
}
},
"feemarket": {
"v1": {
"v2": {
"params": {
"noBaseFee": false,
"baseFeeChangeDenominator": 8,
Expand All @@ -101,7 +101,7 @@
},
"baseFee": "800000000000"
},
"v2": {
"v1": {
"params": {
"noBaseFee": false,
"baseFeeChangeDenominator": 1,
Expand Down
10 changes: 5 additions & 5 deletions modules/cosmos/configs/testnet.module.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"account": "ethm1dakgyqjulg29m5fmv992g2y66m9g2mjn6hahwg"
},
"evm": {
"v1": {
"v2": {
"accounts": [
{
"address": "0x6F6C82025CFA145DD13B614AA4289AD6CA856E53",
Expand All @@ -41,7 +41,7 @@
"params": {
"evmDenom": "axrp",
"extraEips": [
"ethereum_3855"
"3855"
],
"allowUnprotectedTxs": false,
"evmChannels": [],
Expand All @@ -63,7 +63,7 @@
]
}
},
"v2": {
"v1": {
"accounts": [{
"address": "",
"code": "",
Expand All @@ -89,7 +89,7 @@
}
},
"feemarket": {
"v1": {
"v2": {
"params": {
"noBaseFee": false,
"baseFeeChangeDenominator": 8,
Expand All @@ -101,7 +101,7 @@
},
"baseFee": "200000000000"
},
"v2": {
"v1": {
"params": {
"noBaseFee": false,
"baseFeeChangeDenominator": 1,
Expand Down
117 changes: 0 additions & 117 deletions modules/cosmos/module.config.json

This file was deleted.

8 changes: 4 additions & 4 deletions modules/cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test:mainnet": "cp configs/mainnet.module.config.json module.config.json && mocha",
"test:testnet": "cp configs/testnet.module.config.json module.config.json && mocha",
"test:devnet": "cp configs/devnet.module.config.json module.config.json && mocha",
"test:localnet": "cp configs/localnet.module.config.json module.config.json && mocha",
"test:mainnet": "COSMOS_ENV=mainnet mocha",
"test:testnet": "COSMOS_ENV=testnet mocha",
"test:devnet": "COSMOS_ENV=devnet mocha",
"test:localnet": "COSMOS_ENV=localnet mocha",
"clean": "rimraf .turbo dist node_modules"
},
"license": "ISC",
Expand Down
104 changes: 104 additions & 0 deletions modules/cosmos/src/config/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { CosmosModuleConfig } from './module';

// Safe process.env access
declare const process: {
env: Record<string, string | undefined>;
};

// Safe require access
declare const require: (id: string) => any;

export type ConfigEnvironment = 'localnet' | 'devnet' | 'testnet' | 'mainnet';

/**
* Configuration loader for Cosmos module that maintains type safety
*/
export class ConfigLoader {
/**
* Load configuration for the specified environment
* @param env The environment to load configuration for
* @returns Promise<CosmosModuleConfig> The loaded and validated configuration
*/
static async loadConfig(env: ConfigEnvironment): Promise<CosmosModuleConfig> {
try {
// Use require to load the JSON configuration file
// The path is relative to the compiled JavaScript file location
const config = require(`../../configs/${env}.module.config.json`);

// Validate the configuration structure
this.validateConfig(config);

return config;
} catch (error) {
throw new Error(`Failed to load configuration for environment "${env}": ${error instanceof Error ? error.message : error}`);
}
}

/**
* Load configuration from environment variable or default to specified environment
* @param defaultEnv The default environment if COSMOS_ENV is not set
* @returns Promise<CosmosModuleConfig> The loaded configuration
*/
static async loadConfigFromEnv(defaultEnv: ConfigEnvironment = 'localnet'): Promise<CosmosModuleConfig> {
const env = (process.env.COSMOS_ENV as ConfigEnvironment) || defaultEnv;
return this.loadConfig(env);
}

/**
* Validate the configuration structure and required fields
* @param config The configuration to validate
*/
private static validateConfig(config: any): asserts config is CosmosModuleConfig {
if (!config) {
throw new Error('Configuration is null or undefined');
}

// Validate network configuration
if (!config.network) {
throw new Error('Missing network configuration');
}

if (!config.network.id || typeof config.network.id !== 'string') {
throw new Error('Invalid or missing network.id');
}

if (!config.network.urls || !config.network.urls.rpc) {
throw new Error('Missing network.urls.rpc configuration');
}

if (!config.network.type || config.network.type !== 'cosmos') {
throw new Error('Invalid network.type, must be "cosmos"');
}

// Validate bank configuration
if (!config.bank) {
throw new Error('Missing bank configuration');
}

if (!config.bank.account || typeof config.bank.account !== 'string') {
throw new Error('Invalid or missing bank.account');
}

// Validate slashing configuration
if (!config.slashing) {
throw new Error('Missing slashing configuration');
}

if (typeof config.slashing.slashDowntimeFraction !== 'string') {
throw new Error('Invalid slashing.slashDowntimeFraction');
}

if (typeof config.slashing.slashDoubleSignFraction !== 'string') {
throw new Error('Invalid slashing.slashDoubleSignFraction');
}

// Validate poa configuration
if (!config.poa) {
throw new Error('Missing poa configuration');
}

if (!config.poa.stakedAmount || typeof config.poa.stakedAmount !== 'string') {
throw new Error('Invalid or missing poa.stakedAmount');
}
}
}
14 changes: 12 additions & 2 deletions modules/cosmos/src/config/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import { Chain } from "@firewatch/core/chain";
import { BankModuleConfig } from "../modules/bank/config";
import { PoaModuleConfig } from "../modules/poa/config";
import { SlashingModuleConfig } from "../modules/slashing/config";
import { EvmModuleConfig } from "../modules/evm/v2/config";
import { EvmModuleConfig as EvmModuleConfigV2 } from "../modules/evm/v2/config";
import { EvmModuleConfig as EvmModuleConfigV1 } from "../modules/evm/v1/config";
import { FeemarketModuleConfig as FeemarketModuleConfigV1 } from "../modules/feemarket/v1/config";
import { FeemarketModuleConfig as FeemarketModuleConfigV2 } from "../modules/feemarket/v2/config";

export interface CosmosModuleConfig extends Omit<ModuleConfig<Chain, Account>, "accounts" | "door"> {
bank: BankModuleConfig;
poa: PoaModuleConfig;
slashing: SlashingModuleConfig;
evmv2: EvmModuleConfig;
evm: {
v1: EvmModuleConfigV1;
v2: EvmModuleConfigV2;
};
feemarket: {
v1: FeemarketModuleConfigV1;
v2: FeemarketModuleConfigV2;
};
}
5 changes: 3 additions & 2 deletions modules/cosmos/src/modules/evm/v1/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Params } from "@firewatch/proto-evmos/evm";

export type AccountCode = {
address: string;
code: string;
code: string | null;
codeHash: string;
};

export type EvmModuleConfig = {
params: Params;
code: AccountCode[];
accounts: AccountCode[];
};
3 changes: 2 additions & 1 deletion modules/cosmos/src/modules/evm/v2/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Params } from "@firewatch/proto-evm/evm";
export type AccountCode = {
address: string;
code: string;
codeHash: string;
};

export type EvmModuleConfig = {
params: Params;
code: AccountCode[];
accounts: AccountCode[];
};
Loading