-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add default in memory stores * Allow vanilla abi to accept effect layers for stores * Add package configuration and documentation * Changeset * Fix linting issues * Upd doc --------- Co-authored-by: Anastasia Rodionova <argali96@gmail.com>
- Loading branch information
1 parent
c3cd218
commit f8dbcbd
Showing
11 changed files
with
269 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@3loop/transaction-decoder': patch | ||
--- | ||
|
||
Add default in-memory stores for contract and abi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
EtherscanStrategyResolver, | ||
FourByteStrategyResolver, | ||
ContractABI, | ||
AbiStore, | ||
SourcifyStrategyResolver, | ||
OpenchainStrategyResolver, | ||
} from '../effect.js' | ||
import { Config, 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.catchTag('ConfigError', () => { | ||
return Effect.succeed(undefined) | ||
}), | ||
) | ||
|
||
const etherscanStrategy = | ||
etherscanEndpoint && etherscanApiKey | ||
? EtherscanStrategyResolver({ | ||
apikey: etherscanApiKey, | ||
endpoint: etherscanEndpoint, | ||
}) | ||
: undefined | ||
|
||
return AbiStore.of({ | ||
strategies: { | ||
default: [ | ||
etherscanStrategy, | ||
SourcifyStrategyResolver(), | ||
OpenchainStrategyResolver(), | ||
FourByteStrategyResolver(), | ||
].filter(Boolean), | ||
}, | ||
set: (_key, value) => | ||
Effect.sync(() => { | ||
if (value.status === 'success') { | ||
if (value.result.type === 'address') { | ||
abiCache.set(value.result.address, value.result) | ||
} else if (value.result.type === 'event') { | ||
abiCache.set(value.result.event, value.result) | ||
} else if (value.result.type === 'func') { | ||
abiCache.set(value.result.signature, value.result) | ||
} | ||
} | ||
}), | ||
get: (key) => | ||
Effect.sync(() => { | ||
if (abiCache.has(key.address)) { | ||
return { | ||
status: 'success', | ||
result: abiCache.get(key.address)!, | ||
} | ||
} | ||
|
||
if (key.event && abiCache.has(key.event)) { | ||
return { | ||
status: 'success', | ||
result: abiCache.get(key.event)!, | ||
} | ||
} | ||
|
||
if (key.signature && abiCache.has(key.signature)) { | ||
return { | ||
status: 'success', | ||
result: abiCache.get(key.signature)!, | ||
} | ||
} | ||
|
||
return { | ||
status: 'empty', | ||
result: null, | ||
} | ||
}), | ||
}) | ||
}), | ||
) |
42 changes: 42 additions & 0 deletions
42
packages/transaction-decoder/src/in-memory/contract-meta-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { ContractData } from '../types.js' | ||
import { ContractMetaStore, ERC20RPCStrategyResolver, NFTRPCStrategyResolver, PublicClient } 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] }, | ||
get: ({ address, chainID }) => | ||
Effect.sync(() => { | ||
const key = `${address}-${chainID}`.toLowerCase() | ||
const value = contractMetaCache.get(key) | ||
|
||
if (value) { | ||
return { | ||
status: 'success', | ||
result: value, | ||
} | ||
} | ||
|
||
return { | ||
status: 'empty', | ||
result: null, | ||
} | ||
}), | ||
set: ({ address, chainID }, result) => | ||
Effect.sync(() => { | ||
const key = `${address}-${chainID}`.toLowerCase() | ||
|
||
if (result.status === 'success') { | ||
contractMetaCache.set(key, result.result) | ||
} | ||
}), | ||
}) | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './abi-store.js' | ||
export * from './contract-meta-store.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.