Skip to content

Commit

Permalink
Fix default account feature
Browse files Browse the repository at this point in the history
  • Loading branch information
prxgr4mm3r committed Jan 2, 2024
1 parent 356ad63 commit 9b264a7
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 61 deletions.
4 changes: 3 additions & 1 deletion src/commands/account/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
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,
Expand All @@ -77,6 +76,9 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
}

this.swankyConfig.accounts.push(accountData);
if(this.swankyConfig.defaultAccount === null) {
this.swankyConfig.defaultAccount = accountData.alias;
}
await this.storeSystemConfig();

this.log(
Expand Down
23 changes: 4 additions & 19 deletions src/commands/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
async run(): Promise<void> {
const { args } = await this.parse(DefaultAccount);

let defaultAccount = "";

if(args.accountAlias) {
const accountData = this.swankyConfig.accounts.find(
(account: AccountData) => account.alias === args.accountAlias
);
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",
Expand All @@ -42,23 +40,10 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
}),
},
]).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)}`));
}
}
26 changes: 18 additions & 8 deletions src/commands/account/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ export class ListAccounts extends SwankyCommand<typeof ListAccounts> {
static aliases = [`account:ls`];

async run(): Promise<void> {
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") : ""}`);
}
}
}
}
Expand Down
20 changes: 4 additions & 16 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
);
}

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
Expand All @@ -87,21 +87,9 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
);
}

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();
}

Expand Down
8 changes: 2 additions & 6 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,36 +175,32 @@ export class Init extends SwankyCommand<typeof Init> {
{
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));
});

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();
},
Expand Down
14 changes: 12 additions & 2 deletions src/lib/contractCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@ export abstract class ContractCall<T extends typeof Command> 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 ?? "");
Expand Down
11 changes: 4 additions & 7 deletions src/lib/swankyCommand.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -18,7 +18,6 @@ export abstract class SwankyCommand<T extends typeof Command> extends Command {
protected spinner!: Spinner;
protected swankyConfig!: SwankyConfig;
protected logger!: Logger;
protected defaultAccount!: string;

protected flags!: Flags<T>;
protected args!: Args<T>;
Expand All @@ -44,6 +43,7 @@ export abstract class SwankyCommand<T extends typeof Command> extends Command {
supportedInk: "",
},
contracts: {},
defaultAccount: null,
accounts: [],
networks: {},
};
Expand All @@ -68,10 +68,6 @@ export abstract class SwankyCommand<T extends typeof Command> 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)}
Expand All @@ -88,6 +84,7 @@ export abstract class SwankyCommand<T extends typeof Command> extends Command {

protected async storeSystemConfig() {
const systemConfig : SwankySystemConfig = {
defaultAccount: this.swankyConfig.defaultAccount,
accounts: this.swankyConfig.accounts,
networks: this.swankyConfig.networks
}
Expand Down
5 changes: 3 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +44,7 @@ export interface SwankyConfig {
localPath: string;
supportedInk: string;
};
defaultAccount: string | null;
accounts: AccountData[];
contracts: Record<string, ContractData> | Record<string, never>;
networks: Record<string, {url: string}>
Expand All @@ -60,6 +60,7 @@ export interface SwankyLocalConfig {
}

export interface SwankySystemConfig {
defaultAccount: string | null;
accounts: AccountData[];
networks: Record<string, {url: string}>
}
Expand Down

0 comments on commit 9b264a7

Please sign in to comment.