diff --git a/src/commands/account/create.ts b/src/commands/account/create.ts index a3990ef6..a240e2f0 100644 --- a/src/commands/account/create.ts +++ b/src/commands/account/create.ts @@ -59,7 +59,6 @@ export class CreateAccount extends SwankyCommand { const accountData: AccountData = { mnemonic: "", isDev, - default: false, alias: (await inquirer.prompt([{ type: "input", message: "Enter alias: ", name: "alias" }])) .alias, address: new ChainAccount(tmpMnemonic).pair.address, @@ -77,6 +76,9 @@ export class CreateAccount extends SwankyCommand { } this.swankyConfig.accounts.push(accountData); + if(this.swankyConfig.defaultAccount === null) { + this.swankyConfig.defaultAccount = accountData.alias; + } await this.storeSystemConfig(); this.log( diff --git a/src/commands/account/default.ts b/src/commands/account/default.ts index 797acdea..0c2a49e3 100644 --- a/src/commands/account/default.ts +++ b/src/commands/account/default.ts @@ -18,8 +18,6 @@ export class DefaultAccount extends SwankyCommand { async run(): Promise { const { args } = await this.parse(DefaultAccount); - let defaultAccount = ""; - if(args.accountAlias) { const accountData = this.swankyConfig.accounts.find( (account: AccountData) => account.alias === args.accountAlias @@ -27,9 +25,9 @@ export class DefaultAccount extends SwankyCommand { if (!accountData) { throw new ConfigError("Provided account alias not found in swanky.config.json"); } - defaultAccount = accountData.alias; + this.swankyConfig.defaultAccount = accountData.alias; } else { - inquirer.prompt([ + await inquirer.prompt([ { type: "list", name: "defaultAccount", @@ -42,23 +40,10 @@ export class DefaultAccount extends SwankyCommand { }), }, ]).then((answers) => { - defaultAccount = answers.defaultAccount; + this.swankyConfig.defaultAccount = answers.defaultAccount; }); } - this.defaultAccount = defaultAccount; - this.swankyConfig.accounts = this.swankyConfig.accounts.map((account: AccountData) => { - if (account.alias === defaultAccount) { - return { - ...account, - default: true, - }; - } - return { - ...account, - default: false, - }; - }); await this.storeSystemConfig(); - console.log(chalk.greenBright(`Default account set to ${chalk.yellowBright(defaultAccount)}`)); + console.log(chalk.greenBright(`Default account set to ${chalk.yellowBright(this.swankyConfig.defaultAccount)}`)); } } diff --git a/src/commands/account/list.ts b/src/commands/account/list.ts index ea2cd5bb..08bfb4c6 100644 --- a/src/commands/account/list.ts +++ b/src/commands/account/list.ts @@ -6,19 +6,29 @@ export class ListAccounts extends SwankyCommand { static aliases = [`account:ls`]; async run(): Promise { - this.log(`${chalk.greenBright("✔")} Stored dev accounts:`); + const countOfDevAccounts = this.swankyConfig.accounts.filter((account) => account.isDev).length; - for (const account of this.swankyConfig.accounts) { - if(account.isDev){ - this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias}`); + if(countOfDevAccounts !== 0) { + this.log(`${chalk.greenBright("✔")} Stored dev accounts:`); + + for (const account of this.swankyConfig.accounts) { + if(account.isDev){ + this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias} \ +${chalk.yellowBright("Address: ")} ${account.address} ${this.swankyConfig.defaultAccount === account.alias ? chalk.greenBright("<- Default") : ""}`); + } } } - this.log(`${chalk.greenBright("✔")} Stored prod accounts:`); + const countOfProdAccounts = this.swankyConfig.accounts.length - countOfDevAccounts; + + if(countOfProdAccounts !== 0) { + this.log(`${chalk.greenBright("✔")} Stored prod accounts:`); - for (const account of this.swankyConfig.accounts) { - if(!account.isDev){ - this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias}`); + for (const account of this.swankyConfig.accounts) { + if(!account.isDev){ + this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias} \ +${chalk.yellowBright("Address: ")} ${account.address} ${this.swankyConfig.defaultAccount === account.alias ? chalk.greenBright("<- Default") : ""}`); + } } } } diff --git a/src/commands/contract/deploy.ts b/src/commands/contract/deploy.ts index 475ba931..831dbce4 100644 --- a/src/commands/contract/deploy.ts +++ b/src/commands/contract/deploy.ts @@ -68,11 +68,11 @@ export class DeployContract extends SwankyCommand { ); } - if(!flags.account && !this.defaultAccount) { + if(!flags.account && this.swankyConfig.defaultAccount === null) { throw new ConfigError("No default account set. Please set one or provide an account alias with --account"); } - const accountAlias = flags.account ?? this.defaultAccount; + const accountAlias = flags.account ?? this.swankyConfig.defaultAccount; const accountData = this.swankyConfig.accounts.find( (account: AccountData) => account.alias === accountAlias @@ -87,21 +87,9 @@ export class DeployContract extends SwankyCommand { ); } - if(!this.defaultAccount) + if(this.swankyConfig.defaultAccount === null) { - this.defaultAccount = accountData.alias; - this.swankyConfig.accounts = this.swankyConfig.accounts.map((account: AccountData) => { - if (account.alias === accountData.alias) { - return { - ...account, - default: true, - }; - } - return { - ...account, - default: false, - }; - }); + this.swankyConfig.defaultAccount = accountAlias; await this.storeSystemConfig(); } diff --git a/src/commands/init/index.ts b/src/commands/init/index.ts index d4117030..2e4e5591 100644 --- a/src/commands/init/index.ts +++ b/src/commands/init/index.ts @@ -175,19 +175,19 @@ export class Init extends SwankyCommand { { alias: "alice", mnemonic: "//Alice", - default: true, isDev: true, address: new ChainAccount("//Alice").pair.address, }, { alias: "bob", mnemonic: "//Bob", - default: false, isDev: true, address: new ChainAccount("//Bob").pair.address, }, ]; + this.configBuilder.defaultAccount = "alice"; + Object.keys(this.configBuilder.contracts!).forEach(async (contractName) => { await ensureDir(path.resolve(this.projectPath, "artifacts", contractName)); await ensureDir(path.resolve(this.projectPath, "tests", contractName)); @@ -195,16 +195,12 @@ export class Init extends SwankyCommand { this.taskQueue.push({ task: () => { - console.log("\nSwanky config:", this.swankyConfig) if (Object.keys(this.swankyConfig.networks).length === 0 || this.swankyConfig.accounts.length === 0) { this.swankyConfig = this.configBuilder as SwankyConfig; - console.log("\n1\n"); } else { this.swankyConfig.node = this.configBuilder.node!; this.swankyConfig.contracts = this.configBuilder.contracts!; - console.log("\n2\n"); } - console.log("\nFinalised Swanky config:", this.swankyConfig) this.storeLocalConfig(this.projectPath); this.storeSystemConfig(); }, diff --git a/src/lib/contractCall.ts b/src/lib/contractCall.ts index 246e97d0..fafbd2d6 100644 --- a/src/lib/contractCall.ts +++ b/src/lib/contractCall.ts @@ -77,11 +77,21 @@ export abstract class ContractCall extends SwankyComma this.deploymentInfo = deploymentData; + if(!flags.account && this.swankyConfig.defaultAccount === null) { + throw new ConfigError("No default account set in swanky.config.json and no account provided"); + } + + const accountAlias = flags.account ?? this.swankyConfig.defaultAccount; + const accountData = this.swankyConfig.accounts.find( - (account: AccountData) => account.alias === flags.account || "alice" + (account: AccountData) => account.alias === accountAlias ); if (!accountData) { - throw new ConfigError("Provided account alias not found in swanky.config.json"); + throw new ConfigError(`Provided account alias(${chalk.redBright(accountAlias)}) not found in swanky.config.json`); + } + + if(accountData.isDev && (flags.network !== "local" || !flags.network)) { + throw new ConfigError(`Account ${chalk.redBright(accountAlias)} is a dev account and can only be used on the local network`); } const networkUrl = resolveNetworkUrl(this.swankyConfig, flags.network ?? ""); diff --git a/src/lib/swankyCommand.ts b/src/lib/swankyCommand.ts index eec06d86..fa7a6709 100644 --- a/src/lib/swankyCommand.ts +++ b/src/lib/swankyCommand.ts @@ -1,8 +1,8 @@ import { Command, Flags, Interfaces } from "@oclif/core"; import { getSwankySystemConfig, getSwankyLocalConfig, Spinner, findSwankySystemConfigPath } from "./index.js"; -import { AccountData, SwankyConfig, SwankyLocalConfig, SwankySystemConfig } from "../types/index.js"; +import { SwankyConfig, SwankyLocalConfig, SwankySystemConfig } from "../types/index.js"; import { writeJSON } from "fs-extra/esm"; -import {mkdirSync, existsSync} from "fs"; +import { mkdirSync, existsSync } from "fs"; import { BaseError, UnknownError } from "./errors.js"; import { swankyLogger } from "./logger.js"; import { Logger } from "winston"; @@ -18,7 +18,6 @@ export abstract class SwankyCommand extends Command { protected spinner!: Spinner; protected swankyConfig!: SwankyConfig; protected logger!: Logger; - protected defaultAccount!: string; protected flags!: Flags; protected args!: Args; @@ -44,6 +43,7 @@ export abstract class SwankyCommand extends Command { supportedInk: "", }, contracts: {}, + defaultAccount: null, accounts: [], networks: {}, }; @@ -68,10 +68,6 @@ export abstract class SwankyCommand extends Command { this.logger.warn("No system config found") } - this.defaultAccount = this.swankyConfig.accounts.find( - (account: AccountData) => account.default - )?.alias ?? ""; - this.logger.info(`Running command: ${this.ctor.name} Args: ${JSON.stringify(this.args)} Flags: ${JSON.stringify(this.flags)} @@ -88,6 +84,7 @@ export abstract class SwankyCommand extends Command { protected async storeSystemConfig() { const systemConfig : SwankySystemConfig = { + defaultAccount: this.swankyConfig.defaultAccount, accounts: this.swankyConfig.accounts, networks: this.swankyConfig.networks } diff --git a/src/types/index.ts b/src/types/index.ts index 7bc4c775..c8883a59 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -11,11 +11,10 @@ export interface ChainProperty { export type ExtrinsicPayload = SubmittableExtrinsic<"promise">; -export interface Encrypted { iv: string; data: string }; +export interface Encrypted { iv: string; data: string } export interface AccountData { isDev: boolean; - default: boolean; alias: string; mnemonic: string | Encrypted; address: string; @@ -45,6 +44,7 @@ export interface SwankyConfig { localPath: string; supportedInk: string; }; + defaultAccount: string | null; accounts: AccountData[]; contracts: Record | Record; networks: Record @@ -60,6 +60,7 @@ export interface SwankyLocalConfig { } export interface SwankySystemConfig { + defaultAccount: string | null; accounts: AccountData[]; networks: Record }