Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos committed Sep 18, 2024
1 parent 3b85b4b commit 2965d00
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 38 deletions.
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
2 changes: 1 addition & 1 deletion test/local/account-abstraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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;
Expand Down
2 changes: 1 addition & 1 deletion test/local/deposit-withdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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);
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

0 comments on commit 2965d00

Please sign in to comment.