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

Support overriding gasPrice, gasLimit, amount, etc. upon contract deployment. #44

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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
17 changes: 17 additions & 0 deletions contracts/GenerateAdtType.scilla
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 11 additions & 4 deletions src/ScillaContractDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,20 @@ 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,
contractInfo.parsedContract.constructorParams,
...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) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -422,4 +429,4 @@ export async function sc_call(
setup.timeout,
true
);
}
}
18 changes: 17 additions & 1 deletion src/ScillaParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extendEnvironment((hre) => {
contractPath: string,
init: Init,
): Promise<[Transaction, ScillaContract]> => {
return deployFromFile(contractPath, init);
return deployFromFile(contractPath, init, {});
}
);

Expand Down
31 changes: 31 additions & 0 deletions test/scilla-generate-adt-type.test.ts
Original file line number Diff line number Diff line change
@@ -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)");
});
});