Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-squids-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@3loop/transaction-decoder': minor
---

Make in-memory store more flexible by folllowing the same API as SQL
26 changes: 19 additions & 7 deletions apps/docs/src/content/docs/welcome/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ The `InMemoryAbiStoreLive` provides default ABI loading and caching functionalit

```ts
import { InMemoryAbiStoreLive } from '@3loop/transaction-decoder/in-memory'
import { ConfigProvider, Layer } from 'effect'

// We use Effect library to provide custom configuration
const Config = ConfigProvider.fromMap(new Map([['ETHERSCAN_API_KEY', 'YourApiKey']]))
const ABILoaderLayer = Layer.setConfigProvider(Config)
const abiStore = InMemoryAbiStoreLive.pipe(Layer.provide(ABILoaderLayer))
import { EtherscanV2StrategyResolver } from '@3loop/transaction-decoder'

const abiStore = InMemoryAbiStoreLive.make({
default: [
EtherscanV2StrategyResolver({
apikey: 'YourApiKey', // provide Etherscan V2 API key
}),
],
})
```

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

```ts
import { InMemoryContractMetaStoreLive } from '@3loop/transaction-decoder/in-memory'
import { Layer } from 'effect'

const contractMetaStore = Layer.unwrapEffect(
Effect.gen(function* () {
const service = yield* PublicClient

const contractMetaStore = InMemoryContractMetaStoreLive
return InMemoryContractMetaStoreLive.make({
default: [ERC20RPCStrategyResolver(service), ProxyRPCStrategyResolver(service)],
})
}),
)
```

For a custom implementation, see our [How To Decode Transaction (Contract Metadata Store)](/guides/decode-transaction/#3-contract-metadata-store) guide.
Expand Down
53 changes: 9 additions & 44 deletions packages/transaction-decoder/src/in-memory/abi-store.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@
import {
EtherscanStrategyResolver,
FourByteStrategyResolver,
ContractABI,
AbiStore,
SourcifyStrategyResolver,
OpenchainStrategyResolver,
EtherscanV2StrategyResolver,
} from '../effect.js'
import { Config, Effect, Layer } from 'effect'
import { ContractABI, AbiStore } from '../effect.js'
import { Effect, Layer } from 'effect'

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

export const InMemoryAbiStoreLive = Layer.effect(
AbiStore,
Effect.gen(function* () {
const etherscanApiKey = yield* Config.string('ETHERSCAN_API_KEY').pipe(
Effect.catchTag('ConfigError', () => {
return Effect.succeed(undefined)
}),
)
const etherscanEndpoint = yield* Config.string('ETHERSCAN_ENDPOINT').pipe(Effect.orElseSucceed(() => undefined))

const etherscanStrategy =
etherscanEndpoint && etherscanApiKey
? EtherscanStrategyResolver({
apikey: etherscanApiKey,
endpoint: etherscanEndpoint,
})
: etherscanApiKey
? EtherscanV2StrategyResolver({
apikey: etherscanApiKey,
})
: undefined

return AbiStore.of({
strategies: {
default: [
etherscanStrategy,
SourcifyStrategyResolver(),
OpenchainStrategyResolver(),
FourByteStrategyResolver(),
].filter(Boolean),
},
export const make = (strategies: AbiStore['strategies']) =>
Layer.succeed(
AbiStore,
AbiStore.of({
strategies,
set: (_key, value) =>
Effect.sync(() => {
if (value.status === 'success') {
Expand Down Expand Up @@ -82,6 +48,5 @@ export const InMemoryAbiStoreLive = Layer.effect(
result: null,
}
}),
})
}),
)
}),
)
20 changes: 8 additions & 12 deletions packages/transaction-decoder/src/in-memory/contract-meta-store.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import type { ContractData } from '../types.js'
import { ContractMetaStore, ERC20RPCStrategyResolver, NFTRPCStrategyResolver, PublicClient } from '../effect.js'
import { ContractMetaStore } from '../effect.js'
import { Effect, Layer } from 'effect'

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

export const InMemoryContractMetaStoreLive = Layer.effect(
ContractMetaStore,
Effect.gen(function* () {
const publicClient = yield* PublicClient
const erc20Loader = ERC20RPCStrategyResolver(publicClient)
const nftLoader = NFTRPCStrategyResolver(publicClient)
return ContractMetaStore.of({
strategies: { default: [erc20Loader, nftLoader] },
export const make = (strategies: ContractMetaStore['strategies']) =>
Layer.succeed(
ContractMetaStore,
ContractMetaStore.of({
strategies,
get: ({ address, chainID }) =>
Effect.sync(() => {
const key = `${address}-${chainID}`.toLowerCase()
Expand All @@ -37,6 +34,5 @@ export const InMemoryContractMetaStoreLive = Layer.effect(
contractMetaCache.set(key, result.result)
}
}),
})
}),
)
}),
)
4 changes: 2 additions & 2 deletions packages/transaction-decoder/src/in-memory/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './abi-store.js'
export * from './contract-meta-store.js'
export * as InMemoryAbiStoreLive from './abi-store.js'
export * as InMemoryContractMetaStoreLive from './contract-meta-store.js'
Loading