-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathblockscout-abi.ts
More file actions
101 lines (90 loc) · 2.69 KB
/
blockscout-abi.ts
File metadata and controls
101 lines (90 loc) · 2.69 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
import { Effect } from 'effect'
import * as RequestModel from './request-model.js'
type FetchResult =
| { type: 'success'; data: RequestModel.ContractABI[] }
| { type: 'missing'; reason: string }
| { type: 'error'; cause: unknown }
async function fetchContractABI(
{ address, chainId }: RequestModel.GetContractABIStrategyParams,
config: { apikey?: string; endpoint: string },
): Promise<FetchResult> {
try {
const endpoint = config.endpoint
const params: Record<string, string> = {
module: 'contract',
action: 'getabi',
address,
}
if (config?.apikey) {
params['apikey'] = config.apikey
}
const searchParams = new URLSearchParams(params)
const response = await fetch(`${endpoint}?${searchParams.toString()}`)
const json = (await response.json()) as { status: string; result: string; message: string }
if (json.status === '1') {
return {
type: 'success',
data: [
{
chainID: chainId,
address,
abi: json.result,
type: 'address',
},
],
}
}
// If the API request was successful but no ABI was found
if (
json.status === '0' &&
(json.message?.includes('not verified') || json.result === 'Contract source code not verified')
) {
return {
type: 'missing',
reason: `No verified ABI found: ${json.message || json.result}`,
}
}
return {
type: 'error',
cause: json,
}
} catch (error) {
return {
type: 'error',
cause: error,
}
}
}
export const BlockscoutStrategyResolver = (config: {
apikey?: string
endpoint: string
}): RequestModel.ContractAbiResolverStrategy => {
return {
id: 'blockscout-strategy',
type: 'address',
resolver: (req: RequestModel.GetContractABIStrategyParams) =>
Effect.withSpan(
Effect.gen(function* () {
const result = yield* Effect.promise(() => fetchContractABI(req, config))
if (result.type === 'success') {
return result.data
} else if (result.type === 'missing') {
return yield* Effect.fail(
new RequestModel.MissingABIStrategyError(
req.address,
req.chainId,
'blockscout-strategy',
undefined,
undefined,
result.reason,
),
)
} else {
return yield* Effect.fail(new RequestModel.ResolveStrategyABIError('Blockscout', req.address, req.chainId))
}
}),
'AbiStrategy.BlockscoutStrategyResolver',
{ attributes: { chainId: req.chainId, address: req.address } },
),
}
}