@1001-digital/layers.evm 1.0.7 → 1.0.9
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/.claude/settings.local.json +1 -3
- package/.playground/app/app.vue +1 -0
- package/.playground/app/pages/index.vue +1 -2
- package/.playground/nuxt.config.ts +3 -1
- package/AGENTS.md +2 -2
- package/README.md +1 -1
- package/app/composables/base.ts +1 -5
- package/app/composables/chainId.ts +6 -42
- package/app/composables/ens.ts +1 -88
- package/app/composables/gasPrice.ts +1 -36
- package/app/composables/priceFeed.ts +1 -103
- package/app/plugins/wagmi.ts +40 -3
- package/app/utils/addresses.ts +1 -4
- package/app/utils/cache.ts +1 -59
- package/app/utils/chains.ts +1 -13
- package/app/utils/ens.ts +2 -98
- package/app/utils/format-eth.ts +1 -12
- package/app/utils/price.ts +1 -15
- package/nuxt.config.ts +33 -0
- package/package.json +5 -6
- package/app/components/EvmAccount.client.vue +0 -26
- package/app/components/EvmConnect.client.vue +0 -238
- package/app/components/EvmConnectorQR.client.vue +0 -108
- package/app/components/EvmMetaMaskQR.client.vue +0 -13
- package/app/components/EvmTransactionFlow.vue +0 -293
- package/app/components/EvmWalletConnectQR.client.vue +0 -13
- package/app/composables/clipboard.ts +0 -26
- package/app/composables/helpers.ts +0 -1
package/.playground/app/app.vue
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url'
|
|
2
2
|
|
|
3
3
|
const layerDir = fileURLToPath(new URL('..', import.meta.url))
|
|
4
|
+
const componentsDir = fileURLToPath(new URL('../../components/src', import.meta.url))
|
|
4
5
|
|
|
5
6
|
export default defineNuxtConfig({
|
|
6
7
|
extends: ['@1001-digital/layers.base', '..'],
|
|
@@ -9,11 +10,12 @@ export default defineNuxtConfig({
|
|
|
9
10
|
config: {
|
|
10
11
|
// Use the generated ESLint config for lint root project as well
|
|
11
12
|
rootDir: layerDir,
|
|
12
|
-
}
|
|
13
|
+
},
|
|
13
14
|
},
|
|
14
15
|
hooks: {
|
|
15
16
|
'vite:serverCreated': (server) => {
|
|
16
17
|
server.watcher.add(layerDir)
|
|
18
|
+
server.watcher.add(componentsDir)
|
|
17
19
|
},
|
|
18
20
|
},
|
|
19
21
|
})
|
package/AGENTS.md
CHANGED
|
@@ -18,6 +18,7 @@ Nuxt layer for building dAPPs (Ethereum-powered applications). Extends `@1001-di
|
|
|
18
18
|
## Wagmi Configuration
|
|
19
19
|
|
|
20
20
|
Uses modern wagmi 0.4.x patterns:
|
|
21
|
+
|
|
21
22
|
- `useConnection` (not deprecated `useAccount`)
|
|
22
23
|
- `useConnectionEffect` (not deprecated `useAccountEffect`)
|
|
23
24
|
- `useSwitchConnection` (not deprecated `useSwitchAccount`)
|
|
@@ -29,7 +30,7 @@ Connectors: injected, coinbaseWallet, metaMask, walletConnect
|
|
|
29
30
|
## Components
|
|
30
31
|
|
|
31
32
|
- `EvmConnect.client.vue` - Wallet connection button with modal
|
|
32
|
-
- `EvmAccount.client.vue` - Address display
|
|
33
|
+
- `EvmAccount.client.vue` - Address display
|
|
33
34
|
- `EvmTransactionFlow.vue` - Guided transaction execution flow
|
|
34
35
|
- `EvmConnectorQR.client.vue` - Base QR code renderer
|
|
35
36
|
- `EvmWalletConnectQR.client.vue` - WalletConnect QR wrapper
|
|
@@ -42,7 +43,6 @@ Connectors: injected, coinbaseWallet, metaMask, walletConnect
|
|
|
42
43
|
- `useBlockExplorer(key?)` - Get block explorer URL for a named chain
|
|
43
44
|
- `useEnsureChainIdCheck()` - Validate/switch chain before transactions
|
|
44
45
|
- `useBaseURL()` - Get base URL with trailing slash
|
|
45
|
-
- `useClipboard()` - Copy text to clipboard with copied state
|
|
46
46
|
|
|
47
47
|
## Utilities
|
|
48
48
|
|
package/README.md
CHANGED
package/app/composables/base.ts
CHANGED
|
@@ -1,42 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const getDefaultChainKey = () => useAppConfig().evm?.defaultChain || 'mainnet'
|
|
9
|
-
|
|
10
|
-
export const useChainConfig = (key?: string) => {
|
|
11
|
-
const appConfig = useAppConfig()
|
|
12
|
-
const resolvedKey = key || getDefaultChainKey()
|
|
13
|
-
const chains = appConfig.evm?.chains as Record<string, ChainConfig> | undefined
|
|
14
|
-
const chain = chains?.[resolvedKey]
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
id: chain?.id ?? 1,
|
|
18
|
-
blockExplorer: chain?.blockExplorer ?? 'https://etherscan.io',
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const useMainChainId = () => useChainConfig().id
|
|
23
|
-
|
|
24
|
-
export const useBlockExplorer = (key?: string) => useChainConfig(key).blockExplorer
|
|
25
|
-
|
|
26
|
-
export const useEnsureChainIdCheck = () => {
|
|
27
|
-
const chainId = useMainChainId()
|
|
28
|
-
const { switchChain } = useSwitchChain()
|
|
29
|
-
const { chainId: currentChainId } = useConnection()
|
|
30
|
-
|
|
31
|
-
return async () => {
|
|
32
|
-
if (chainId !== currentChainId.value) {
|
|
33
|
-
switchChain({ chainId })
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (chainId === currentChainId.value) {
|
|
37
|
-
return true
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return false
|
|
41
|
-
}
|
|
42
|
-
}
|
|
1
|
+
export {
|
|
2
|
+
useChainConfig,
|
|
3
|
+
useMainChainId,
|
|
4
|
+
useBlockExplorer,
|
|
5
|
+
useEnsureChainIdCheck,
|
|
6
|
+
} from '@1001-digital/components'
|
package/app/composables/ens.ts
CHANGED
|
@@ -1,88 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Config } from '@wagmi/vue'
|
|
3
|
-
|
|
4
|
-
type EnsMode = 'indexer' | 'chain'
|
|
5
|
-
|
|
6
|
-
interface UseEnsOptions {
|
|
7
|
-
mode?: MaybeRefOrGetter<EnsMode | undefined>
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface EnsRuntimeConfig {
|
|
11
|
-
ens?: { indexer1?: string, indexer2?: string, indexer3?: string }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function getIndexerUrls(config: EnsRuntimeConfig): string[] {
|
|
15
|
-
if (!config.ens) return []
|
|
16
|
-
return [config.ens.indexer1, config.ens.indexer2, config.ens.indexer3].filter(Boolean) as string[]
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function resolve(
|
|
20
|
-
identifier: string,
|
|
21
|
-
strategies: EnsMode[],
|
|
22
|
-
indexerUrls: string[],
|
|
23
|
-
wagmi: Config,
|
|
24
|
-
chainKeys: string[],
|
|
25
|
-
): Promise<EnsProfile> {
|
|
26
|
-
for (const strategy of strategies) {
|
|
27
|
-
try {
|
|
28
|
-
if (strategy === 'indexer') {
|
|
29
|
-
if (!indexerUrls.length) continue
|
|
30
|
-
return await fetchEnsFromIndexer(identifier, indexerUrls)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (strategy === 'chain') {
|
|
34
|
-
const client = getPublicClient(wagmi, { chainId: 1 })
|
|
35
|
-
if (!client) continue
|
|
36
|
-
return await fetchEnsFromChain(identifier, client, chainKeys)
|
|
37
|
-
}
|
|
38
|
-
} catch {
|
|
39
|
-
continue
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return { address: identifier, ens: null, data: null }
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function useEnsBase(
|
|
47
|
-
tier: string,
|
|
48
|
-
identifier: MaybeRefOrGetter<string | undefined>,
|
|
49
|
-
chainKeys: string[],
|
|
50
|
-
options: UseEnsOptions = {},
|
|
51
|
-
) {
|
|
52
|
-
const { $wagmi } = useNuxtApp()
|
|
53
|
-
const appConfig = useAppConfig()
|
|
54
|
-
const runtimeConfig = useRuntimeConfig()
|
|
55
|
-
|
|
56
|
-
const mode = computed<EnsMode>(() => toValue(options.mode) || appConfig.evm?.ens?.mode || 'indexer')
|
|
57
|
-
const indexerUrls = computed(() => getIndexerUrls(runtimeConfig.public.evm as EnsRuntimeConfig))
|
|
58
|
-
const cacheKey = computed(() => `ens-${tier}-${toValue(identifier)}`)
|
|
59
|
-
|
|
60
|
-
return useAsyncData(
|
|
61
|
-
cacheKey.value,
|
|
62
|
-
async () => {
|
|
63
|
-
const id = toValue(identifier)
|
|
64
|
-
if (!id) return null
|
|
65
|
-
|
|
66
|
-
const strategies: EnsMode[] = mode.value === 'indexer'
|
|
67
|
-
? ['indexer', 'chain']
|
|
68
|
-
: ['chain', 'indexer']
|
|
69
|
-
|
|
70
|
-
return ensCache.fetch(cacheKey.value, () =>
|
|
71
|
-
resolve(id, strategies, indexerUrls.value, $wagmi as Config, chainKeys),
|
|
72
|
-
)
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
watch: [() => toValue(identifier)],
|
|
76
|
-
getCachedData: () => ensCache.get(cacheKey.value) ?? undefined,
|
|
77
|
-
},
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export const useEns = (identifier: MaybeRefOrGetter<string | undefined>, options?: UseEnsOptions) =>
|
|
82
|
-
useEnsBase('resolve', identifier, [], options)
|
|
83
|
-
|
|
84
|
-
export const useEnsWithAvatar = (identifier: MaybeRefOrGetter<string | undefined>, options?: UseEnsOptions) =>
|
|
85
|
-
useEnsBase('avatar', identifier, [...ENS_KEYS_AVATAR], options)
|
|
86
|
-
|
|
87
|
-
export const useEnsProfile = (identifier: MaybeRefOrGetter<string | undefined>, options?: UseEnsOptions) =>
|
|
88
|
-
useEnsBase('profile', identifier, [...ENS_KEYS_PROFILE], options)
|
|
1
|
+
export { useEns, useEnsWithAvatar, useEnsProfile } from '@1001-digital/components'
|
|
@@ -1,36 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { formatEther, formatGwei } from 'viem'
|
|
3
|
-
import { getGasPrice } from '@wagmi/core'
|
|
4
|
-
import { useConfig, useBlockNumber } from '@wagmi/vue'
|
|
5
|
-
|
|
6
|
-
let priceWatcher: WatchStopHandle | null = null
|
|
7
|
-
const price: Ref<bigint> = ref(0n)
|
|
8
|
-
|
|
9
|
-
export const useGasPrice = () => {
|
|
10
|
-
const config = useConfig()
|
|
11
|
-
const { data: blockNumber } = useBlockNumber()
|
|
12
|
-
|
|
13
|
-
const updatePrice = async () => {
|
|
14
|
-
price.value = await getGasPrice(config)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (!priceWatcher) {
|
|
18
|
-
updatePrice()
|
|
19
|
-
priceWatcher = watch(blockNumber, () => updatePrice())
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const unitPrice = computed(() => ({
|
|
23
|
-
wei: price.value,
|
|
24
|
-
gwei: formatGwei(price.value),
|
|
25
|
-
eth: formatEther(price.value),
|
|
26
|
-
|
|
27
|
-
formatted: {
|
|
28
|
-
gwei: price.value > 2_000_000_000_000n
|
|
29
|
-
? Math.round(parseFloat(formatGwei(price.value)))
|
|
30
|
-
: parseFloat(formatGwei(price.value)).toFixed(1),
|
|
31
|
-
eth: formatEther(price.value),
|
|
32
|
-
},
|
|
33
|
-
}))
|
|
34
|
-
|
|
35
|
-
return unitPrice
|
|
36
|
-
}
|
|
1
|
+
export { useGasPrice } from '@1001-digital/components'
|
|
@@ -1,103 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const CHAINLINK_ETH_USD_ABI = [
|
|
4
|
-
{
|
|
5
|
-
inputs: [],
|
|
6
|
-
name: 'latestRoundData',
|
|
7
|
-
outputs: [
|
|
8
|
-
{ internalType: 'uint80', name: 'roundId', type: 'uint80' },
|
|
9
|
-
{ internalType: 'int256', name: 'answer', type: 'int256' },
|
|
10
|
-
{ internalType: 'uint256', name: 'startedAt', type: 'uint256' },
|
|
11
|
-
{ internalType: 'uint256', name: 'updatedAt', type: 'uint256' },
|
|
12
|
-
{ internalType: 'uint80', name: 'answeredInRound', type: 'uint80' },
|
|
13
|
-
],
|
|
14
|
-
stateMutability: 'view',
|
|
15
|
-
type: 'function',
|
|
16
|
-
},
|
|
17
|
-
] as const
|
|
18
|
-
|
|
19
|
-
const CHAINLINK_ETH_USD = '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419'
|
|
20
|
-
const STORAGE_KEY = 'evm:price-feed'
|
|
21
|
-
const CACHE_TTL = 3_600 // 1 hour in seconds
|
|
22
|
-
|
|
23
|
-
interface PriceFeedState {
|
|
24
|
-
ethUSDRaw: bigint | null
|
|
25
|
-
lastUpdated: number
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const state = reactive<PriceFeedState>({
|
|
29
|
-
ethUSDRaw: null,
|
|
30
|
-
lastUpdated: 0,
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
function loadFromStorage() {
|
|
34
|
-
if (!import.meta.client) return
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
const stored = localStorage.getItem(STORAGE_KEY)
|
|
38
|
-
if (!stored) return
|
|
39
|
-
|
|
40
|
-
const parsed = parseJSON(stored) as PriceFeedState
|
|
41
|
-
if (parsed.ethUSDRaw) state.ethUSDRaw = parsed.ethUSDRaw
|
|
42
|
-
if (parsed.lastUpdated) state.lastUpdated = parsed.lastUpdated
|
|
43
|
-
} catch {
|
|
44
|
-
// Ignore corrupted storage
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function saveToStorage() {
|
|
49
|
-
if (!import.meta.client) return
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
localStorage.setItem(STORAGE_KEY, stringifyJSON({
|
|
53
|
-
ethUSDRaw: state.ethUSDRaw,
|
|
54
|
-
lastUpdated: state.lastUpdated,
|
|
55
|
-
}))
|
|
56
|
-
} catch {
|
|
57
|
-
// Ignore storage errors
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const usePriceFeed = () => {
|
|
62
|
-
const { $wagmi } = useNuxtApp()
|
|
63
|
-
|
|
64
|
-
// Load cached data on first use
|
|
65
|
-
if (!state.lastUpdated) loadFromStorage()
|
|
66
|
-
|
|
67
|
-
const ethUSD = computed(() => state.ethUSDRaw ? state.ethUSDRaw / BigInt(1e8) : 0n)
|
|
68
|
-
const ethUSC = computed(() => state.ethUSDRaw ? state.ethUSDRaw / BigInt(1e6) : 0n)
|
|
69
|
-
const ethUSDFormatted = computed(() => formatPrice(Number(ethUSC.value) / 100, 2))
|
|
70
|
-
|
|
71
|
-
const weiToUSD = (wei: bigint) => {
|
|
72
|
-
const cents = (wei * (state.ethUSDRaw || 0n)) / (10n ** 18n) / (10n ** 6n)
|
|
73
|
-
return formatPrice(Number(cents) / 100, 2)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async function fetchPrice() {
|
|
77
|
-
if (nowInSeconds() - state.lastUpdated < CACHE_TTL) return
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
const [, answer] = await readContract($wagmi, {
|
|
81
|
-
address: CHAINLINK_ETH_USD,
|
|
82
|
-
abi: CHAINLINK_ETH_USD_ABI,
|
|
83
|
-
functionName: 'latestRoundData',
|
|
84
|
-
chainId: 1,
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
state.ethUSDRaw = answer
|
|
88
|
-
state.lastUpdated = nowInSeconds()
|
|
89
|
-
saveToStorage()
|
|
90
|
-
} catch (error) {
|
|
91
|
-
console.warn('Error fetching ETH/USD price:', error)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
ethUSDRaw: computed(() => state.ethUSDRaw),
|
|
97
|
-
ethUSD,
|
|
98
|
-
ethUSC,
|
|
99
|
-
ethUSDFormatted,
|
|
100
|
-
weiToUSD,
|
|
101
|
-
fetchPrice,
|
|
102
|
-
}
|
|
103
|
-
}
|
|
1
|
+
export { usePriceFeed } from '@1001-digital/components'
|
package/app/plugins/wagmi.ts
CHANGED
|
@@ -9,14 +9,25 @@ import {
|
|
|
9
9
|
type Config,
|
|
10
10
|
type CreateConnectorFn,
|
|
11
11
|
} from '@wagmi/vue'
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
coinbaseWallet,
|
|
14
|
+
injected,
|
|
15
|
+
metaMask,
|
|
16
|
+
walletConnect,
|
|
17
|
+
} from '@wagmi/vue/connectors'
|
|
13
18
|
import type { Chain, Transport } from 'viem'
|
|
19
|
+
import {
|
|
20
|
+
EvmConfigKey,
|
|
21
|
+
resolveChain,
|
|
22
|
+
type EvmConfig,
|
|
23
|
+
} from '@1001-digital/components'
|
|
14
24
|
|
|
15
25
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
16
26
|
const appConfig = useAppConfig()
|
|
17
27
|
const runtimeConfig = nuxtApp.$config.public.evm as {
|
|
18
28
|
walletConnectProjectId: string
|
|
19
|
-
chains: Record<string, { rpc1?: string
|
|
29
|
+
chains: Record<string, { rpc1?: string; rpc2?: string; rpc3?: string }>
|
|
30
|
+
ens: { indexer1?: string; indexer2?: string; indexer3?: string }
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
const title = appConfig.evm?.title || 'EVM Layer'
|
|
@@ -78,7 +89,33 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
78
89
|
transports,
|
|
79
90
|
})
|
|
80
91
|
|
|
81
|
-
|
|
92
|
+
// Build EvmConfig from Nuxt app/runtime config
|
|
93
|
+
const indexerUrls = [
|
|
94
|
+
runtimeConfig.ens?.indexer1,
|
|
95
|
+
runtimeConfig.ens?.indexer2,
|
|
96
|
+
runtimeConfig.ens?.indexer3,
|
|
97
|
+
].filter(Boolean) as string[]
|
|
98
|
+
|
|
99
|
+
const evmConfig: EvmConfig = {
|
|
100
|
+
title,
|
|
101
|
+
defaultChain: appConfig.evm?.defaultChain || 'mainnet',
|
|
102
|
+
chains: Object.fromEntries(
|
|
103
|
+
Object.entries(chainEntries).map(([key, entry]) => [
|
|
104
|
+
key,
|
|
105
|
+
{ id: entry.id!, blockExplorer: entry.blockExplorer },
|
|
106
|
+
]),
|
|
107
|
+
),
|
|
108
|
+
ens: {
|
|
109
|
+
mode: appConfig.evm?.ens?.mode || 'indexer',
|
|
110
|
+
indexerUrls,
|
|
111
|
+
},
|
|
112
|
+
baseURL: nuxtApp.$config.app.baseURL,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
nuxtApp.vueApp
|
|
116
|
+
.use(WagmiPlugin, { config: wagmiConfig })
|
|
117
|
+
.use(VueQueryPlugin, {})
|
|
118
|
+
.provide(EvmConfigKey, evmConfig)
|
|
82
119
|
|
|
83
120
|
return {
|
|
84
121
|
provide: {
|
package/app/utils/addresses.ts
CHANGED
package/app/utils/cache.ts
CHANGED
|
@@ -1,59 +1 @@
|
|
|
1
|
-
|
|
2
|
-
data: T
|
|
3
|
-
expiresAt: number
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export function createCache<T>(ttl: number, max: number) {
|
|
7
|
-
const entries = new Map<string, CacheEntry<T>>()
|
|
8
|
-
const pending = new Map<string, Promise<T>>()
|
|
9
|
-
|
|
10
|
-
function prune() {
|
|
11
|
-
const now = Date.now()
|
|
12
|
-
for (const [key, entry] of entries) {
|
|
13
|
-
if (entry.expiresAt <= now) entries.delete(key)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (entries.size > max) {
|
|
17
|
-
const excess = entries.size - max
|
|
18
|
-
const keys = entries.keys()
|
|
19
|
-
for (let i = 0; i < excess; i++) {
|
|
20
|
-
entries.delete(keys.next().value!)
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function get(key: string): T | undefined {
|
|
26
|
-
const entry = entries.get(key)
|
|
27
|
-
if (!entry) return undefined
|
|
28
|
-
if (entry.expiresAt <= Date.now()) {
|
|
29
|
-
entries.delete(key)
|
|
30
|
-
return undefined
|
|
31
|
-
}
|
|
32
|
-
return entry.data
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function fetch(key: string, fn: () => Promise<T>): Promise<T> {
|
|
36
|
-
const cached = get(key)
|
|
37
|
-
if (cached) return Promise.resolve(cached)
|
|
38
|
-
|
|
39
|
-
const inflight = pending.get(key)
|
|
40
|
-
if (inflight) return inflight
|
|
41
|
-
|
|
42
|
-
const promise = fn()
|
|
43
|
-
.then((result) => {
|
|
44
|
-
entries.set(key, { data: result, expiresAt: Date.now() + ttl })
|
|
45
|
-
pending.delete(key)
|
|
46
|
-
if (entries.size > max) prune()
|
|
47
|
-
return result
|
|
48
|
-
})
|
|
49
|
-
.catch((err) => {
|
|
50
|
-
pending.delete(key)
|
|
51
|
-
throw err
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
pending.set(key, promise)
|
|
55
|
-
return promise
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return { get, fetch }
|
|
59
|
-
}
|
|
1
|
+
export { createCache } from '@1001-digital/components'
|
package/app/utils/chains.ts
CHANGED
|
@@ -1,13 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { mainnet, sepolia, holesky, optimism, arbitrum, base, polygon, localhost } from 'viem/chains'
|
|
3
|
-
|
|
4
|
-
const KNOWN: Chain[] = [mainnet, sepolia, holesky, optimism, arbitrum, base, polygon, localhost]
|
|
5
|
-
const byId = new Map<number, Chain>(KNOWN.map(c => [c.id, c]))
|
|
6
|
-
|
|
7
|
-
export const resolveChain = (id: number): Chain =>
|
|
8
|
-
byId.get(id) ?? defineChain({
|
|
9
|
-
id,
|
|
10
|
-
name: `Chain ${id}`,
|
|
11
|
-
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
|
|
12
|
-
rpcUrls: { default: { http: [] } },
|
|
13
|
-
})
|
|
1
|
+
export { resolveChain } from '@1001-digital/components'
|
package/app/utils/ens.ts
CHANGED
|
@@ -1,98 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// --- Types ---
|
|
5
|
-
|
|
6
|
-
export interface EnsProfile {
|
|
7
|
-
address: string
|
|
8
|
-
ens: string | null
|
|
9
|
-
data: {
|
|
10
|
-
avatar: string
|
|
11
|
-
header: string
|
|
12
|
-
description: string
|
|
13
|
-
links: {
|
|
14
|
-
url: string
|
|
15
|
-
email: string
|
|
16
|
-
twitter: string
|
|
17
|
-
github: string
|
|
18
|
-
}
|
|
19
|
-
} | null
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// --- Text record keys ---
|
|
23
|
-
|
|
24
|
-
const ALL_KEYS = ['avatar', 'header', 'description', 'url', 'email', 'com.twitter', 'com.github'] as const
|
|
25
|
-
|
|
26
|
-
export const ENS_KEYS_AVATAR = ['avatar'] as const
|
|
27
|
-
export const ENS_KEYS_PROFILE = [...ALL_KEYS]
|
|
28
|
-
|
|
29
|
-
// --- Cache ---
|
|
30
|
-
|
|
31
|
-
export const ensCache = createCache<EnsProfile>(5 * 60 * 1000, 500)
|
|
32
|
-
|
|
33
|
-
// --- Fetchers ---
|
|
34
|
-
|
|
35
|
-
export async function fetchEnsFromIndexer(
|
|
36
|
-
identifier: string,
|
|
37
|
-
urls: string[],
|
|
38
|
-
): Promise<EnsProfile> {
|
|
39
|
-
let lastError: Error | undefined
|
|
40
|
-
|
|
41
|
-
for (const url of urls) {
|
|
42
|
-
try {
|
|
43
|
-
return await $fetch<EnsProfile>(`${url}/${identifier}`)
|
|
44
|
-
} catch (err) {
|
|
45
|
-
lastError = err as Error
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
throw lastError ?? new Error('No indexer URLs provided')
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function fetchEnsFromChain(
|
|
53
|
-
identifier: string,
|
|
54
|
-
client: PublicClient,
|
|
55
|
-
keys: string[] = [],
|
|
56
|
-
): Promise<EnsProfile> {
|
|
57
|
-
const isAddr = isAddress(identifier)
|
|
58
|
-
|
|
59
|
-
let address: string
|
|
60
|
-
let ens: string | null
|
|
61
|
-
|
|
62
|
-
if (isAddr) {
|
|
63
|
-
address = identifier
|
|
64
|
-
ens = await client.getEnsName({ address: identifier as Address }) ?? null
|
|
65
|
-
} else {
|
|
66
|
-
ens = identifier
|
|
67
|
-
const resolved = await client.getEnsAddress({ name: normalize(identifier) })
|
|
68
|
-
if (!resolved) return { address: '', ens, data: null }
|
|
69
|
-
address = resolved
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (!ens || !keys.length) return { address, ens: ens ?? null, data: null }
|
|
73
|
-
|
|
74
|
-
const name = normalize(ens)
|
|
75
|
-
const results = await Promise.all(
|
|
76
|
-
keys.map(key => client.getEnsText({ name, key }).catch(() => null)),
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
return { address, ens, data: toProfileData(keys, results.map(r => r || '')) }
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// --- Helpers ---
|
|
83
|
-
|
|
84
|
-
function toProfileData(keys: string[], results: string[]): EnsProfile['data'] {
|
|
85
|
-
const get = (key: string) => results[keys.indexOf(key)] || ''
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
avatar: get('avatar'),
|
|
89
|
-
header: get('header'),
|
|
90
|
-
description: get('description'),
|
|
91
|
-
links: {
|
|
92
|
-
url: get('url'),
|
|
93
|
-
email: get('email'),
|
|
94
|
-
twitter: get('com.twitter'),
|
|
95
|
-
github: get('com.github'),
|
|
96
|
-
},
|
|
97
|
-
}
|
|
98
|
-
}
|
|
1
|
+
export { ensCache, fetchEnsFromIndexer, fetchEnsFromChain, ENS_KEYS_AVATAR, ENS_KEYS_PROFILE } from '@1001-digital/components'
|
|
2
|
+
export type { EnsProfile } from '@1001-digital/components'
|
package/app/utils/format-eth.ts
CHANGED
|
@@ -1,12 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
const numberValue = typeof value === 'string' ? parseFloat(value) : value
|
|
3
|
-
|
|
4
|
-
if (isNaN(numberValue)) {
|
|
5
|
-
throw new Error('Invalid number input')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return new Intl.NumberFormat('en-US', {
|
|
9
|
-
minimumFractionDigits: 0,
|
|
10
|
-
maximumFractionDigits: maxDecimals,
|
|
11
|
-
}).format(numberValue)
|
|
12
|
-
}
|
|
1
|
+
export { formatETH } from '@1001-digital/components'
|
package/app/utils/price.ts
CHANGED
|
@@ -1,15 +1 @@
|
|
|
1
|
-
|
|
2
|
-
if (typeof value === 'bigint') return value.toString() + 'n'
|
|
3
|
-
return value
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
const reviver = (_: string, value: unknown) => {
|
|
7
|
-
if (typeof value === 'string' && /^\d+n$/.test(value)) return BigInt(value.slice(0, -1))
|
|
8
|
-
return value
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const stringifyJSON = (obj: unknown): string => JSON.stringify(obj, replacer)
|
|
12
|
-
export const parseJSON = (json: string): unknown => JSON.parse(json, reviver)
|
|
13
|
-
|
|
14
|
-
export const formatPrice = (num: number, digits: number = 2) =>
|
|
15
|
-
num?.toLocaleString('en-US', { maximumFractionDigits: digits })
|
|
1
|
+
export { stringifyJSON, parseJSON, formatPrice } from '@1001-digital/components'
|