askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
assets.ts · 2.4 KB · 68 lines
| 1 | /** |
| 2 | * Pinned asset registry — the root of trust for pricing and signing. |
| 3 | * |
| 4 | * The x402 reference client takes the EIP-712 domain (`name`, `version`) and the |
| 5 | * asset address straight from the resource server's 402 response, then signs against |
| 6 | * them. That means the party being paid chooses the domain your wallet signs over, |
| 7 | * and chooses the token whose decimals determine what `amount` actually costs you. |
| 8 | * |
| 9 | * We refuse to do that. An asset is payable only if it appears here, and when we sign |
| 10 | * we use THIS table's domain and decimals, ignoring whatever `extra` claimed. If a |
| 11 | * server names an asset we don't know, we cannot price it, so we do not pay it. |
| 12 | */ |
| 13 | |
| 14 | export interface PinnedAsset { |
| 15 | symbol: string; |
| 16 | address: string; // lowercase |
| 17 | network: string; // CAIP-2 |
| 18 | decimals: number; |
| 19 | /** EIP-712 domain, pinned. Not read from the 402 response. */ |
| 20 | eip712: { name: string; version: string }; |
| 21 | /** Micro-USD per whole token. USDC == 1_000_000. */ |
| 22 | usdMicroPerToken: bigint; |
| 23 | } |
| 24 | |
| 25 | const ASSETS: PinnedAsset[] = [ |
| 26 | { |
| 27 | symbol: "USDC", |
| 28 | address: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913", |
| 29 | network: "eip155:8453", // Base mainnet |
| 30 | decimals: 6, |
| 31 | eip712: { name: "USD Coin", version: "2" }, |
| 32 | usdMicroPerToken: 1_000_000n, |
| 33 | }, |
| 34 | { |
| 35 | symbol: "USDC", |
| 36 | address: "0x036cbd53842c5426634e7929541ec2318f3dcf7e", |
| 37 | network: "eip155:84532", // Base Sepolia |
| 38 | decimals: 6, |
| 39 | eip712: { name: "USDC", version: "2" }, |
| 40 | usdMicroPerToken: 1_000_000n, |
| 41 | }, |
| 42 | ]; |
| 43 | |
| 44 | const key = (network: string, address: string) => `${network}|${address.toLowerCase()}`; |
| 45 | |
| 46 | const INDEX = new Map(ASSETS.map(a => [key(a.network, a.address), a])); |
| 47 | |
| 48 | export function lookupAsset(network: string, address: string): PinnedAsset | undefined { |
| 49 | return INDEX.get(key(network, address)); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Convert an atomic-unit amount to micro-USD using OUR pinned decimals. |
| 54 | * This is the only place a price becomes a dollar figure. |
| 55 | */ |
| 56 | export function priceUsdMicro(asset: PinnedAsset, atomicAmount: string): bigint { |
| 57 | const amount = BigInt(atomicAmount); |
| 58 | if (amount < 0n) throw new Error("negative amount"); |
| 59 | return (amount * asset.usdMicroPerToken) / 10n ** BigInt(asset.decimals); |
| 60 | } |
| 61 | |
| 62 | export function formatUsd(usdMicro: bigint): string { |
| 63 | const neg = usdMicro < 0n; |
| 64 | const v = neg ? -usdMicro : usdMicro; |
| 65 | const dollars = v / 1_000_000n; |
| 66 | const frac = (v % 1_000_000n).toString().padStart(6, "0").replace(/0+$/, "") || "0"; |
| 67 | return `${neg ? "-" : ""}$${dollars}.${frac.padEnd(2, "0")}`; |
| 68 | } |