Skip to content

Commit

Permalink
fix: addressing flakiness of uniswap_trade_on_l1_from_l2.test.ts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Mar 26, 2024
1 parent 501c5e9 commit 2db9cad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
7 changes: 6 additions & 1 deletion yarn-project/aztec-faucet/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import http from 'http';
import Koa from 'koa';
import cors from 'koa-cors';
import Router from 'koa-router';
import { Hex, http as ViemHttp, createWalletClient, parseEther } from 'viem';
import { Hex, http as ViemHttp, createPublicClient, createWalletClient, parseEther } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';

const {
Expand Down Expand Up @@ -68,6 +68,10 @@ async function transferEth(address: string) {
chain: chain.chainInfo,
transport: ViemHttp(chain.rpcUrl),
});
const publicClient = createPublicClient({
chain: chain.chainInfo,
transport: ViemHttp(chain.rpcUrl),
});
const hexAddress = createHex(address);
checkThrottle(hexAddress);
try {
Expand All @@ -76,6 +80,7 @@ async function transferEth(address: string) {
to: hexAddress,
value: parseEther(ETH_AMOUNT),
});
await publicClient.waitForTransactionReceipt({ hash });
mapping[hexAddress] = new Date();
logger.info(`Sent ${ETH_AMOUNT} ETH to ${hexAddress} in tx ${hash}`);
} catch (error) {
Expand Down
9 changes: 5 additions & 4 deletions yarn-project/end-to-end/src/e2e_cheat_codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,25 @@ describe('e2e_cheat_codes', () => {
// without impersonation we wouldn't be able to send funds.
const myAddress = (await walletClient.getAddresses())[0];
const randomAddress = EthAddress.random().toString();
await walletClient.sendTransaction({
const tx1Hash = await walletClient.sendTransaction({
account: myAddress,
to: randomAddress,
value: parseEther('1'),
});
await publicClient.waitForTransactionReceipt({ hash: tx1Hash });
const beforeBalance = await publicClient.getBalance({ address: randomAddress });

// impersonate random address
await cc.eth.startImpersonating(EthAddress.fromString(randomAddress));
// send funds from random address
const amountToSend = parseEther('0.1');
const txHash = await walletClient.sendTransaction({
const tx2Hash = await walletClient.sendTransaction({
account: randomAddress,
to: myAddress,
value: amountToSend,
});
const tx = await publicClient.waitForTransactionReceipt({ hash: txHash });
const feePaid = tx.gasUsed * tx.effectiveGasPrice;
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: tx2Hash });
const feePaid = txReceipt.gasUsed * txReceipt.effectiveGasPrice;
expect(await publicClient.getBalance({ address: randomAddress })).toBe(beforeBalance - amountToSend - feePaid);

// stop impersonating
Expand Down
12 changes: 6 additions & 6 deletions yarn-project/end-to-end/src/shared/uniswap_l1_l2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,24 @@ export const uniswapL1L2TestSuite = (
[registryAddress.toString(), uniswapL2Contract.address.toString()],
{} as any,
);
});

beforeEach(async () => {
// Give me some WETH so I can deposit to L2 and do the swap...
logger('Getting some weth');
await walletClient.sendTransaction({ to: WETH9_ADDRESS.toString(), value: parseEther('1') });
const hash = await walletClient.sendTransaction({ to: WETH9_ADDRESS.toString(), value: parseEther('1000') });
await publicClient.waitForTransactionReceipt({ hash });

const wethBalance = await wethCrossChainHarness.getL1BalanceOf(ownerEthAddress);
expect(wethBalance).toBe(parseEther('1000'));
});
// docs:end:uniswap_l1_l2_test_beforeAll

afterAll(async () => {
await cleanup();
});

// docs:start:uniswap_private
it('should uniswap trade on L1 from L2 funds privately (swaps WETH -> DAI)', async () => {
const wethL1BeforeBalance = await wethCrossChainHarness.getL1BalanceOf(ownerEthAddress);
if (wethL1BeforeBalance < wethAmountToBridge) {
throw new Error('Not enough WETH to run this test. Try restarting anvil.');
}

// 1. Approve and deposit weth to the portal and move to L2
const [secretForMintingWeth, secretHashForMintingWeth] = wethCrossChainHarness.generateClaimSecret();
Expand Down

0 comments on commit 2db9cad

Please sign in to comment.