Skip to content

Read launches from events

Index Stargaze launches straight from contract events.

Stargaze has no off-chain launch registry: the app builds its launch list from three events (see lib/services/stargaze.ts). You can do the same from any viem client, or call GET /api/launches to get the already-indexed result.

Events to index

ContractEventGives you
StargazeLaunchFactoryTokenLaunchedtoken, creator, pair, allocations, ETH paired, whether LP was burned
StargazeFairLaunchSaleCreatedsaleId, token, creator, sale/liquidity tokens, reserve, start/end, claim delay, fee
StargazeFairLaunchFinalizedoutcome, total raised, pair, liquidity used, tokens burned

Fetch all launches

read-launches.ts
import { createPublicClient, http, zeroAddress } from "viem";import { sepolia } from "viem/chains";import { fairLaunchAbi, launchFactoryAbi } from "@/lib/web3/abis";import { stargazeContracts } from "@/lib/config/chains"; const { launchFactory, fairLaunch, deployBlock } = stargazeContracts[11155111];const client = createPublicClient({ chain: sepolia, transport: http(process.env.SEPOLIA_RPC_URL) });const toBlock = await client.getBlockNumber(); const instant = launchFactory  ? await client.getContractEvents({      address: launchFactory, abi: launchFactoryAbi, eventName: "TokenLaunched",      fromBlock: deployBlock, toBlock,    })  : []; const [created, finalized] = fairLaunch  ? await Promise.all([      client.getContractEvents({ address: fairLaunch, abi: fairLaunchAbi, eventName: "SaleCreated", fromBlock: deployBlock, toBlock }),      client.getContractEvents({ address: fairLaunch, abi: fairLaunchAbi, eventName: "Finalized", fromBlock: deployBlock, toBlock }),    ])  : [[], []]; for (const l of instant) {  const { token, creator, pair, symbol, liquidityBurned } = l.args;  console.log("instant", symbol, token, creator, pair === zeroAddress ? null : pair, liquidityBurned);} const outcome = new Map(finalized.map((l) => [l.args.saleId!, l.args]));for (const l of created) {  const f = outcome.get(l.args.saleId!);  console.log("fair", l.args.symbol, l.args.saleId, f ? (f.succeeded ? "succeeded" : "failed") : "open");}

Large block ranges

RPC providers cap eth_getLogs ranges and result sizes differently. The app splits the range in half on any error, recursively, down to 500 blocks (and at most 14 levels deep):

lib/services/stargaze.ts (simplified)
async function getLogsAdaptive(client, address, event, from: bigint, to: bigint, depth = 0) {  try {    return await client.getLogs({ address, event, fromBlock: from, toBlock: to });  } catch (e) {    if (to - from < 500n || depth > 14) throw e;    const mid = from + (to - from) / 2n;    const [a, b] = await Promise.all([      getLogsAdaptive(client, address, event, from, mid, depth + 1),      getLogsAdaptive(client, address, event, mid + 1n, to, depth + 1),    ]);    return [...a, ...b];  }}

Use getAbiItem({ abi: launchFactoryAbi, name: "TokenLaunched" }) to get the event argument for getLogs. Always start at deployBlock — scanning from genesis is slow and may be rejected.

Timestamps

Logs carry a block number, not a time. The app fetches getBlock({ blockNumber }) for each distinct block (the 300 most recent) and leaves timestamp: null when a block cannot be read, rather than guessing.

Live state of a fair sale

Events tell you what happened; for a running sale read the contract. status(saleId) returns 0 Pending, 1 Live, 2 Ended (awaiting finalize), 3 Succeeded or 4 Failed.

sale-state.ts
const [sale, status] = await Promise.all([  client.readContract({ address: fairLaunch!, abi: fairLaunchAbi, functionName: "getSale", args: [saleId] }),  client.readContract({ address: fairLaunch!, abi: fairLaunchAbi, functionName: "status", args: [saleId] }),]);const progress = sale.reserveEth > 0n ? Number((sale.totalCommitted * 10_000n) / sale.reserveEth) / 100 : 100; // % of reserve