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

Fix tests. Prepare before publish #65

Merged
merged 2 commits into from
Sep 18, 2024
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
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,11 +1036,11 @@ const customGetTransactionReceipt = async (
transactionHash as HexString,
);
if (isNullish(response)) {
throw new Error(`Transaction with hash ${transactionHash} not found`);
return response as unknown as TransactionReceipt;
}
return web3Utils.format(
ZKTransactionReceiptSchema,
response as unknown as TransactionReceipt,
response as TransactionReceipt,
returnFormat ?? web3Context.defaultReturnFormat,
);
};
Expand Down
12 changes: 9 additions & 3 deletions test/integration/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ describe('Wallet', () => {
to: ADDRESS2,
value: web3Utils.toHex(7_000_000_000),
});
console.log('result', result);
deepEqualExcluding(result, tx, [
'gasLimit',
'gasPrice',
Expand All @@ -232,9 +233,9 @@ describe('Wallet', () => {
]);
expect(toBigInt(result.gasLimit) > 0n).toEqual(true);
expect(
toBigInt(result.maxPriorityFeePerGas) > 0n ||
toBigInt(result.maxFeePerGas) > 0n ||
toBigInt(result.gasPrice) > 0n,
(result.maxPriorityFeePerGas && toBigInt(result.maxPriorityFeePerGas) > 0n) ||
(result.maxFeePerGas && toBigInt(result.maxFeePerGas) > 0n) ||
(result.gasPrice && toBigInt(result.gasPrice) > 0n),
).toEqual(true);
});
it('should return a populated transaction with default values if are omitted', async () => {
Expand Down Expand Up @@ -1311,6 +1312,11 @@ describe('Wallet', () => {
const l2BalanceAfterExecution = await wallet.getBalance();
const l1BalanceAfterExecution = await wallet.getBalanceL1();
expect(result).not.toBeNull();
console.log({
l2BalanceAfterExecution,
l2BalanceBeforeExecution,
amount,
});
expect(l2BalanceAfterExecution - l2BalanceBeforeExecution >= amount).toEqual(true);
expect(l1BalanceBeforeExecution - l1BalanceAfterExecution >= amount).toEqual(true);
});
Expand Down
4 changes: 1 addition & 3 deletions test/local/account-abstraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import { L2_CHAIN_URL } from '../utils';
const { ETH_ADDRESS } = constants;
const accounts = getAccounts();

jest.setTimeout(300000);
jest.setTimeout(60000);
describe('Account Abstraction', () => {
const l2Provider = new Web3ZKsyncL2(L2_CHAIN_URL);
const PRIVATE_KEY1 = accounts[0].privateKey;
const ADDRESS1 = accounts[0].address;
const wallet = new ZKsyncWallet(PRIVATE_KEY1, l2Provider);
const acc = l2Provider.eth.accounts.privateKeyToAccount(PRIVATE_KEY1);
l2Provider.eth.accounts.wallet.add(acc);
it('use the ERC20 token for paying transaction fee', async () => {
const InitMintAmount = 10n;
const mintAmount = 3n;
Expand Down
5 changes: 1 addition & 4 deletions test/local/deposit-withdraw.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Web3ZKsyncL2, Web3ZKsyncL1, ZKsyncWallet } from '../../src';
import { ETH_ADDRESS } from '../../src/constants';
import * as utils from '../../src/utils';
import { getAccounts } from './fixtures';
import { L1_CHAIN_URL, L2_CHAIN_URL } from '../utils';

Expand All @@ -9,10 +8,8 @@ import { L1_CHAIN_URL, L2_CHAIN_URL } from '../utils';
// to be able to run the test.
// Additionally, the test needs to be fixed as `wallet.deposit` throws an internal exception.

jest.setTimeout(10000);
jest.setTimeout(60000);
describe('wallet', () => {
// @ts-ignore
TransactionFactory.registerTransactionType(EIP712_TX_TYPE, utils.EIP712Transaction);
const l1Provider = new Web3ZKsyncL1(L1_CHAIN_URL);
const accounts = getAccounts();
const l2Provider = new Web3ZKsyncL2(L2_CHAIN_URL);
Expand Down
2 changes: 1 addition & 1 deletion test/local/smart-account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../../src';
import { Address } from 'web3';

jest.setTimeout(50000);
jest.setTimeout(60000);
const accounts = getAccounts();
const mainAccount = accounts[0];
const PRIVATE_KEY = mainAccount.privateKey;
Expand Down
2 changes: 1 addition & 1 deletion test/local/wallet-send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getPreparedWallet,
} from './fixtures';

jest.setTimeout(600000);
jest.setTimeout(60000);

const accounts = getAccounts();
const mainAccount = accounts[0];
Expand Down
2 changes: 1 addition & 1 deletion test/local/wallet-transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getPreparedWallet,
} from './fixtures';

jest.setTimeout(600000);
jest.setTimeout(60000);

const accounts = getAccounts();
const mainAccount = accounts[0];
Expand Down
2 changes: 1 addition & 1 deletion test/local/wallet-withdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
prepareAccount,
} from './fixtures';
import { toWei } from 'web3-utils';
jest.setTimeout(600000);
jest.setTimeout(60000);

const accounts = getAccounts();
const mainAccount = accounts[0];
Expand Down
110 changes: 80 additions & 30 deletions test/local/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from './fixtures';
import { ReceiptError } from '../../src/errors';

jest.setTimeout(600000);
jest.setTimeout(60000);

const accounts = getAccounts();
const mainAccount = accounts[0];
Expand Down Expand Up @@ -552,7 +552,12 @@ describe('Wallet', () => {
});
result.to = result.to.toLowerCase();
const { data: a, maxFeePerGas: b, maxPriorityFeePerGas: c, ...otherTx } = tx;
const { data: d, maxFeePerGas: e, maxPriorityFeePerGas: f, ...otherResult } = result;
const {
data: d,
maxFeePerGas: e,
maxPriorityFeePerGas: f,
...otherResult
} = result;

expect(otherResult).toEqual(otherTx);
expect(web3Utils.toBigInt(result.maxPriorityFeePerGas) > 0n).toEqual(true);
Expand Down Expand Up @@ -704,7 +709,8 @@ describe('Wallet', () => {
const l2Hash = (error as ReceiptError).hash!;
while (!blockNumber) {
try {
blockNumber = (await wallet.provider!.eth.getTransaction(l2Hash)).blockNumber!;
blockNumber = (await wallet.provider!.eth.getTransaction(l2Hash))
.blockNumber!;
} catch (e) {
console.log('e', e);
}
Expand Down Expand Up @@ -778,7 +784,9 @@ describe('Wallet', () => {
to: wallet.getAddress(),
});
} catch (e) {
expect((e as Error).message).toEqual('Not enough allowance to cover the deposit!');
expect((e as Error).message).toEqual(
'Not enough allowance to cover the deposit!',
);
}
});

Expand Down Expand Up @@ -886,7 +894,10 @@ describe('Wallet', () => {
);

// mint base token to random wallet
const baseToken = new ethProvider.eth.Contract(IERC20ABI, await wallet.getBaseToken());
const baseToken = new ethProvider.eth.Contract(
IERC20ABI,
await wallet.getBaseToken(),
);

await baseToken.methods
.mint(randomWallet.getAddress(), web3Utils.toWei('0.5', 'ether'))
Expand All @@ -908,7 +919,9 @@ describe('Wallet', () => {
to: wallet.getAddress(),
});
} catch (e) {
expect((e as Error).message).toEqual('Not enough token allowance to cover the deposit!');
expect((e as Error).message).toEqual(
'Not enough token allowance to cover the deposit!',
);
}
});
}
Expand Down Expand Up @@ -1095,8 +1108,12 @@ describe('Wallet', () => {
PAYMASTER,
);
const senderBalanceBeforeTransfer = await wallet.getBalance(token);
const senderApprovalTokenBalanceBeforeTransfer = await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceBeforeTransfer = await provider.getTokenBalance(token, ADDRESS2);
const senderApprovalTokenBalanceBeforeTransfer =
await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceBeforeTransfer = await provider.getTokenBalance(
token,
ADDRESS2,
);

const result = await wallet.transfer({
token: token,
Expand All @@ -1116,22 +1133,30 @@ describe('Wallet', () => {
PAYMASTER,
);
const senderBalanceAfterTransfer = await wallet.getBalance(token);
const senderApprovalTokenBalanceAfterTransfer = await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceAfterTransfer = await provider.getTokenBalance(token, ADDRESS2);

expect(paymasterBalanceBeforeTransfer - paymasterBalanceAfterTransfer >= 0n).toEqual(true);
expect(paymasterTokenBalanceAfterTransfer - paymasterTokenBalanceBeforeTransfer).toEqual(
minimalAllowance,
const senderApprovalTokenBalanceAfterTransfer =
await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceAfterTransfer = await provider.getTokenBalance(
token,
ADDRESS2,
);

expect(
paymasterBalanceBeforeTransfer - paymasterBalanceAfterTransfer >= 0n,
).toEqual(true);
expect(
paymasterTokenBalanceAfterTransfer - paymasterTokenBalanceBeforeTransfer,
).toEqual(minimalAllowance);

expect(senderBalanceBeforeTransfer - senderBalanceAfterTransfer).toEqual(amount);
expect(
senderApprovalTokenBalanceAfterTransfer ===
senderApprovalTokenBalanceBeforeTransfer - minimalAllowance,
).toEqual(true);

expect(result).not.toBeNull();
expect(receiverBalanceAfterTransfer - receiverBalanceBeforeTransfer).toEqual(amount);
expect(receiverBalanceAfterTransfer - receiverBalanceBeforeTransfer).toEqual(
amount,
);
});
}

Expand Down Expand Up @@ -1165,8 +1190,13 @@ describe('Wallet', () => {
APPROVAL_TOKEN,
);
const senderBalanceBeforeTransfer = await wallet.getBalance(l2DAI);
const senderApprovalTokenBalanceBeforeTransfer = await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceBeforeTransfer = await provider.getBalance(toAddress, 'latest', l2DAI);
const senderApprovalTokenBalanceBeforeTransfer =
await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceBeforeTransfer = await provider.getBalance(
toAddress,
'latest',
l2DAI,
);
const tx = await wallet.transfer({
token: l2DAI,
to: toAddress,
Expand All @@ -1188,9 +1218,15 @@ describe('Wallet', () => {
);
const senderBalanceAfterTransfer = await wallet.getBalance(l2DAI);
const senderApprovalTokenBalanceAfterTransfer = await wallet.getBalance(APPROVAL_TOKEN);
const receiverBalanceAfterTransfer = await provider.getBalance(toAddress, 'latest', l2DAI);
const receiverBalanceAfterTransfer = await provider.getBalance(
toAddress,
'latest',
l2DAI,
);

expect(paymasterBalanceBeforeTransfer - paymasterBalanceAfterTransfer >= 0n).toBeTruthy();
expect(
paymasterBalanceBeforeTransfer - paymasterBalanceAfterTransfer >= 0n,
).toBeTruthy();
expect(paymasterTokenBalanceAfterTransfer - paymasterTokenBalanceBeforeTransfer).toBe(
minimalAllowance,
);
Expand Down Expand Up @@ -1239,7 +1275,9 @@ describe('Wallet', () => {
const result = await wallet.finalizeWithdrawal(txHash);
const l2BalanceAfterWithdrawal = await wallet.getBalance();
expect(result).not.toBeNull();
expect(l2BalanceBeforeWithdrawal - l2BalanceAfterWithdrawal >= amount).toEqual(true);
expect(l2BalanceBeforeWithdrawal - l2BalanceAfterWithdrawal >= amount).toEqual(
true,
);
});
} else {
it('should withdraw ETH to the L1 network', async () => {
Expand All @@ -1252,12 +1290,16 @@ describe('Wallet', () => {
amount: amount,
});
const withdrawTx = await tx.waitFinalize();
expect(await wallet.isWithdrawalFinalized(withdrawTx.transactionHash)).toEqual(false);
expect(await wallet.isWithdrawalFinalized(withdrawTx.transactionHash)).toEqual(
false,
);

const result = await wallet.finalizeWithdrawal(withdrawTx.transactionHash);
const l2BalanceAfterWithdrawal = await wallet.getBalance(token);
expect(result).not.toBeNull();
expect(l2BalanceBeforeWithdrawal - l2BalanceAfterWithdrawal >= amount).toEqual(true);
expect(l2BalanceBeforeWithdrawal - l2BalanceAfterWithdrawal >= amount).toEqual(
true,
);
});

it('should withdraw base token to the L1 network', async () => {
Expand All @@ -1270,12 +1312,16 @@ describe('Wallet', () => {
amount: amount,
});
const withdrawTx = await tx.waitFinalize();
expect(await wallet.isWithdrawalFinalized(withdrawTx.transactionHash)).toEqual(false);
expect(await wallet.isWithdrawalFinalized(withdrawTx.transactionHash)).toEqual(
false,
);

const result = await wallet.finalizeWithdrawal(withdrawTx.transactionHash);
const l2BalanceAfterWithdrawal = await wallet.getBalance();
expect(result).not.toBeNull();
expect(l2BalanceBeforeWithdrawal - l2BalanceAfterWithdrawal >= amount).toEqual(true);
expect(l2BalanceBeforeWithdrawal - l2BalanceAfterWithdrawal >= amount).toEqual(
true,
);
});

it('should withdraw ETH to the L1 network using paymaster to cover fee', async () => {
Expand All @@ -1289,7 +1335,8 @@ describe('Wallet', () => {
PAYMASTER,
);
const l2BalanceBeforeWithdrawal = await wallet.getBalance(token);
const l2ApprovalTokenBalanceBeforeWithdrawal = await wallet.getBalance(APPROVAL_TOKEN);
const l2ApprovalTokenBalanceBeforeWithdrawal =
await wallet.getBalance(APPROVAL_TOKEN);

const tx = await wallet.withdraw({
token: token,
Expand All @@ -1303,7 +1350,9 @@ describe('Wallet', () => {
}),
});
const withdrawTx = await tx.waitFinalize();
expect(await wallet.isWithdrawalFinalized(withdrawTx.transactionHash)).toEqual(false);
expect(await wallet.isWithdrawalFinalized(withdrawTx.transactionHash)).toEqual(
false,
);

const result = await wallet.finalizeWithdrawal(withdrawTx.transactionHash);

Expand All @@ -1313,11 +1362,12 @@ describe('Wallet', () => {
PAYMASTER,
);
const l2BalanceAfterWithdrawal = await wallet.getBalance(token);
const l2ApprovalTokenBalanceAfterWithdrawal = await wallet.getBalance(APPROVAL_TOKEN);
const l2ApprovalTokenBalanceAfterWithdrawal =
await wallet.getBalance(APPROVAL_TOKEN);

expect(paymasterBalanceBeforeWithdrawal - paymasterBalanceAfterWithdrawal >= 0n).toEqual(
true,
);
expect(
paymasterBalanceBeforeWithdrawal - paymasterBalanceAfterWithdrawal >= 0n,
).toEqual(true);
expect(
paymasterTokenBalanceAfterWithdrawal - paymasterTokenBalanceBeforeWithdrawal,
).toEqual(minimalAllowance);
Expand Down