Skip to content

Instantly share code, notes, and snippets.

@gr1zix
Last active July 13, 2025 11:55
Show Gist options
  • Save gr1zix/103afa21eb72f020906c23de652da6c9 to your computer and use it in GitHub Desktop.
Save gr1zix/103afa21eb72f020906c23de652da6c9 to your computer and use it in GitHub Desktop.
Simulate adding to pool
const increaseLiqXpath = '/html/body/div[1]/div/span/span/div/div[2]/div[1]/div[3]/div/div/div[1]/div/div[2]/a[1]'
const usdcInputXpath = '/html/body/div[1]/div/span/span/div/div[2]/div[1]/div[3]/main/div[2]/div/div[1]/div[2]/div/div/div[2]/div/div[1]/input';
const previewXpath = '/html/body/div[1]/div/span/span/div/div[2]/div[1]/div[3]/main/div[2]/div/div[2]/div/button';
const addXpath = '/html/body/reach-portal[4]/div[2]/div/div/div/div/div[2]/button';
const backXpath = '/html/body/div[1]/div/span/span/div/div[2]/div[1]/div[3]/main/div[1]/div/a';
async function sendForm() {
try {
await sleep(500);
const increaseLiqEl = getElementByXpath(increaseLiqXpath);
if (!increaseLiqEl) {
throw new Error('increaseLiqEl not found');
}
increaseLiqEl.click()
await sleep(300);
const usdcAmountInputElement = getElementByXpath(usdcInputXpath);
if (!usdcAmountInputElement) {
throw new Error('Amount input not found');
}
setReactInputValue(usdcAmountInputElement, getRandomAmount());
await sleep(1500);
const previewButton = await waitForButton(previewXpath);
if (!previewButton) {
throw new Error('previewEl not found');
}
previewButton.click()
await sleep(1200);
const addButton = await waitForButton(addXpath);
addButton.click()
await sleep(1200);
let backButtonEl = getElementByXpath(backXpath);
if (!backButtonEl) {
await sleep(500)
backButtonEl = getElementByXpath(backXpath);
}
backButtonEl.click()
return true;
} catch (error) {
console.error('Error during form submission:', error);
return false;
}
}
function waitForButton(buttonXpath, timeout = 10000, maxRetries = 5, retryInterval = 500) {
return new Promise((resolve, reject) => {
let retries = 0;
let checkInterval;
const findButtonWithRetry = () => {
const button = getElementByXpath(buttonXpath);
if (button) {
checkInterval = setInterval(() => {
if (!button.disabled) {
clearInterval(checkInterval);
resolve(button);
}
}, 100);
} else if (retries < maxRetries) {
retries++;
setTimeout(findButtonWithRetry, retryInterval);
} else {
reject(new Error('Button not found after ' + maxRetries + ' retries'));
}
};
findButtonWithRetry();
setTimeout(() => {
clearInterval(checkInterval);
reject(new Error('Waiting timeout left'));
}, timeout);
});
}
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
function getRandomAmount(min = 0.5, max = 5) {
return (Math.random() * (max - min) + min).toFixed(3);
}
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function setReactInputValue(element, value) {
try {
if (!element || typeof element.value === 'undefined') {
throw new Error('Element is not a valid input');
}
const descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
if (!descriptor || !descriptor.set) {
element.value = value;
element.dispatchEvent(new Event('input', { bubbles: true }));
return;
}
descriptor.set.call(element, value);
const events = ['input', 'change', 'blur'];
events.forEach(type => {
element.dispatchEvent(new Event(type, { bubbles: true }));
});
} catch (error) {
console.error('Error in setReactInputValue:', error);
if (element) {
element.value = value;
element.dispatchEvent(new Event('input', { bubbles: true }));
}
}
}
async function runIterations(iterations, delayBetweenIterationsFrom = 500, delayBetweenIterationsTo = 2000) {
let isCancelled = false;
let completedIterations = 0;
// Function to cancel execution
const cancel = () => {
isCancelled = true;
};
// To be able to cancel from console, we can expose `cancel` function globally
window.cancelScript = cancel;
console.log("To stop execution, type 'window.cancelScript()' in the console.");
for (let i = 0; i < iterations && !isCancelled; i++) {
console.log(`Starting iteration ${i + 1}/${iterations}`);
const success = await sendForm();
if (!success) {
console.log(`Iteration ${i + 1} failed. Stopping.`);
break;
}
completedIterations++;
console.log(`Iteration ${i + 1} completed successfully.`);
if (i < iterations - 1 && !isCancelled) {
const delay = getRandom(delayBetweenIterationsFrom, delayBetweenIterationsTo)
console.log(`Waiting for ${delay}ms before next iteration...`);
await sleep(delay);
}
}
console.log(`Completed ${completedIterations} of ${iterations} iterations.`);
window.cancelScript = undefined; // Clean up
return { completed: completedIterations, cancelled: isCancelled };
}
/**
* 30 - iterations
* 500 - start of range delay in ms between iterations
* 1500 - end of range delay in ms between iterations
*/
runIterations(10, 500, 1400);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment