@surged/sdk
v0.3.1
Published
Pricing formula, quotes, calldata encoders and ABIs for Surged, the token launchpad on Arc.
Maintainers
Readme
@surged/sdk
TypeScript for Surged, the token launchpad on Arc: the pricing formula, quotes with the snipe tax, calldata encoders, the bundle consent, unit helpers and the generated ABIs. It is the same code the Surged app runs, and its tests compare it byte for byte against what the contracts compute, so a quote from this package matches what the chain will do.
Pure TypeScript on top of viem: no React, no Node APIs, no network calls of its own. It runs the same in a browser, in a worker and on a server.
Install
npm add @surged/sdk viemviem is a peer in practice. The package returns viem's types and expects you to send the calldata with it,
though nothing stops you passing the data and value it returns to ethers or to a raw JSON-RPC call.
Where things live
| | |
| -------- | ---------------------------------------------------------------- |
| App | surged.fun |
| Api | https://api.surged.fun |
| Chain | Arc mainnet, chain id 5042 (testnet 5042002 is also shipped) |
| Explorer | explorer.arc.io |
The shape of a trade
- Read the curve.
GET /launches/{token}returns its terms and its current state. - Price the trade locally.
previewBuyandpreviewSellrun the exact formula of the contract. No round trip, noeth_call, and you can chain quotes to simulate a sequence of trades. - Send the calldata.
encodeBuyandencodeSellbuild it; you sign and send it.
Before you start
Two things about Arc catch people out:
- USDC is the native coin and it has 18 decimals, while the ERC-20 view of the same balance has 6. Every
amount in this package is in the 18-decimal units unless it says otherwise;
toNativeUnitsandtoErc20Unitsare the only conversions you need. - Gas is paid in USDC, so a buy that spends the whole balance leaves nothing to pay for itself.
Modules
| Module | What it gives you |
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| curve | previewBuy, previewSell, quoteBuy, quoteSell, applyBuy, applySell, sellableTokens, readyToGraduate, curveStateFromApi and the math underneath. |
| units | toNativeUnits, toErc20Units, minOutWithSlippage, BPS. |
| encoders | encodeLaunchToken, encodeBuy, encodeSell, encodeLaunchAndBuy, encodeLaunchAndBuyMany, encodeLaunchAndBuyManyFrom. |
| bundle | bundleBuyTypedData, hashBundleBuy: the consent a wallet signs to join a bundle. |
| errors | explainError, decodeLaunchpadError: a stable code and a sentence, never raw calldata. |
| chains | arc, arcTestnet, USDC_PREDEPLOY, NATIVE_QUOTE, ARC_BLOCK_TIME_MS. |
| abi | The generated ABIs: factory, curve, hooked curve, forwarder, escrow, token, locker. |
| addresses | deployments[chainId]: the live contracts on each chain. |
Every amount is a bigint
Nothing in this package takes or returns a number for an amount. Token balances and USDC amounts on Arc do
not fit in a JavaScript number, and a quote computed through one is wrong without failing.
The api sends amounts as decimal strings for the same reason. curveStateFromApi is the one place they
become bigint; use it rather than mapping the fields yourself.
Quote a buy
import {
curveStateFromApi,
previewBuy,
minOutWithSlippage,
DEFAULT_SLIPPAGE_BPS,
} from '@surged/sdk'
const launch = await fetch(`${API}/launches/${token}`).then((r) => r.json())
const { head } = await fetch(`${API}/health`).then((r) => r.json())
// The buy lands in the next block, which is the block the snipe tax is priced at.
const quote = previewBuy(curveStateFromApi(launch), quoteIn, { currentBlock: BigInt(head) + 1n })
const minTokensOut = minOutWithSlippage(quote.tokensOut, DEFAULT_SLIPPAGE_BPS)quote.crossing tells you the buy sells the last tokens and graduates the token into its Uniswap pool;
quote.refund is what the curve sends back in that case.
Launch a token with a first buy
import { encodeLaunchAndBuy, deployments, NATIVE_QUOTE } from '@surged/sdk'
const { data, value } = encodeLaunchAndBuy({
params, // TokenParams: name, symbol, logoUri, description, socials, creatorFeeRecipient, creatorTaxBps, expectedEconomics, salt
launchConfigId: 0n,
pairToken: NATIVE_QUOTE,
snipeTaxExemptions: [],
launchFee, // LaunchFactory.launchFee(), also served by the api
quoteIn,
minTokensOut,
recipient,
})
await wallet.sendTransaction({ to: deployments[5042].launchForwarder, data, value })Sign a bundle consent
Version 0.3.0 requires a deployed v2 forwarder. Check /config.bundleConsent.supported and verify the live getter immediately before approvals/signatures. Never infer compatibility from the package version or stored deployment address. Revalidate the launch hash and nonces after simulation before sending; changed terms require new signatures.
import { bundleAuthorizationCalls, bundleBuyTypedData } from '@surged/sdk'
import { isHex } from 'viem'
// Read against the exact launch being signed; this includes current factory policy.
const [version, launchHash, nonce] = await client.multicall({
contracts: bundleAuthorizationCalls(forwarder, launch, [buyer]),
allowFailure: false,
batchSize: 0,
})
if (version !== 2n || !isHex(launchHash, { strict: true }) || launchHash.length !== 66 || typeof nonce !== 'bigint') {
throw new Error('Secure bundle consent is unavailable for this forwarder')
}
const signature = await wallet.signTypedData(
bundleBuyTypedData(chainId, forwarder, {
buyer,
quoteIn,
minTokensOut,
initiator,
launchHash,
nonce,
deadline,
}),
)quoteIn must be a multiple of 1e12 for a native quote: the forwarder pulls it through the USDC predeploy.
Versions
The package ships recorded deployment addresses and encoders. The unreleased 0.3.0 v2 forwarder changes require a new verified deployment before use; existing addresses do not become v2 when this package is installed.
- A redeploy of any contract is a minor bump, never a patch. Pinning
~0.2.0keeps you on contracts that still exist. - The formula and the encoders change only on a minor. A patch is for a bug that made this package disagree with the chain, where the old behaviour was already wrong.
While the package is 0.x the public surface may still change between minors. Pin an exact version if you
run unattended.
License
MIT
