A bot that buys new launches
10 minutes · Node 20+ · test BNB
watch-launches.mjs
buy.mjs
Two parts: a watcher that sees every launch without missing one, and a buyer that turns a decision into a signed, simulated trade.
1. Watch every launch
Section titled “1. Watch every launch”GET /launches returns the newest launches and a next cursor. Ask again with ?after=<next> and you get only what launched since, oldest first. Pages never end part-way through a block, so nothing slips between two polls.
// Every new launch, the moment it is indexed, none missed. Polls with the// `next` cursor: pages never end part-way through a block.// node watch-launches.mjs (SECONDS=60 to stop after a minute)const API = process.env.MM_API ?? 'https://marketmayhem.co/api/v1';const stopAt = process.env.SECONDS ? Date.now() + Number(process.env.SECONDS) * 1000 : Infinity;
// Start from now: the newest launch's block.let { next } = await (await fetch(`${API}/launches?limit=1`)).json();let seen = 0;console.log(`watching from block ${next}`);
while (Date.now() < stopAt) { const page = await (await fetch(`${API}/launches?after=${next}`)).json(); for (const l of page.launches) { seen += 1; console.log(`new: $${l.symbol} ${l.token} by ${l.creator} https://marketmayhem.co/t/${l.token}`); } next = page.next; await new Promise((r) => setTimeout(r, 3000));}console.log('RESULT', JSON.stringify({ seen, next }));2. Buy
Section titled “2. Buy”The API builds the trade for your wallet and simulates it. Your key signs it locally. It waits until the trade is indexed, meaning our reads show it, before reporting success.
// Buy a token with BNB, from your own wallet. The API builds and simulates the// transaction; your key signs it here and never leaves this process.// PRIVATE_KEY=0x… TOKEN=0x… node buy.mjsimport { JsonRpcProvider, Wallet } from 'ethers';
const API = process.env.MM_API ?? 'https://marketmayhem.co/api/v1';const token = process.env.TOKEN;const spend = process.env.AMOUNT ?? '0.001';
// The network, from the API: never hard-code it.const settings = await (await fetch(`${API}/settings`)).json();const rpc = settings.chainId === 56 ? 'https://bsc-dataseed.bnbchain.org' : 'https://bsc-testnet-rpc.publicnode.com';const wallet = new Wallet(process.env.PRIVATE_KEY, new JsonRpcProvider(rpc, settings.chainId));
async function build(path, body) { const res = await fetch(`${API}${path}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }); const json = await res.json(); if (!res.ok) throw new Error(`${json.error.code}: ${json.error.message} ${json.error.fix ?? ''}`); return json;}
async function send(tx) { const sent = await wallet.sendTransaction({ to: tx.to, data: tx.data, value: BigInt(tx.value), gasLimit: tx.gas ? BigInt(tx.gas) : undefined }); const receipt = await sent.wait(); if (receipt.status !== 1) throw new Error(`reverted: ${sent.hash}`); return sent.hash;}
async function waitUntilIndexed(hash) { for (;;) { const { status } = await (await fetch(`${API}/tx/${hash}`)).json(); if (status === 'indexed' || status === 'failed') return status; await new Promise((r) => setTimeout(r, 2000)); }}
const request = { wallet: wallet.address, token, amount: spend, slippageBps: 100 };let built = await build('/build/buy', request);// Buying a BNB-paired token needs no approval. A token-paired one may: send the// one-time steps, then build again so the trade itself is simulated.while (built.requires.length) { for (const step of built.requires) await send(step.tx); built = await build('/build/buy', request);}
console.log(built.explain);const hash = await send(built.tx);const status = await waitUntilIndexed(hash);console.log(`${status}: ${hash}`);console.log('RESULT', JSON.stringify({ hash, status }));npm i ethers, and save both files.- Try the buyer on testnet:
MM_API=https://marketmayhem.co/api/v1 PRIVATE_KEY=0x… TOKEN=0x… AMOUNT=0.001 node buy.mjs - Join them: in the watcher’s loop, call the buyer’s
buildandsendfor each launch you want.
Make it earn
Section titled “Make it earn”Pass your own registered address as referrer in each build (or use an API key), and trades your bot routes pay you 0.2% of each trade. See earn from referrals.