Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
saeed-zil committed Mar 5, 2024
1 parent be3649b commit 78fac50
Show file tree
Hide file tree
Showing 25 changed files with 343 additions and 294 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.eslintrc.js
.eslintrc.js
dist
node_modules
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"zilliqa"
],
"scripts": {
"lint:fix": "prettier --write 'src/**/*.{js,ts}' 'test/**/*.{js,ts}' && eslint --fix -c .eslintrc.js --ext .ts src",
"lint": "eslint -c .eslintrc.js --ext .ts src",
"lint:fix": "prettier --write 'src/**/*.{js,ts}' 'test/**/*.{js,ts}' && eslint --fix . --ext .ts --fix",
"lint": "eslint . --ext .ts",
"test": "mocha --exit --recursive 'test/**/*.test.ts' --exclude 'test/**/*.live.test.ts'",
"test-all": "mocha --exit --recursive 'test/**/*.test.ts'",
"test-live": "mocha --exit --recursive 'test/**/*.live.test.ts'",
Expand Down
1 change: 0 additions & 1 deletion scripts/force-scilla-download.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parseScilla } from "../src/ScillaParser"


parseScilla("test/fixture-projects/hardhat-project/contracts/scilla/HelloWorld.scilla");
26 changes: 13 additions & 13 deletions src/ScillaChaiMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ export const scillaChaiEventMatcher = function (
);
});

