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 deployment builder. #61

Merged
merged 9 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,29 @@ TxParams {
let contract: ScillaContract = await hre.deployScillaContract("HelloWorld", "Hello World", {gasLimit: 8000}); // Override a parameter
```

Alternatively, you can deploy them using the `contractDeployer` object injected to `hre`:
```typescript
const contract = await hre.contractDeployer
.withName("Codehash")
.deploy();

const contract = await this.hre.contractDeployer
.withName("HelloWorld")
.withContractParams("Hello world!")
.deploy();

const contract = await this.hre.contractDeployer
.withName("HelloWorld")
.withContractParams("sss")
.withContractCompression() // To enable contract compression.
.deploy();
```

In the same way, you can deploy your libraries with their names:
```typescript
let library: ScillaContract = await hre.deployScillaLibrary("MyLibrary");
let library: ScillaContract = await hre.deployScillaLibrary("MyLibrary", false);
```
Pass `true` as the second parameter if you want your library's contract gets compressed before deployment.

and finally, here is how you can deploy a contract importing a user-defined library:
```typescript
Expand Down
2 changes: 1 addition & 1 deletion scripts/force-scilla-download.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { parseScilla } from "../src/ScillaParser"
import { parseScilla } from "../src/parser/ScillaParser"

parseScilla("test/fixture-projects/hardhat-project/contracts/scilla/HelloWorld.scilla");
4 changes: 2 additions & 2 deletions src/ZilliqaHardhatObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Zilliqa } from "@zilliqa-js/zilliqa";
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import * as ScillaContractDeployer from "./ScillaContractDeployer";
import * as ScillaContractDeployer from "./deployer/ScillaContractDeployer";

