-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathetherscan.ts
More file actions
43 lines (37 loc) · 1.01 KB
/
etherscan.ts
File metadata and controls
43 lines (37 loc) · 1.01 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
const endpoints: { [k: number]: string } = {
1: 'https://api.etherscan.io/api',
3: 'https://api-ropsten.etherscan.io/api',
4: 'https://api-rinkeby.etherscan.io/api',
5: 'https://api-goerli.etherscan.io/api',
8453: 'https://api.basescan.org/api',
84531: 'https://api-goerli.basescan.org/api',
84532: 'https://api-sepolia.basescan.org/api',
}
export interface Transfer {
timeStamp: string
uniqueId: string
hash: string
from: string
to: string
interpretation?: any
}
export async function getTransactions(chainId: number, address: string) {
const url = endpoints[chainId]
const data = new URLSearchParams({
module: 'account',
action: 'txlist',
address,
sort: 'desc',
offset: '5',
page: '1',
apikey: process.env.ETHERSCAN_API_KEY || '',
})
const resp = await fetch(`${url}?${data.toString()}`, {
next: { revalidate: 60 * 5 },
})
if (!resp.ok) {
throw new Error(resp.statusText)
}
const json = (await resp.json()).result as Transfer[]
return json
}