From 613f76a99a70118ce2bcf15448ff5fa7a6216da0 Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Fri, 29 Sep 2023 12:34:29 +0330 Subject: [PATCH 1/4] Support overriding gasPrice, gasLimit, amount, etc. upon contract deployment. --- README.md | 20 ++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- src/ScillaContractDeployer.ts | 17 ++++++++++++----- src/index.ts | 2 +- 5 files changed, 36 insertions(+), 9 deletions(-) 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/package-lock.json b/package-lock.json index 7b0a5f8..8e6d22d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hardhat-scilla-plugin", - "version": "3.5.0", + "version": "3.5.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "hardhat-scilla-plugin", - "version": "3.5.0", + "version": "3.5.3", "license": "MIT", "dependencies": { "@ethersproject/bignumber": "^5.7.0", diff --git a/package.json b/package.json index 98b78b8..fb6f552 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hardhat-scilla-plugin", - "version": "3.5.3", + "version": "3.6.0", "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..398d832 100644 --- a/src/ScillaContractDeployer.ts +++ b/src/ScillaContractDeployer.ts @@ -1,6 +1,6 @@ // This is necessary so that tsc can resolve some of the indirect types for // sc_call, otherwise it errors out - richard@zilliqa.com 2023-03-09 -import { Account, Transaction } from "@zilliqa-js/account"; +import { Account, Transaction, TxParams } from "@zilliqa-js/account"; import { CallParams, Contract, Init, State } from "@zilliqa-js/contract"; import { BN, bytes, Long, units } from "@zilliqa-js/util"; import { Zilliqa } from "@zilliqa-js/zilliqa"; @@ -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, {}); 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/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, {}); } ); From 9f6f4be933078b60b4bed35a4655783657b9351a Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Fri, 29 Sep 2023 12:39:33 +0330 Subject: [PATCH 2/4] remove useless import. --- src/ScillaContractDeployer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ScillaContractDeployer.ts b/src/ScillaContractDeployer.ts index 398d832..d594b49 100644 --- a/src/ScillaContractDeployer.ts +++ b/src/ScillaContractDeployer.ts @@ -1,6 +1,6 @@ // This is necessary so that tsc can resolve some of the indirect types for // sc_call, otherwise it errors out - richard@zilliqa.com 2023-03-09 -import { Account, Transaction, TxParams } from "@zilliqa-js/account"; +import { Account, Transaction } from "@zilliqa-js/account"; import { CallParams, Contract, Init, State } from "@zilliqa-js/contract"; import { BN, bytes, Long, units } from "@zilliqa-js/util"; import { Zilliqa } from "@zilliqa-js/zilliqa"; From faa7d3dd86d34e5fb74f22b222572424dfee256d Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Fri, 29 Sep 2023 12:54:44 +0330 Subject: [PATCH 3/4] Add a fixme --- src/ScillaContractDeployer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ScillaContractDeployer.ts b/src/ScillaContractDeployer.ts index d594b49..08c816b 100644 --- a/src/ScillaContractDeployer.ts +++ b/src/ScillaContractDeployer.ts @@ -283,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; From d4dd61757f8a3d129871d5becce6f01b5b990f3b Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Wed, 4 Oct 2023 13:06:49 +0330 Subject: [PATCH 4/4] Fix adt type for complex ones --- contracts/GenerateAdtType.scilla | 17 +++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- src/ScillaParser.ts | 18 +++++++++++++++- test/scilla-generate-adt-type.test.ts | 31 +++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 contracts/GenerateAdtType.scilla create mode 100644 test/scilla-generate-adt-type.test.ts 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 8e6d22d..170e33e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hardhat-scilla-plugin", - "version": "3.5.3", + "version": "3.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "hardhat-scilla-plugin", - "version": "3.5.3", + "version": "3.6.0", "license": "MIT", "dependencies": { "@ethersproject/bignumber": "^5.7.0", diff --git a/package.json b/package.json index fb6f552..4daa0df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hardhat-scilla-plugin", - "version": "3.6.0", + "version": "3.6.1", "description": "Hardhat TypeScript plugin for scilla testing", "repository": "github:Zilliqa/hardhat-scilla-plugin", "author": "Saeed Dadkhah", 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/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)"); + }); +});