-
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.
Allow having custom abi loader strategy per each chain (#21)
* Bubble trace and log decoding errors * Add blockscout strategy * Allow having custom abi loader per chain Having one array for all might result on fetching the abi from a different chain, resulting in bad caching and decoding. * changeset
- Loading branch information
Showing
12 changed files
with
127 additions
and
95 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": minor | ||
--- | ||
|
||
Allow having custom abi loader strategy per each chain |
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
42 changes: 42 additions & 0 deletions
42
packages/transaction-decoder/src/abi-strategy/blockscout-abi.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 { Effect, RequestResolver } from 'effect' | ||
import * as RequestModel from './request-model.js' | ||
|
||
async function fetchContractABI( | ||
{ address, chainID }: RequestModel.GetContractABIStrategy, | ||
config: { apikey?: string; endpoint: string }, | ||
) { | ||
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 { | ||
address: { | ||
[address]: json.result, | ||
}, | ||
} | ||
} | ||
|
||
throw new Error(`Failed to fetch ABI for ${address} on chain ${chainID}`) | ||
} | ||
|
||
export const BlockscoutStrategyResolver = (config: { apikey?: string; endpoint: string }) => | ||
RequestResolver.fromFunctionEffect((req: RequestModel.GetContractABIStrategy) => | ||
Effect.tryPromise({ | ||
try: () => fetchContractABI(req, config), | ||
catch: () => new RequestModel.ResolveStrategyABIError('Blockscout', req.address, req.chainID), | ||
}), | ||
) |
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
Oops, something went wrong.