Skip to content

Commit

Permalink
Merge branch 'ink-devhub-1' into feature/e2e-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas authored Feb 26, 2024
2 parents 8be59c6 + 0914aed commit f29c4bb
Show file tree
Hide file tree
Showing 28 changed files with 2,429 additions and 1,407 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "swanky-env",
"image": "ghcr.io/inkdevhub/swanky-cli/swanky-base:swanky3.1.0-beta.0_v2.1.1",

"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2.8.0": {}
},
// Mount the workspace volume
"mounts": ["source=${localWorkspaceFolder},target=/workspaces,type=bind,consistency=cached"],
"workspaceFolder": "/workspaces",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ A newly generated project will have a `swanky.config.json` file that will get po
"node": {
"localPath": "/Users/sasapul/Work/astar/swanky-cli/temp_proj/bin/swanky-node",
"polkadotPalletVersions": "polkadot-v0.9.39",
"supportedInk": "v4.2.0"
"supportedInk": "v4.3.0"
},
"accounts": [
{
Expand Down
12 changes: 6 additions & 6 deletions base-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ RUN curl -L https://github.com/swankyhub/swanky-cli/releases/download/v3.1.0-bet
# Install Rustup and Rust, additional components, packages, and verify the installations
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
/bin/bash -c "source $HOME/.cargo/env && \
rustup toolchain install nightly-2023-03-05 && \
rustup default nightly-2023-03-05 && \
rustup component add rust-src --toolchain nightly-2023-03-05 && \
rustup target add wasm32-unknown-unknown --toolchain nightly-2023-03-05 && \
cargo +stable install cargo-dylint dylint-link && \
cargo +stable install cargo-contract --force --version 4.0.0-alpha && \
rustup install 1.72 && \
rustup default 1.72 && \
rustup component add rust-src && \
rustup target add wasm32-unknown-unknown && \
cargo install cargo-dylint dylint-link && \
cargo install cargo-contract --version 4.0.0-rc.1 && \
rustc --version"

# Install Yarn 1.x
Expand Down
63 changes: 63 additions & 0 deletions src/commands/account/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 { SwankyCommand } from "../../lib/swankyCommand.js";
import { InputError } from "../../lib/errors.js";
import { formatBalance } from "@polkadot/util";

export class Balance extends SwankyCommand<typeof Balance> {
static description = "Balance of an account";

static args = {
alias: Args.string({
name: "alias",
description: "Alias of account to be used",
}),
};
async run(): Promise<void> {
const { args } = await this.parse(Balance);

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.findAccountByAlias(args.alias);
const networkUrl = resolveNetworkUrl(this.swankyConfig, "");

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

const decimals = api.registry.chainDecimals[0];
formatBalance.setDefaults({ unit: "UNIT", decimals });

const { nonce, data: balance } = await api.query.system.account<AccountInfo>(
accountData.address
);
const { free, reserved, miscFrozen, feeFrozen } = balance;

let frozen: BalanceType;
if (feeFrozen.gt(miscFrozen)) {
frozen = feeFrozen;
} else {
frozen = miscFrozen;
}

const transferrableBalance = free.sub(frozen);
const totalBalance = free.add(reserved);

console.log("Transferrable Balance:", formatBalance(transferrableBalance));
if (!transferrableBalance.eq(totalBalance)) {
console.log("Total Balance:", formatBalance(totalBalance));
console.log("Raw Balances:", balance.toHuman());
}
console.log("Account Nonce:", nonce.toHuman());

await api.disconnect();
}
}
9 changes: 6 additions & 3 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import chalk from "chalk";
import { ChainAccount, encrypt } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.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 @@ -35,7 +36,7 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
);
}

let tmpMnemonic = "";
let tmpMnemonic: string;
if (flags.generate) {
tmpMnemonic = ChainAccount.generate();
console.log(
Expand Down Expand Up @@ -84,5 +85,7 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
accountData.alias
)} stored to config`
);

await this.performFaucetTransfer(accountData, true);
}
}
23 changes: 23 additions & 0 deletions src/commands/account/faucet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Args } from "@oclif/core";
import { SwankyAccountCommand } from "./swankyAccountCommands.js";

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

static aliases = [`account:faucet`];

static args = {
alias: Args.string({
name: "alias",
required: true,
description: "Alias of account to be used",
}),
};

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

const accountData = this.findAccountByAlias(args.alias);
await this.performFaucetTransfer(accountData);
}
}
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();
}
}
}
}
Loading

0 comments on commit f29c4bb

Please sign in to comment.