accounts 0.8.6 → 0.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/core/Schema.d.ts +2 -0
  3. package/dist/core/Schema.d.ts.map +1 -1
  4. package/dist/core/Storage.js +1 -1
  5. package/dist/core/Storage.js.map +1 -1
  6. package/dist/core/zod/rpc.d.ts +2 -0
  7. package/dist/core/zod/rpc.d.ts.map +1 -1
  8. package/dist/core/zod/rpc.js +2 -0
  9. package/dist/core/zod/rpc.js.map +1 -1
  10. package/dist/server/internal/handlers/exchange.d.ts.map +1 -1
  11. package/dist/server/internal/handlers/exchange.js +5 -19
  12. package/dist/server/internal/handlers/exchange.js.map +1 -1
  13. package/dist/server/internal/handlers/relay.d.ts +20 -12
  14. package/dist/server/internal/handlers/relay.d.ts.map +1 -1
  15. package/dist/server/internal/handlers/relay.js +152 -84
  16. package/dist/server/internal/handlers/relay.js.map +1 -1
  17. package/dist/server/internal/kv.d.ts +7 -4
  18. package/dist/server/internal/kv.d.ts.map +1 -1
  19. package/dist/server/internal/kv.js +70 -11
  20. package/dist/server/internal/kv.js.map +1 -1
  21. package/dist/server/internal/tokenlist.d.ts +53 -0
  22. package/dist/server/internal/tokenlist.d.ts.map +1 -0
  23. package/dist/server/internal/tokenlist.js +38 -0
  24. package/dist/server/internal/tokenlist.js.map +1 -0
  25. package/package.json +1 -1
  26. package/src/core/Storage.ts +1 -1
  27. package/src/core/zod/rpc.ts +2 -0
  28. package/src/server/internal/handlers/exchange.ts +7 -31
  29. package/src/server/internal/handlers/relay.test.ts +61 -15
  30. package/src/server/internal/handlers/relay.ts +184 -94
  31. package/src/server/internal/kv.test.ts +129 -0
  32. package/src/server/internal/kv.ts +76 -10
  33. package/src/server/internal/tokenlist.ts +91 -0
@@ -0,0 +1,91 @@
1
+ import type { Address } from 'viem'
2
+
3
+ import type * as Kv from '../Kv.js'
4
+ import { cached } from './kv.js'
5
+
6
+ /** Default cache TTL in seconds (10 minutes) for tokenlist responses. */
7
+ const defaultCacheTtl = 10 * 60
8
+
9
+ /** Default base URL for the verified tokenlist service. */
10
+ const defaultBaseUrl = 'https://tokenlist.tempo.xyz'
11
+
12
+ /** A single tokenlist entry. */
13
+ export type Token = {
14
+ /** Token address. */
15
+ address: Address
16
+ /** Token decimals. */
17
+ decimals: number
18
+ /** Token logo URI. */
19
+ logoUri?: string | undefined
20
+ /** Token name. */
21
+ name: string
22
+ /** Token symbol. */
23
+ symbol: string
24
+ }
25
+
26
+ /**
27
+ * Fetches the verified tokenlist for `chainId`. Reads through `kv` so
28
+ * concurrent callers in the same scope share a single upstream request,
29
+ * and so independent handlers (e.g. `Handler.relay` + `Handler.exchange`)
30
+ * reuse the same KV cache key.
31
+ *
32
+ * Returns an empty list on any non-OK response — callers should fall back
33
+ * to chain-supplied behavior rather than treating an empty list as fatal.
34
+ *
35
+ * @param chainId - Chain id to fetch the tokenlist for.
36
+ * @param kv - Kv used to cache responses across requests.
37
+ * @param options - Options.
38
+ * @returns Tokens for the chain.
39
+ */
40
+ export async function fetch(
41
+ chainId: number,
42
+ kv: Kv.Kv,
43
+ options: fetch.Options = {},
44
+ ): Promise<readonly Token[]> {
45
+ const { baseUrl = defaultBaseUrl, cacheTtl = defaultCacheTtl, resolver } = options
46
+ return cached(
47
+ kv,
48
+ `tokenlist:${chainId}`,
49
+ async () => (resolver ? resolver(chainId) : fetchUncached(chainId, baseUrl)),
50
+ { ttl: cacheTtl },
51
+ )
52
+ }
53
+
54
+ export declare namespace fetch {
55
+ /** Options for `fetch()`. */
56
+ type Options = {
57
+ /**
58
+ * Base URL of the tokenlist service.
59
+ * @default 'https://tokenlist.tempo.xyz'
60
+ */
61
+ baseUrl?: string | undefined
62
+ /**
63
+ * TTL in seconds for the cached response.
64
+ * @default 600 (10 minutes)
65
+ */
66
+ cacheTtl?: number | undefined
67
+ /**
68
+ * Override resolver. When provided, the cache hit path is unchanged but
69
+ * cache misses route through this function instead of fetching the
70
+ * default `tokenlist.tempo.xyz` URL. Useful for tests or for vendoring
71
+ * a curated list per app.
72
+ */
73
+ resolver?: ((chainId: number) => readonly Token[] | Promise<readonly Token[]>) | undefined
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Default fetcher — resolves the verified tokenlist for `chainId` directly
79
+ * from `tokenlist.tempo.xyz`. Returns an empty list on any non-OK response.
80
+ */
81
+ async function fetchUncached(chainId: number, baseUrl: string): Promise<readonly Token[]> {
82
+ const response = await globalThis.fetch(`${baseUrl}/list/${chainId}`)
83
+ if (!response.ok) return []
84
+ const data = (await response.json()) as {
85
+ tokens: readonly (Token & { logoURI?: string })[]
86
+ }
87
+ return data.tokens.map(({ logoURI, ...token }) => ({
88
+ ...token,
89
+ logoUri: token.logoUri ?? logoURI,
90
+ }))
91
+ }