Skip to content

Commit

Permalink
chore: findAccountByAlias created
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 12, 2024
1 parent c1f773c commit 449f834
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
11 changes: 2 additions & 9 deletions src/commands/account/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Args } from "@oclif/core";
import { ApiPromise } from "@polkadot/api";
import type { AccountInfo, Balance as BalanceType } from "@polkadot/types/interfaces";
import { ChainApi, resolveNetworkUrl } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, InputError } from "../../lib/errors.js";
import { InputError } from "../../lib/errors.js";
import { formatBalance } from "@polkadot/util";

export class Balance extends SwankyCommand<typeof Balance> {
Expand All @@ -25,13 +24,7 @@ export class Balance extends SwankyCommand<typeof Balance> {
);
}

const accountData = this.swankyConfig.accounts.find(
(account: AccountData) => account.alias === args.alias
);
if (!accountData) {
throw new ConfigError("Provided account alias not found in swanky.config.json");
}

const accountData = this.findAccountByAlias(args.alias);
const networkUrl = resolveNetworkUrl(this.swankyConfig, "");

const api = (await this.spinner.runCommand(async () => {
Expand Down
10 changes: 0 additions & 10 deletions src/commands/account/faucet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Args } from "@oclif/core";
import { AccountData } from "../../types/index.js";
import { ConfigError } from "../../lib/errors.js";
import { SwankyAccountCommand } from "./swankyAccountCommands.js";

export class Faucet extends SwankyAccountCommand<typeof Faucet> {
Expand All @@ -20,14 +18,6 @@ export class Faucet extends SwankyAccountCommand<typeof Faucet> {
const { args } = await this.parse(Faucet);

const accountData = this.findAccountByAlias(args.alias);
if (!accountData) {
throw new ConfigError("Provided account alias not found in swanky.config.json");
}

await this.performFaucetTransfer(accountData);
}

findAccountByAlias(alias: string): AccountData | undefined {
return this.swankyConfig.accounts.find(account => account.alias === alias);
}
}
10 changes: 2 additions & 8 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "node:path";
import { writeJSON } from "fs-extra/esm";
import { cryptoWaitReady } from "@polkadot/util-crypto/crypto";
import { resolveNetworkUrl, ChainApi, ChainAccount, decrypt, AbiType } from "../../lib/index.js";
import { AccountData, Encrypted } from "../../types/index.js";
import { Encrypted } from "../../types/index.js";
import inquirer from "inquirer";
import chalk from "chalk";
import { Contract } from "../../lib/contract.js";
Expand Down Expand Up @@ -70,13 +70,7 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
);
}

const accountData = this.swankyConfig.accounts.find(
(account: AccountData) => account.alias === flags.account
);
if (!accountData) {
throw new ConfigError("Provided account alias not found in swanky.config.json");
}

const accountData = this.findAccountByAlias(flags.account);
const mnemonic = accountData.isDev
? (accountData.mnemonic as string)
: decrypt(
Expand Down
10 changes: 2 additions & 8 deletions src/lib/contractCall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbiType, ChainAccount, ChainApi, decrypt, resolveNetworkUrl } from "./index.js";
import { AccountData, ContractData, DeploymentData, Encrypted } from "../types/index.js";
import { ContractData, DeploymentData, Encrypted } from "../types/index.js";
import { Args, Command, Flags, Interfaces } from "@oclif/core";
import inquirer from "inquirer";
import chalk from "chalk";
Expand Down Expand Up @@ -77,13 +77,7 @@ export abstract class ContractCall<T extends typeof Command> extends SwankyComma

this.deploymentInfo = deploymentData;

const accountData = this.swankyConfig.accounts.find(
(account: AccountData) => account.alias === flags.account || "alice"
);
if (!accountData) {
throw new ConfigError("Provided account alias not found in swanky.config.json");
}

const accountData = this.findAccountByAlias(flags.account || "alice");
const networkUrl = resolveNetworkUrl(this.swankyConfig, flags.network ?? "");
const api = await ChainApi.create(networkUrl);
this.api = api;
Expand Down
15 changes: 14 additions & 1 deletion src/lib/swankyCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, Flags, Interfaces } from "@oclif/core";
import chalk from "chalk";
import { getSwankyConfig, Spinner } from "./index.js";
import { SwankyConfig } from "../types/index.js";
import { AccountData, SwankyConfig } from "../types/index.js";
import { writeJSON } from "fs-extra/esm";
import { BaseError, ConfigError, UnknownError } from "./errors.js";
import { swankyLogger } from "./logger.js";
Expand Down Expand Up @@ -51,6 +52,18 @@ export abstract class SwankyCommand<T extends typeof Command> extends Command {
Full command: ${JSON.stringify(process.argv)}`);
}

protected findAccountByAlias(alias: string): AccountData {
const accountData = this.swankyConfig.accounts.find(
(account: AccountData) => account.alias === alias
);

if (!accountData) {
throw new ConfigError(`Provided account alias ${chalk.yellowBright(alias)} not found in swanky.config.json`);
}

return accountData;
}

protected async storeConfig() {
await writeJSON("swanky.config.json", this.swankyConfig, { spaces: 2 });
}
Expand Down

0 comments on commit 449f834

Please sign in to comment.