From 2965d001fa21cfc7ec0c7505e9e83fd11f19ed1a Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Tue, 17 Sep 2024 20:42:41 -0400 Subject: [PATCH 1/2] Fix tests --- src/utils.ts | 4 +- test/local/account-abstraction.test.ts | 2 +- test/local/deposit-withdraw.test.ts | 2 +- test/local/smart-account.test.ts | 2 +- test/local/wallet-send.test.ts | 2 +- test/local/wallet-transfer.test.ts | 2 +- test/local/wallet-withdraw.test.ts | 2 +- test/local/wallet.test.ts | 110 ++++++++++++++++++------- 8 files changed, 88 insertions(+), 38 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 534cf2f..c1f066d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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, ); }; diff --git a/test/local/account-abstraction.test.ts b/test/local/account-abstraction.test.ts index 87ec008..2156678 100644 --- a/test/local/account-abstraction.test.ts +++ b/test/local/account-abstraction.test.ts @@ -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; diff --git a/test/local/deposit-withdraw.test.ts b/test/local/deposit-withdraw.test.ts index 21fa844..9c57069 100644 --- a/test/local/deposit-withdraw.test.ts +++ b/test/local/deposit-withdraw.test.ts @@ -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); diff --git a/test/local/smart-account.test.ts b/test/local/smart-account.test.ts index 5efa64d..bbff36b 100644 --- a/test/local/smart-account.test.ts +++ b/test/local/smart-account.test.ts @@ -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; diff --git a/test/local/wallet-send.test.ts b/test/local/wallet-send.test.ts index 1a35be0..0504b8f 100644 --- a/test/local/wallet-send.test.ts +++ b/test/local/wallet-send.test.ts @@ -10,7 +10,7 @@ import { getPreparedWallet, } from './fixtures'; -jest.setTimeout(600000); +jest.setTimeout(60000); const accounts = getAccounts(); const mainAccount = accounts[0]; diff --git a/test/local/wallet-transfer.test.ts b/test/local/wallet-transfer.test.ts index 698c436..e4c2b44 100644 --- a/test/local/wallet-transfer.test.ts +++ b/test/local/wallet-transfer.test.ts @@ -10,7 +10,7 @@ import { getPreparedWallet, } from './fixtures'; -jest.setTimeout(600000); +jest.setTimeout(60000); const accounts = getAccounts(); const mainAccount = accounts[0]; diff --git a/test/local/wallet-withdraw.test.ts b/test/local/wallet-withdraw.test.ts index fb6e888..2aaac2c 100644 --- a/test/local/wallet-withdraw.test.ts +++ b/test/local/wallet-withdraw.test.ts @@ -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]; diff --git a/test/local/wallet.test.ts b/test/local/wallet.test.ts index 999546d..1ff79cc 100644 --- a/test/local/wallet.test.ts +++ b/test/local/wallet.test.ts @@ -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]; @@ -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); @@ -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); } @@ -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!', + ); } }); @@ -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')) @@ -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!', + ); } }); } @@ -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, @@ -1116,14 +1133,20 @@ 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 === @@ -1131,7 +1154,9 @@ describe('Wallet', () => { ).toEqual(true); expect(result).not.toBeNull(); - expect(receiverBalanceAfterTransfer - receiverBalanceBeforeTransfer).toEqual(amount); + expect(receiverBalanceAfterTransfer - receiverBalanceBeforeTransfer).toEqual( + amount, + ); }); } @@ -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, @@ -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, ); @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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, @@ -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); @@ -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); From b4daf3e30bf20c316690ab9b1d761c6b7cfd50e9 Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Tue, 17 Sep 2024 21:01:07 -0400 Subject: [PATCH 2/2] fix tests --- test/integration/wallet.test.ts | 12 +++++++++--- test/local/account-abstraction.test.ts | 2 -- test/local/deposit-withdraw.test.ts | 3 --- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/test/integration/wallet.test.ts b/test/integration/wallet.test.ts index 85b340e..4675388 100644 --- a/test/integration/wallet.test.ts +++ b/test/integration/wallet.test.ts @@ -224,6 +224,7 @@ describe('Wallet', () => { to: ADDRESS2, value: web3Utils.toHex(7_000_000_000), }); + console.log('result', result); deepEqualExcluding(result, tx, [ 'gasLimit', 'gasPrice', @@ -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 () => { @@ -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); }); diff --git a/test/local/account-abstraction.test.ts b/test/local/account-abstraction.test.ts index 2156678..cb64aee 100644 --- a/test/local/account-abstraction.test.ts +++ b/test/local/account-abstraction.test.ts @@ -20,8 +20,6 @@ describe('Account Abstraction', () => { 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; diff --git a/test/local/deposit-withdraw.test.ts b/test/local/deposit-withdraw.test.ts index 9c57069..4d59f1f 100644 --- a/test/local/deposit-withdraw.test.ts +++ b/test/local/deposit-withdraw.test.ts @@ -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'; @@ -11,8 +10,6 @@ import { L1_CHAIN_URL, L2_CHAIN_URL } from '../utils'; 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);