-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdecoder.ts
More file actions
47 lines (40 loc) · 1.46 KB
/
decoder.ts
File metadata and controls
47 lines (40 loc) · 1.46 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
import { TransactionDecoder } from '@3loop/transaction-decoder'
import { createPublicClient, http } from 'viem'
import { InMemoryAbiStoreLive, InMemoryContractMetaStoreLive } from '@3loop/transaction-decoder/in-memory'
import { ConfigProvider, Layer } from 'effect'
/*
* Example of decoding a transaction by hash
* This example uses the default in-memory data loaders for ABIs and Contracts metadata: InMemoryAbiStoreLive and InMemoryContractMetaStoreLive
*/
// Create a public client for the Ethereum Mainnet network
const getPublicClient = (chainId: number) => {
if (chainId === 1) {
return {
client: createPublicClient({
transport: http('https://rpc.ankr.com/eth'),
}),
}
}
}
// Create a config for the ABI loader to provide your Etherscan API key
const Config = ConfigProvider.fromMap(new Map([['ETHERSCAN_API_KEY', 'YourApiKey']]))
const ABILoaderLayer = Layer.setConfigProvider(Config)
const abiStore = InMemoryAbiStoreLive.pipe(Layer.provide(ABILoaderLayer))
const contractMetaStore = InMemoryContractMetaStoreLive
const decoder = new TransactionDecoder({
getPublicClient: getPublicClient,
abiStore,
contractMetaStore,
})
export async function main({ hash }: { hash: string }): Promise<string | null> {
try {
const decoded = await decoder.decodeTransaction({
chainID: 1,
hash: hash,
})
return JSON.stringify(decoded, null, 2)
} catch (e) {
console.error(JSON.stringify(e, null, 2))
return null
}
}