Skip to content

Commit

Permalink
chore: lint packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-txfusion committed Dec 22, 2023
1 parent 76245d8 commit 7f47602
Show file tree
Hide file tree
Showing 144 changed files with 2,197 additions and 1,994 deletions.
15 changes: 10 additions & 5 deletions packages/hardhat-zksync-chai-matchers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@
"singleQuote": true,
"bracketSpacing": true
},
"dependencies": {},
"dependencies": {
"chai": "^4.3.7",
"zksync-ethers": "^6.0.0",
"ethers": "^6.7.1",
"hardhat": "^2.19.2",
"ordinal":"1.0.3",
"chai-as-promised": "^7.1.1",
"@matterlabs/hardhat-zksync-deploy": "1.1.2",
"@matterlabs/hardhat-zksync-solc": "1.0.6"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
"@types/chai": "^4.2.0",
Expand All @@ -48,20 +57,16 @@
"@typescript-eslint/eslint-plugin": "6.13.1",
"@typescript-eslint/parser": "6.13.1",
"bignumber.js": "^9.1.0",
"chai": "^4.3.7",
"eslint": "^8.54.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.29.0",
"eslint-plugin-no-only-tests": "3.1.0",
"eslint-plugin-prettier": "5.0.1",
"ethers": "^6.7.1",
"hardhat": "^2.19.2",
"mocha": "^10.1.0",
"prettier": "3.1.0",
"rimraf": "^3.0.2",
"ts-node": "^10.6.0",
"typescript": "^5.1.6",
"zksync-ethers": "^6.0.0",
"@matterlabs/zksync-contracts": "^0.6.1",
"@openzeppelin/contracts": "^4.8.3",
"c8": "^8.0.1"
Expand Down
10 changes: 5 additions & 5 deletions packages/hardhat-zksync-chai-matchers/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export class ZkSyncChaiMatchersPluginError extends HardhatPluginError {
}
}

