Skip to content

Commit

Permalink
rmv hardcodes & improve zk down (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
juan518munoz committed Jan 19, 2024
1 parent eafbb3a commit 2691733
Show file tree
Hide file tree
Showing 6 changed files with 601 additions and 643 deletions.
93 changes: 44 additions & 49 deletions core/tests/ts-integration/src/context-owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,8 @@ 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(this.env.mainWalletPK, this.l1Provider);
this.mainSyncWallet = new zksync.Wallet(this.env.mainWalletPK, this.l2Provider, this.l1Provider);
}

// Returns the required amount of L1 ETH
Expand All @@ -102,6 +95,9 @@ export class TestContextOwner {
*/
async setupContext(): Promise<TestContext> {
try {
if (this.env.nativeErc20Testing) {
this.reporter.message('Using native ERC20 implementation');
}
this.reporter.startAction('Setting up the context');
await this.cancelPendingTxs();
this.wallets = await this.prepareWallets();
Expand Down Expand Up @@ -244,27 +240,27 @@ export class TestContextOwner {
// Scaled gas price to be used to prevent transactions from being stuck.
const gasPrice = await scaledGasPrice(this.mainEthersWallet);

// Deposit L2 tokens (if needed).
// if (!l2ETHAmountToDeposit.isZero()) {
// // Given that we've already sent a number of transactions,
// // we have to correctly send nonce.
// const depositHandle = this.mainSyncWallet
// .deposit({
// to: this.mainEthersWallet.address,
// approveERC20: true,
// token: this.env.erc20Token.l1Address,
// amount: l2ETHAmountToDeposit,
// refundRecipient: this.mainEthersWallet.address,
// })
// .then((tx) => {
// const amount = ethers.utils.formatEther(l2ETHAmountToDeposit);
// this.reporter.debug(`Sent ETH deposit. Nonce ${tx.nonce}, amount: ${amount}, hash: ${tx.hash}`);
// tx.wait();
// });

// // Add this promise to the list of L1 tx promises.
// l1TxPromises.push(depositHandle);
// }
// Deposit L2 tokens (if needed). Depositing ETH with current native implementation is not supported.
if (!l2ETHAmountToDeposit.isZero() && !this.env.nativeErc20Testing) {
// Given that we've already sent a number of transactions,
// we have to correctly send nonce.
const depositHandle = this.mainSyncWallet
.deposit({
to: this.mainEthersWallet.address,
approveERC20: true,
token: this.env.erc20Token.l1Address,
amount: l2ETHAmountToDeposit,
refundRecipient: this.mainEthersWallet.address
})
.then((tx) => {
const amount = ethers.utils.formatEther(l2ETHAmountToDeposit);
this.reporter.debug(`Sent ETH deposit. Nonce ${tx.nonce}, amount: ${amount}, hash: ${tx.hash}`);
tx.wait();
});

// Add this promise to the list of L1 tx promises.
l1TxPromises.push(depositHandle);
}

// Define values for handling ERC20 transfers/deposits.
const erc20Token = this.env.erc20Token.l1Address;
Expand All @@ -284,21 +280,17 @@ export class TestContextOwner {
});

// Deposit ERC20.
const approveOverrides = !this.env.nativeErc20Testing ? { nonce: nonce++, gasPrice } : undefined;
const overrides = !this.env.nativeErc20Testing ? { nonce: nonce++, gasPrice } : undefined;
const erc20DepositPromise = this.mainSyncWallet
.deposit(
{
to: this.mainEthersWallet.address,
token: erc20Token,
amount: l2erc20DepositAmount,
approveERC20: true
// approveOverrides: {
// nonce: nonce++,
// gasPrice
// },
// overrides: {
// nonce: nonce++,
// gasPrice
// }
approveERC20: true,
approveOverrides,
overrides
},
erc20Token
)
Expand Down Expand Up @@ -353,16 +345,19 @@ export class TestContextOwner {
const l2TxPromises: Promise<any>[] = [];

// ETH transfers.
// const l2TxPromises = await sendTransfers(
// zksync.utils.ETH_ADDRESS,
// this.mainSyncWallet,
// wallets,
// L2_ETH_PER_ACCOUNT,
// l2startNonce,
// undefined
// this.reporter
// );
// l2startNonce += l2TxPromises.length;
if (!this.env.nativeErc20Testing) {
const ethPromises = await sendTransfers(
zksync.utils.ETH_ADDRESS,
this.mainSyncWallet,
wallets,
L2_ETH_PER_ACCOUNT,
l2startNonce,
undefined,
this.reporter
);
l2startNonce += l2TxPromises.length;
l2TxPromises.push(...ethPromises);
}

// ERC20 transfers.
const l2TokenAddress = await this.mainSyncWallet.l2TokenAddress(this.env.erc20Token.l1Address);
Expand Down
29 changes: 24 additions & 5 deletions core/tests/ts-integration/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ export async function waitForServer() {
*/
export async function loadTestEnvironment(): Promise<TestEnvironment> {
const network = process.env.CHAIN_ETH_NETWORK || 'localhost';
const nativeErc20Testing = process.env.NATIVE_ERC20_ADDRESS ? true : false; // if set, we assume user wants to test native erc20 tokens

let mainWalletPK;
if (network == 'localhost') {
if (nativeErc20Testing) {
mainWalletPK = '0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705';
} else if (network == 'localhost') {
const testConfigPath = path.join(process.env.ZKSYNC_HOME!, `etc/test_config/constant`);
const ethTestConfig = JSON.parse(fs.readFileSync(`${testConfigPath}/eth.json`, { encoding: 'utf-8' }));
mainWalletPK = ethers.Wallet.fromMnemonic(ethTestConfig.test_mnemonic as string, "m/44'/60'/0'/0/0").privateKey;
Expand All @@ -75,21 +78,27 @@ export async function loadTestEnvironment(): Promise<TestEnvironment> {
// wBTC is chosen because it has decimals different from ETH (8 instead of 18).
// Using this token will help us to detect decimals-related errors.
// but if it's not available, we'll use the first token from the list.
let token = tokens.find((token: { symbol: string }) => token.symbol == 'LBC')!;
let token = undefined;
if (nativeErc20Testing) {
const nativeToken = getNativeTokens();
token = nativeToken;
} else {
token = tokens.find((token: { symbol: string }) => token.symbol == 'wBTC')!;
}
if (!token) {
token = tokens[0];
}
const weth = tokens.find((token: { symbol: string }) => token.symbol == 'WETH')!;

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

const l2WethAddress = await new zksync.Wallet(
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
mainWalletPK,
new zksync.Provider(l2NodeUrl),
ethers.getDefaultProvider(l1NodeUrl)
).l2TokenAddress(weth.address);
Expand All @@ -114,7 +123,8 @@ export async function loadTestEnvironment(): Promise<TestEnvironment> {
decimals: weth.decimals,
l1Address: weth.address,
l2Address: l2WethAddress
}
},
nativeErc20Testing
};
}

Expand Down Expand Up @@ -143,3 +153,12 @@ function getTokens(network: string): L1Token[] {
})
);
}

function getNativeTokens(): L1Token {
const configPath = `${process.env.ZKSYNC_HOME}/etc/tokens/native_erc20.json`;
return JSON.parse(
fs.readFileSync(configPath, {
encoding: 'utf-8'
})
);
}
6 changes: 1 addition & 5 deletions core/tests/ts-integration/src/test-master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ export class TestMaster {
this.l2Provider.pollingInterval = 5000;
}

this.mainWallet = new zksync.Wallet(
'0xe131bc3f481277a8f73d680d9ba404cc6f959e64296e0914dded403030d4f705',
this.l2Provider,
this.l1Provider
);
this.mainWallet = new zksync.Wallet(this.env.mainWalletPK, 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;
/**
* Flag indicating whether the tests are being run against the native ERC20 implementation.
*/
nativeErc20Testing: boolean;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion infrastructure/zk/src/down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Command } from 'commander';
import * as utils from './utils';

export async function down() {
await utils.spawn('docker-compose stop geth postgres');
await utils.spawn('docker compose down -v');
await utils.spawn('docker compose rm -s -f -v');
await utils.spawn('docker run --rm -v ./volumes:/volumes postgres:14 bash -c "rm -rf /volumes/*"');
}

export const command = new Command('down').description('stop development containers').action(down);
Loading

0 comments on commit 2691733

Please sign in to comment.