Skip to content

Commit

Permalink
(fix) #46 Fix PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
rrw-zilliqa committed Oct 6, 2023
1 parent 67c6573 commit 5a07876
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ contract2 = await hre.deployScillaWithLib("TestContract2",
```
To change the deployer of the contract, you can send an instance of `Account` class to `hre.setActiveAccount`.
### Change the default parameters when deploying a contract
You can call
```
hre.setScillaDefaults( obj )
```
to set the defaults used when deploying a Scilla contract. Parameters supported are:
* `gasPrice` - a string denoting the gas price in `Li` (to match the `initZilliqa` use).
* `gasLimit` - a string denoting the gas limit (in `Qa`, to match `initZilliqa` use)
* `attempts` - a number denoting the number of attempts to make to check whether a transaction has been accepted
* `timeout` - the space between attempts, in milliseconds.
### Connect to an existing Scilla contract
Call
```
hre.interactWithScillaContract(address)
```
To:
* Retrieve the code for a contract from the configured chain.
* Parse it.
* Construct a proxy contract object for it.
* Return that object, or `undefined` if we failed.
`address` should be a string, and the function returns `ScillaContract | undefined`.
### Call a transition
It's not harder than calling a normal function in typescript.
Expand Down Expand Up @@ -150,6 +183,7 @@ await contract.Set(12, {nonce: 12, amount: new BN(1000)});
```
### call a transition with a new account
You can call `connect` on a contract to change its default account which is used to execute transitions.
```typescript
Expand Down Expand Up @@ -224,7 +258,7 @@ For easier value matching, some value conversions are done under the hood.
* 128/256 bit integer values are converted to `BigNumber`
* `Option` is converted to its inner value if exists any, or `null` otherwise.
* `Bool` is converted to underlying boolean value.
for more tests please take look at [scilla tests](https://github.com/Zilliqa/Zilliqa/tree/master/tests/EvmAcceptanceTests/test/scilla).
### TODO
Expand Down
23 changes: 14 additions & 9 deletions src/ScillaContractDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,20 @@ export function updateSetup(args: any) {
if (setup === null) {
throw new HardhatPluginError("hardhat-scilla-plugin", "Please call the initZilliqa function.");
}
let newSetup : Setup = {
zilliqa: setup.zilliqa,
version: setup.version,
gasPrice: args.gasPrice ? units.toQa(args.gasPrice.toString(), units.Units.Li) : setup.gasPrice,
gasLimit: args.gasLimit ? Long.fromNumber(args.gasLimit) : setup.gasLimit,
attempts: args.attempts ?? setup.attempts,
timeout: args.timeout ?? setup.timeout,
accounts : setup.accounts
};
let overrides : any = { }
if (args.gasPrice) {
overrides.gasPrice = units.toQa(args.gasPrice.toString(), units.Units.Li);
}
if (args.gasLimit) {
overrides.gasLimit = Long.fromNumber(args.gasLimit);
}
if (args.timeout) {
overrides.timeout = args.timeout;
}
if (args.attempts) {
overrides.attempts = args.attempts;
}
let newSetup : Setup = { ...setup, ... overrides };
setup = newSetup;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ScillaContractInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export async function contractFromAddress(hre: HardhatRuntimeEnvironment, addres
if (codeResult !== undefined && codeResult.result !== undefined
&& codeResult.result.code !== undefined) {
let codeText = codeResult.result.code;
// Now parse it. Sadly, need a file for this. Even more sadly,m
// Now parse it. Sadly, need a file for this. Even more sadly, there's no good way to do it
// generically (tempy causes us to throw module errors :-( )
let tempFile = ZilliqaUtils.createTemporaryFile('contract', 'scilla');
fs.writeFileSync( tempFile, codeText );
let parsed = await parseScilla(tempFile);
Expand Down
1 change: 0 additions & 1 deletion src/ScillaContractsInfoUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type ContractMapByPath = Record<ContractPath, ContractInfo>;
export const updateContractsInfo = async () => {
let contractsInfo: ContractMapByName = {};
const files = glob.sync("contracts/**/*(*.scilla|*.scillib)");
console.log(`files = ${files}`);
if (files.length === 0) {
console.log(
clc.yellowBright("No scilla contracts were found in contracts directory.")
Expand Down
35 changes: 35 additions & 0 deletions test/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BN, Long } from "@zilliqa-js/util";
import { expect } from "chai";

import * as ZilliqaHardhatObject from "../src/ZilliqaHardhatObject";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { useEnvironment } from "./helpers"

import chai from 'chai';
import { scillaChaiEventMatcher } from '../src/ScillaChaiMatchers';
import { setup } from '../src/ScillaContractDeployer';

chai.use(scillaChaiEventMatcher);

describe("", function () {

var hre : HardhatRuntimeEnvironment;
var zobj : ZilliqaHardhatObject.ZilliqaHardhatObject;

useEnvironment("hardhat-project");
describe("Contract deployment", function () {
it("Should be able to override deployment parameters", async function () {
this.hre.setScillaDefaults( { "gasPrice" : "400", "gasLimit" : "100000", "attempts" : 46, "timeout" : 235 } );
expect(setup!.attempts).to.equal(46);
expect(setup!.timeout).to.equal(235);
expect(setup!.gasPrice.cmp(new BN('400000000'))).to.equal(0);
expect(setup!.gasLimit.equals(Long.fromNumber(100000))).to.be.true;
});
it("Should be possible to set partial overrides", async function () {
let oldTimeout = setup!.timeout;
this.hre.setScillaDefaults( { "attempts" : 52 } );
expect(setup!.attempts).to.equal(52);
expect(setup!.timeout).to.equal(oldTimeout);
});
});
});
1 change: 1 addition & 0 deletions test/deploy.live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useEnvironment } from "./helpers"

import chai from 'chai';
import { scillaChaiEventMatcher } from '../src/ScillaChaiMatchers';
import { setup } from '../src/ScillaContractDeployer';

chai.use(scillaChaiEventMatcher);

Expand Down

0 comments on commit 5a07876

Please sign in to comment.