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

Kl factory - deposit tests #190

Closed
wants to merge 6 commits into from
Closed
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
18 changes: 16 additions & 2 deletions core/tests/ts-integration/src/context-owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class TestContextOwner {
const l2ETHAmountToDeposit = await this.ensureBalances(accountsAmount);
const l2ERC20AmountToDeposit = ERC20_PER_ACCOUNT.mul(accountsAmount);
const wallets = this.createTestWallets(suites);
await this.distributeL1BaseToken(l2ERC20AmountToDeposit);
await this.distributeL1BaseToken(wallets, l2ERC20AmountToDeposit);
await this.cancelAllowances();
await this.distributeL1Tokens(wallets, l2ETHAmountToDeposit, l2ERC20AmountToDeposit);
await this.distributeL2Tokens(wallets);
Expand Down Expand Up @@ -257,7 +257,7 @@ export class TestContextOwner {
* Sends L1 tokens to the test wallet accounts.
* Additionally, deposits L1 tokens to the main account for further distribution on L2 (if required).
*/
private async distributeL1BaseToken(l2erc20DepositAmount: ethers.BigNumber) {
private async distributeL1BaseToken(wallets: TestWallets, l2erc20DepositAmount: ethers.BigNumber) {
this.reporter.startAction(`Distributing base tokens on L1`);
const baseTokenAddress = process.env.CONTRACTS_BASE_TOKEN_ADDR!;
if (baseTokenAddress != zksync.utils.ETH_ADDRESS_IN_CONTRACTS) {
Expand Down Expand Up @@ -321,6 +321,20 @@ export class TestContextOwner {
l1TxPromises.push(baseDepositPromise);

this.reporter.debug(`Sent ${l1TxPromises.length} base token initial transactions on L1`);

// Transfer base token to wallets
const baseTransfers = await sendTransfers(
baseTokenAddress,
this.mainEthersWallet,
wallets,
ERC20_PER_ACCOUNT,
nonce,
gasPrice,
this.reporter
);
nonce += baseTransfers.length;
l1TxPromises.push(...baseTransfers);

await Promise.all(l1TxPromises);
}
this.reporter.finishAction();
Expand Down
9 changes: 9 additions & 0 deletions core/tests/ts-integration/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ethers from 'ethers';
import * as zksync from 'zksync-ethers';
import { TestEnvironment } from './types';
import { Reporter } from './reporter';
import { L2_ETH_TOKEN_ADDRESS } from 'zksync-web3/build/src/utils';

/**
* Attempts to connect to server.
Expand Down Expand Up @@ -80,6 +81,7 @@ export async function loadTestEnvironment(): Promise<TestEnvironment> {
token = tokens[0];
}
const weth = tokens.find((token: { symbol: string }) => token.symbol == 'WETH')!;
const baseToken = tokens.find((token: { symbol: string }) => token.symbol == 'BAT')!;

// `waitForServer` is expected to be executed. Otherwise this call may throw.
const l2TokenAddress = await new zksync.Wallet(
Expand Down Expand Up @@ -114,6 +116,13 @@ export async function loadTestEnvironment(): Promise<TestEnvironment> {
decimals: weth.decimals,
l1Address: weth.address,
l2Address: l2WethAddress
},
baseToken: {
name: baseToken.name,
symbol: baseToken.symbol,
decimals: baseToken.decimals,
l1Address: baseToken.address,
l2Address: L2_ETH_TOKEN_ADDRESS
}
};
}
Expand Down
1 change: 1 addition & 0 deletions core/tests/ts-integration/src/test-master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class TestMaster {
this.l2Provider.pollingInterval = 5000;
}

// TODO: suiteWalletPK should be the same as the mainWalletPK used in context-owner.ts, otherwise, account won't have funds to operate with.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Care to explain bit more about this comment?
Do we have an issue for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further inspecting how the testing works, it turns out that the individual wallet used for each test wasn't getting native erc20 funds needed, this is now done inside of distributeL1BaseToken().

this.mainWallet = new zksync.Wallet(suiteWalletPK, this.l2Provider, this.l1Provider);
}

Expand Down
4 changes: 4 additions & 0 deletions core/tests/ts-integration/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface TestEnvironment {
* Description of the WETH token used in the tests.
*/
wethToken: Token;
/**
* Description of the "base" ERC20 token used in the tests.
*/
baseToken: Token;
}

/**
Expand Down
97 changes: 97 additions & 0 deletions core/tests/ts-integration/tests/native-erc20.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* This suite contains tests checking default ERC-20 contract behavior.
*/

import { TestMaster } from '../src/index';
import { Token } from '../src/types';

import * as zksync from 'zksync-ethers';
import { BigNumber } from 'ethers';
import * as ethers from 'ethers';
import { scaledGasPrice } from '../src/helpers';

describe('ERC20 contract checks', () => {
let testMaster: TestMaster;
let alice: zksync.Wallet;
let baseTokenDetails: Token;
let aliceBaseErc20: ethers.Contract;
let chainId: ethers.BigNumberish;

beforeAll(async () => {
testMaster = TestMaster.getInstance(__filename);
alice = testMaster.mainAccount();
chainId = process.env.CHAIN_ETH_ZKSYNC_NETWORK_ID!;

baseTokenDetails = testMaster.environment().baseToken;
aliceBaseErc20 = new ethers.Contract(baseTokenDetails.l1Address, zksync.utils.IERC20, alice._providerL1());
});

test('Can perform a deposit', async () => {
const amount = 1; // 1 wei is enough.
const gasPrice = scaledGasPrice(alice);

const initialEthBalance = await alice.getBalanceL1();
const initialL1Balance = await alice.getBalanceL1(baseTokenDetails.l1Address);
const initialL2Balance = await alice.getBalance();

const depositTx = await alice.deposit({
token: baseTokenDetails.l1Address,
amount: amount,
approveERC20: true,
approveBaseERC20: true,
approveBaseOverrides: {
gasPrice
},
approveOverrides: {
gasPrice
},
overrides: {
gasPrice
}
});
const depositHash = depositTx.hash;
await depositTx.wait();

const receipt = await alice._providerL1().getTransactionReceipt(depositHash);
const fee = receipt.effectiveGasPrice.mul(receipt.gasUsed);

// TODO: should all the following tests use strict equality?

const finalEthBalance = await alice.getBalanceL1();
expect(initialEthBalance).bnToBeGt(finalEthBalance.add(fee)); // Fee should be taken from the ETH balance on L1.

const finalL1Balance = await alice.getBalanceL1(baseTokenDetails.l1Address);
expect(initialL1Balance).bnToBeGte(finalL1Balance.add(amount));

const finalL2Balance = await alice.getBalance();
expect(initialL2Balance).bnToBeLte(finalL2Balance.add(amount));
});

test('Not enough balance should revert', async () => {
const amount = BigNumber.from('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
const gasPrice = scaledGasPrice(alice);
let errorMessage;

await expect(
alice.deposit({
token: baseTokenDetails.l1Address,
amount: amount,
approveERC20: true,
approveBaseERC20: true,
approveBaseOverrides: {
gasPrice
},
approveOverrides: {
gasPrice
},
overrides: {
gasPrice
}
})
).toBeRejected(errorMessage);
});

afterAll(async () => {
await testMaster.deinitialize();
});
});
Loading