diff --git a/src/deployer/Deployer.ts b/src/deployer/Deployer.ts index 6ea690f..91897e0 100644 --- a/src/deployer/Deployer.ts +++ b/src/deployer/Deployer.ts @@ -69,7 +69,7 @@ export class ContractDeployer { this.contractParams.push(this.txParams); } - const contract = deploy( + const contract = await deploy( this.hre, this.contractName, this.compress, diff --git a/src/deployer/ScillaContractDeployer.ts b/src/deployer/ScillaContractDeployer.ts index 85f3102..4e902d7 100644 --- a/src/deployer/ScillaContractDeployer.ts +++ b/src/deployer/ScillaContractDeployer.ts @@ -129,7 +129,7 @@ export type OptionalUserDefinedLibraryList = UserDefinedLibrary[] | null; export async function deploy( hre: HardhatRuntimeEnvironment, contractName: string, - compressContract: boolean, + compress: boolean, userDefinedLibraries: OptionalUserDefinedLibraryList, ...args: any[] ): Promise { @@ -157,6 +157,7 @@ export async function deploy( const [tx, sc] = await deployFromFile( contractInfo.path, init, + compress, txParamsForContractDeployment ); sc.deployed_by = tx; @@ -168,7 +169,8 @@ export async function deploy( export const deployLibrary = async ( hre: HardhatRuntimeEnvironment, - libraryName: string + libraryName: string, + compress: boolean ): Promise => { const contractInfo: ContractInfo = hre.scillaContracts[libraryName]; if (contractInfo === undefined) { @@ -177,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; @@ -262,6 +264,7 @@ const fillInit = ( export async function deployFromFile( path: string, init: Init, + compress: boolean, txParamsForContractDeployment: any ): Promise<[Transaction, ScillaContract]> { if (setup === null) { @@ -272,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 }, @@ -288,3 +294,8 @@ export async function deployFromFile( return [tx, sc]; } + +export function compressContract(code: string): string { + code = code.replace(/\(\*.*?\*\)/gms, ""); + return code.replace(/(^[ \t]*\n)/gm, ""); +} diff --git a/src/index.ts b/src/index.ts index d40c250..a729c53 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,8 +69,11 @@ extendEnvironment((hre) => { hre.deployScillaLibrary = lazyFunction( () => - async (libraryName: string): Promise => { - return deployLibrary(hre, libraryName); + async ( + libraryName: string, + compress: boolean + ): Promise => { + return deployLibrary(hre, libraryName, compress); } ); @@ -78,9 +81,10 @@ extendEnvironment((hre) => { () => async ( contractPath: string, - init: Init + init: Init, + compress: boolean ): Promise<[Transaction, ScillaContract]> => { - return deployFromFile(contractPath, init, {}); + return deployFromFile(contractPath, init, compress, {}); } ); diff --git a/src/type-extensions.ts b/src/type-extensions.ts index 1d3d113..97b7bac 100644 --- a/src/type-extensions.ts +++ b/src/type-extensions.ts @@ -33,10 +33,14 @@ declare module "hardhat/types/runtime" { userDefinedLibraries: UserDefinedLibrary[], ...args: any[] ) => Promise; - deployScillaLibrary: (contractName: string) => Promise; + deployScillaLibrary: ( + contractName: string, + compress: boolean + ) => Promise; deployScillaFile: ( contractName: string, - init: Init + init: Init, + compress: boolean ) => Promise<[Transaction, ScillaContract]>; getZilliqaChainId: () => number; getNetworkUrl: () => string; diff --git a/test/deploy.live.test.ts b/test/deploy.live.test.ts index bec632d..760cd82 100644 --- a/test/deploy.live.test.ts +++ b/test/deploy.live.test.ts @@ -37,7 +37,24 @@ describe("", function () { .withContractParams("Hello world!") .deploy(); + console.log(contract.isDeployed()); expect(contract.address).not.null; }); + + it("Should be able to deploy a contract with compression", async function () { + const contract = await this.hre.contractDeployer + .withName("HelloWorld") + .withContractParams("sss") + .withContractCompression() + .deploy(); + + console.log(contract.error); + + const tx = await contract.getHello(); + await expect(tx).to.have.eventLogWithParams("getHello()", { + value: "", + vname: "msg", + }); + }); }); }); diff --git a/test/fixture-projects/hardhat-project/contracts/scilla/WithComment.scilla b/test/fixture-projects/hardhat-project/contracts/scilla/WithComment.scilla new file mode 100644 index 0000000..0194a51 --- /dev/null +++ b/test/fixture-projects/hardhat-project/contracts/scilla/WithComment.scilla @@ -0,0 +1,31 @@ + +(***************************************************) +(* The contract definition *) +(***************************************************) +scilla_version 0 (*version*) + +contract WithComment (*contract name*) +() +(*fields*) +field welcome_msg : String = "" (*welcome*) + +(*first transition*) +transition setHello (msg : String) + is_owner = builtin eq owner _sender; + match is_owner with + | False => + e = {_eventname : "setHello()"; code : not_owner_code}; + event e + | True => + welcome_msg := msg; + e = {_eventname : "setHello()"; code : set_hello_code}; + event e + end +end + +(*second transition*) +transition getHello () + r <- welcome_msg; (*dummy comment*) + e = {_eventname: "getHello()"; msg: r}; + event e +end