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

Help swaps flakiness #1683

Closed
wants to merge 16 commits into from
91 changes: 88 additions & 3 deletions e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs from 'node:fs';

import { Contract } from '@ethersproject/contracts';
import { getDefaultProvider } from '@ethersproject/providers';
import { providers } from 'ethers';
import {
Builder,
By,
Expand Down Expand Up @@ -172,9 +173,9 @@ const addPermissionForAllWebsites = async (driver: WebDriver) => {
id: 'details-deck-button-permissions',
driver,
});
await driver.executeScript(
`document.querySelectorAll('[class="permission-info"]')[0].children[0].click();`,
);
// await driver.executeScript(
// `document.querySelectorAll('[class="permission-info"]')[0].children[0].click();`,
// );
};

export async function getExtensionIdByName(
Expand Down Expand Up @@ -776,6 +777,90 @@ export async function transactionStatus() {
return txnStatus;
}

export async function waitForAndCheckTransaction(
provider: providers.Provider,
txHash: string,
maxAttempts = 20,
): Promise<{
status: 'success' | 'failure' | 'timeout';
receipt: providers.TransactionReceipt | null;
}> {
let attempts = 0;
while (attempts < maxAttempts) {
try {
console.log(`Checking transaction status, attempt ${attempts + 1}`);
const txnReceipt = await provider.getTransactionReceipt(txHash);
BrodyHughes marked this conversation as resolved.
Show resolved Hide resolved

if (txnReceipt) {
const txnStatus = txnReceipt.status === 1 ? 'success' : 'failure';
console.log('Transaction receipt:', txnReceipt);

if (txnStatus === 'success') {
console.log(
'Transaction successful, waiting for 30 seconds to give balances time to update ...',
);
await new Promise((resolve) => setTimeout(resolve, 30000));
} else {
throw new Error(`Swap transaction failed. Status: ${txnStatus}`);
}

return { status: txnStatus, receipt: txnReceipt };
}
} catch (error) {
console.error('Error checking transaction:', error);
throw error; // Re-throw the error to be caught by the caller
}

attempts++;
await new Promise((resolve) => setTimeout(resolve, 10_000));
}

throw new Error('Swap transaction timed out');
}

export async function getLatestTransactionHash(
provider: providers.Provider,
address: string,
maxAttempts = 10,
delayMs = 10_000,
): Promise<string | null> {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
console.log(`Searching for transaction, attempt ${attempt + 1}`);
const currentBlock = await provider.getBlockNumber();

for (let i = 0; i < 3; i++) {
// Check last 3 blocks
const block = await provider.getBlockWithTransactions(currentBlock - i);

for (const tx of block.transactions) {
if (tx.from.toLowerCase() === address.toLowerCase()) {
return tx.hash;
}
}
}

// If not found, wait before next attempt
await new Promise((resolve) => setTimeout(resolve, delayMs));
}

return null;
}

export async function getPendingTransactionHash(
provider: providers.JsonRpcProvider,
address: string,
): Promise<string | null> {
const pendingTransactions = await provider.send(
'eth_pendingTransactions',
[],
);
const transaction = pendingTransactions.find(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(tx: any) => tx.from.toLowerCase() === address.toLowerCase(),
);
return transaction ? transaction.hash : null;
}

export const fillSeedPhrase = async (driver: WebDriver, seedPhrase: string) => {
const words = seedPhrase.split(' ');
for (let i = 0; i < 12; i++) {
Expand Down
Loading
Loading