Skip to content

Commit

Permalink
Merge branch 'native_erc20' into native_erc20_test_deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoPittella authored Jan 18, 2024
2 parents 3eb8863 + eafbb3a commit 6f1b965
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 19 deletions.
42 changes: 26 additions & 16 deletions core/tests/ts-integration/src/context-owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ export class TestContextOwner {
this.l2Provider.pollingInterval = 100;
}

this.mainEthersWallet = new ethers.Wallet("0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705", this.l1Provider);
this.mainSyncWallet = new zksync.Wallet("0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705", this.l2Provider, this.l1Provider);
this.mainEthersWallet = new ethers.Wallet(
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
this.l1Provider
);
this.mainSyncWallet = new zksync.Wallet(
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
this.l2Provider,
this.l1Provider
);
}

// Returns the required amount of L1 ETH
Expand Down Expand Up @@ -278,20 +285,23 @@ export class TestContextOwner {

// Deposit ERC20.
const erc20DepositPromise = this.mainSyncWallet
.deposit({
to: this.mainEthersWallet.address,
token: erc20Token,
amount: l2erc20DepositAmount,
approveERC20: true,
// approveOverrides: {
// nonce: nonce++,
// gasPrice
// },
// overrides: {
// nonce: nonce++,
// gasPrice
// }
}, erc20Token)
.deposit(
{
to: this.mainEthersWallet.address,
token: erc20Token,
amount: l2erc20DepositAmount,
approveERC20: true
// approveOverrides: {
// nonce: nonce++,
// gasPrice
// },
// overrides: {
// nonce: nonce++,
// gasPrice
// }
},
erc20Token
)
.then((tx) => {
// Note: there is an `approve` tx, not listed here.
this.reporter.debug(`Sent ERC20 deposit transaction. Hash: ${tx.hash}, nonce: ${tx.nonce}`);
Expand Down
4 changes: 2 additions & 2 deletions core/tests/ts-integration/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export async function loadTestEnvironment(): Promise<TestEnvironment> {

// `waitForServer` is expected to be executed. Otherwise this call may throw.
const l2TokenAddress = await new zksync.Wallet(
"0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705",
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
new zksync.Provider(l2NodeUrl),
ethers.getDefaultProvider(l1NodeUrl)
).l2TokenAddress(token.address);

const l2WethAddress = await new zksync.Wallet(
"0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705",
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
new zksync.Provider(l2NodeUrl),
ethers.getDefaultProvider(l1NodeUrl)
).l2TokenAddress(weth.address);
Expand Down
6 changes: 5 additions & 1 deletion core/tests/ts-integration/src/test-master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export class TestMaster {
this.l2Provider.pollingInterval = 5000;
}

this.mainWallet = new zksync.Wallet("0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705", this.l2Provider, this.l1Provider);
this.mainWallet = new zksync.Wallet(
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
this.l2Provider,
this.l1Provider
);
}

/**
Expand Down
80 changes: 80 additions & 0 deletions core/tests/ts-integration/tests/withdraw.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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-web3';
import * as ethers from 'ethers';
import { ETH_ADDRESS } from 'zksync-web3/build/src/utils';

describe('ERC20 contract checks', () => {
let testMaster: TestMaster;
let alice: zksync.Wallet;
let tokenDetails: Token;
let aliceErc20: ethers.Contract;
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

beforeAll(async () => {
testMaster = TestMaster.getInstance(__filename);
alice = testMaster.mainAccount();

tokenDetails = testMaster.environment().erc20Token;
aliceErc20 = (await alice.getL1BridgeContracts()).erc20;
});

test('Can perform a valid withdrawal', async () => {
// TODO: make sure that the test is not run in the fast mode
if (testMaster.isFastMode()) {
return;
}
const amount = 1;

const initialBalanceL2 = await alice.getBalance();
const initialBalanceL1 = await alice.getBalanceL1(tokenDetails.l1Address);

// First, a withdraw transaction is done on the L2,
const withdraw = await alice.withdraw({ token: ETH_ADDRESS, amount });
const withdrawalHash = withdraw.hash;
await withdraw.waitFinalize();

// Get receipt of withdraw transaction, and check the gas cost (fee)
const receipt = await alice.provider.getTransactionReceipt(withdrawalHash);
const fee = receipt.effectiveGasPrice.mul(receipt.gasUsed);

const finalBalanceL2 = await alice.getBalance();
let expected = initialBalanceL2.sub(amount).sub(fee);
let actual = finalBalanceL2;
expect(actual == expected);

// Afterwards, a withdraw-finalize is done on the L1,
(await alice.finalizeWithdrawal(withdrawalHash)).wait();

// make sure that the balance on the L1 has increased by the amount withdrawn
const finalBalanceL1 = await alice.getBalanceL1(tokenDetails.l1Address);
expected = initialBalanceL1.add(amount);
actual = finalBalanceL1;
expect(actual == expected);
});

test(`Can't perform an invalid withdrawal`, async () => {
// TODO: make sure that the test is not run in the fast mode
if (testMaster.isFastMode()) {
return;
}

const initialBalanceL2 = await alice.getBalance();
const amount = initialBalanceL2.add(1);
try {
await alice.withdraw({ token: ETH_ADDRESS, amount });
} catch (e: any) {
const err = e.toString();
expect(err.includes('insufficient balance for transfer'));
}
});

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

0 comments on commit 6f1b965

Please sign in to comment.