-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathquery.ts
More file actions
109 lines (98 loc) · 2.3 KB
/
Copy pathquery.ts
File metadata and controls
109 lines (98 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { drpc } from 'evm-providers'
import { type Hex, createPublicClient, http } from 'viem'
import {
arbitrum,
arbitrumSepolia,
arcTestnet,
base,
baseSepolia,
celo,
celoSepolia,
linea,
lineaSepolia,
optimism,
optimismSepolia,
polygon,
polygonAmoy,
scroll,
scrollSepolia,
worldchain,
worldchainSepolia,
} from 'viem/chains'
import { decodeFunctionData } from 'viem/utils'
import { type Env, envVar } from '../env'
import { dnsDecodeName, resolverAbi } from './utils'
const supportedChains = [
arcTestnet,
arbitrum,
arbitrumSepolia,
base,
baseSepolia,
celo,
celoSepolia,
linea,
lineaSepolia,
optimism,
optimismSepolia,
polygon,
polygonAmoy,
scroll,
scrollSepolia,
worldchain,
worldchainSepolia,
]
type HandleQueryArgs = {
dnsEncodedName: Hex
encodedResolveCall: Hex
targetChainId: bigint
targetRegistryAddress: Hex
env: Env
}
export async function handleQuery({
dnsEncodedName,
encodedResolveCall,
targetChainId,
targetRegistryAddress,
env,
}: HandleQueryArgs) {
const name = dnsDecodeName(dnsEncodedName)
// Decode the internal resolve call like addr(), text() or contenthash()
const { functionName, args } = decodeFunctionData({
abi: resolverAbi,
data: encodedResolveCall,
})
const chain = supportedChains.find(
(chain) => BigInt(chain.id) === targetChainId
)
if (!chain) {
console.error(`Unsupported chain ${targetChainId} for ${name}`)
return '0x' as const
}
const DRPC_API_KEY = envVar('DRPC_API_KEY', env)
const l2Client = createPublicClient({
chain,
transport: http(
// World subsidizes RPC usage, so we'll use those endpoints for mainnet and testnet
chain.id === worldchainSepolia.id
? 'https://worldchain-sepolia.g.alchemy.com/public'
: chain.id === worldchain.id
? 'https://worldchain-mainnet.g.alchemy.com/public'
: chain.id === arcTestnet.id
? `https://lb.drpc.live/arc-testnet/${DRPC_API_KEY}`
: drpc(chain.id, DRPC_API_KEY)
),
})
console.log({
targetChainId,
targetRegistryAddress,
name,
functionName,
args,
})
return l2Client.readContract({
address: targetRegistryAddress,
abi: [resolverAbi[1]],
functionName: 'resolve',
args: [dnsEncodedName, encodedResolveCall],
})
}