Skip to content

Commit

Permalink
fix(upgrade.test.ts): minting from a clean state (#2402)
Browse files Browse the repository at this point in the history
## What ❔

* fixed a bug that made upgrade test setup fail if it was run from a
clean state (without running any other tests previously)
* erc20 test with max balance deposit cleaned up

<!-- 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 Aug 21, 2024
1 parent d65588f commit efa3bd6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
53 changes: 29 additions & 24 deletions core/tests/ts-integration/tests/erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('ERC20 contract checks', () => {
let testMaster: TestMaster;
let alice: zksync.Wallet;
let bob: zksync.Wallet;
let isETHBasedChain: boolean;
let baseTokenAddress: string;
let tokenDetails: Token;
let aliceErc20: zksync.Contract;

Expand All @@ -23,6 +25,10 @@ describe('ERC20 contract checks', () => {
alice = testMaster.mainAccount();
bob = testMaster.newEmptyAccount();

// Get the information about base token address directly from the L2.
baseTokenAddress = await alice._providerL2().getBaseTokenContractAddress();
isETHBasedChain = baseTokenAddress == zksync.utils.ETH_ADDRESS_IN_CONTRACTS;

tokenDetails = testMaster.environment().erc20Token;
aliceErc20 = new zksync.Contract(tokenDetails.l2Address, zksync.utils.IERC20, alice);
});
Expand Down Expand Up @@ -207,48 +213,47 @@ describe('ERC20 contract checks', () => {
});

test('Can perform a deposit with precalculated max value', async () => {
const baseTokenAddress = await alice._providerL2().getBaseTokenContractAddress();
const isETHBasedChain = baseTokenAddress == zksync.utils.ETH_ADDRESS_IN_CONTRACTS;
if (!isETHBasedChain) {
// approving whole base token balance
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.
const maxAmount = await alice.getBalanceL1(tokenDetails.l1Address);
await (await alice.approveERC20(tokenDetails.l1Address, maxAmount)).wait();
// depositing the max amount: the whole balance of the token
const tokenDepositAmount = await alice.getBalanceL1(tokenDetails.l1Address);

// approving the needed allowance for the deposit
await (await alice.approveERC20(tokenDetails.l1Address, tokenDepositAmount)).wait();

// fee of the deposit in ether
const depositFee = await alice.getFullRequiredDepositFee({
token: tokenDetails.l1Address
});

// checking if alice has enough funds to pay the fee
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 aliceBalance = await alice.getBalanceL1();
if (aliceBalance < l1Fee + l2Fee) {
throw new Error('Not enough balance to pay the fee');
}

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

const overrides: ethers.Overrides = depositFee.gasPrice
? { gasPrice: depositFee.gasPrice }
: {
maxFeePerGas: depositFee.maxFeePerGas,
maxPriorityFeePerGas: depositFee.maxPriorityFeePerGas
};
overrides.gasLimit = depositFee.l1GasLimit;
const depositOp = await alice.deposit({
// deposit handle with the precalculated max amount
const depositHandle = await alice.deposit({
token: tokenDetails.l1Address,
amount: maxAmount,
amount: tokenDepositAmount,
l2GasLimit: depositFee.l2GasLimit,
overrides
approveBaseERC20: true,
approveERC20: true,
overrides: depositFee
});
await expect(depositOp).toBeAccepted([l2ERC20BalanceChange]);

// checking the l2 balance change
const l2TokenBalanceChange = await shouldChangeTokenBalances(tokenDetails.l2Address, [
{ wallet: alice, change: tokenDepositAmount }
]);
await expect(depositHandle).toBeAccepted([l2TokenBalanceChange]);
});

afterAll(async () => {
Expand Down
11 changes: 8 additions & 3 deletions core/tests/upgrade-test/tests/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('Upgrade test', function () {

if (!zksync.utils.isAddressEq(baseToken, zksync.utils.ETH_ADDRESS_IN_CONTRACTS)) {
await (await tester.syncWallet.approveERC20(baseToken, ethers.MaxUint256)).wait();
await mintToWallet(baseToken, tester.syncWallet, depositAmount * 10n);
await mintToAddress(baseToken, tester.ethWallet, tester.syncWallet.address, depositAmount * 10n);
}

const firstDepositHandle = await tester.syncWallet.deposit({
Expand Down Expand Up @@ -575,10 +575,15 @@ function prepareGovernanceCalldata(to: string, data: BytesLike): UpgradeCalldata
};
}

async function mintToWallet(baseTokenAddress: zksync.types.Address, ethersWallet: ethers.Wallet, amountToMint: bigint) {
async function mintToAddress(
baseTokenAddress: zksync.types.Address,
ethersWallet: ethers.Wallet,
addressToMintTo: string,
amountToMint: bigint
) {
const l1Erc20ABI = ['function mint(address to, uint256 amount)'];
const l1Erc20Contract = new ethers.Contract(baseTokenAddress, l1Erc20ABI, ethersWallet);
await (await l1Erc20Contract.mint(ethersWallet.address, amountToMint)).wait();
await (await l1Erc20Contract.mint(addressToMintTo, amountToMint)).wait();
}

const SEMVER_MINOR_VERSION_MULTIPLIER = 4294967296;
Expand Down

0 comments on commit efa3bd6

Please sign in to comment.