Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faucet feature for SwankyNode #192

Merged
merged 11 commits into from
Feb 12, 2024
40 changes: 40 additions & 0 deletions src/commands/account/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Args } from "@oclif/core";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { resolveNetworkUrl } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError } from "../../lib/errors.js";

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

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

const wsProvider = new WsProvider(networkUrl);
const api = await ApiPromise.create({ provider: wsProvider });
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
await api.isReady;

const balance = (await api.query.system.account(accountData.address)).data.free.toBigInt();
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved

this.log(`Account balance now is: ${balance}`);

await wsProvider.disconnect();
}
}
16 changes: 14 additions & 2 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flags } from "@oclif/core";
import chalk from "chalk";
import { ChainAccount, encrypt } from "../../lib/index.js";
import { ChainAccount, ChainApi, encrypt, resolveNetworkUrl } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
Expand Down Expand Up @@ -35,7 +35,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 +84,17 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
accountData.alias
)} 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 () => {await api.faucet(accountData)}
, `Fauceting 100000000000000000000 units to ${accountData.alias}`);
}
pmikolajczyk41 marked this conversation as resolved.
Show resolved Hide resolved
}
41 changes: 41 additions & 0 deletions src/commands/account/faucet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 { ConfigError } from "../../lib/errors.js";

export class Faucet extends SwankyCommand<typeof Faucet> {
static description = "Faucet some tokens to an account";
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved

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.swankyConfig.accounts.find(
(account: AccountData) => account.alias === 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.spinner.runCommand( async () => {await api.faucet(accountData)}
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
, `Fauceting 100000000000000000000 units to ${args.alias}`);
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
}
}
16 changes: 13 additions & 3 deletions src/lib/substrate-api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ApiPromise } from "@polkadot/api/promise";
import { WsProvider } from "@polkadot/api";
import { Keyring, WsProvider } from "@polkadot/api";
import { SignerOptions } from "@polkadot/api/types";
import { Codec, ITuple } from "@polkadot/types-codec/types";
import { ISubmittableResult } from "@polkadot/types/types";
import { TypeRegistry } from "@polkadot/types";
import { DispatchError, BlockHash } from "@polkadot/types/interfaces";
import { ChainAccount } from "./account.js";
import BN from "bn.js";
import { ChainProperty, ExtrinsicPayload } from "../types/index.js";
import { ChainProperty, ExtrinsicPayload, AccountData } from "../types/index.js";

import { KeyringPair } from "@polkadot/keyring/types";
import { Abi, CodePromise } from "@polkadot/api-contract";
Expand Down Expand Up @@ -210,7 +210,6 @@ export class ChainApi {
if (handler) handler(result);
});
}

public async deploy(
abi: Abi,
wasm: Buffer,
Expand Down Expand Up @@ -247,4 +246,15 @@ export class ChainApi {
});
});
}

public async faucet(accountData: AccountData) {
const keyring = new Keyring({ type: "sr25519" });
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
const alicePair = keyring.addFromUri("//Alice");

await this._api.tx.balances
.transfer(accountData.address, BigInt(100000000000000000000n))
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
.signAndSend(alicePair);
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved

await this._provider.disconnect();
}
}
Loading