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

Detect build mode from metadata on deploy #193

Merged
merged 5 commits into from
Feb 26, 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
20 changes: 18 additions & 2 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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 { AccountData, Encrypted } from "../../types/index.js";
import { AbiType, ChainAccount, ChainApi, decrypt, resolveNetworkUrl } from "../../lib/index.js";
import { AccountData, BuildMode, Encrypted } from "../../types/index.js";
import inquirer from "inquirer";
import chalk from "chalk";
import { Contract } from "../../lib/contract.js";
Expand Down Expand Up @@ -97,6 +97,22 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
return new ChainAccount(mnemonic);
}, "Initialising")) as ChainAccount;

const buildMode = await contract.getBuildMode();
if(buildMode !== BuildMode.Verifiable) {
await inquirer.prompt([
{
type: "confirm",
message: `You are deploying a not verified contract in ${buildMode === BuildMode.Release ? "release" : "debug"} mode. Are you sure you want to continue?`,
name: "confirm",
},
]).then((answers) => {
if(!answers.confirm) {
this.log(`${chalk.redBright('✖')} Aborted deployment of ${chalk.yellowBright(args.contractName)}`);
process.exit(0);
}
})
}

const { abi, wasm } = (await this.spinner.runCommand(async () => {
const abi = await contract.getABI();
const wasm = await contract.getWasm();
Expand Down
24 changes: 22 additions & 2 deletions src/lib/contract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AbiType, consts, printContractInfo } from "./index.js";
import { ContractData, DeploymentData } from "../types/index.js";
import { BuildMode, ContractData, DeploymentData } from "../types/index.js";
import { pathExists, readJSON } from "fs-extra/esm";
import path from "node:path";
import { FileError } from "./errors.js";
import fs from "fs";

export class Contract {
static artifactTypes = [".json", ".contract"];
Expand Down Expand Up @@ -49,14 +50,33 @@ export class Contract {
return readJSON(path.resolve(this.artifactsPath, `${this.moduleName}.json`));
}

async getBuildMode(): Promise<BuildMode> {
const check = await this.artifactsExist();
if (!check.result && check.missingTypes.includes(".contract")) {
throw new FileError(
`Cannot read .contract bundle, path not found: ${check.missingPaths.join(', ')}`
);
}

const contractFilePath = path.resolve(this.artifactsPath, `${this.moduleName}.json`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I'm not mistaken, in case when only .json file is missing, we will fail at the 61st line; maybe let's just check only check.result (?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it should be enough @prxgr4mm3r

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I optimized the Contract class by initializing the build mode directly within the constructor since contract's build lifecycle remains consistent across various commands such as contract deploy, contract call, and contract test.

const contractFileContents = await fs.promises.readFile(contractFilePath, 'utf8');
const contractJson = JSON.parse(contractFileContents);

const isVerifiable = contractJson.image
&& typeof contractJson.image === 'string'
&& contractJson.image.startsWith("paritytech/contracts-verifiable");

return isVerifiable ? BuildMode.Verifiable : contractJson.source.build_info.build_mode;
}

async getBundle() {
const check = await this.artifactsExist();
if (!check.result && check.missingTypes.includes(".contract")) {
throw new FileError(
`Cannot read .contract bundle, path not found: ${check.missingPaths.toString()}`
);
}
return readJSON(path.resolve(this.artifactsPath, `${this.moduleName}.contract`));
return readJSON(path.resolve(this.artifactsPath, `${this.moduleName}.contract`), 'utf8');
}

async getWasm(): Promise<Buffer> {
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ export interface SwankyConfig {
networks: Record<string, {url: string}>
}

export enum BuildMode {
Debug = "Debug",
Release = "Release",
Verifiable = "Verifiable",
}

export type SupportedPlatforms = "darwin" | "linux";
export type SupportedArch = "arm64" | "x64";
Loading