// We carefully don't cache the setup object, in case it changes underneath us.
export class ZilliqaHardhatObject {
Expand All @@ -27,7 +27,7 @@ export class ZilliqaHardhatObject {
return this.getZilliqaSetup().accounts;
}

// Retrieve the default acount used to sign transactions.
// Retrieve the default account used to sign transactions.
public getDefaultAccount(): Account | undefined {
const wallet = this.getZilliqaSetup().zilliqa.wallet;
const defaultAccount = wallet.defaultAccount;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from "@ethersproject/bignumber";

import { isNumeric } from "./ScillaParser";
import { isNumeric } from "../parser/ScillaParser";

export const simplifyLogs = function (logs: any) {
for (const log of logs) {
Expand Down
File renamed without changes.
82 changes: 82 additions & 0 deletions src/deployer/Deployer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { TxParams } from "@zilliqa-js/account";
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import {
OptionalUserDefinedLibraryList,
ScillaContract,
UserDefinedLibrary,
deploy,
} from "./ScillaContractDeployer";

export class ContractDeployer {
private contractName: string;
private compress: boolean;
private userDefinedLibraries: OptionalUserDefinedLibraryList;
private txParams: TxParams | null;
private contractParams: any[];

constructor(private hre: HardhatRuntimeEnvironment) {
this.contractName = "";
this.compress = false;
this.userDefinedLibraries = null;
this.txParams = null;
this.contractParams = [];
}

reset(): ContractDeployer {
this.contractName = "";
this.compress = false;
this.userDefinedLibraries = null;
this.txParams = null;
this.contractParams = [];
return this;
}

withName(contractName: string): ContractDeployer {
this.contractName = contractName;
return this;
}

withContractParams(...params: any[]): ContractDeployer {
this.contractParams = params;
return this;
}

withTxParams(params: TxParams): ContractDeployer {
this.txParams = params;
return this;
}

withContractCompression(): ContractDeployer {
this.compress = true;
return this;
}

withUserDefinedLibraries(libraries: UserDefinedLibrary[]) {
this.userDefinedLibraries = libraries;
return this;
}

async deploy(): Promise<ScillaContract> {
if (this.contractName.trim().length === 0) {
throw new HardhatPluginError(
"hardhat-scilla-plugin",
"You must specify the contract name in order to deploy it."
);
}
if (this.txParams) {
this.contractParams.push(this.txParams);
}

const contract = await deploy(
this.hre,
this.contractName,
this.compress,
this.userDefinedLibraries,
...this.contractParams
);
this.reset();
return contract;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import fs from "fs";
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import * as ScillaContractProxy from "./ScillaContractProxy";
import { ContractInfo } from "./ScillaContractsInfoUpdater";
import { Field, Fields, isNumeric } from "./ScillaParser";
import * as ScillaContractProxy from "../parser/ScillaContractProxy";
import { ContractInfo } from "../parser/ScillaContractsInfoUpdater";
import { Field, Fields, isNumeric } from "../parser/ScillaParser";

export interface Value {
vname: string;
Expand Down Expand Up @@ -124,11 +124,12 @@ export interface UserDefinedLibrary {
address: string;
}

type OptionalUserDefinedLibraryList = UserDefinedLibrary[] | null;
export type OptionalUserDefinedLibraryList = UserDefinedLibrary[] | null;

export async function deploy(
hre: HardhatRuntimeEnvironment,
contractName: string,
compress: boolean,
userDefinedLibraries: OptionalUserDefinedLibraryList,
...args: any[]
): Promise<ScillaContract> {
Expand Down Expand Up @@ -156,6 +157,7 @@ export async function deploy(
const [tx, sc] = await deployFromFile(
contractInfo.path,
init,
compress,
txParamsForContractDeployment
);
sc.deployed_by = tx;
Expand All @@ -167,7 +169,8 @@ export async function deploy(

export const deployLibrary = async (
hre: HardhatRuntimeEnvironment,
libraryName: string
libraryName: string,
compress: boolean
): Promise<ScillaContract> => {
const contractInfo: ContractInfo = hre.scillaContracts[libraryName];
if (contractInfo === undefined) {
Expand All @@ -176,7 +179,7 @@ export const deployLibrary = async (

const init: Init = fillLibraryInit();

const [tx, sc] = await deployFromFile(contractInfo.path, init, {}); // FIXME: In #45
const [tx, sc] = await deployFromFile(contractInfo.path, init, compress, {}); // FIXME: In #45
sc.deployed_by = tx;

return sc;
Expand Down Expand Up @@ -261,6 +264,7 @@ const fillInit = (
export async function deployFromFile(
path: string,
init: Init,
compress: boolean,
txParamsForContractDeployment: any
): Promise<[Transaction, ScillaContract]> {
if (setup === null) {
Expand All @@ -271,7 +275,10 @@ export async function deployFromFile(
}

const deployer = setup.zilliqa.wallet.defaultAccount ?? setup.accounts[0];
const code = read(path);
let code = read(path);
if (compress) {
code = compressContract(code);
}
const contract = setup.zilliqa.contracts.new(code, init);
const [tx, sc] = await contract.deploy(
{ ...setup, pubKey: deployer.publicKey, ...txParamsForContractDeployment },
Expand All @@ -287,3 +294,14 @@ export async function deployFromFile(

return [tx, sc];
}

export function compressContract(code: string): string {
// Remove comments
code = code.replace(/(\(\*.*?\*\))/gms, "");

// Remove empty lines
code = code.replace(/(^[ \t]*\n)/gm, "");

// Remove extra whitespace at the end of the lines
return code.replace(/[ \t]+$/gm, "");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import fs from "fs";
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import { ScillaContract } from "./ScillaContractDeployer";
import * as ScillaContractProxy from "../parser/ScillaContractProxy";
import { ContractInfo } from "../parser/ScillaContractsInfoUpdater";
import * as ScillaContractsInfoUpdater from "../parser/ScillaContractsInfoUpdater";
import { parseScilla } from "../parser/ScillaParser";
import * as ZilliqaUtils from "../ZilliqaUtils";

import * as ScillaContractDeployer from "./ScillaContractDeployer";
import * as ScillaContractProxy from "./ScillaContractProxy";
import { ContractInfo } from "./ScillaContractsInfoUpdater";
import * as ScillaContractsInfoUpdater from "./ScillaContractsInfoUpdater";
import { parseScilla } from "./ScillaParser";
import * as ZilliqaUtils from "./ZilliqaUtils";
import { ScillaContract } from "./ScillaContractDeployer";

export async function contractFromAddress(
hre: HardhatRuntimeEnvironment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import clc from "cli-color";
import { glob } from "glob";
import path from "path";

import * as ZilliqaUtils from "./ZilliqaUtils";
import * as ZilliqaUtils from "../ZilliqaUtils";

export async function runScillaChecker(contracts: any, libdir: any) {
let files: string[] = [];
Expand Down
28 changes: 17 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Init } from "@zilliqa-js/contract";
import { extendEnvironment } from "hardhat/config";
import { lazyFunction, lazyObject } from "hardhat/plugins";

import { ContractDeployer } from "./deployer/Deployer";
import {
deploy,
deployFromFile,
Expand All @@ -11,9 +12,9 @@ import {
UserDefinedLibrary,
updateSetup,
setAccount,
} from "./ScillaContractDeployer";
import { contractFromAddress } from "./ScillaContractInteractor";
import { loadScillaContractsInfo } from "./ScillaContractsInfoUpdater";
} from "./deployer/ScillaContractDeployer";
import { contractFromAddress } from "./deployer/ScillaContractInteractor";
import { loadScillaContractsInfo } from "./parser/ScillaContractsInfoUpdater";
import "./task-extensions";
// This import is needed to let the TypeScript compiler know that it should include your type
// extensions in your npm package's types file.
Expand All @@ -25,17 +26,18 @@ export {
Setup,
initZilliqa,
UserDefinedLibrary,
} from "./ScillaContractDeployer";
} from "./deployer/ScillaContractDeployer";
export { ZilliqaHardhatObject } from "./ZilliqaHardhatObject";
export { BN } from "@zilliqa-js/util";

export { scillaChaiEventMatcher } from "./ScillaChaiMatchers";
export { scillaChaiEventMatcher } from "./chai-matcher/ScillaChaiMatchers";

extendEnvironment((hre) => {
// We add a field to the Hardhat Runtime Environment here.
// We use lazyObject to avoid initializing things until they are actually
// needed.
hre.scillaContracts = lazyObject(() => loadScillaContractsInfo());
hre.contractDeployer = lazyObject(() => new ContractDeployer(hre));
hre.setScillaDefaults = lazyFunction(() => (params) => {
return updateSetup(params);
});
Expand All @@ -50,7 +52,7 @@ extendEnvironment((hre) => {
hre.deployScillaContract = lazyFunction(
() =>
async (contractName: string, ...args: any[]): Promise<ScillaContract> => {
return deploy(hre, contractName, [], ...args);
return deploy(hre, contractName, false, [], ...args);
}
);

Expand All @@ -61,24 +63,28 @@ extendEnvironment((hre) => {
userDefinedLibraries: UserDefinedLibrary[],
...args: any[]
): Promise<ScillaContract> => {
return deploy(hre, contractName, userDefinedLibraries, ...args);
return deploy(hre, contractName, false, userDefinedLibraries, ...args);
}
);

hre.deployScillaLibrary = lazyFunction(
() =>
async (libraryName: string): Promise<ScillaContract> => {
return deployLibrary(hre, libraryName);
async (
libraryName: string,
compress: boolean
): Promise<ScillaContract> => {
return deployLibrary(hre, libraryName, compress);
}
);

hre.deployScillaFile = lazyFunction(
() =>
async (
contractPath: string,
init: Init
init: Init,
compress: boolean
): Promise<[Transaction, ScillaContract]> => {
return deployFromFile(contractPath, init, {});
return deployFromFile(contractPath, init, compress, {});
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { CallParams, State } from "@zilliqa-js/contract";
import { BN } from "@zilliqa-js/util";
import { HardhatPluginError } from "hardhat/plugins";

import * as ScillaContractDeployer from "./ScillaContractDeployer";
import { ScillaContract, Value, Setup } from "./ScillaContractDeployer";
import * as ScillaContractDeployer from "../deployer/ScillaContractDeployer";
import {
ScillaContract,
Value,
Setup,
} from "../deployer/ScillaContractDeployer";

import { ContractInfo } from "./ScillaContractsInfoUpdater";
import {
Field,
Expand Down
2 changes: 1 addition & 1 deletion src/ScillaParser.ts → src/parser/ScillaParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HardhatPluginError } from "hardhat/plugins";
import path from "path";
import readline from "readline";

import * as ZilliqaUtils from "./ZilliqaUtils";
import * as ZilliqaUtils from "../ZilliqaUtils";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const parse: any = require("s-expression");
Expand Down
4 changes: 2 additions & 2 deletions src/task-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import clc from "cli-color";
import { task } from "hardhat/config";

import { runScillaChecker } from "./ScillaChecker";
import { updateContractsInfo as updateScillaContractsInfo } from "./ScillaContractsInfoUpdater";
import { runScillaChecker } from "./hardhat-tasks/ScillaChecker";
import { updateContractsInfo as updateScillaContractsInfo } from "./parser/ScillaContractsInfoUpdater";

task(
"scilla-check",
Expand Down
Loading
Loading