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

Add swanky node version or equivalent #191

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
7 changes: 7 additions & 0 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Ctx {
cargoContract?: string | null;
};
contracts: Record<string, Record<string, string>>;
node?: string | null;
};
swankyConfig?: SwankyConfig;
mismatchedVersions?: Record<string, string>;
Expand Down Expand Up @@ -59,6 +60,12 @@ export default class Check extends SwankyCommand<typeof Check> {
ctx.versions.tools.cargoContract = await commandStdoutOrNull("cargo contract -V");
},
},
{
title: "Check swanky node",
task: async (ctx) => {
ctx.versions.node = this.swankyConfig.node.version !== "" ? this.swankyConfig.node.version : null;
},
},
{
title: "Read ink dependencies",
task: async (ctx) => {
Expand Down
35 changes: 26 additions & 9 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { execaCommand, execaCommandSync } from "execa";
import { paramCase, pascalCase, snakeCase } from "change-case";
import inquirer from "inquirer";
import TOML from "@iarna/toml";
import { choice, email, name, pickTemplate } from "../../lib/prompts.js";
import { choice, email, name, pickNodeVersion, pickTemplate } from "../../lib/prompts.js";
import {
checkCliDependencies,
copyCommonTemplateFiles,
Expand All @@ -15,12 +15,11 @@ import {
installDeps,
ChainAccount,
processTemplates,
swankyNode,
getTemplates,
getTemplates, swankyNodeVersions,
} from "../../lib/index.js";
import {
DEFAULT_ASTAR_NETWORK_URL,
DEFAULT_NETWORK_URL,
DEFAULT_NETWORK_URL, DEFAULT_NODE_INFO,
DEFAULT_SHIBUYA_NETWORK_URL,
DEFAULT_SHIDEN_NETWORK_URL,
} from "../../lib/consts.js";
Expand Down Expand Up @@ -93,11 +92,13 @@ export class Init extends SwankyCommand<typeof Init> {
}
projectPath = "";


configBuilder: Partial<SwankyConfig> = {
node: {
localPath: "",
polkadotPalletVersions: swankyNode.polkadotPalletVersions,
supportedInk: swankyNode.supportedInk,
polkadotPalletVersions: "",
supportedInk: "",
version: "",
},
accounts: [],
networks: {
Expand Down Expand Up @@ -161,12 +162,28 @@ export class Init extends SwankyCommand<typeof Init> {
choice("useSwankyNode", "Do you want to download Swanky node?"),
]);
if (useSwankyNode) {
const versions = Array.from(swankyNodeVersions.keys());
let nodeVersion = DEFAULT_NODE_INFO.version;
await inquirer.prompt([
pickNodeVersion(versions),
]).then((answers) => {
nodeVersion = answers.version;
});

const nodeInfo = swankyNodeVersions.get(nodeVersion)!;

this.taskQueue.push({
task: downloadNode,
args: [this.projectPath, swankyNode, this.spinner],
args: [this.projectPath, nodeInfo, this.spinner],
runningMessage: "Downloading Swanky node",
callback: (result) =>
this.configBuilder.node ? (this.configBuilder.node.localPath = result) : null,
callback: (result) => {
this.configBuilder.node = {
supportedInk: nodeInfo.supportedInk,
polkadotPalletVersions: nodeInfo.polkadotPalletVersions,
version: nodeInfo.version,
localPath: result,
};
}
});
}
}
Expand Down
39 changes: 34 additions & 5 deletions src/commands/node/install.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ux } from "@oclif/core";
import { downloadNode, swankyNode } from "../../lib/index.js";
import { ux, Flags } from "@oclif/core";
import { downloadNode, swankyNodeVersions } from "../../lib/index.js";
import path from "node:path";
import { writeJSON } from "fs-extra/esm";
import inquirer from "inquirer";
import { DEFAULT_NODE_INFO } from "../../lib/consts.js";
import { pickNodeVersion } from "../../lib/prompts.js";
import { InputError } from "../../lib/errors.js";

export class InstallNode extends SwankyCommand<typeof InstallNode> {
static description = "Install swanky node binary";

static flags = {
"set-version": Flags.string({
description: "Specify version of swanky node to install. \n List of supported versions: " + Array.from(swankyNodeVersions.keys()).join(", "),
required: false,
}),
}
async run(): Promise<void> {
const { flags } = await this.parse(InstallNode);
if (flags.verbose) {
this.spinner.verbose = true;
}
let nodeVersion= DEFAULT_NODE_INFO.version;

if (flags["set-version"]) {
nodeVersion = flags["set-version"];
if(!swankyNodeVersions.has(nodeVersion)) {
throw new InputError(`Version ${nodeVersion} is not supported.\n List of supported versions: ${Array.from(swankyNodeVersions.keys()).join(", ")}`);
}
} else {
const versions = Array.from(swankyNodeVersions.keys());
await inquirer.prompt([
pickNodeVersion(versions),
]).then((answers) => {
nodeVersion = answers.version;
});
}

const projectPath = path.resolve();

Expand All @@ -24,16 +49,20 @@ export class InstallNode extends SwankyCommand<typeof InstallNode> {
}
}

const nodeInfo = swankyNodeVersions.get(nodeVersion)!;

const taskResult = (await this.spinner.runCommand(
() => downloadNode(projectPath, swankyNode, this.spinner),
() => downloadNode(projectPath, nodeInfo, this.spinner),
"Downloading Swanky node"
)) as string;
const nodePath = path.relative(projectPath, taskResult);


this.swankyConfig.node = {
localPath: nodePath,
polkadotPalletVersions: swankyNode.polkadotPalletVersions,
supportedInk: swankyNode.supportedInk,
polkadotPalletVersions: nodeInfo.polkadotPalletVersions,
supportedInk: nodeInfo.supportedInk,
version: nodeInfo.version,
};

await this.spinner.runCommand(
Expand Down
7 changes: 6 additions & 1 deletion src/commands/node/start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flags } from "@oclif/core";
import { execaCommand } from "execa";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import semver from "semver";
export class StartNode extends SwankyCommand<typeof StartNode> {
static description = "Start a local node";

Expand Down Expand Up @@ -28,11 +29,15 @@ export class StartNode extends SwankyCommand<typeof StartNode> {
async run(): Promise<void> {
const { flags } = await this.parse(StartNode);

if (this.swankyConfig.node.version === "") {
this.log("Node is not installed");
return;
}
// Run persistent mode by default. non-persistent mode in case flag is provided.
// Non-Persistent mode (`--dev`) allows all CORS origin, without `--dev`, users need to specify origins by `--rpc-cors`.
await execaCommand(
`${this.swankyConfig.node.localPath} \
--finalize-delay-sec ${flags.finalizeDelaySec} \
${ semver.gt(this.swankyConfig.node.version, "1.6.0") ? `--finalize-delay-sec ${flags.finalizeDelaySec}` : ""} \
ipapandinas marked this conversation as resolved.
Show resolved Hide resolved
${flags.tmp ? "--dev" : `--rpc-cors ${flags.rpcCors}`}`,
{
stdio: "inherit",
Expand Down
12 changes: 12 additions & 0 deletions src/commands/node/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SwankyCommand } from "../../lib/swankyCommand.js";
export class NodeVersion extends SwankyCommand<typeof NodeVersion> {
static description = "Show swanky node version";
async run(): Promise<void> {
if(this.swankyConfig.node.version === ""){
this.log("Swanky node is not installed");
}
else {
this.log(`Swanky node version: ${this.swankyConfig.node.version}`);
}
}
}
4 changes: 4 additions & 0 deletions src/lib/consts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { swankyNodeVersions } from "./nodeInfo.js";

export const DEFAULT_NETWORK_URL = "ws://127.0.0.1:9944";
export const DEFAULT_ASTAR_NETWORK_URL = "wss://rpc.astar.network";
export const DEFAULT_SHIDEN_NETWORK_URL = "wss://rpc.shiden.astar.network";
export const DEFAULT_SHIBUYA_NETWORK_URL = "wss://shibuya.public.blastapi.io";

export const ARTIFACTS_PATH = "artifacts";
export const TYPED_CONTRACTS_PATH = "typedContracts";

export const DEFAULT_NODE_INFO = swankyNodeVersions.get("1.6.0")!;
132 changes: 119 additions & 13 deletions src/lib/nodeInfo.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,123 @@
export type nodeInfo = typeof swankyNode;

export const swankyNode = {
version: "1.6.0",
polkadotPalletVersions: "polkadot-v0.9.39",
supportedInk: "v4.2.0",
export interface nodeInfo {
version: string;
polkadotPalletVersions: string;
supportedInk: string;
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-macOS-universal.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-macOS-universal.tar.gz"
},
"arm64"?: string;
"x64"?: string;
};
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-ubuntu-aarch64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-ubuntu-x86_64.tar.gz",
"arm64"?: string;
"x64"?: string;
};
};
}

export const swankyNodeVersions = new Map<string, nodeInfo>([
["1.6.0", {
version: "1.6.0",
polkadotPalletVersions: "polkadot-v0.9.39",
supportedInk: "v4.2.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-macOS-universal.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-macOS-universal.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-ubuntu-aarch64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.6.0/swanky-node-v1.6.0-ubuntu-x86_64.tar.gz",
}
}
}],
["1.5.0", {
version: "1.5.0",
polkadotPalletVersions: "polkadot-v0.9.39",
supportedInk: "v4.0.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.5.0/swanky-node-v1.5.0-macOS-universal.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.5.0/swanky-node-v1.5.0-macOS-universal.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.5.0/swanky-node-v1.5.0-ubuntu-aarch64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.5.0/swanky-node-v1.5.0-ubuntu-x86_64.tar.gz",
}
}
}],
["1.4.0", {
version: "1.4.0",
polkadotPalletVersions: "polkadot-v0.9.37",
supportedInk: "v4.0.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.4.0/swanky-node-v1.4.0-macOS-universal.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.4.0/swanky-node-v1.4.0-macOS-universal.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.4.0/swanky-node-v1.4.0-ubuntu-aarch64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.4.0/swanky-node-v1.4.0-ubuntu-x86_64.tar.gz",
}
}
}],
["1.3.0", {
version: "1.3.0",
polkadotPalletVersions: "polkadot-v0.9.37",
supportedInk: "v4.0.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.3.0/swanky-node-v1.3.0-macOS-universal.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.3.0/swanky-node-v1.3.0-macOS-universal.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.3.0/swanky-node-v1.3.0-ubuntu-aarch64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.3.0/swanky-node-v1.3.0-ubuntu-x86_64.tar.gz",
}
}
}],
["1.2.0", {
version: "1.2.0",
polkadotPalletVersions: "polkadot-v0.9.37",
supportedInk: "v4.0.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.2.0/swanky-node-v1.2.0-macOS-universal.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.2.0/swanky-node-v1.2.0-macOS-universal.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.2.0/swanky-node-v1.2.0-ubuntu-aarch64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.2.0/swanky-node-v1.2.0-ubuntu-x86_64.tar.gz",
}
}
}],
["1.1.0", {
version: "1.1.0",
polkadotPalletVersions: "polkadot-v0.9.37",
supportedInk: "v4.0.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.1.0/swanky-node-v1.1.0-macOS-x86_64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.1.0/swanky-node-v1.1.0-macOS-x86_64.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.1.0/swanky-node-v1.1.0-ubuntu-x86_64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.1.0/swanky-node-v1.1.0-ubuntu-x86_64.tar.gz",
}
}
}],
["1.0.0", {
version: "1.0.0",
polkadotPalletVersions: "polkadot-v0.9.30",
supportedInk: "v3.4.0",
downloadUrl: {
darwin: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.0.0/swanky-node-v1.0.0-macOS-x86_64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.0.0/swanky-node-v1.0.0-macOS-x86_64.tar.gz"
},
linux: {
"arm64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.0.0/swanky-node-v1.0.0-ubuntu-x86_64.tar.gz",
"x64": "https://github.com/AstarNetwork/swanky-node/releases/download/v1.0.0/swanky-node-v1.0.0-ubuntu-x86_64.tar.gz",
}
}
},
};
}]
]);
10 changes: 10 additions & 0 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export function pickTemplate(templateList: string[]): ListQuestion<Answers> {
};
}

export function pickNodeVersion(nodeVersions: string[]): ListQuestion<Answers> {
if (!nodeVersions?.length) throw new ConfigError("Node version list is empty!");
return {
name: "version",
type: "list",
choices: nodeVersions,
message: "Which node version should we use?",
};
}

export function name(
subject: string,
initial?: (answers: Answers) => string,
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ChainProperty {

export type ExtrinsicPayload = SubmittableExtrinsic<"promise">;

export interface Encrypted { iv: string; data: string };
export interface Encrypted { iv: string; data: string }

export interface AccountData {
isDev: boolean;
Expand Down Expand Up @@ -43,6 +43,7 @@ export interface SwankyConfig {
polkadotPalletVersions: string;
localPath: string;
supportedInk: string;
version: string;
};
accounts: AccountData[];
contracts: Record<string, ContractData> | Record<string, never>;
Expand Down
Loading