mirror of
https://github.com/hanzoai/viem.git
synced 2026-10-30 19:36:39 +00:00
* feat(tokens): add viem/tokens entrypoint with ERC-20 + USDC abstractions Adds a composable viem/tokens entrypoint: - Generic Actions.erc20.* actions (read/write/sync/watch) with .call + extractEvent - Decorators.defineErc20 factory + usdc() client decorator (client.usdc.*) - Per-chain USDC address map (Addresses.usdc) - String amounts parsed via decimals; address resolvable from client chain, an explicit address, or a chain override - Docs (site/pages/tokens.mdx) wired into core + Tokens sidebars Checkpoint before refactor to chain-config tokens + tokenActions() proxy. Amp-Thread-ID: https://ampcode.com/threads/T-019f00b4-7aca-73df-8be4-4e0aa526a460 * feat(tokens): add token actions * docs: add tokens entrypoint changeset * docs: update token overview wording * docs: add token page descriptions * docs: revise token page descriptions * docs: restore concise token descriptions * fix(tokens): repair ci failures * test(tokens): update client token snapshots * Update bright-tempo-amounts.md * fix(chains): inline token configs * fix(types): mark token name helpers internal * test(actions): update token export snapshot * test(types): bump wallet client baseline * ci: skip wagmi verification * ci: document wagmi verification skip * test(types): update token bench baselines * feat: resolve token selectors from client tokens * feat: use token arrays on clients * feat: add token metadata * test: move client token types * refactor: rename client token type * test: cover token chain mappings * refactor(scripts): use Actions.token.getMetadata for tokenlist currency Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * Delete .changeset/fresh-token-metadata.md * test(tempo): declare client tokens for fee token resolution Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * test(clients): add tokens to client snapshots; typecheck scripts against src Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * fix(tempo): resolve tokens from client tokens instead of chain Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * refactor: remove chain tokens; resolve tokens from client tokens Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * ci: drop TypeScript 5.8.2 from verify matrix Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * test: remove stale chain tokens snapshots Amp-Thread-ID: https://ampcode.com/threads/T-019f162d-c5f7-77fa-a5f5-f2571de82ccd * test(tempo): use token address selectors after removing chain tokens Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * test(tempo): update genesis token name snapshots to PathUSD Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a * test(tempo): make tokenlessClient truly tokenless; align genesis token snapshots Amp-Thread-ID: https://ampcode.com/threads/T-019f154e-c2d9-759c-a0e2-54d158f5687a
142 lines
3.5 KiB
TypeScript
142 lines
3.5 KiB
TypeScript
import { mkdirSync } from 'node:fs'
|
|
|
|
type Address = `0x${string}`
|
|
|
|
type Chain = {
|
|
id: number
|
|
name: string
|
|
testnet?: boolean | undefined
|
|
}
|
|
|
|
type TokenLookupChain = {
|
|
address: Address
|
|
id: number
|
|
name: string
|
|
}
|
|
|
|
type TokenLookupToken = {
|
|
chains: TokenLookupChain[]
|
|
currency?: string | undefined
|
|
decimals: number
|
|
importName: string
|
|
name: string
|
|
popular?: boolean | undefined
|
|
symbol: string
|
|
}
|
|
|
|
type Token = {
|
|
addresses: Record<number, Address>
|
|
currency?: string | undefined
|
|
decimals: number
|
|
name?: string | undefined
|
|
popular?: boolean | undefined
|
|
symbol?: string | undefined
|
|
}
|
|
|
|
const chainsPath = new URL('../src/chains/index.ts', import.meta.url).href
|
|
const tokensPath = new URL('../src/tokens/index.ts', import.meta.url).href
|
|
|
|
const Chains = (await import(chainsPath)) as Record<string, unknown>
|
|
const Tokens = (await import(tokensPath)) as Record<string, unknown>
|
|
|
|
const chainById = new Map<number, Chain>()
|
|
for (const chain of Object.values(Chains)) {
|
|
if (!isChain(chain)) continue
|
|
if (chain.testnet) continue
|
|
if (chainById.has(chain.id)) continue
|
|
chainById.set(chain.id, chain)
|
|
}
|
|
|
|
const tokenLookupData = Object.entries(Tokens)
|
|
.filter((entry): entry is [string, Token] => {
|
|
const [importName, token] = entry
|
|
return importName !== 'defineToken' && isToken(token)
|
|
})
|
|
.map(([importName, token]) => getTokenLookupToken(importName, token))
|
|
.filter((token) => token.chains.length > 0)
|
|
.sort((a, b) => {
|
|
if (a.popular !== b.popular) return a.popular ? -1 : 1
|
|
return a.symbol.localeCompare(b.symbol)
|
|
})
|
|
|
|
const output = new URL('../site/data/tokens.ts', import.meta.url)
|
|
mkdirSync(new URL('.', output), { recursive: true })
|
|
|
|
await Bun.write(
|
|
output,
|
|
`// Generated with \`pnpm gen:token-lookup\`. Do not modify manually.
|
|
|
|
export type TokenLookupChain = {
|
|
readonly address: string
|
|
readonly id: number
|
|
readonly name: string
|
|
}
|
|
|
|
export type TokenLookupToken = {
|
|
readonly chains: readonly TokenLookupChain[]
|
|
readonly currency?: string | undefined
|
|
readonly decimals: number
|
|
readonly importName: string
|
|
readonly name: string
|
|
readonly popular?: boolean | undefined
|
|
readonly symbol: string
|
|
}
|
|
|
|
export const tokenLookupData = ${JSON.stringify(tokenLookupData, null, 2)} as const satisfies readonly TokenLookupToken[]
|
|
`,
|
|
)
|
|
|
|
function getTokenLookupToken(
|
|
importName: string,
|
|
token: Token,
|
|
): TokenLookupToken {
|
|
return {
|
|
chains: getTokenLookupChains(token),
|
|
...(token.currency ? { currency: token.currency } : {}),
|
|
decimals: token.decimals,
|
|
importName,
|
|
name: token.name ?? token.symbol ?? importName,
|
|
...(token.popular ? { popular: token.popular } : {}),
|
|
symbol: token.symbol ?? importName,
|
|
}
|
|
}
|
|
|
|
function getTokenLookupChains(token: Token) {
|
|
return Object.entries(token.addresses)
|
|
.flatMap(([chainId_, address]) => {
|
|
const chainId = Number(chainId_)
|
|
const chain = chainById.get(chainId)
|
|
if (!chain) return []
|
|
return [
|
|
{
|
|
address,
|
|
id: chain.id,
|
|
name: chain.name,
|
|
},
|
|
]
|
|
})
|
|
.sort((a, b) => a.id - b.id)
|
|
}
|
|
|
|
function isToken(value: unknown): value is Token {
|
|
return (
|
|
typeof value === 'function' &&
|
|
'addresses' in value &&
|
|
typeof value.addresses === 'object' &&
|
|
value.addresses !== null &&
|
|
'decimals' in value &&
|
|
typeof value.decimals === 'number'
|
|
)
|
|
}
|
|
|
|
function isChain(value: unknown): value is Chain {
|
|
return (
|
|
typeof value === 'object' &&
|
|
value !== null &&
|
|
'id' in value &&
|
|
Number.isInteger(value.id) &&
|
|
'name' in value &&
|
|
typeof value.name === 'string'
|
|
)
|
|
}
|