Skip to content

Commit

Permalink
fix(erc20-test): only approving baseToken allowance when needed (#2379)
Browse files Browse the repository at this point in the history
## What ❔

Refactored the `deposit with precalculated max value` test case of
`erc20.test.ts` to only approve baseToken allowance when needed.
<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔
<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
benceharomi authored Jul 9, 2024
1 parent e9d63db commit 087a3c4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions core/tests/ts-integration/tests/erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('ERC20 contract checks', () => {
let alice: zksync.Wallet;
let bob: zksync.Wallet;
let tokenDetails: Token;
let baseTokenDetails: Token;
let aliceErc20: zksync.Contract;

beforeAll(async () => {
Expand All @@ -25,7 +24,6 @@ describe('ERC20 contract checks', () => {
bob = testMaster.newEmptyAccount();

tokenDetails = testMaster.environment().erc20Token;
baseTokenDetails = testMaster.environment().baseToken;
aliceErc20 = new zksync.Contract(tokenDetails.l2Address, zksync.utils.IERC20, alice);
});

Expand Down Expand Up @@ -209,23 +207,34 @@ describe('ERC20 contract checks', () => {
});

test('Can perform a deposit with precalculated max value', async () => {
const maxAmountBase = await alice.getBalanceL1(baseTokenDetails.l1Address);
const maxAmount = await alice.getBalanceL1(tokenDetails.l1Address);
const baseTokenAddress = await alice._providerL2().getBaseTokenContractAddress();
const isETHBasedChain = baseTokenAddress == zksync.utils.ETH_ADDRESS_IN_CONTRACTS;
if (!isETHBasedChain) {
const baseTokenDetails = testMaster.environment().baseToken;
const baseTokenMaxAmount = await alice.getBalanceL1(baseTokenDetails.l1Address);
await (await alice.approveERC20(baseTokenDetails.l1Address, baseTokenMaxAmount)).wait();
}

// Approving the needed allowance to ensure that the user has enough funds.
await (await alice.approveERC20(baseTokenDetails.l1Address, maxAmountBase)).wait();
const maxAmount = await alice.getBalanceL1(tokenDetails.l1Address);
await (await alice.approveERC20(tokenDetails.l1Address, maxAmount)).wait();

const depositFee = await alice.getFullRequiredDepositFee({
token: tokenDetails.l1Address
});

const l1Fee = depositFee.l1GasLimit * (depositFee.maxFeePerGas! || depositFee.gasPrice!);
const l2Fee = depositFee.baseCost;
const aliceETHBalance = await alice.getBalanceL1();

if (aliceETHBalance < l1Fee + l2Fee) {
throw new Error('Not enough ETH to perform a deposit');
}

const l2ERC20BalanceChange = await shouldChangeTokenBalances(tokenDetails.l2Address, [
{ wallet: alice, change: maxAmount }
]);

const overrides: ethers.Overrides = depositFee.gasPrice
? { gasPrice: depositFee.gasPrice }
: {
Expand Down

0 comments on commit 087a3c4

Please sign in to comment.