Quote a buy (JavaScript)
Example
This is an example. Every value in it (names, amounts, addresses) is there to show you how; change any of them to suit you. It’s quote.mjs from developers/examples, as it is. Anything that sends waits for SEND=1: there is no testnet, so a sent transaction spends real ETH.
// What 0.001 ETH buys of a coin right now, simulated through every route it trades on. Sends nothing.// COIN=0x… node quote.mjs (no COIN: the busiest coin on Robinhood Chain today)const API = process.env.UPRALLY_API ?? 'https://uprally.co/api/v1';
async function get(path) { const res = await fetch(`${API}${path}`); const json = await res.json(); if (!res.ok) throw new Error(`${json.error.code}: ${json.error.message} ${json.error.fix ?? ''}`); return json;}
const coin = process.env.COIN ?? (await get('/coins?sort=volume&limit=1')).coins[0].address;const q = await get(`/quote?token=${coin}&side=buy&amount=${process.env.AMOUNT ?? '0.001'}`);
console.log(`${q.amountIn.amount} ETH buys ${q.amountOut.amount} ${q.symbol} (at least ${q.minOut.amount})`);console.log(`route: ${q.venue.kind === 'curve' ? `the ${q.venue.launchpad} curve` : q.venue.hops.map((h) => `Uniswap ${h.version}`).join(' → ')}, best of ${q.routesCompared}`);console.log(`price impact ${q.priceImpactPct}%; selling it straight back returns ${q.sellBack.eth.amount} ETH`);console.log(`RESULT ${q.amountOut.wei}`);