Skip to content

Commit be8bc09

Browse files
authored
Make in memory stores more flexible (#206)
1 parent c5bbd68 commit be8bc09

File tree

5 files changed

+43
-65
lines changed

5 files changed

+43
-65
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@3loop/transaction-decoder': minor
3+
---
4+
5+
Make in-memory store more flexible by folllowing the same API as SQL

apps/docs/src/content/docs/welcome/getting-started.mdx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ The `InMemoryAbiStoreLive` provides default ABI loading and caching functionalit
4040

4141
```ts
4242
import { InMemoryAbiStoreLive } from '@3loop/transaction-decoder/in-memory'
43-
import { ConfigProvider, Layer } from 'effect'
44-
45-
// We use Effect library to provide custom configuration
46-
const Config = ConfigProvider.fromMap(new Map([['ETHERSCAN_API_KEY', 'YourApiKey']]))
47-
const ABILoaderLayer = Layer.setConfigProvider(Config)
48-
const abiStore = InMemoryAbiStoreLive.pipe(Layer.provide(ABILoaderLayer))
43+
import { EtherscanV2StrategyResolver } from '@3loop/transaction-decoder'
44+
45+
const abiStore = InMemoryAbiStoreLive.make({
46+
default: [
47+
EtherscanV2StrategyResolver({
48+
apikey: 'YourApiKey', // provide Etherscan V2 API key
49+
}),
50+
],
51+
})
4952
```
5053

5154
For a custom implementation, see our [How To Decode Transaction (ABI Data Store)](/guides/decode-transaction/#2-abi-data-store) guide.
@@ -59,8 +62,17 @@ The `InMemoryContractMetaStoreLive` handles contract metadata resolution:
5962

6063
```ts
6164
import { InMemoryContractMetaStoreLive } from '@3loop/transaction-decoder/in-memory'
65+
import { Layer } from 'effect'
66+
67+
const contractMetaStore = Layer.unwrapEffect(
68+
Effect.gen(function* () {
69+
const service = yield* PublicClient
6270

63-
const contractMetaStore = InMemoryContractMetaStoreLive
71+
return InMemoryContractMetaStoreLive.make({
72+
default: [ERC20RPCStrategyResolver(service), ProxyRPCStrategyResolver(service)],
73+
})
74+
}),
75+
)
6476
```
6577

6678
For a custom implementation, see our [How To Decode Transaction (Contract Metadata Store)](/guides/decode-transaction/#3-contract-metadata-store) guide.
Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,13 @@
1-
import {
2-
EtherscanStrategyResolver,
3-
FourByteStrategyResolver,
4-
ContractABI,
5-
AbiStore,
6-
SourcifyStrategyResolver,
7-
OpenchainStrategyResolver,
8-
EtherscanV2StrategyResolver,
9-
} from '../effect.js'
10-
import { Config, Effect, Layer } from 'effect'
1+
import { ContractABI, AbiStore } from '../effect.js'
2+
import { Effect, Layer } from 'effect'
113

124
const abiCache = new Map<string, ContractABI>()
135

14-
export const InMemoryAbiStoreLive = Layer.effect(
15-
AbiStore,
16-
Effect.gen(function* () {
17-
const etherscanApiKey = yield* Config.string('ETHERSCAN_API_KEY').pipe(
18-
Effect.catchTag('ConfigError', () => {
19-
return Effect.succeed(undefined)
20-
}),
21-
)
22-
const etherscanEndpoint = yield* Config.string('ETHERSCAN_ENDPOINT').pipe(Effect.orElseSucceed(() => undefined))
23-
24-
const etherscanStrategy =
25-
etherscanEndpoint && etherscanApiKey
26-
? EtherscanStrategyResolver({
27-
apikey: etherscanApiKey,
28-
endpoint: etherscanEndpoint,
29-
})
30-
: etherscanApiKey
31-
? EtherscanV2StrategyResolver({
32-
apikey: etherscanApiKey,
33-
})
34-
: undefined
35-
36-
return AbiStore.of({
37-
strategies: {
38-
default: [
39-
etherscanStrategy,
40-
SourcifyStrategyResolver(),
41-
OpenchainStrategyResolver(),
42-
FourByteStrategyResolver(),
43-
].filter(Boolean),
44-
},
6+
export const make = (strategies: AbiStore['strategies']) =>
7+
Layer.succeed(
8+
AbiStore,
9+
AbiStore.of({
10+
strategies,
4511
set: (_key, value) =>
4612
Effect.sync(() => {
4713
if (value.status === 'success') {
@@ -82,6 +48,5 @@ export const InMemoryAbiStoreLive = Layer.effect(
8248
result: null,
8349
}
8450
}),
85-
})
86-
}),
87-
)
51+
}),
52+
)

packages/transaction-decoder/src/in-memory/contract-meta-store.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import type { ContractData } from '../types.js'
2-
import { ContractMetaStore, ERC20RPCStrategyResolver, NFTRPCStrategyResolver, PublicClient } from '../effect.js'
2+
import { ContractMetaStore } from '../effect.js'
33
import { Effect, Layer } from 'effect'
44

55
const contractMetaCache = new Map<string, ContractData>()
66

7-
export const InMemoryContractMetaStoreLive = Layer.effect(
8-
ContractMetaStore,
9-
Effect.gen(function* () {
10-
const publicClient = yield* PublicClient
11-
const erc20Loader = ERC20RPCStrategyResolver(publicClient)
12-
const nftLoader = NFTRPCStrategyResolver(publicClient)
13-
return ContractMetaStore.of({
14-
strategies: { default: [erc20Loader, nftLoader] },
7+
export const make = (strategies: ContractMetaStore['strategies']) =>
8+
Layer.succeed(
9+
ContractMetaStore,
10+
ContractMetaStore.of({
11+
strategies,
1512
get: ({ address, chainID }) =>
1613
Effect.sync(() => {
1714
const key = `${address}-${chainID}`.toLowerCase()
@@ -37,6 +34,5 @@ export const InMemoryContractMetaStoreLive = Layer.effect(
3734
contractMetaCache.set(key, result.result)
3835
}
3936
}),
40-
})
41-
}),
42-
)
37+
}),
38+
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './abi-store.js'
2-
export * from './contract-meta-store.js'
1+
export * as InMemoryAbiStoreLive from './abi-store.js'
2+
export * as InMemoryContractMetaStoreLive from './contract-meta-store.js'

0 commit comments

Comments
 (0)