Assertion.addMethod("eventLogWithParams", async function (
eventName: string,
...params: EventParam[]
) {
const tx: Transaction = this._obj;
Assertion.addMethod(
"eventLogWithParams",
async function (eventName: string, ...params: EventParam[]) {
const tx: Transaction = this._obj;

const receipt = tx.getReceipt()!;
await new Assertion(this._obj).to.eventLog(eventName);
const receipt = tx.getReceipt()!;
await new Assertion(this._obj).to.eventLog(eventName);

const event_logs = simplifyLogs(receipt.event_logs!);
const desiredLog = event_logs.filter(
(log: any) => log._eventname === eventName
);
const event_logs = simplifyLogs(receipt.event_logs!);
const desiredLog = event_logs.filter(
(log: any) => log._eventname === eventName
);

new Assertion(desiredLog[0].params).to.containSubset(params);
});
new Assertion(desiredLog[0].params).to.containSubset(params);
}
);
};
22 changes: 13 additions & 9 deletions src/ScillaChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import * as ZilliqaUtils from "./ZilliqaUtils";
export async function runScillaChecker(contracts: any, libdir: any) {
let files: string[] = [];
if (libdir === undefined && process.env.USE_NATIVE_SCILLA) {
throw new Error("You must specify a libDir (to stdlib) if running scilla-checker natively")
throw new Error(
"You must specify a libDir (to stdlib) if running scilla-checker natively"
);
}

if (contracts.length === 0) {
files = glob.sync("contracts/**/*.scilla");
} else {
files = contracts;
}
}
files.forEach((file) => {
try {
console.log(clc.greenBright.bold(`🔍Checking ${file}...`));
Expand All @@ -27,16 +29,18 @@ export async function runScillaChecker(contracts: any, libdir: any) {
} else {
let libArg;
let programArg;
const resolvedFilename = path.resolve(file)
const resolvedFilename = path.resolve(file);
if (libdir === undefined) {
programArg = "-libdir /scilla/0/src/stdlib"
libArg = ""
programArg = "-libdir /scilla/0/src/stdlib";
libArg = "";
} else {
const absPath = path.resolve(libdir)
libArg = `-v ${absPath}:/stdlib`
programArg = "-libdir /stdlib"
const absPath = path.resolve(libdir);
libArg = `-v ${absPath}:/stdlib`;
programArg = "-libdir /stdlib";
}
value = execSync(`docker run --rm -v ${resolvedFilename}:/tmp/input.scilla ${libArg} zilliqa/scilla:v0.13.3 /scilla/0/bin/scilla-checker -gaslimit 10000 ${programArg} /tmp/input.scilla`)
value = execSync(
`docker run --rm -v ${resolvedFilename}:/tmp/input.scilla ${libArg} zilliqa/scilla:v0.13.3 /scilla/0/bin/scilla-checker -gaslimit 10000 ${programArg} /tmp/input.scilla`
);
}
console.log(value.toString());
} catch (error) {
Expand Down
40 changes: 23 additions & 17 deletions src/ScillaContractDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";

import * as ScillaContractProxy from "./ScillaContractProxy";
import { ContractInfo } from "./ScillaContractsInfoUpdater";
import {
Field,
Fields,
isNumeric,
} from "./ScillaParser";

import { Field, Fields, isNumeric } from "./ScillaParser";

export interface Value {
vname: string;
Expand All @@ -33,7 +28,6 @@ export interface Setup {
accounts: Account[];
}


export let setup: Setup | null = null;

// The optional params are listed in popularity order.
Expand Down Expand Up @@ -76,11 +70,14 @@ function read(f: string) {
/// gasPrice, gasLimit, attempts, timeout.
export function updateSetup(args: any) {
if (setup === null) {
throw new HardhatPluginError("hardhat-scilla-plugin", "Please call the initZilliqa function.");
throw new HardhatPluginError(
"hardhat-scilla-plugin",
"Please call the initZilliqa function."
);
}
const overrides : any = { }
const overrides: any = {};
if (args.gasPrice) {
overrides.gasPrice = units.toQa(args.gasPrice.toString(), units.Units.Li);
overrides.gasPrice = units.toQa(args.gasPrice.toString(), units.Units.Li);
}
if (args.gasLimit) {
overrides.gasLimit = Long.fromNumber(args.gasLimit);
Expand All @@ -91,7 +88,7 @@ export function updateSetup(args: any) {
if (args.attempts) {
overrides.attempts = args.attempts;
}
const newSetup : Setup = { ...setup, ... overrides };
const newSetup: Setup = { ...setup, ...overrides };
setup = newSetup;
}

Expand Down Expand Up @@ -133,14 +130,18 @@ export async function deploy(
hre: HardhatRuntimeEnvironment,
contractName: string,
userDefinedLibraries: OptionalUserDefinedLibraryList,
...args: any[]): Promise<ScillaContract> {
...args: any[]
): Promise<ScillaContract> {
const contractInfo: ContractInfo = hre.scillaContracts[contractName];
if (contractInfo === undefined) {
throw new Error(`Scilla contract ${contractName} doesn't exist.`);
}

let txParamsForContractDeployment = {};
if (contractInfo.parsedContract.constructorParams && args.length === contractInfo.parsedContract.constructorParams.length + 1) {
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();
}
Expand All @@ -152,7 +153,11 @@ export async function deploy(
...args
);

const [tx, sc] = await deployFromFile(contractInfo.path, init, txParamsForContractDeployment);
const [tx, sc] = await deployFromFile(
contractInfo.path,
init,
txParamsForContractDeployment
);
sc.deployed_by = tx;

ScillaContractProxy.injectProxies(setup!, contractInfo, sc);
Expand All @@ -162,7 +167,7 @@ export async function deploy(

export const deployLibrary = async (
hre: HardhatRuntimeEnvironment,
libraryName: string,
libraryName: string
): Promise<ScillaContract> => {
const contractInfo: ContractInfo = hre.scillaContracts[libraryName];
if (contractInfo === undefined) {
Expand All @@ -171,7 +176,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, {}); // FIXME: In #45
sc.deployed_by = tx;

return sc;
Expand Down Expand Up @@ -232,7 +237,8 @@ const fillInit = (
type: param.type,
value: userSpecifiedArgs[index].toString(),
});
} else { // It's an ADT or string
} else {
// It's an ADT or string
init.push({
vname: param.name,
type: param.type,
Expand Down
37 changes: 22 additions & 15 deletions src/ScillaContractInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,45 @@ import * as ScillaContractDeployer from "./ScillaContractDeployer";
import * as ScillaContractProxy from "./ScillaContractProxy";
import { ContractInfo } from "./ScillaContractsInfoUpdater";
import * as ScillaContractsInfoUpdater from "./ScillaContractsInfoUpdater";
import {
parseScilla,
} from "./ScillaParser";
import * as ZilliqaUtils from './ZilliqaUtils';
import { parseScilla } from "./ScillaParser";
import * as ZilliqaUtils from "./ZilliqaUtils";

export async function contractFromAddress(hre: HardhatRuntimeEnvironment, address: string) : Promise<ScillaContract | undefined> {
export async function contractFromAddress(
hre: HardhatRuntimeEnvironment,
address: string
): Promise<ScillaContract | undefined> {
// Fetch the code from the blockchain
if (ScillaContractDeployer.setup === null) {
throw new HardhatPluginError("hardhat-scilla-plugin", "Please call the initZilliqa function.");
throw new HardhatPluginError(
"hardhat-scilla-plugin",
"Please call the initZilliqa function."
);
}
const setup = ScillaContractDeployer.setup
const zilliqa = setup.zilliqa
const setup = ScillaContractDeployer.setup;
const zilliqa = setup.zilliqa;
const codeResult = await zilliqa.blockchain.getSmartContractCode(address);
if (codeResult !== undefined && codeResult.result !== undefined
&& codeResult.result.code !== undefined) {
if (
codeResult !== undefined &&
codeResult.result !== undefined &&
codeResult.result.code !== undefined
) {
const codeText = codeResult.result.code;
// 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 :-( )
const tempFile = ZilliqaUtils.createTemporaryFile('contract', 'scilla');
fs.writeFileSync( tempFile, codeText );
const tempFile = ZilliqaUtils.createTemporaryFile("contract", "scilla");
fs.writeFileSync(tempFile, codeText);
const parsed = await parseScilla(tempFile);
const hash = ScillaContractsInfoUpdater.getFileHash(tempFile);
const contractInfo : ContractInfo = {
const contractInfo: ContractInfo = {
hash,
path: address,
parsedContract: parsed
parsedContract: parsed,
};

ZilliqaUtils.deleteTemporaryFile(tempFile);
// OK. Now I need to create a contract factory ..
// We don't actually need the ABI
const contract = zilliqa.contracts.at( address, undefined );
const contract = zilliqa.contracts.at(address, undefined);
// Now we can fill the proxies in.
ScillaContractProxy.injectConnectors(setup, contract);
// Set the default deployer
Expand Down
27 changes: 17 additions & 10 deletions src/ScillaContractProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CallParams, State } from "@zilliqa-js/contract";
import { BN } from "@zilliqa-js/util";
import { HardhatPluginError } from "hardhat/plugins";

import * as ScillaContractDeployer from './ScillaContractDeployer';
import { ScillaContract, Value, Setup } from './ScillaContractDeployer';
import * as ScillaContractDeployer from "./ScillaContractDeployer";
import { ScillaContract, Value, Setup } from "./ScillaContractDeployer";
import { ContractInfo } from "./ScillaContractsInfoUpdater";
import {
Field,
Expand Down Expand Up @@ -90,16 +90,24 @@ export function injectConnectors(setup: Setup, sc: ScillaContract) {
sc.executer = signer;

// If account is not added already, add it to the list.
if (setup?.accounts.findIndex((acc) => acc.privateKey === signer.privateKey) === -1) {
if (
setup?.accounts.findIndex(
(acc) => acc.privateKey === signer.privateKey
) === -1
) {
setup?.zilliqa.wallet.addByPrivateKey(signer.privateKey);
setup?.accounts.push(signer);
}
return sc;
}
};
}

// Inject proxy functions into a contract.
export function injectProxies(setup: Setup, contractInfo: ContractInfo, sc: ScillaContract) {
// Inject proxy functions into a contract.
export function injectProxies(
setup: Setup,
contractInfo: ContractInfo,
sc: ScillaContract
) {
contractInfo.parsedContract.transitions.forEach((transition) => {
sc[transition.name] = async (...args: any[]) => {
let callParams: CallParams = {
Expand Down Expand Up @@ -166,9 +174,8 @@ export async function sc_call(
args: Value[] = [],
callParams: CallParams
) {

if (ScillaContractDeployer.setup === null) {
throw new HardhatPluginError(
if (ScillaContractDeployer.setup === null) {
throw new HardhatPluginError(
"hardhat-scilla-plugin",
"Please call initZilliqa function."
);
Expand All @@ -185,5 +192,5 @@ export async function sc_call(
ScillaContractDeployer.setup.attempts,
ScillaContractDeployer.setup.timeout,
true
);
);
}
1 change: 0 additions & 1 deletion src/ScillaContractsInfoUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export const getFileHash = (fileName: string): string => {
return hashSum.digest("hex");
};


const parseScillaFile = async (
fileName: string
): Promise<ContractInfo | null> => {
Expand Down
Loading

0 comments on commit 78fac50

Please sign in to comment.