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

Improved config feature #198

Merged
merged 36 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d4a9163
Update SwankyConfig, make local and system configs
prxgr4mm3r Dec 29, 2023
cb80429
Clean up imports
prxgr4mm3r Dec 29, 2023
356ad63
Clean up imports
prxgr4mm3r Dec 29, 2023
9b264a7
Fix default account feature
prxgr4mm3r Jan 2, 2024
b70851d
Fixes
prxgr4mm3r Jan 24, 2024
355eb39
fix: Swanky config pre-populated in context to remove type assertions
ipapandinas Jan 25, 2024
2e3da70
fix: config update logic fixed for account create & default command
ipapandinas Jan 25, 2024
6007e31
fix: isLocalConfigCheck fixed
ipapandinas Jan 25, 2024
39bf8fa
fix: Allow some commands to be used without local config
ipapandinas Jan 25, 2024
5cff10e
fix: Wait for config store async functions
ipapandinas Jan 25, 2024
b8b947f
fix: isLocalConfigCheck fixed
ipapandinas Jan 26, 2024
e4fffc7
fix: Make config name dynamic
prxgr4mm3r Jan 26, 2024
fc0a000
fix: Make config name dynamic
prxgr4mm3r Jan 26, 2024
00ea4c5
Merge remote-tracking branch 'origin/feature/update-swanky-config' in…
prxgr4mm3r Jan 26, 2024
ede09f6
fix: Add alice and bob to accountsby default
prxgr4mm3r Jan 26, 2024
1c95829
fix: Display account alias in Errors
prxgr4mm3r Jan 26, 2024
7a42fd3
fix: Remove useless warning
prxgr4mm3r Jan 26, 2024
30000e8
fix: Change config merging
prxgr4mm3r Jan 26, 2024
b96ebfa
fix: Fix merging accounts created outside of project
prxgr4mm3r Jan 26, 2024
bb5f10b
fix: Fix mismatched versions displaying
prxgr4mm3r Jan 26, 2024
ebde7ad
fix: Remove useless debug output
prxgr4mm3r Jan 26, 2024
ba681bc
fix: Fix dev account check in local network
prxgr4mm3r Jan 26, 2024
15a0330
fix: Add check for node binary existence
prxgr4mm3r Jan 26, 2024
1ab38dd
fix: Correct account alias used for deploy and tx commands
ipapandinas Jan 30, 2024
9a25dd8
fix: Fix default command
prxgr4mm3r Jan 30, 2024
f9a604c
fix: Correct work when env SWANKY_CONFIG variable is set
prxgr4mm3r Jan 30, 2024
3a70313
feat: (WIP) Config builder created
ipapandinas Jan 30, 2024
64cda21
Merge remote-tracking branch 'origin/exploring-config-builder' into f…
prxgr4mm3r Jan 31, 2024
fe99bb1
fix: Correct use of new store config func
prxgr4mm3r Jan 31, 2024
93d41d6
fix: Use ConfigBuilder to change SwankyConfig
prxgr4mm3r Jan 31, 2024
4708ab4
fix: refactoring
ipapandinas Feb 1, 2024
78e0208
fix: Fix account storing
prxgr4mm3r Feb 2, 2024
01b7cf6
feat: addContractDeployment methode added to config builder
ipapandinas Feb 5, 2024
6a574a0
chore: chain configuration into a single builder expression
ipapandinas Feb 26, 2024
8ec01e2
Merge branch 'ink-devhub-1' into feature/update-swanky-config
ipapandinas Feb 26, 2024
563c96e
fix: config update for contract build verification
ipapandinas Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Flags } from "@oclif/core";
import chalk from "chalk";
import { ChainAccount, encrypt } from "../../lib/index.js";
import { ChainAccount, encrypt, isLocalConfigCheck } 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> {
static description = "Create a new dev account in config";

static flags = {
global: Flags.boolean({
description: "Create account globally",
}),
generate: Flags.boolean({
char: "g",
}),
Expand Down Expand Up @@ -76,8 +79,18 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
}

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

await this.storeConfig();
if(flags.global) {
await this.storeSystemConfig();
}
else if(isLocalConfigCheck()) {
await this.storeConfig(process.cwd());
} else {
throw new Error("Cannot store account to config. Please run this command in a swanky project directory");
}

this.log(
`${chalk.greenBright("✔")} Account with alias ${chalk.yellowBright(
Expand Down
66 changes: 66 additions & 0 deletions src/commands/account/default.ts
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Args, Flags } from "@oclif/core";
import chalk from "chalk";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError } from "../../lib/errors.js";
import { isLocalConfigCheck } from "../../lib/index.js";
export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
static description = "Set default account to use";

static flags = {
global: Flags.boolean({
char: "g",
description: "Set default account globally",
}),
}

static args = {
accountAlias: Args.string({
name: "accountAlias",
required: false,
description: "Alias of account to be used as default",
}),
};

async run(): Promise<void> {
const { args, flags } = await this.parse(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");
}
this.swankyConfig.defaultAccount = accountData.alias;
} else {
await inquirer.prompt([
{
type: "list",
name: "defaultAccount",
message: "Select default account",
choices: this.swankyConfig.accounts.map((account: AccountData) => {
return {
name: `${account.alias} (${account.address})`,
value: account.alias,
};
}),
},
]).then((answers) => {
this.swankyConfig.defaultAccount = answers.defaultAccount;
});
}

if(flags.global) {
await this.storeSystemConfig();
}
else if(isLocalConfigCheck()) {
await this.storeConfig(process.cwd());
} else {
throw new Error("Cannot store account to config. Please run this command in a swanky project directory");
}

console.log(chalk.greenBright(`Default account set to ${chalk.yellowBright(this.swankyConfig.defaultAccount)}`));
}
}
26 changes: 23 additions & 3 deletions src/commands/account/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@ 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) {
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") : ""}`);
}
}
}

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} \
${chalk.yellowBright("Address: ")} ${account.address} ${this.swankyConfig.defaultAccount === account.alias ? chalk.greenBright("<- Default") : ""}`);
}
}
}
}
}
23 changes: 17 additions & 6 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Listr } from "listr2";
import { commandStdoutOrNull } from "../../lib/index.js";
import { commandStdoutOrNull, isLocalConfigCheck } from "../../lib/index.js";
import { SwankyConfig } from "../../types/index.js";
import { pathExistsSync, readJSON } from "fs-extra/esm";
import { pathExistsSync} from "fs-extra/esm";
import { readFileSync } from "fs";
import path from "node:path";
import TOML from "@iarna/toml";
import semver from "semver";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { FileError } from "../../lib/errors.js";

interface Ctx {
versions: {
Expand Down Expand Up @@ -59,13 +60,21 @@ export default class Check extends SwankyCommand<typeof Check> {
ctx.versions.tools.cargoContract = await commandStdoutOrNull("cargo contract -V");
},
},
{
title: "Check Swanky Config",
task: async (ctx) => {
if (this.swankyConfig == undefined){
throw new FileError("Swanky config not found")
}
ctx.swankyConfig = this.swankyConfig;
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
}
},
{
title: "Read ink dependencies",
enabled: isLocalConfigCheck(),
skip: (ctx) => ctx.swankyConfig == undefined || Object.keys(ctx.swankyConfig.contracts).length == 0,
task: async (ctx) => {
const swankyConfig = await readJSON("swanky.config.json");
ctx.swankyConfig = swankyConfig;

for (const contract in swankyConfig.contracts) {
for (const contract in ctx.swankyConfig!.contracts) {
const tomlPath = path.resolve(`contracts/${contract}/Cargo.toml`);
const doesCargoTomlExist = pathExistsSync(tomlPath);
if (!doesCargoTomlExist) {
Expand All @@ -90,6 +99,8 @@ export default class Check extends SwankyCommand<typeof Check> {
},
{
title: "Verify ink version",
enabled: isLocalConfigCheck(),
skip: (ctx) => ctx.swankyConfig == undefined || Object.keys(ctx.swankyConfig.contracts).length == 0,
task: async (ctx) => {
const supportedInk = ctx.swankyConfig?.node.supportedInk;

Expand Down
32 changes: 24 additions & 8 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Args, Flags } from "@oclif/core";
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 { resolveNetworkUrl, ChainApi, ChainAccount, decrypt, AbiType, ensureAccountIsSet } from "../../lib/index.js";
import { AccountData, Encrypted } from "../../types/index.js";
import inquirer from "inquirer";
import chalk from "chalk";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError, ConfigError, FileError } from "../../lib/errors.js";
import { DEFAULT_ACCOUNT } from "../../lib/consts.js";

export class DeployContract extends SwankyCommand<typeof DeployContract> {
static description = "Deploy contract to a running node";

static flags = {
account: Flags.string({
required: true,
default: DEFAULT_ACCOUNT,
description: "Alias of account to be used",
}),
gas: Flags.integer({
Expand All @@ -32,6 +31,7 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
}),
network: Flags.string({
char: "n",
default: "local",
description: "Network name to connect to",
}),
};
Expand All @@ -47,6 +47,8 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
async run(): Promise<void> {
const { args, flags } = await this.parse(DeployContract);

console.log("flags", flags);

const contractRecord = this.swankyConfig.contracts[args.contractName];
if (!contractRecord) {
throw new ConfigError(
Expand All @@ -70,13 +72,29 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
);
}

ensureAccountIsSet(flags.account, this.swankyConfig);

const accountAlias = flags.account ?? this.swankyConfig.defaultAccount;
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved

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

if (accountData.isDev && flags.network !== "local") {
throw new ConfigError(
`Account ${accountAlias} is a DEV account and can only be used with local network`
);
}

if(this.swankyConfig.defaultAccount === null)
{
this.swankyConfig.defaultAccount = accountAlias;
await this.storeSystemConfig();
}

const mnemonic = accountData.isDev
? (accountData.mnemonic as string)
: decrypt(
Expand Down Expand Up @@ -138,9 +156,7 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
},
];

await writeJSON(path.resolve("swanky.config.json"), this.swankyConfig, {
spaces: 2,
});
await this.storeConfig(process.cwd());
}, "Writing config");

this.log(`Contract deployed!`);
Expand Down
5 changes: 2 additions & 3 deletions src/commands/contract/new.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Args, Flags } from "@oclif/core";
import path from "node:path";
import { ensureDir, pathExists, writeJSON } from "fs-extra/esm";
import { ensureDir, pathExists } from "fs-extra/esm";
import {
checkCliDependencies,
copyContractTemplateFiles,
Expand Down Expand Up @@ -102,8 +102,7 @@ export class NewContract extends SwankyCommand<typeof NewContract> {
deployments: [],
};

await writeJSON(path.resolve("swanky.config.json"), this.swankyConfig, { spaces: 2 });
}, "Writing config");
await this.storeConfig(process.cwd())}, "Writing config");

this.log("😎 New contract successfully generated! 😎");
}
Expand Down
Loading
Loading