diff --git a/src/deployer/Builder.ts b/src/deployer/Builder.ts deleted file mode 100644 index a317f03..0000000 --- a/src/deployer/Builder.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { TxParams } from "@zilliqa-js/account"; - -import { - OptionalUserDefinedLibraryList, - UserDefinedLibrary, -} from "./ScillaContractDeployer"; - -export class DeployBuilder { - private contractName: string; - private compress: boolean; - private userDefinedLibraries: OptionalUserDefinedLibraryList; - private txParams: TxParams | null; - private contractParams: any[]; - - constructor() { - this.contractName = ""; - this.compress = false; - this.userDefinedLibraries = null; - this.txParams = null; - this.contractParams = []; - } - - reset(): DeployBuilder { - this.contractName = ""; - this.compress = false; - this.userDefinedLibraries = null; - this.txParams = null; - this.contractParams = []; - return this; - } - - withName(contractName: string): DeployBuilder { - this.contractName = contractName; - return this; - } - - withContractParams(params: any[]): DeployBuilder { - this.contractParams = params; - return this; - } - - withTxParams(params: TxParams): DeployBuilder { - this.txParams = params; - return this; - } - - withContractCompression(): DeployBuilder { - this.compress = true; - return this; - } - - withUserDefinedLibraries(libraries: UserDefinedLibrary[]) { - this.userDefinedLibraries = libraries; - return this; - } - - build(): Deployment { - return new Deployment( - this.contractName, - this.compress, - this.userDefinedLibraries, - this.txParams, - this.contractParams - ); - } -} - -class Deployment { - constructor( - private contractName: string, - private compress: boolean, - private userDefinedLibraries: OptionalUserDefinedLibraryList, - private txParams: TxParams | null, - private contractParams: any[] - ) {} -} diff --git a/src/deployer/Deployer.ts b/src/deployer/Deployer.ts new file mode 100644 index 0000000..64713d2 --- /dev/null +++ b/src/deployer/Deployer.ts @@ -0,0 +1,88 @@ +import { TxParams } from "@zilliqa-js/account"; +import { HardhatPluginError } from "hardhat/plugins"; +import { HardhatRuntimeEnvironment } from "hardhat/types"; + +import { + OptionalUserDefinedLibraryList, + ScillaContract, + UserDefinedLibrary, +} 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 { + if (!!this.contractName.trim()) { + 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); + } + + let contract; + if (this.userDefinedLibraries) { + contract = await this.hre.deployScillaContractWithLib( + this.contractName, + this.userDefinedLibraries, + ...this.contractParams + ); + } else { + contract = await this.hre.deployScillaContract( + this.contractName, + ...this.contractParams + ); + } + + this.reset(); + return contract; + } +} diff --git a/src/index.ts b/src/index.ts index 4a9b808..b6e927c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -36,6 +37,7 @@ extendEnvironment((hre) => { // 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); }); diff --git a/src/type-extensions.ts b/src/type-extensions.ts index 72fb387..1d3d113 100644 --- a/src/type-extensions.ts +++ b/src/type-extensions.ts @@ -6,6 +6,7 @@ import { Init } from "@zilliqa-js/contract"; import "hardhat/types/config"; import "hardhat/types/runtime"; +import { ContractDeployer } from "./deployer/Deployer"; import { ScillaContract, UserDefinedLibrary, @@ -16,9 +17,12 @@ import { ZilliqaHardhatObject } from "./ZilliqaHardhatObject"; declare module "hardhat/types/runtime" { export interface HardhatRuntimeEnvironment { + zilliqa: ZilliqaHardhatObject; scillaContracts: ScillaContracts; + contractDeployer: ContractDeployer; + interactWithScillaContract: ( - contractAdress: string + contractAddress: string ) => Promise; deployScillaContract: ( contractName: string, @@ -34,7 +38,6 @@ declare module "hardhat/types/runtime" { contractName: string, init: Init ) => Promise<[Transaction, ScillaContract]>; - zilliqa: ZilliqaHardhatObject; getZilliqaChainId: () => number; getNetworkUrl: () => string; getPrivateKeys: () => string[]; diff --git a/test/defaults.test.ts b/test/defaults.test.ts index 0af8958..ecc1733 100644 --- a/test/defaults.test.ts +++ b/test/defaults.test.ts @@ -1,8 +1,9 @@ import { BN, Long } from "@zilliqa-js/util"; import chai, { expect } from "chai"; -import { scillaChaiEventMatcher } from "./chai-matcher/ScillaChaiMatchers"; -import { setup } from "./deployer/ScillaContractDeployer"; +import { scillaChaiEventMatcher } from "../src/chai-matcher/ScillaChaiMatchers"; +import { setup } from "../src/deployer/ScillaContractDeployer"; + import { useEnvironment } from "./helpers"; chai.use(scillaChaiEventMatcher); diff --git a/test/helpers.ts b/test/helpers.ts index b3ee2e4..374701e 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -2,11 +2,10 @@ import { resetHardhatContext } from "hardhat/plugins-testing"; import { HardhatRuntimeEnvironment } from "hardhat/types"; import path from "path"; +import { initZilliqa } from "../src/deployer/ScillaContractDeployer"; +import { updateContractsInfo } from "../src/parser/ScillaContractsInfoUpdater"; import * as ZilliqaHardhatObject from "../src/ZilliqaHardhatObject"; -import { initZilliqa } from "./deployer/ScillaContractDeployer"; -import { updateContractsInfo } from "./parser/ScillaContractsInfoUpdater"; - declare module "mocha" { interface Context { hre: HardhatRuntimeEnvironment; diff --git a/test/logs-simplifier.test.ts b/test/logs-simplifier.test.ts index 0630e2d..b471be6 100644 --- a/test/logs-simplifier.test.ts +++ b/test/logs-simplifier.test.ts @@ -1,7 +1,7 @@ import { BigNumber } from "@ethersproject/bignumber"; import { expect } from "chai"; -import { simplifyLogs } from "./chai-matcher/LogsSimplifier"; +import { simplifyLogs } from "../src/chai-matcher/LogsSimplifier"; describe("", function () { describe("Logs simplifier", function () { diff --git a/test/scilla-checker.test.ts b/test/scilla-checker.test.ts index c6af56a..108c5ab 100644 --- a/test/scilla-checker.test.ts +++ b/test/scilla-checker.test.ts @@ -1,7 +1,7 @@ import chai from "chai"; import chaiSubset from "chai-subset"; -import { runScillaChecker } from "./hardhat-tasks/ScillaChecker"; +import { runScillaChecker } from "../src/hardhat-tasks/ScillaChecker"; chai.use(chaiSubset); describe("", function () { diff --git a/test/scilla-contracts-updater.test.ts b/test/scilla-contracts-updater.test.ts index 66bd3bc..f539ecd 100644 --- a/test/scilla-contracts-updater.test.ts +++ b/test/scilla-contracts-updater.test.ts @@ -6,7 +6,7 @@ import { ContractInfo, loadScillaContractsInfo, updateContractsInfo, -} from "./parser/ScillaContractsInfoUpdater"; +} from "../src/parser/ScillaContractsInfoUpdater"; chai.use(chaiSubset); describe("", function () { diff --git a/test/scilla-generate-adt-type.test.ts b/test/scilla-generate-adt-type.test.ts index e4b65f9..259ef94 100644 --- a/test/scilla-generate-adt-type.test.ts +++ b/test/scilla-generate-adt-type.test.ts @@ -1,7 +1,7 @@ import chai, { expect } from "chai"; import chaiSubset from "chai-subset"; -import { ParsedContract, parseScilla } from "./parser/ScillaParser"; +import { ParsedContract, parseScilla } from "../src/parser/ScillaParser"; chai.use(chaiSubset); describe("Scilla Parser should parse contracts successfully", function () { diff --git a/test/scilla-parser.test.ts b/test/scilla-parser.test.ts index 227256d..ecc7863 100644 --- a/test/scilla-parser.test.ts +++ b/test/scilla-parser.test.ts @@ -6,7 +6,7 @@ import { ParsedContract, parseScilla, parseScillaLibrary, -} from "./parser/ScillaParser"; +} from "../src/parser/ScillaParser"; chai.use(chaiSubset); describe("", function () { diff --git a/test/zilliqa.test.ts b/test/zilliqa.test.ts index 01d8d1e..5a04917 100644 --- a/test/zilliqa.test.ts +++ b/test/zilliqa.test.ts @@ -1,8 +1,8 @@ import { expect } from "chai"; +import { initZilliqa } from "../src/deployer/ScillaContractDeployer"; import * as ZilliqaHardhatObject from "../src/ZilliqaHardhatObject"; -import { initZilliqa } from "./deployer/ScillaContractDeployer"; describe("", function () { let zobj: ZilliqaHardhatObject.ZilliqaHardhatObject;