tessel-sdk
v0.2.0
Published
Tessel SDK: contract ABIs, route finder, bin distribution math, and x402 (EIP-3009, USDG) paywall and payer.
Maintainers
Readme
tessel-sdk
Shared code for the Tessel site, agents and bots: contract ABIs, the route finder, bin distribution helpers, and an x402 client for buying Tessel pool data and quotes in USDG.
Buy data over x402
- Read the free discovery document:
GET <site>/api/x402. It lists the network, the USDGasset,payTo, and each paid resource with its price. - Call a resource without payment. It answers
402with the price in thePAYMENT-REQUIREDheader (v2) and in the body (v1). - Sign a USDG
TransferWithAuthorization(EIP-3009) for exactly that price topayTo, and send the request again with thePAYMENT-SIGNATURE(v2) orX-PAYMENT(v1) header. - The server settles the payment on chain and only then returns
200with{ data, payment }, wherepaymentholds the payer, the transaction hash and the network.
X402Payer does steps 2–4 for you:
import { Wallet } from "ethers";
import { X402Payer } from "tessel-sdk/payer";
import { createProvider } from "tessel-sdk/relayer";
const provider = createProvider(RPC_URL, CHAIN_ID);
const payer = new X402Payer({
signer: new Wallet(PAYER_KEY, provider),
provider,
chainId: CHAIN_ID,
usdg: ASSET, // "asset" from the discovery document
maxPrice: 10_000n, // never sign more than 0.01 USDG per request
});
const pre = await payer.preview(url); // price, balance, canProceed, reasons
const res = await payer.pay(url); // { response, paid, transaction }
const { data } = await res.response.json();Rules the payer follows: it refuses a price above maxPrice without signing, checks the USDG balance first, and only
signs for USDG on the chain you gave it.
Rules the server follows:
- Nothing is charged for bad parameters or when the chain cannot be read.
- A signed payment is charged once at most. Sending it again to the same resource returns the data again; sending it to another resource is refused.
- Payments must be addressed to
payTo, for at least the price, inside the time window.
A full example is agents/src/x402-bot.ts (pnpm run bot WETH USDG in agents/).
Other exports
| Import | What |
|---|---|
| tessel-sdk/deployments | contract addresses per chain id |
| tessel-sdk/abis | ABIs generated from contracts/out (pnpm gen:abi in web/) |
| tessel-sdk/routes | candidatePaths, quoteRoutes: direct and two-hop routes, quoted on chain through the router |
| tessel-sdk/binDist | idForPrice, priceForId, distribute for spot, curve and bid-ask shapes |
| tessel-sdk/x402 | the x402 "exact" scheme: requirements, signing, verification, paywall |
| tessel-sdk/x402-store | settlement records: memoryStore, sqlStore |
| tessel-sdk/relayer | providers and the settlement relayer signer |
Tests
pnpm test runs the unit tests. The end-to-end test runs against a live site:
X402_E2E_API=http://localhost:3100 X402_E2E_PAYER_KEY=<key with USDG> npx tsx --test test/x402.e2e.test.mjs.
