Skip to content

Commit

Permalink
chore: SwankyAccountCommand class created
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 12, 2024
1 parent 54f1672 commit c1f773c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 60 deletions.
6 changes: 4 additions & 2 deletions src/commands/account/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export class Balance extends SwankyCommand<typeof Balance> {
async run(): Promise<void> {
const { args } = await this.parse(Balance);

if(!args.alias) {
throw new InputError("Missing argument! Please provide an alias to get the balance of an account.");
if (!args.alias) {
throw new InputError(
"Missing argument! Please provide an alias account to get the balance from. Example usage: `swanky account balance <YourAliasAccount>`"
);
}

const accountData = this.swankyConfig.accounts.find(
Expand Down
32 changes: 5 additions & 27 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Flags } from "@oclif/core";
import chalk from "chalk";
import { ChainAccount, ChainApi, encrypt, resolveNetworkUrl } from "../../lib/index.js";
import { ChainAccount, encrypt } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError } from "../../lib/errors.js";
import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js";
export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
import { SwankyAccountCommand } from "./swankyAccountCommands.js";

export class CreateAccount extends SwankyAccountCommand<typeof CreateAccount> {
static description = "Create a new dev account in config";

static flags = {
Expand Down Expand Up @@ -87,27 +86,6 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
)} stored to config`
);

const networkUrl = resolveNetworkUrl(this.swankyConfig, "");

const api = (await this.spinner.runCommand(async () => {
const api = await ChainApi.create(networkUrl);
await api.start();
return api;
}, "Connecting to node")) as ChainApi;


await this.spinner.runCommand(
async () => {
try {
await api.faucet(accountData);
} catch (cause) {
throw new ApiError("Error transferring tokens from faucet account", { cause });
}
},
`Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`,
`Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`,
`Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`,
true
);
await this.performFaucetTransfer(accountData, true);
}
}
37 changes: 9 additions & 28 deletions src/commands/account/faucet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Args } from "@oclif/core";
import { ChainApi, resolveNetworkUrl } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError, ConfigError } from "../../lib/errors.js";
import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js";
import { ConfigError } from "../../lib/errors.js";
import { SwankyAccountCommand } from "./swankyAccountCommands.js";

export class Faucet extends SwankyCommand<typeof Faucet> {
export class Faucet extends SwankyAccountCommand<typeof Faucet> {
static description = "Transfer some tokens from faucet to an account";

static aliases = [`account:faucet`];
Expand All @@ -17,36 +15,19 @@ export class Faucet extends SwankyCommand<typeof Faucet> {
description: "Alias of account to be used",
}),
};

async run(): Promise<void> {
const { args } = await this.parse(Faucet);

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

const networkUrl = resolveNetworkUrl(this.swankyConfig, "");

const api = (await this.spinner.runCommand(async () => {
const api = await ChainApi.create(networkUrl);
await api.start();
return api;
}, "Connecting to node")) as ChainApi;
await this.performFaucetTransfer(accountData);
}

await this.spinner.runCommand(
async () => {
try {
await api.faucet(accountData);
} catch (cause) {
throw new ApiError("Error transferring tokens from faucet account", { cause });
}
},
`Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${args.alias}`,
`Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${args.alias}`,
`Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${args.alias}`,
true
);
findAccountByAlias(alias: string): AccountData | undefined {
return this.swankyConfig.accounts.find(account => account.alias === alias);
}
}
45 changes: 45 additions & 0 deletions src/commands/account/swankyAccountCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Command } from "@oclif/core";
import chalk from "chalk";
import { AccountData, ChainApi, resolveNetworkUrl } from "../../index.js";
import { LOCAL_FAUCET_AMOUNT } from "../../lib/consts.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError } from "../../lib/errors.js";

export abstract class SwankyAccountCommand<T extends typeof Command> extends SwankyCommand<T> {
async performFaucetTransfer(accountData: AccountData, canBeSkipped = false) {
let api: ChainApi | null = null;
try {
api = (await this.spinner.runCommand(async () => {
const networkUrl = resolveNetworkUrl(this.swankyConfig, "");
const api = await ChainApi.create(networkUrl);
await api.start();
return api;
}, "Connecting to node")) as ChainApi;

if (api)
await this.spinner.runCommand(
async () => {
if (api) await api.faucet(accountData);
},
`Transferring ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`,
`Transferred ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`,
`Failed to transfer ${LOCAL_FAUCET_AMOUNT} units from faucet account to ${accountData.alias}`,
true
);
} catch (cause) {
if (cause instanceof Error) {
if (cause.message.includes('ECONNREFUSED') && canBeSkipped) {
this.warn(`Unable to connect to the node. Skipping faucet transfer for ${chalk.yellowBright(accountData.alias)}.`);
} else {
throw new ApiError("Error transferring tokens from faucet account", { cause });
}
} else {
throw new ApiError("An unknown error occurred during faucet transfer", { cause: new Error(String(cause)) });
}
} finally {
if (api) {
await api.disconnect();
}
}
}
}
9 changes: 6 additions & 3 deletions src/lib/substrate-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export class ChainApi {
return this._registry;
}

public async disconnect(): Promise<void> {
await this._provider.disconnect();
}

public async start(): Promise<void> {
const chainProperties = await this._api.rpc.system.properties();

Expand Down Expand Up @@ -249,7 +253,7 @@ export class ChainApi {
});
}

public async faucet(accountData: AccountData) : Promise<void> {
public async faucet(accountData: AccountData): Promise<void> {
const keyring = new Keyring({ type: KEYPAIR_TYPE });
const alicePair = keyring.addFromUri(ALICE_URI);

Expand All @@ -267,9 +271,8 @@ export class ChainApi {
return;
}
resolve();
this._provider.disconnect();
}
}).catch(error => reject(error)).finally(() => this._provider.disconnect());
}).catch((error) => reject(error));
});
}
}

0 comments on commit c1f773c

Please sign in to comment.