Skip to content

Commit

Permalink
Fix/deployment gas issue (#166)
Browse files Browse the repository at this point in the history
This fixes #161 

- add `WeightV2` type construct to deploy command
- make maxGas flag optional
- some small changes to the test template. (doesn't change how it works,
just locks the mocharc file to the one in the root directory, and
prevents it from looking into parent dir)

Will be promoted from draft after #165 is merged and this rebased to
master
  • Loading branch information
codespool committed May 30, 2023
1 parent 3cbd2c1 commit 0d70e54
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 55 deletions.
7 changes: 5 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"require": ["src/test/helpers/init.js", "ts-node/register"],
"require": ["src/test/helpers/init.js, mocha-suppress-logs"],
"loader": "ts-node/esm",
"spec": "src/test/**/*.test.ts",
"watch-extensions": ["ts"],
"recursive": true,
"reporter": "spec",
"timeout": 60000
"timeout": 60000,
"forbid-only": true
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
"typescript": "5.0.4"
},
"peerDependencies": {
"@polkadot/util": "10.4.2",
"@polkadot/util-crypto": "10.4.2"
"@polkadot/util": "12.2.1",
"@polkadot/util-crypto": "12.2.1"
},
"oclif": {
"bin": "swanky",
Expand Down Expand Up @@ -111,7 +111,7 @@
"lint": "eslint . --ext .ts --quiet --config .eslintrc",
"postpack": "shx rm -f oclif.manifest.json",
"prepack": "yarn build && oclif manifest && oclif readme",
"test": "mocha --require mocha-suppress-logs --forbid-only -r ts-node/register \"src/test/**/*.test.ts\"",
"test": "mocha --config .mocharc.json",
"version": "oclif readme && git add README.md",
"tarball:macos": "oclif pack tarballs --targets=darwin-x64 --no-xz",
"tarball:linux": "oclif pack tarballs --targets=linux-x64 --no-xz",
Expand Down
5 changes: 2 additions & 3 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class DeployContract extends Command {
description: "Alias of account to be used",
}),
gas: Flags.integer({
required: true,
char: "g",
}),
args: Flags.string({
Expand Down Expand Up @@ -125,8 +124,8 @@ export class DeployContract extends Command {
wasm,
flags.constructorName,
account.pair,
flags.gas,
flags.args as string[]
flags.args as string[],
flags.gas
);
return contractAddress;
} catch (e) {
Expand Down
29 changes: 15 additions & 14 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,8 @@ export class Init extends BaseCommand {
runningMessage: "Copying common template files",
});

this.taskQueue.push({
task: processTemplates,
args: [
this.projectPath,
{
project_name: paramCase(args.projectName),
},
],
runningMessage: "Processing common templates",
});

if (flags.convert) {
await this.convert(flags.convert);
await this.convert(flags.convert, args.projectName);
} else {
await this.generate(args.projectName);
}
Expand Down Expand Up @@ -295,7 +284,7 @@ export class Init extends BaseCommand {
contract_name_pascal: pascalCase(contractName),
},
],
runningMessage: "Processing contract template",
runningMessage: "Processing templates",
});

this.configBuilder.contracts = {
Expand All @@ -307,7 +296,7 @@ export class Init extends BaseCommand {
};
}

async convert(pathToExistingProject: string) {
async convert(pathToExistingProject: string, projectName: string) {
try {
const pathStat = await stat(pathToExistingProject);
if (pathStat.isDirectory()) {
Expand Down Expand Up @@ -349,6 +338,18 @@ export class Init extends BaseCommand {

const confirmedCopyList = await detectModuleNames(await confirmCopyList(candidatesList));

this.taskQueue.push({
task: processTemplates,
args: [
this.projectPath,
{
project_name: paramCase(projectName),
swanky_version: this.config.pjson.version,
},
],
runningMessage: "Processing templates",
});

this.taskQueue.push({
task: copyWorkspaceContracts,
args: [confirmedCopyList, this.projectPath],
Expand Down
19 changes: 8 additions & 11 deletions src/lib/deploy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import { KeyringPair } from "@polkadot/keyring/types";
import { ChainApi } from "./substrate-api.js";

export type AbiType = Abi;

const TEN_B = 10_000_000_000;
export class DeployApi extends ChainApi {
// eslint-disable-next-line no-useless-constructor
constructor(endpoint: string) {
super(endpoint);
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
public async getGasCost() {}

public async deploy(
abi: Abi,
wasm: Buffer,
constructorName: string,
signerPair: KeyringPair,
gasLimit: number,
args: string[]
args: string[],
customGas?: number
) {
const gasLimit = this.apiInst.registry.createType("WeightV2", {
refTime: BigInt(TEN_B),
proofSize: BigInt(customGas || TEN_B),
});

const code = new CodePromise(this._api, abi, wasm);
const storageDepositLimit = null;
if (typeof code.tx[constructorName] !== "function") {
Expand Down
30 changes: 11 additions & 19 deletions src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export async function copyCommonTemplateFiles(templatesPath: string, projectPath
})
);
await rename(path.resolve(projectPath, "gitignore"), path.resolve(projectPath, ".gitignore"));
await rename(
path.resolve(projectPath, "mocharc.json"),
path.resolve(projectPath, ".mocharc.json")
);
await copy(path.resolve(templatesPath, "github"), path.resolve(projectPath, ".github"));
}

Expand All @@ -59,25 +63,13 @@ export async function processTemplates(projectPath: string, templateData: Record
expandDirectories: { extensions: ["hbs"] },
});

handlebars.registerHelper("if_eq", function (a, b, options): boolean {
if (a === b) {
// @ts-ignore
return options.fn(this);
} else {
// @ts-ignore
return options.inverse(this);
}
});

await Promise.all(
templateFiles.map(async (tplFilePath) => {
const rawTemplate = await readFile(tplFilePath, "utf8");
const template = handlebars.compile(rawTemplate);
const compiledFile = template(templateData);
await rm(tplFilePath);
await writeFile(tplFilePath.split(".hbs")[0], compiledFile);
})
);
for (const tplFilePath of templateFiles) {
const rawTemplate = await readFile(tplFilePath, "utf8");
const template = handlebars.compile(rawTemplate);
const compiledFile = template(templateData);
await rm(tplFilePath);
await writeFile(tplFilePath.split(".hbs")[0], compiledFile);
}
}

export async function downloadNode(projectPath: string, nodeInfo: nodeInfo, spinner: Spinner) {
Expand Down
6 changes: 6 additions & 0 deletions src/templates/mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": "ts-node/register",
"spec": "tests/**/*.test.ts",
"exit": true,
"timeout": 20000
}
7 changes: 4 additions & 3 deletions src/templates/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@
"license": "MIT",
"scripts": {
"run-node": "swanky node start",
"test": "mocha -r ts-node/register \"test/**/*.test.ts\" --exit --timeout 20000"
"test": "mocha --config .mocharc.json"
},
"engines": {
"node": ">=18.0.0"
},
"dependencies": {
"@727-ventures/typechain-types": "1.0.0-beta.1",
"@727-ventures/typechain-polkadot": "1.0.0-beta.2",
"typescript": "^4.9.3"
"@types/node": "^18",
"typescript": "^5.0.4"
},
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^8.0.3",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"mocha": "10.1.0",
"mocha": "^10.1.0",
"mochawesome": "^7.1.3",
"ts-node": "^10.8.0"
}
Expand Down

0 comments on commit 0d70e54

Please sign in to comment.