export class ZkSyncChaiMatchersPluginDefaultError extends HardhatPluginError{
constructor(message:string){
super(PLUGIN_NAME,message)
export class ZkSyncChaiMatchersPluginDefaultError extends HardhatPluginError {
constructor(message: string) {
super(PLUGIN_NAME, message);
}
}

export class ZkSyncChaiMatchersPluginAssertionError extends HardhatPluginError {
constructor(message: string) {
super(PLUGIN_NAME,`Assertion error: ${message}`);
super(PLUGIN_NAME, `Assertion error: ${message}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import * as zk from 'zksync-ethers';
import { buildAssert } from '@nomicfoundation/hardhat-chai-matchers/utils';
import { ensure } from '@nomicfoundation/hardhat-chai-matchers/internal/calledOnContract/utils';

import { HttpNetworkConfig } from 'hardhat/types';
import { Account, getAddressOf } from './misc/account';
import { BalanceChangeOptions } from './misc/balance';
import { HttpNetworkConfig } from 'hardhat/types';

export function supportChangeEtherBalance(Assertion: Chai.AssertionStatic) {
Assertion.addMethod(
Expand All @@ -18,7 +18,7 @@ export function supportChangeEtherBalance(Assertion: Chai.AssertionStatic) {
options?: {
balanceChangeOptions?: BalanceChangeOptions;
overrides?: ethers.Overrides;
}
},
) {
const negated = this.__flags.negate;
const subject = this._obj;
Expand All @@ -29,7 +29,7 @@ export function supportChangeEtherBalance(Assertion: Chai.AssertionStatic) {
assert(
actualChange === toBigInt(balanceChange),
`Expected the ether balance of "${address}" to change by ${balanceChange.toString()} wei, but it changed by ${actualChange.toString()} wei`,
`Expected the ether balance of "${address}" NOT to change by ${balanceChange.toString()} wei, but it did`
`Expected the ether balance of "${address}" NOT to change by ${balanceChange.toString()} wei, but it did`,
);
};

Expand All @@ -41,7 +41,7 @@ export function supportChangeEtherBalance(Assertion: Chai.AssertionStatic) {
this.catch = derivedPromise.catch.bind(derivedPromise);
this.promise = derivedPromise;
return this;
}
},
);
}

Expand All @@ -52,9 +52,9 @@ export async function getBalanceChange(
| (() => Promise<zk.types.TransactionResponse> | zk.types.TransactionResponse),
account: Account | string,
options?: BalanceChangeOptions,
overrides?: ethers.Overrides
overrides?: ethers.Overrides,
) {
const hre = await import("hardhat");
const hre = await import('hardhat');
const provider = new zk.Provider((hre.network.config as HttpNetworkConfig).url);

let txResponse: zk.types.TransactionResponse;
Expand All @@ -76,16 +76,14 @@ export async function getBalanceChange(

const balanceAfterHex = await provider.send('eth_getBalance', [address, `0x${txBlockNumber.toString(16)}`]);
const balanceBeforeHex = await provider.send('eth_getBalance', [address, `0x${(txBlockNumber - 1).toString(16)}`]);

const balanceAfter = BigInt(balanceAfterHex);
const balanceBefore = BigInt(balanceBeforeHex);

if (options?.includeFee !== true && address === txResponse.from) {
const gasPrice = overrides?.maxFeePerGas
? (overrides?.maxFeePerGas)
: txReceipt.gasPrice ?? txResponse.gasPrice;
const gasPrice = overrides?.maxFeePerGas ? overrides?.maxFeePerGas : txReceipt.gasPrice ?? txResponse.gasPrice;
const gasUsed = txReceipt.gasUsed;
const txFee:bigint = toBigInt(gasPrice)*gasUsed;
const txFee: bigint = toBigInt(gasPrice) * gasUsed;

return balanceAfter + txFee - balanceBefore;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function supportChangeEtherBalances(Assertion: Chai.AssertionStatic) {
options?: {
balanceChangeOptions?: BalanceChangeOptions;
overrides?: ethers.Overrides;
}
},
) {
const negated = this.__flags.negate;

Expand All @@ -37,10 +37,10 @@ export function supportChangeEtherBalances(Assertion: Chai.AssertionStatic) {
if (!(change === toBigInt(balanceChanges[i]))) {
lines.push(
`Expected the ether balance of ${accountAddresses[i]} (the ${ordinal(
i + 1
i + 1,
)} address in the list) to change by ${balanceChanges[
i
].toString()} wei, but it changed by ${change.toString()} wei`
].toString()} wei, but it changed by ${change.toString()} wei`,
);
}
});
Expand All @@ -52,15 +52,15 @@ export function supportChangeEtherBalances(Assertion: Chai.AssertionStatic) {
if (change === toBigInt(balanceChanges[i])) {
lines.push(
`Expected the ether balance of ${accountAddresses[i]} (the ${ordinal(
i + 1
i + 1,
)} address in the list) NOT to change by ${balanceChanges[
i
].toString()} wei, but it did`
].toString()} wei, but it did`,
);
}
});
return lines.join('\n');
}
},
);
};

Expand All @@ -72,15 +72,15 @@ export function supportChangeEtherBalances(Assertion: Chai.AssertionStatic) {
this.catch = derivedPromise.catch.bind(derivedPromise);
this.promise = derivedPromise;
return this;
}
},
);
}

export async function getBalanceChanges(
transaction: zk.types.TransactionResponse | Promise<zk.types.TransactionResponse>,
accounts: Array<Account | string>,
options?: BalanceChangeOptions,
overrides?: ethers.Overrides
overrides?: ethers.Overrides,
) {
const txResponse = await transaction;

Expand All @@ -99,23 +99,22 @@ async function getTxFees(
accounts: Array<Account | string>,
txResponse: zk.types.TransactionResponse,
options?: BalanceChangeOptions,
overrides?: ethers.Overrides
overrides?: ethers.Overrides,
) {

return Promise.all(
accounts.map(async (account) => {
if (options?.includeFee !== true && (await getAddressOf(account)) === txResponse.from) {
const txReceipt = await txResponse.wait();
const gasPrice = overrides?.maxFeePerGas
? (overrides?.maxFeePerGas)
? overrides?.maxFeePerGas
: txReceipt.gasPrice ?? txResponse.gasPrice;
const gasUsed = txReceipt.gasUsed;
const txFee = toBigInt(gasPrice)*gasUsed;
const txFee = toBigInt(gasPrice) * gasUsed;

return txFee;
}

return 0n
})
return 0n;
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ import * as zk from 'zksync-ethers';
import { buildAssert } from '@nomicfoundation/hardhat-chai-matchers/utils';
import { ensure } from '@nomicfoundation/hardhat-chai-matchers/internal/calledOnContract/utils';

import { Account, getAddressOf } from './misc/account';
import { HttpNetworkConfig } from 'hardhat/types';
import { BaseContract, BaseContractMethod, BigNumberish, ContractTransactionResponse, toBigInt } from 'ethers';
import { BaseContractMethod, BigNumberish, ContractTransactionResponse, toBigInt } from 'ethers';
import { Account, getAddressOf } from './misc/account';

export type Token = zk.Contract & {
balanceOf: BaseContractMethod<[string], bigint, bigint>;
name: BaseContractMethod<[], string, string>;
transfer: BaseContractMethod<
[string, BigNumberish],
boolean,
ContractTransactionResponse
>;
transfer: BaseContractMethod<[string, BigNumberish], boolean, ContractTransactionResponse>;
symbol: BaseContractMethod<[], string, string>;
};

export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
Assertion.addMethod(
'changeTokenBalance',
function (this: any, token: Token, account: Account | string, balanceChange: BigNumberish) {

const negated = this.__flags.negate;

let subject = this._obj;
Expand All @@ -38,7 +33,7 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
assert(
actualChange === toBigInt(balanceChange),
`Expected the balance of ${tokenDescription} tokens for "${address}" to change by ${balanceChange.toString()}, but it changed by ${actualChange.toString()}`,
`Expected the balance of ${tokenDescription} tokens for "${address}" NOT to change by ${balanceChange.toString()}, but it did`
`Expected the balance of ${tokenDescription} tokens for "${address}" NOT to change by ${balanceChange.toString()}, but it did`,
);
};

Expand All @@ -52,7 +47,7 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
this.catch = derivedPromise.catch.bind(derivedPromise);

return this;
}
},
);

Assertion.addMethod(
Expand All @@ -69,19 +64,19 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {

if (accounts.length !== balanceChanges.length) {
throw new Error(
`The number of accounts (${accounts.length}) is different than the number of expected balance changes (${balanceChanges.length})`
`The number of accounts (${accounts.length}) is different than the number of expected balance changes (${balanceChanges.length})`,
);
}

const balanceChangesPromise = Promise.all(
accounts.map((account) => getBalanceChange(subject, token, account))
accounts.map((account) => getBalanceChange(subject, token, account)),
);
const addressesPromise = Promise.all(accounts.map(getAddressOf));

const checkBalanceChanges = ([actualChanges, addresses, tokenDescription]: [
bigint[],
string[],
string
string,
]) => {
const assert = buildAssert(negated, checkBalanceChanges);

Expand All @@ -92,7 +87,7 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
}, respectively, but they changed by ${actualChanges as any}`,
`Expected the balances of ${tokenDescription} tokens for ${addresses as any} NOT to change by ${
balanceChanges as any
}, respectively, but they did`
}, respectively, but they did`,
);
};

Expand All @@ -106,24 +101,24 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
this.catch = derivedPromise.catch.bind(derivedPromise);

return this;
}
},
);
}

function checkToken(token: unknown, method: string) {
if (typeof token !== 'object' || token === null || !('interface' in token)) {
throw new Error(`The first argument of ${method} must be the contract instance of the token`);
} else if ((token as any).interface.getFunction("balanceOf") === null) {
} else if ((token as any).interface.getFunction('balanceOf') === null) {
throw new Error('The given contract instance is not an ERC20 token');
}
}

export async function getBalanceChange(
transaction: zk.types.TransactionResponse | Promise<zk.types.TransactionResponse>,
token: Token,
account: Account | string
account: Account | string,
) {
const hre = await import("hardhat");
const hre = await import('hardhat');
const provider = new zk.Provider((hre.network.config as HttpNetworkConfig).url);

const txResponse = await transaction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import type { Signer, Wallet,Contract } from 'zksync-ethers';
import type { Signer, Wallet, Contract } from 'zksync-ethers';
import { ZkSyncChaiMatchersPluginAssertionError } from '../../errors';

export type Account = Signer | Wallet | Contract;
Expand All @@ -9,17 +9,15 @@ export function isWalletOrContract(account: Account): account is Contract | Wall
return account instanceof zk.Contract || account instanceof zk.Wallet;
}

export async function getAddressOf(account: Account | string):Promise<string> {
const { isAddressable } = await import("ethers");
export async function getAddressOf(account: Account | string): Promise<string> {
const { isAddressable } = await import('ethers');

if (typeof account === 'string') {
assert(/^0x[0-9a-fA-F]{40}$/.test(account), `Invalid address ${account}`);
return account;
}

if(isAddressable(account)) return await account.getAddress();

throw new ZkSyncChaiMatchersPluginAssertionError(
`Expected string or addressable, got ${account as any}`
);

if (isAddressable(account)) return account.getAddress();

throw new ZkSyncChaiMatchersPluginAssertionError(`Expected string or addressable, got ${account as any}`);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as zk from 'zksync-ethers';

import { Account, getAddressOf } from './account';
import { HttpNetworkConfig } from 'hardhat/types';
import { toBigInt } from 'ethers';
import { Account, getAddressOf } from './account';

export interface BalanceChangeOptions {
includeFee?: boolean;
Expand All @@ -13,14 +13,14 @@ export function getAddresses(accounts: Array<Account | string>) {
}

export async function getBalances(accounts: Array<Account | string>, blockNumber?: number) {
const hre = await import("hardhat");
const hre = await import('hardhat');
const provider = new zk.Provider((hre.network.config as HttpNetworkConfig).url);

return Promise.all(
accounts.map(async (account) => {
const address = await getAddressOf(account);
const result = await provider.send('eth_getBalance', [address, `0x${blockNumber?.toString(16) ?? 0}`]);
return toBigInt(result)
})
return toBigInt(result);
}),
);
}
Loading

0 comments on commit 7f47602

Please sign in to comment.