@w3lib/evm-batch-call 0.6.0 → 0.8.0
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.
- package/CHANGELOG.md +54 -0
- package/README.md +367 -3
- package/contracts/TokenProbe.sol +103 -0
- package/dist/aggregate.d.ts.map +1 -1
- package/dist/aggregate.js +114 -3
- package/dist/aggregate.js.map +1 -1
- package/dist/approvals.d.ts +169 -0
- package/dist/approvals.d.ts.map +1 -0
- package/dist/approvals.js +310 -0
- package/dist/approvals.js.map +1 -0
- package/dist/chains.d.ts.map +1 -1
- package/dist/chains.js +4 -0
- package/dist/chains.js.map +1 -1
- package/dist/dividends.d.ts +135 -0
- package/dist/dividends.d.ts.map +1 -0
- package/dist/dividends.js +253 -0
- package/dist/dividends.js.map +1 -0
- package/dist/fees.d.ts +97 -0
- package/dist/fees.d.ts.map +1 -0
- package/dist/fees.js +115 -0
- package/dist/fees.js.map +1 -0
- package/dist/history.d.ts +154 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +247 -0
- package/dist/history.js.map +1 -0
- package/dist/historySources.d.ts +32 -0
- package/dist/historySources.d.ts.map +1 -0
- package/dist/historySources.js +319 -0
- package/dist/historySources.js.map +1 -0
- package/dist/http.d.ts +10 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +30 -0
- package/dist/http.js.map +1 -0
- package/dist/liquidity/v3.d.ts +2 -0
- package/dist/liquidity/v3.d.ts.map +1 -1
- package/dist/liquidity/v3.js +1 -1
- package/dist/liquidity/v3.js.map +1 -1
- package/dist/liquidity/v4.d.ts +21 -0
- package/dist/liquidity/v4.d.ts.map +1 -1
- package/dist/liquidity/v4.js +4 -3
- package/dist/liquidity/v4.js.map +1 -1
- package/dist/lp.d.ts +10 -1
- package/dist/lp.d.ts.map +1 -1
- package/dist/lp.js +19 -2
- package/dist/lp.js.map +1 -1
- package/dist/lpV4.d.ts +4 -0
- package/dist/lpV4.d.ts.map +1 -1
- package/dist/lpV4.js +27 -3
- package/dist/lpV4.js.map +1 -1
- package/dist/multicall.d.ts.map +1 -1
- package/dist/multicall.js +3 -0
- package/dist/multicall.js.map +1 -1
- package/dist/names.d.ts +55 -0
- package/dist/names.d.ts.map +1 -0
- package/dist/names.js +93 -0
- package/dist/names.js.map +1 -0
- package/dist/owner.d.ts +4 -26
- package/dist/owner.d.ts.map +1 -1
- package/dist/owner.js +7 -112
- package/dist/owner.js.map +1 -1
- package/dist/permit.d.ts +137 -0
- package/dist/permit.d.ts.map +1 -0
- package/dist/permit.js +355 -0
- package/dist/permit.js.map +1 -0
- package/dist/portfolio.d.ts +56 -0
- package/dist/portfolio.d.ts.map +1 -0
- package/dist/portfolio.js +54 -0
- package/dist/portfolio.js.map +1 -0
- package/dist/price.d.ts +95 -0
- package/dist/price.d.ts.map +1 -0
- package/dist/price.js +329 -0
- package/dist/price.js.map +1 -0
- package/dist/rewards/farms.d.ts +26 -0
- package/dist/rewards/farms.d.ts.map +1 -0
- package/dist/rewards/farms.js +210 -0
- package/dist/rewards/farms.js.map +1 -0
- package/dist/rewards/index.d.ts +42 -0
- package/dist/rewards/index.d.ts.map +1 -0
- package/dist/rewards/index.js +382 -0
- package/dist/rewards/index.js.map +1 -0
- package/dist/rewards/types.d.ts +170 -0
- package/dist/rewards/types.d.ts.map +1 -0
- package/dist/rewards/types.js +2 -0
- package/dist/rewards/types.js.map +1 -0
- package/dist/risk.d.ts +42 -0
- package/dist/risk.d.ts.map +1 -0
- package/dist/risk.js +134 -0
- package/dist/risk.js.map +1 -0
- package/dist/rpcNodes.d.ts.map +1 -1
- package/dist/rpcNodes.js +18 -0
- package/dist/rpcNodes.js.map +1 -1
- package/dist/simulate.d.ts +186 -0
- package/dist/simulate.d.ts.map +1 -0
- package/dist/simulate.js +559 -0
- package/dist/simulate.js.map +1 -0
- package/dist/simulateDecode.d.ts +33 -0
- package/dist/simulateDecode.d.ts.map +1 -0
- package/dist/simulateDecode.js +321 -0
- package/dist/simulateDecode.js.map +1 -0
- package/dist/smartAccount/account.d.ts +31 -0
- package/dist/smartAccount/account.d.ts.map +1 -0
- package/dist/smartAccount/account.js +33 -0
- package/dist/smartAccount/account.js.map +1 -0
- package/dist/smartAccount/clients.d.ts +77 -0
- package/dist/smartAccount/clients.d.ts.map +1 -0
- package/dist/smartAccount/clients.js +122 -0
- package/dist/smartAccount/clients.js.map +1 -0
- package/dist/smartAccount/eip5792.d.ts +77 -0
- package/dist/smartAccount/eip5792.d.ts.map +1 -0
- package/dist/smartAccount/eip5792.js +152 -0
- package/dist/smartAccount/eip5792.js.map +1 -0
- package/dist/smartAccount/send.d.ts +40 -0
- package/dist/smartAccount/send.d.ts.map +1 -0
- package/dist/smartAccount/send.js +271 -0
- package/dist/smartAccount/send.js.map +1 -0
- package/dist/smartAccount/userOp.d.ts +91 -0
- package/dist/smartAccount/userOp.d.ts.map +1 -0
- package/dist/smartAccount/userOp.js +124 -0
- package/dist/smartAccount/userOp.js.map +1 -0
- package/dist/subpaths/approvals.d.ts +26 -0
- package/dist/subpaths/approvals.d.ts.map +1 -0
- package/dist/subpaths/approvals.js +27 -0
- package/dist/subpaths/approvals.js.map +1 -0
- package/dist/subpaths/dividends.d.ts +28 -0
- package/dist/subpaths/dividends.d.ts.map +1 -0
- package/dist/subpaths/dividends.js +29 -0
- package/dist/subpaths/dividends.js.map +1 -0
- package/dist/subpaths/fees.d.ts +23 -0
- package/dist/subpaths/fees.d.ts.map +1 -0
- package/dist/subpaths/fees.js +25 -0
- package/dist/subpaths/fees.js.map +1 -0
- package/dist/subpaths/history.d.ts +22 -0
- package/dist/subpaths/history.d.ts.map +1 -0
- package/dist/subpaths/history.js +23 -0
- package/dist/subpaths/history.js.map +1 -0
- package/dist/subpaths/names.d.ts +35 -0
- package/dist/subpaths/names.d.ts.map +1 -0
- package/dist/subpaths/names.js +39 -0
- package/dist/subpaths/names.js.map +1 -0
- package/dist/subpaths/permit.d.ts +14 -0
- package/dist/subpaths/permit.d.ts.map +1 -0
- package/dist/subpaths/permit.js +14 -0
- package/dist/subpaths/permit.js.map +1 -0
- package/dist/subpaths/price.d.ts +38 -0
- package/dist/subpaths/price.d.ts.map +1 -0
- package/dist/subpaths/price.js +40 -0
- package/dist/subpaths/price.js.map +1 -0
- package/dist/subpaths/rewards.d.ts +27 -0
- package/dist/subpaths/rewards.d.ts.map +1 -0
- package/dist/subpaths/rewards.js +25 -0
- package/dist/subpaths/rewards.js.map +1 -0
- package/dist/subpaths/risk.d.ts +19 -0
- package/dist/subpaths/risk.d.ts.map +1 -0
- package/dist/subpaths/risk.js +20 -0
- package/dist/subpaths/risk.js.map +1 -0
- package/dist/subpaths/simulate.d.ts +25 -0
- package/dist/subpaths/simulate.d.ts.map +1 -0
- package/dist/subpaths/simulate.js +28 -0
- package/dist/subpaths/simulate.js.map +1 -0
- package/dist/subpaths/smart-account.d.ts +24 -0
- package/dist/subpaths/smart-account.d.ts.map +1 -0
- package/dist/subpaths/smart-account.js +24 -0
- package/dist/subpaths/smart-account.js.map +1 -0
- package/dist/subpaths/swap.d.ts +19 -0
- package/dist/subpaths/swap.d.ts.map +1 -0
- package/dist/subpaths/swap.js +20 -0
- package/dist/subpaths/swap.js.map +1 -0
- package/dist/swap.d.ts +168 -0
- package/dist/swap.d.ts.map +1 -0
- package/dist/swap.js +387 -0
- package/dist/swap.js.map +1 -0
- package/dist/tokenProbe.d.ts +3 -0
- package/dist/tokenProbe.d.ts.map +1 -0
- package/dist/tokenProbe.js +4 -0
- package/dist/tokenProbe.js.map +1 -0
- package/dist/write/batch.d.ts +34 -2
- package/dist/write/batch.d.ts.map +1 -1
- package/dist/write/batch.js +22 -3
- package/dist/write/batch.js.map +1 -1
- package/dist/write/collect.d.ts +9 -0
- package/dist/write/collect.d.ts.map +1 -1
- package/dist/write/collect.js +41 -1
- package/dist/write/collect.js.map +1 -1
- package/dist/write/send.d.ts +4 -0
- package/dist/write/send.d.ts.map +1 -1
- package/dist/write/send.js.map +1 -1
- package/dist/write/signer.d.ts +5 -1
- package/dist/write/signer.d.ts.map +1 -1
- package/dist/write/signer.js.map +1 -1
- package/dist/write/transfer.d.ts +5 -3
- package/dist/write/transfer.d.ts.map +1 -1
- package/dist/write/transfer.js +12 -6
- package/dist/write/transfer.js.map +1 -1
- package/dist/write/tron.d.ts +25 -0
- package/dist/write/tron.d.ts.map +1 -1
- package/dist/write/tron.js +26 -0
- package/dist/write/tron.js.map +1 -1
- package/package.json +73 -16
- package/src/aggregate.ts +110 -3
- package/src/approvals.ts +462 -0
- package/src/chains.ts +4 -0
- package/src/dividends.ts +360 -0
- package/src/fees.ts +201 -0
- package/src/history.ts +367 -0
- package/src/historySources.ts +360 -0
- package/src/http.ts +34 -0
- package/src/liquidity/v3.ts +1 -1
- package/src/liquidity/v4.ts +4 -3
- package/src/lp.ts +21 -2
- package/src/lpV4.ts +32 -2
- package/src/multicall.ts +3 -0
- package/src/names.ts +141 -0
- package/src/owner.ts +7 -138
- package/src/permit.ts +457 -0
- package/src/portfolio.ts +98 -0
- package/src/price.ts +415 -0
- package/src/rewards/farms.ts +229 -0
- package/src/rewards/index.ts +431 -0
- package/src/rewards/types.ts +179 -0
- package/src/risk.ts +166 -0
- package/src/rpcNodes.ts +18 -0
- package/src/simulate.ts +727 -0
- package/src/simulateDecode.ts +352 -0
- package/src/smartAccount/account.ts +63 -0
- package/src/smartAccount/clients.ts +180 -0
- package/src/smartAccount/eip5792.ts +187 -0
- package/src/smartAccount/send.ts +341 -0
- package/src/smartAccount/userOp.ts +180 -0
- package/src/subpaths/approvals.ts +48 -0
- package/src/subpaths/dividends.ts +47 -0
- package/src/subpaths/fees.ts +43 -0
- package/src/subpaths/history.ts +41 -0
- package/src/subpaths/names.ts +43 -0
- package/src/subpaths/permit.ts +39 -0
- package/src/subpaths/price.ts +60 -0
- package/src/subpaths/rewards.ts +46 -0
- package/src/subpaths/risk.ts +23 -0
- package/src/subpaths/simulate.ts +51 -0
- package/src/subpaths/smart-account.ts +40 -0
- package/src/subpaths/swap.ts +36 -0
- package/src/swap.ts +534 -0
- package/src/tokenProbe.ts +5 -0
- package/src/write/batch.ts +44 -5
- package/src/write/collect.ts +60 -1
- package/src/write/send.ts +4 -0
- package/src/write/signer.ts +5 -1
- package/src/write/transfer.ts +17 -10
- package/src/write/tron.ts +43 -0
package/src/names.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 域名解析(子路径 `@w3lib/evm-batch-call/names`):ENS(Ethereum)、SPACE ID(BSC 的 .bnb)、Basenames(Base 的 .base.eth),
|
|
3
|
+
* 名字 → 地址、地址 → 名字(反查后再正查核对)。都是 ENS 式的注册表合约,读取合成几次 multicall
|
|
4
|
+
*/
|
|
5
|
+
import { ensNormalize, namehash } from 'ethers'
|
|
6
|
+
|
|
7
|
+
import type { Provider } from './provider.js'
|
|
8
|
+
import { isTronChain } from './tron.js'
|
|
9
|
+
|
|
10
|
+
export interface NameService {
|
|
11
|
+
id: string
|
|
12
|
+
name: string
|
|
13
|
+
chainId: number
|
|
14
|
+
/** ENS 式的注册表合约(resolver(node)) */
|
|
15
|
+
registry: string
|
|
16
|
+
/** 这个服务的名字后缀(如 ['bnb']);null 表示任意名字都交给它(ENS 还支持导入的 DNS 域名) */
|
|
17
|
+
suffixes: readonly string[] | null
|
|
18
|
+
/** 反查用的节点后缀:ENS 是 addr.reverse,L2 上按 ENSIP-19 是 <coinType 十六进制>.reverse */
|
|
19
|
+
reverseSuffix: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 内置的域名服务(2026-10 在链上核对过注册表和正查 / 反查) */
|
|
23
|
+
export const NAME_SERVICES: readonly NameService[] = [
|
|
24
|
+
{ id: 'ens', name: 'ENS', chainId: 1, registry: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', suffixes: null, reverseSuffix: 'addr.reverse' },
|
|
25
|
+
{ id: 'spaceid', name: 'SPACE ID', chainId: 56, registry: '0x08CEd32a7f3eeC915Ba84415e9C07a7286977956', suffixes: ['bnb'], reverseSuffix: 'addr.reverse' },
|
|
26
|
+
{ id: 'basenames', name: 'Basenames', chainId: 8453, registry: '0xb94704422c2a1e396835a571837aa5ae53285a95', suffixes: ['base.eth'], reverseSuffix: '80002105.reverse' },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
export interface NamesOptions {
|
|
30
|
+
/** 用哪些域名服务,默认 NAME_SERVICES 里 Provider 这条链的 */
|
|
31
|
+
services?: readonly NameService[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ResolvedName {
|
|
35
|
+
/** 传入的名字(原样) */
|
|
36
|
+
name: string
|
|
37
|
+
/** 规范化后的名字(ENSIP-15);名字不合法时为 null */
|
|
38
|
+
normalized: string | null
|
|
39
|
+
/** 解析出的地址;没有注册、没有设置地址、名字不合法时为 null */
|
|
40
|
+
address: string | null
|
|
41
|
+
/** 解析这个名字的服务 id;没有服务认这个后缀时为 null */
|
|
42
|
+
service: string | null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface AddressName {
|
|
46
|
+
/** 传入的地址(原样) */
|
|
47
|
+
address: string
|
|
48
|
+
/** 反查到、并且正查回到同一个地址的名字;没有时为 null */
|
|
49
|
+
name: string | null
|
|
50
|
+
service: string | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const REGISTRY_ABI = ['function resolver(bytes32 node) view returns (address)']
|
|
54
|
+
const RESOLVER_ABI = ['function addr(bytes32 node) view returns (address)', 'function name(bytes32 node) view returns (string)']
|
|
55
|
+
const ZERO = '0x0000000000000000000000000000000000000000'
|
|
56
|
+
const present = (a: string | null | undefined): a is string => !!a && a.toLowerCase() !== ZERO
|
|
57
|
+
|
|
58
|
+
function servicesOf(chainId: number, options: NamesOptions): readonly NameService[] {
|
|
59
|
+
return options.services ?? NAME_SERVICES.filter((s) => s.chainId === chainId)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 名字归哪个服务:后缀最长的匹配优先,其次是不限后缀的服务 */
|
|
63
|
+
function serviceFor(name: string, services: readonly NameService[]): NameService | null {
|
|
64
|
+
let best: { s: NameService; len: number } | null = null
|
|
65
|
+
for (const s of services) {
|
|
66
|
+
for (const suffix of s.suffixes ?? []) {
|
|
67
|
+
if ((name === suffix || name.endsWith(`.${suffix}`)) && (!best || suffix.length > best.len)) best = { s, len: suffix.length }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return best?.s ?? services.find((s) => s.suffixes === null) ?? null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function normalize(name: string): string | null {
|
|
74
|
+
try {
|
|
75
|
+
const n = ensNormalize(name.trim())
|
|
76
|
+
return n.includes('.') ? n : null
|
|
77
|
+
} catch {
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 名字 → 地址(读注册表的 resolver,再读 resolver 的 addr,两次 multicall)。
|
|
84
|
+
* 不支持通配解析(ENSIP-10)和链下解析(CCIP-Read,如 cb.id 等子域名),这些名字返回 null
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* await resolveNames(new Provider(1), ['vitalik.eth', 'nick.eth'])
|
|
88
|
+
* // [{ name: 'vitalik.eth', normalized: 'vitalik.eth', address: '0xd8dA…6045', service: 'ens' }, …]
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export async function resolveNames(provider: Provider, names: readonly string[], options: NamesOptions = {}): Promise<ResolvedName[]> {
|
|
92
|
+
const chainId = await provider.getChainId()
|
|
93
|
+
const services = isTronChain(chainId) ? [] : servicesOf(chainId, options)
|
|
94
|
+
const items = names.map((name) => {
|
|
95
|
+
const normalized = normalize(name)
|
|
96
|
+
const service = normalized ? serviceFor(normalized, services) : null
|
|
97
|
+
return { name, normalized, service, node: normalized && service ? namehash(normalized) : null }
|
|
98
|
+
})
|
|
99
|
+
const todo = items.filter((x) => x.node && x.service)
|
|
100
|
+
const resolvers = todo.length ? await provider.tryAll<string>(todo.map((x) => provider.contract((x.service as NameService).registry, REGISTRY_ABI).resolver(x.node as string))) : []
|
|
101
|
+
const withResolver = todo.flatMap((x, i) => (present(resolvers[i]) ? [{ x, resolver: resolvers[i] as string }] : []))
|
|
102
|
+
const addresses = withResolver.length ? await provider.tryAll<string>(withResolver.map(({ x, resolver }) => provider.contract(resolver, RESOLVER_ABI).addr(x.node as string))) : []
|
|
103
|
+
const found = new Map(withResolver.map(({ x }, i) => [x, present(addresses[i]) ? (addresses[i] as string) : null]))
|
|
104
|
+
return items.map((x) => ({ name: x.name, normalized: x.normalized, address: found.get(x) ?? null, service: x.service?.id ?? null }))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 地址 → 名字:读反查记录(主名字),再正查核对名字确实指回这个地址(没有核对的反查记录可以随便设置,不能信)。
|
|
109
|
+
* 共四次 multicall;Tron 上没有域名服务,都返回 null
|
|
110
|
+
*
|
|
111
|
+
* ```ts
|
|
112
|
+
* await lookupAddresses(new Provider(56), [user]) // [{ address: user, name: 'alice.bnb', service: 'spaceid' }]
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export async function lookupAddresses(provider: Provider, addresses: readonly string[], options: NamesOptions = {}): Promise<AddressName[]> {
|
|
116
|
+
const chainId = await provider.getChainId()
|
|
117
|
+
const services = isTronChain(chainId) ? [] : servicesOf(chainId, options)
|
|
118
|
+
const queries = addresses.flatMap((address, i) =>
|
|
119
|
+
/^0x[0-9a-fA-F]{40}$/.test(address) ? services.map((s) => ({ i, s, node: namehash(`${address.slice(2).toLowerCase()}.${s.reverseSuffix}`) })) : [],
|
|
120
|
+
)
|
|
121
|
+
const resolvers = queries.length ? await provider.tryAll<string>(queries.map((q) => provider.contract(q.s.registry, REGISTRY_ABI).resolver(q.node))) : []
|
|
122
|
+
const withResolver = queries.flatMap((q, j) => (present(resolvers[j]) ? [{ q, resolver: resolvers[j] as string }] : []))
|
|
123
|
+
const names = withResolver.length ? await provider.tryAll<string>(withResolver.map(({ q, resolver }) => provider.contract(resolver, RESOLVER_ABI).name(q.node))) : []
|
|
124
|
+
const claimed = withResolver.flatMap(({ q }, j) => (typeof names[j] === 'string' && names[j] ? [{ q, name: names[j] as string }] : []))
|
|
125
|
+
// 正查核对:名字要由同一个服务解析回这个地址
|
|
126
|
+
const forward = await resolveNames(
|
|
127
|
+
provider,
|
|
128
|
+
claimed.map((c) => c.name),
|
|
129
|
+
{ services },
|
|
130
|
+
)
|
|
131
|
+
const result: AddressName[] = addresses.map((address) => ({ address, name: null, service: null }))
|
|
132
|
+
claimed.forEach((c, j) => {
|
|
133
|
+
const f = forward[j]
|
|
134
|
+
const out = result[c.q.i] as AddressName
|
|
135
|
+
if (out.name === null && f?.address && f.service === c.q.s.id && f.address.toLowerCase() === out.address.toLowerCase()) {
|
|
136
|
+
out.name = f.normalized
|
|
137
|
+
out.service = c.q.s.id
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
return result
|
|
141
|
+
}
|
package/src/owner.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { CallOverrides } from './aggregate.js'
|
|
2
2
|
import { NATIVE_TOKEN, formatUnits, type BalanceToken } from './erc20.js'
|
|
3
3
|
import type { Provider } from './provider.js'
|
|
4
|
+
import { HttpStatusError, fetchJson } from './http.js'
|
|
5
|
+
import { defillamaPrices, type PriceSource } from './price.js'
|
|
4
6
|
import { transferScan, type TransferScanOptions } from './scan.js'
|
|
5
|
-
import {
|
|
7
|
+
import { isTronAddress, isTronChain, toTronAddress } from './tron.js'
|
|
6
8
|
|
|
7
9
|
// ---------------------------------------------------------------------------
|
|
8
10
|
// 代币来源:负责“这个地址可能持有哪些代币”。余额和精度一律由本库用 multicall 在链上核对。
|
|
@@ -31,41 +33,10 @@ export interface TokenSource {
|
|
|
31
33
|
discover(ctx: TokenSourceContext): Promise<DiscoveredToken[] | null>
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
const REQUEST_TIMEOUT = 20_000
|
|
35
36
|
const LIST_TTL = 60 * 60 * 1000
|
|
36
37
|
const MAX_CACHED_LISTS = 16
|
|
37
38
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
|
38
39
|
|
|
39
|
-
export class HttpStatusError extends Error {
|
|
40
|
-
constructor(
|
|
41
|
-
readonly status: number,
|
|
42
|
-
target: string,
|
|
43
|
-
) {
|
|
44
|
-
super(`HTTP ${status} from ${target}`)
|
|
45
|
-
this.name = 'HttpStatusError'
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @param secret 请求里带的 Key:出错时错误信息只写域名,不写完整地址,避免 Key 出现在日志里
|
|
51
|
-
*/
|
|
52
|
-
function fetchJson<T>(fetchFn: typeof fetch, url: string, init?: RequestInit, secret?: string): Promise<T> {
|
|
53
|
-
return fetchFn(url, { ...init, signal: AbortSignal.timeout(REQUEST_TIMEOUT) }).then(async (res) => {
|
|
54
|
-
if (!res.ok) {
|
|
55
|
-
throw new HttpStatusError(res.status, secret ? safeHost(url) : url)
|
|
56
|
-
}
|
|
57
|
-
return (await res.json()) as T
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function safeHost(url: string): string {
|
|
62
|
-
try {
|
|
63
|
-
return new URL(url).host
|
|
64
|
-
} catch {
|
|
65
|
-
return 'request'
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
40
|
// 公开代币列表:缓存“按链过滤、转换后”的结果(不缓存原始 JSON),1 小时;缓存 Promise,同时发起的请求共用,失败不缓存。
|
|
70
41
|
// 公开列表与使用哪个 fetch 无关,所以缓存键只看列表和链
|
|
71
42
|
const listCache = new Map<string, { at: number; promise: Promise<DiscoveredToken[] | null> }>()
|
|
@@ -410,111 +381,9 @@ function dedupe<T extends DiscoveredToken>(tokens: T[]): T[] {
|
|
|
410
381
|
return [...seen.values()]
|
|
411
382
|
}
|
|
412
383
|
|
|
413
|
-
//
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
export interface PriceSourceContext {
|
|
418
|
-
chainId: number
|
|
419
|
-
/** 代币地址(不含主币) */
|
|
420
|
-
tokens: readonly string[]
|
|
421
|
-
/** 主币 symbol(需要主币价格时);不需要时为 null */
|
|
422
|
-
nativeSymbol: string | null
|
|
423
|
-
fetch: typeof fetch
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
export interface PriceSource {
|
|
427
|
-
readonly name: string
|
|
428
|
-
/** 返回 去重键 → 美元价格:EVM 是小写 0x 地址;Tron 是 T 开头的 base58 地址(区分大小写);主币用键 'native'。没有价格的不返回 */
|
|
429
|
-
prices(ctx: PriceSourceContext): Promise<Map<string, number>>
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/** DefiLlama 的链标识:chainId → slug(2026-10 实测可用) */
|
|
433
|
-
export const DEFILLAMA_CHAINS: Readonly<Record<number, string>> = {
|
|
434
|
-
1: 'ethereum',
|
|
435
|
-
10: 'optimism',
|
|
436
|
-
56: 'bsc',
|
|
437
|
-
130: 'unichain',
|
|
438
|
-
137: 'polygon',
|
|
439
|
-
143: 'monad',
|
|
440
|
-
196: 'xlayer',
|
|
441
|
-
204: 'op_bnb',
|
|
442
|
-
324: 'era',
|
|
443
|
-
999: 'hyperliquid',
|
|
444
|
-
1116: 'core',
|
|
445
|
-
2818: 'morph',
|
|
446
|
-
5000: 'mantle',
|
|
447
|
-
8453: 'base',
|
|
448
|
-
9745: 'plasma',
|
|
449
|
-
34443: 'mode',
|
|
450
|
-
42161: 'arbitrum',
|
|
451
|
-
43114: 'avax',
|
|
452
|
-
57073: 'ink',
|
|
453
|
-
59144: 'linea',
|
|
454
|
-
80094: 'berachain',
|
|
455
|
-
81457: 'blast',
|
|
456
|
-
534352: 'scroll',
|
|
457
|
-
[TRON_CHAIN_ID.mainnet]: 'tron',
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/** 主币价格:按主币 symbol 对应 CoinGecko 的币种 id(DefiLlama 用 coingecko:<id> 查) */
|
|
461
|
-
const NATIVE_PRICE_IDS: Readonly<Record<string, string>> = {
|
|
462
|
-
ETH: 'ethereum',
|
|
463
|
-
BNB: 'binancecoin',
|
|
464
|
-
POL: 'polygon-ecosystem-token',
|
|
465
|
-
AVAX: 'avalanche-2',
|
|
466
|
-
MNT: 'mantle',
|
|
467
|
-
OKB: 'okb',
|
|
468
|
-
HYPE: 'hyperliquid',
|
|
469
|
-
BERA: 'berachain-bera',
|
|
470
|
-
CORE: 'coredaoorg',
|
|
471
|
-
MON: 'monad',
|
|
472
|
-
XPL: 'plasma',
|
|
473
|
-
TRX: 'tron',
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* DefiLlama 价格(免费、不需要 Key)。minConfidence:DefiLlama 给出的置信度下限(0~1),低于它视为没有价格,默认 0.9。
|
|
478
|
-
* 注意:流动性很差的代币价格可能虚高,展示总资产时建议配合 minUsd 或自己的价格源。
|
|
479
|
-
*/
|
|
480
|
-
export function defillamaPrices(options: { minConfidence?: number } = {}): PriceSource {
|
|
481
|
-
const minConfidence = options.minConfidence ?? 0.9
|
|
482
|
-
return {
|
|
483
|
-
name: 'defillama',
|
|
484
|
-
async prices({ chainId, tokens, nativeSymbol, fetch: fetchFn }) {
|
|
485
|
-
const result = new Map<string, number>()
|
|
486
|
-
const slug = DEFILLAMA_CHAINS[chainId]
|
|
487
|
-
const nativeId = nativeSymbol ? NATIVE_PRICE_IDS[nativeSymbol.toUpperCase()] : undefined
|
|
488
|
-
// 查询键 → 结果里用的去重键:EVM 地址小写;Tron 地址区分大小写,DefiLlama 要用原样的 base58(T…)地址
|
|
489
|
-
const resultKey = new Map<string, string>()
|
|
490
|
-
const tokenKeys = slug
|
|
491
|
-
? tokens.map((t) => {
|
|
492
|
-
const tron = slug === 'tron'
|
|
493
|
-
const query = `${slug}:${tron ? toTronAddress(t) : t.toLowerCase()}`
|
|
494
|
-
resultKey.set(query.toLowerCase(), tron ? toTronAddress(t) : t.toLowerCase())
|
|
495
|
-
return query
|
|
496
|
-
})
|
|
497
|
-
: []
|
|
498
|
-
const keys = [...(nativeId ? [`coingecko:${nativeId}`] : []), ...tokenKeys]
|
|
499
|
-
const batches: string[][] = []
|
|
500
|
-
for (let i = 0; i < keys.length; i += 100) {
|
|
501
|
-
batches.push(keys.slice(i, i + 100))
|
|
502
|
-
}
|
|
503
|
-
const responses = await Promise.all(
|
|
504
|
-
batches.map((batch) => fetchJson<{ coins?: Record<string, { price?: number; confidence?: number }> }>(fetchFn, `https://coins.llama.fi/prices/current/${batch.join(',')}`)),
|
|
505
|
-
)
|
|
506
|
-
for (const res of responses) {
|
|
507
|
-
for (const [key, coin] of Object.entries(res.coins ?? {})) {
|
|
508
|
-
if (typeof coin.price !== 'number' || !Number.isFinite(coin.price) || (coin.confidence !== undefined && coin.confidence < minConfidence)) {
|
|
509
|
-
continue
|
|
510
|
-
}
|
|
511
|
-
result.set(key.startsWith('coingecko:') ? 'native' : (resultKey.get(key.toLowerCase()) ?? key.slice(key.indexOf(':') + 1).toLowerCase()), coin.price)
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return result
|
|
515
|
-
},
|
|
516
|
-
}
|
|
517
|
-
}
|
|
384
|
+
// 价格来源在 price.ts(子路径 /price);这里继续导出,旧的 import 不受影响
|
|
385
|
+
export { DEFILLAMA_CHAINS, defillamaPrices, type PriceSource, type PriceSourceContext } from './price.js'
|
|
386
|
+
export { HttpStatusError }
|
|
518
387
|
|
|
519
388
|
// ---------------------------------------------------------------------------
|
|
520
389
|
// ownerTokens
|
|
@@ -617,7 +486,7 @@ export async function ownerTokens(provider: Provider, owner: string, options: Ow
|
|
|
617
486
|
// 价格源的键:Tron 上统一用 T 地址(代币来源给的可能是 0x 格式)
|
|
618
487
|
const priceKey = (address: string) => (isTronChain(chainId) ? toTronAddress(address) : addressKey(address))
|
|
619
488
|
const priceMap = priceSource
|
|
620
|
-
? await priceSource.prices({ chainId, tokens: held.map((x) => (isTronChain(chainId) ? toTronAddress(x.meta.address) : x.meta.address)), nativeSymbol: nativeAddress ? (nativeInfo?.symbol ?? null) : null, fetch: fetchFn })
|
|
489
|
+
? await priceSource.prices({ chainId, tokens: held.map((x) => (isTronChain(chainId) ? toTronAddress(x.meta.address) : x.meta.address)), nativeSymbol: nativeAddress ? (nativeInfo?.symbol ?? null) : null, fetch: fetchFn, provider })
|
|
621
490
|
: new Map<string, number>()
|
|
622
491
|
const valueOf = (formatted: string, price: number | undefined) => (price === undefined ? null : Number(formatted) * price)
|
|
623
492
|
|