diff --git a/README.md b/README.md index a02f0e3..fbbf92f 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,26 @@ let contract: ScillaContract = await hre.deployScillaContract("SetGet"); let contract: ScillaContract = await hre.deployScillaContract("HelloWorld", "Hello World"); // Contract with initial parameters. ``` +You can override the following parameters while deploying a contract: +```typescript +TxParams { + version: number; + toAddr: string; + amount: BN; + gasPrice: BN; + gasLimit: Long; + code?: string; + data?: string; + receipt?: TxReceipt; + nonce?: number; + pubKey?: string; + signature?: string; +} +``` +```typescript +let contract: ScillaContract = await hre.deployScillaContract("HelloWorld", "Hello World", {gasLimit: 8000}); // Override a parameter +``` + In the same way, you can deploy your libraries with their names: ```typescript let library: ScillaContract = await hre.deployScillaLibrary("MyLibrary"); diff --git a/contracts/GenerateAdtType.scilla b/contracts/GenerateAdtType.scilla new file mode 100644 index 0000000..4f36ba5 --- /dev/null +++ b/contracts/GenerateAdtType.scilla @@ -0,0 +1,17 @@ +scilla_version 0 +contract GenerateAdtType +( +) + +transition tr0(arg: (List (Pair ByStr20 (List (Pair Uint32 Uint128))))) +end + +transition tr1(arg: (List Uint128)) +end + +transition tr2(arg: (Pair Uint32 Uint128)) +end + +transition tr3(arg: (List (Pair ByStr20 ByStr20))) +end + diff --git a/package-lock.json b/package-lock.json index 7b0a5f8..170e33e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hardhat-scilla-plugin", - "version": "3.5.0", + "version": "3.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "hardhat-scilla-plugin", - "version": "3.5.0", + "version": "3.6.0", "license": "MIT", "dependencies": { "@ethersproject/bignumber": "^5.7.0", diff --git a/package.json b/package.json index 98b78b8..4daa0df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hardhat-scilla-plugin", - "version": "3.5.3", + "version": "3.6.1", "description": "Hardhat TypeScript plugin for scilla testing", "repository": "github:Zilliqa/hardhat-scilla-plugin", "author": "Saeed Dadkhah", diff --git a/src/ScillaContractDeployer.ts b/src/ScillaContractDeployer.ts index 6e44a88..08c816b 100644 --- a/src/ScillaContractDeployer.ts +++ b/src/ScillaContractDeployer.ts @@ -193,6 +193,12 @@ export async function deploy( let sc: ScillaContract; let tx: Transaction; + let txParamsForContractDeployment = {}; + if (contractInfo.parsedContract.constructorParams && args.length === contractInfo.parsedContract.constructorParams.length + 1) { + // The last param is Tx info such as amount, nonce, gasPrice + txParamsForContractDeployment = args.pop(); + } + const init: Init = fillInit( contractName, userDefinedLibraries, @@ -200,7 +206,7 @@ export async function deploy( ...args ); - [tx, sc] = await deployFromFile(contractInfo.path, init); + [tx, sc] = await deployFromFile(contractInfo.path, init, txParamsForContractDeployment); sc.deployed_by = tx; contractInfo.parsedContract.transitions.forEach((transition) => { @@ -277,7 +283,7 @@ export const deployLibrary = async ( let tx: Transaction; const init: Init = fillLibraryInit(); - [tx, sc] = await deployFromFile(contractInfo.path, init); + [tx, sc] = await deployFromFile(contractInfo.path, init, {}); // FIXME: In #45 sc.deployed_by = tx; return sc; @@ -361,6 +367,7 @@ const fillInit = ( export async function deployFromFile( path: string, init: Init, + txParamsForContractDeployment: any ): Promise<[Transaction, ScillaContract]> { if (setup === null) { throw new HardhatPluginError( @@ -373,7 +380,7 @@ export async function deployFromFile( const code = read(path); const contract = setup.zilliqa.contracts.new(code, init); const [tx, sc] = await contract.deploy( - { ...setup, pubKey: deployer.publicKey }, + { ...setup, pubKey: deployer.publicKey, ...txParamsForContractDeployment }, setup.attempts, setup.timeout, false @@ -422,4 +429,4 @@ export async function sc_call( setup.timeout, true ); -} \ No newline at end of file +} diff --git a/src/ScillaParser.ts b/src/ScillaParser.ts index e0c2ee4..0798b65 100644 --- a/src/ScillaParser.ts +++ b/src/ScillaParser.ts @@ -270,6 +270,22 @@ function parseAdt(row: any): ADTField { }; } +function generateAdtType(field: ADTField): string { + if (field.argtypes.length === 0) { + return field.ctor; + } + + const type = `${field.ctor} ${field.argtypes.map((arg: Field) =>{ + // Here we're sure that type is ADTField + const typeJson: ADTField = arg.typeJSON as ADTField; + if (["Pair", "List"].includes(typeJson.ctor)) + return `(${arg.type})` + else + return arg.type + }).reduce((prev, current) => `${prev} ${current}`)}`; + return type; +} + function parseField(row: any): Field { const field_type = row[0]; @@ -285,7 +301,7 @@ function parseField(row: any): Field { const name = row[0][1][1]; return { typeJSON: adt, - type: adt.ctor + adt.argtypes.map((arg: Field) => " " + arg.type).join(" "), + type: generateAdtType(adt), name, }; } else if (field_type === "Address") { diff --git a/src/index.ts b/src/index.ts index 3d12b12..2b4446d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,7 +69,7 @@ extendEnvironment((hre) => { contractPath: string, init: Init, ): Promise<[Transaction, ScillaContract]> => { - return deployFromFile(contractPath, init); + return deployFromFile(contractPath, init, {}); } ); diff --git a/test/scilla-generate-adt-type.test.ts b/test/scilla-generate-adt-type.test.ts new file mode 100644 index 0000000..70f8335 --- /dev/null +++ b/test/scilla-generate-adt-type.test.ts @@ -0,0 +1,31 @@ +import chai, { expect } from "chai"; +import chaiSubset from "chai-subset"; + +import { + ParsedContract, + parseScilla, +} from "../src/ScillaParser"; +chai.use(chaiSubset); + +describe("Scilla Parser should parse contracts successfully", function () { + let adtContract: ParsedContract; + before(function () { + adtContract = parseScilla("contracts/GenerateAdtType.scilla"); + }); + + it("should generate valid type for (List (Pair ByStr20 (List (Pair Uint32 Uint128)))))", async () => { + expect(adtContract.transitions[0].params[0].type).to.be.eq("List (Pair ByStr20 (List (Pair Uint32 Uint128)))"); + }); + + it("should generate valid type for (List Uint128)", async () => { + expect(adtContract.transitions[1].params[0].type).to.be.eq("List Uint128"); + }); + + it("should generate valid type for (Pair Uint32 Uint128)", async () => { + expect(adtContract.transitions[2].params[0].type).to.be.eq("Pair Uint32 Uint128"); + }); + + it("should generate valid type for (List (Pair ByStr20 ByStr20))", async () => { + expect(adtContract.transitions[3].params[0].type).to.be.eq("List (Pair ByStr20 ByStr20)"); + }); +});