@routerx.exchange/sdk
v0.1.12
Published
RouterX frontend SDK — EIP-1193 quote & swap against RXRouter
Readme
@routerx.exchange/sdk
RouterX frontend SDK: quote and swap against on-chain RXRouter via an EIP-1193 provider (no routerx-api HTTP).
Provider configuration
provider is optional for read-only operations. RouterX selects its read provider in this order:
provider(an EIP-1193 wallet provider)rpcUrls(your HTTPS JSON-RPC endpoints)- Curated built-in public RPC URLs
Public RPCs are best-effort only; supply rpcUrls for production reliability. A wallet is still required
for approve and swap. You can connect or replace a wallet later with setProvider(provider), clear it
with setProvider(undefined), and update the RPC fallback with setRpcUrls(urls).
Install
npm install @routerx.exchange/sdkQuote before wallet connect
import { RouterX, NATIVE_PLACEHOLDER } from '@routerx.exchange/sdk';
const rx = new RouterX({
chainId: 'base',
rpcUrls: ['https://your-base-rpc.example'],
});
const quote = await rx.swap.quote({
tokenIn: NATIVE_PLACEHOLDER,
tokenOut: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
amountIn: '1000000000000000000',
});
// When the user connects a wallet:
rx.setProvider(window.ethereum);Quick start
import { RouterX, NATIVE_PLACEHOLDER } from '@routerx.exchange/sdk';
const rx = new RouterX({ provider: window.ethereum, chainId: 8453 });
const q = await rx.swap.quote({
tokenIn: NATIVE_PLACEHOLDER,
tokenOut: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
amountIn: '1000000000000000000',
});
const { tx, hash } = await rx.swap.swap({
tokenIn: q.tokenIn,
tokenOut: q.tokenOut,
amountIn: q.amountIn,
to: account,
});For ERC-20 inputs on RouterX chains, prefer swapWithPermit2 (approve Permit2 once,
then EIP-712 SignatureTransfer + swapNoSplit*WithPermit2). Native tokenIn still uses swap.
await rx.swap.swapWithPermit2({
tokenIn: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
tokenOut: '0x4200000000000000000000000000000000000006',
amountIn: '1000000',
to: account,
// nonce / deadline optional — SDK picks an unused Permit2 nonce and a 20-minute deadline
});chainId accepts a number (e.g. 8453) or a chain code (e.g. 'base').
Limit orders (Maker)
The default Maker API is https://limitorder-api.routerx.exchange. Override it with
limitOrderApiUrl when using another deployment:
const rx = new RouterX({
chainId: 8453,
provider: window.ethereum,
limitOrderApiUrl: 'https://limitorder-api.routerx.exchange',
});Creating an order does not approve tokens automatically. Approve the Maker asset for the chain's limit-order protocol, then create and submit the signed order:
await rx.limitOrder.approve({ token: makerAsset, amount: makingAmount });
const created = await rx.limitOrder.create({
makerAsset,
takerAsset,
makingAmount,
takingAmount,
maker: account,
});Use cancelOrders({ orderHashes }) for gasless cancellation. For a hard on-chain cancellation,
pass the full Maker order to cancelOrderOnChain(order).
buildOrder/create leave getMakerAmount/getTakerAmount empty (0x) by default, which the
limit-order protocol treats as exact-fill only — the order must be filled in full in a single
fill. Pass partialFill: true to enable linear partial fills (sets AmountCalculator getters via
encodeLinearAmountGetters, also exported from the package). You can still pass custom
getMakerAmount/getTakerAmount calldata explicitly; explicit getters always win. DCA orders set
these getters automatically since they always fill partially across many swaps.
DCA (experimental)
dca is experimental: its create/list submission and listing paths call planned API endpoints
(/v1/dca/orders) that may not be deployed on every environment yet, and request/response shapes
may still change. cancelOrders currently reuses the plain limit-order cancel endpoints
(/v1/orders/cancel-sign, /v1/orders/cancel) — there is no dedicated DCA cancel endpoint yet, so
cancellation may 404 or behave unexpectedly until the API adds first-class DCA cancel support.
Endpoint paths and payload shapes for this module should be expected to change without a major
version bump while it remains experimental.
DCA requires an executor address, supplied as executorAddress when constructing RouterX
or returned by the limit-order API chain configuration. It reuses the same limit-order approval
and gasless cancellation methods.
API
| Namespace | Method | Description |
| --- | --- | --- |
| swap | quote | On-chain quote (findBestPathWithGas → fallback findBestPath) |
| swap | buildSwapTx | Quote + build an unsigned swap transaction |
| swap | getAllowance / approve | ERC-20 allowance (spender = Router) |
| swap | swap | Send swap, optionally with auto-approve |
| swap | getPermit2TypedData / buildSwapTxWithPermit2 | Permit2 typed data + *WithPermit2 calldata |
| swap | getPermit2Allowance / approvePermit2 | ERC-20 allowance (spender = Permit2) |
| swap | swapWithPermit2 | Approve Permit2 (optional) + sign + send *WithPermit2 |
| limitOrder | buildOrder / getSignMessage / signOrder | Build and sign Maker orders |
| limitOrder | create / createFromOrder / submitOrder | Submit Maker orders |
| limitOrder | list / activeMakingAmount | Query Maker orders and active amounts |
| limitOrder | cancelOrders / cancelOrderOnChain | Gasless or hard cancellation |
| limitOrder | getAllowance / approve | LOP allowance and approval |
| dca (experimental) | buildOrder / buildSchedule / create / list | Build, sign, submit, and list DCA orders |
| dca (experimental) | cancelOrders / getAllowance / approve | Shared LOP cancellation and approval |
