Skip to content

Commit

Permalink
feat: add base blockchain support + blockscout explorer abi
Browse files Browse the repository at this point in the history
  • Loading branch information
amalcaraz committed Jul 30, 2024
1 parent f6369f2 commit c94d81e
Show file tree
Hide file tree
Showing 25 changed files with 351 additions and 5 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
"typescript": "^4.8.4"
},
"workspaces": [
"packages/core",
"packages/base",
"packages/bsc",
"packages/core",
"packages/ethereum",
"packages/framework",
"packages/indexer-example",
"packages/solana",
"packages/oasys",
"packages/oasys-verse"
"packages/oasys-verse",
"packages/solana"
]
}
1 change: 1 addition & 0 deletions packages/base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Framework Ethereum Package
32 changes: 32 additions & 0 deletions packages/base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { BlockchainFrameworkImplementation } from '@aleph-indexer/framework'
import { baseFetcherClientFactory } from './src/services/fetcher/client.js'
import { baseIndexerClientFactory } from './src/services/indexer/client.js'
import { baseParserClientFactory } from './src/services/parser/client.js'
import { baseFetcherFactory } from './src/services/fetcher/factory.js'
import { baseIndexerFactory } from './src/services/indexer/factory.js'
import { baseParserFactory } from './src/services/parser/factory.js'
import { baseWorkerDomainFactory } from './src/domain/worker.js'

export * from './src/domain/worker.js'
export * from './src/services/fetcher/index.js'
export * from './src/services/parser/index.js'
export * from './src/services/indexer/index.js'

export default {
fetcher: {
main: baseFetcherFactory,
client: baseFetcherClientFactory,
},
parser: {
main: baseParserFactory,
client: baseParserClientFactory,
},
indexer: {
main: baseIndexerFactory,
client: baseIndexerClientFactory,
domain: {
worker: baseWorkerDomainFactory,
main: null,
},
},
} as BlockchainFrameworkImplementation
43 changes: 43 additions & 0 deletions packages/base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@aleph-indexer/base",
"version": "1.1.11",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ALEPH.im",
"license": "ISC",
"peerDependencies": {
"graphql": "^15.8.0",
"graphql-type-json": "^0.3.2",
"luxon": "3.2.1"
},
"devDependencies": {
"@types/luxon": "3.2.0",
"@types/node": "^17.0.33",
"graphql": "^15.8.0",
"graphql-type-json": "^0.3.2",
"luxon": "3.2.1"
},
"dependencies": {
"@aleph-indexer/core": "^1.1.10",
"@aleph-indexer/ethereum": "^1.1.11",
"@aleph-indexer/framework": "^1.1.11",
"moleculer": "^0.14.24"
},
"typedoc": {
"entryPoint": "./index.ts",
"displayName": "Base Indexer Framework"
}
}
1 change: 1 addition & 0 deletions packages/base/src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './worker.js'
31 changes: 31 additions & 0 deletions packages/base/src/domain/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ParserContext } from '@aleph-indexer/framework'
import {
BaseParsedLog,
BaseParsedTransaction,
} from '../services/parser/src/types.js'

export type BaseTransactionIndexerWorkerDomainI = {
baseTransactionBufferLength?: number // default 1000
baseFilterTransaction(
context: ParserContext,
entity: BaseParsedTransaction,
): Promise<boolean>
baseIndexTransactions(
context: ParserContext,
entities: BaseParsedTransaction[],
): Promise<void>
}

export type BaseLogIndexerWorkerDomainI = {
baseLogBufferLength?: number // default 1000
baseFilterLog(context: ParserContext, entity: BaseParsedLog): Promise<boolean>
baseIndexLogs(
context: ParserContext,
entities: BaseParsedLog[],
): Promise<void>
}

export type BaseIndexerWorkerDomainI = BaseTransactionIndexerWorkerDomainI &
BaseLogIndexerWorkerDomainI

export { ethereumWorkerDomainFactory as baseWorkerDomainFactory } from '@aleph-indexer/ethereum'
36 changes: 36 additions & 0 deletions packages/base/src/sdk/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BlockchainId } from '@aleph-indexer/framework'
import {
EthereumAccountTransactionHistoryStorage,
EthereumClient,
EthereumClientOptions,
EthereumLogBloomStorage,
} from '@aleph-indexer/ethereum'

export class BaseClient extends EthereumClient {
protected genesisBlockTimestamp = 1686789347000

constructor(
protected blockchainId: BlockchainId,
protected options: EthereumClientOptions,
protected accountSignatureDAL?: EthereumAccountTransactionHistoryStorage,
protected logBloomDAL?: EthereumLogBloomStorage,
) {
super(blockchainId, options, accountSignatureDAL, logBloomDAL)

// @note: Quick fix for BASE it doesn't affect ethereum
// Take a look at https://github.com/web3/web3.js/pull/3948#issuecomment-821779691
// @note: Take a look at:
// @note: https://github.com/web3/web3.js/pull/3948#issuecomment-821779691
console.log('⚡️ monkey patched "web3-utils" "hexToNumber" for BASE chain')

const hexToNumberOld = this.sdk.utils.hexToNumber
this.sdk.utils.hexToNumber = function (value) {
try {
return hexToNumberOld(value)
} catch (e: any) {
if (e?.message !== 'Number can only safely store up to 53 bits') throw e
return Number.MAX_SAFE_INTEGER
}
}
}
}
1 change: 1 addition & 0 deletions packages/base/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './client.js'
3 changes: 3 additions & 0 deletions packages/base/src/services/fetcher/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ethereumFetcherClientFactory } from '@aleph-indexer/ethereum'

export { ethereumFetcherClientFactory as baseFetcherClientFactory }
59 changes: 59 additions & 0 deletions packages/base/src/services/fetcher/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable prettier/prettier */
import { ServiceBroker } from 'moleculer'
import { Utils } from '@aleph-indexer/core'
import {
BlockchainFetcherI,
FetcherMsClient,
BlockchainId,
getBlockchainEnv
} from '@aleph-indexer/framework'
import { ethereumConfigFactory, ethereumDalsFetcherFactory, ethereumFetcherInstanceFactory } from '@aleph-indexer/ethereum'
import { BaseClient } from '../../sdk/index.js'

export function baseClientFetcherFactory(
blockchainId: BlockchainId,
config: ReturnType<typeof ethereumConfigFactory>,
DALs: ReturnType<typeof ethereumDalsFetcherFactory>): BaseClient {
const url = getBlockchainEnv(blockchainId, 'RPC', true)

return new BaseClient(
blockchainId,
{ ...config, url },
DALs.accountTransactionHistoryDAL,
DALs.logBloomDAL
)
}

export async function baseFetcherFactory(
blockchainId: BlockchainId,
basePath: string,
broker: ServiceBroker,
fetcherClient: FetcherMsClient,
): Promise<BlockchainFetcherI> {
if (basePath) await Utils.ensurePath(basePath)

// Config

const config = ethereumConfigFactory(blockchainId)

// DALs

const DALs = ethereumDalsFetcherFactory(basePath)

// Instances

const baseClient = baseClientFetcherFactory(
blockchainId,
config,
DALs
)

return ethereumFetcherInstanceFactory(
blockchainId,
broker,
fetcherClient,
baseClient,
config,
DALs
)
}
2 changes: 2 additions & 0 deletions packages/base/src/services/fetcher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './client.js'
export * from './factory.js'
3 changes: 3 additions & 0 deletions packages/base/src/services/indexer/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ethereumIndexerClientFactory } from '@aleph-indexer/ethereum'

export { ethereumIndexerClientFactory as baseIndexerClientFactory }
3 changes: 3 additions & 0 deletions packages/base/src/services/indexer/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ethereumIndexerFactory } from '@aleph-indexer/ethereum'

export { ethereumIndexerFactory as baseIndexerFactory }
2 changes: 2 additions & 0 deletions packages/base/src/services/indexer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './client.js'
export * from './factory.js'
3 changes: 3 additions & 0 deletions packages/base/src/services/parser/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ethereumParserClientFactory } from '@aleph-indexer/ethereum'

export { ethereumParserClientFactory as baseParserClientFactory }
39 changes: 39 additions & 0 deletions packages/base/src/services/parser/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable prettier/prettier */
import { BlockchainId, BlockchainParserI, getBlockchainEnv } from '@aleph-indexer/framework'
import {ethereumAbiParserFactory, ethereumParserInstanceFactory } from '@aleph-indexer/ethereum'
import { BaseClient } from '../../sdk/index.js'
import { BaseParsedTransaction, BaseRawTransaction } from './src/types.js'

export function baseClientParserFactory(
blockchainId: BlockchainId,
): BaseClient {
const url = getBlockchainEnv(blockchainId, 'RPC', true)

return new BaseClient(blockchainId, { url })
}

export async function baseParserFactory(
blockchainId: BlockchainId,
basePath: string,
layoutPath?: string,
): Promise<
BlockchainParserI<
BaseRawTransaction,
BaseParsedTransaction
>
> {
const baseClient = baseClientParserFactory(blockchainId)

const baseAbiFactory = await ethereumAbiParserFactory(
blockchainId,
baseClient,
basePath,
layoutPath
)

return ethereumParserInstanceFactory(
blockchainId,
baseClient,
baseAbiFactory,
)
}
3 changes: 3 additions & 0 deletions packages/base/src/services/parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './client.js'
export * from './factory.js'
export * from './src/types.js'
11 changes: 11 additions & 0 deletions packages/base/src/services/parser/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
EthereumParsedAccountState,
EthereumParsedLog,
EthereumParsedTransaction,
EthereumRawTransaction,
} from '@aleph-indexer/ethereum'

export type BaseRawTransaction = EthereumRawTransaction
export type BaseParsedTransaction = EthereumParsedTransaction
export type BaseParsedLog = EthereumParsedLog
export type BaseParsedAccountState = EthereumParsedAccountState
16 changes: 16 additions & 0 deletions packages/base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"exclude": [
"node_modules",
"dist",
"scripts",
"tests",
"**/*.spec.ts",
"**/*.test.ts",
"**/__tests__",
"**/__mocks__"
]
}
1 change: 1 addition & 0 deletions packages/base/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../types'
6 changes: 6 additions & 0 deletions packages/ethereum/src/services/parser/src/abiFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export class EthereumAbiFactory {

const body: any = await response.json()

// @note: We are supporting etherscan and blockscout kind of EVM explorer

if (body.abi) {
return body.abi as Abi
}

// @note: Rate limit error sent with status 200 OK...
// {
// "status": "0",
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"author": "ALEPH.im",
"license": "ISC",
"peerDependencies": {
"@aleph-indexer/base": "^1.x",
"@aleph-indexer/bsc": "^1.x",
"@aleph-indexer/ethereum": "^1.x",
"@aleph-indexer/oasys": "^1.x",
Expand All @@ -30,6 +31,9 @@
"nats": "^2.x"
},
"peerDependenciesMeta": {
"@aleph-indexer/base": {
"optional": true
},
"@aleph-indexer/bsc": {
"optional": true
},
Expand Down Expand Up @@ -64,4 +68,4 @@
"entryPoint": "./index.ts",
"displayName": "Indexer Framework"
}
}
}
3 changes: 2 additions & 1 deletion packages/framework/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export type ParsedEntity<P> = RawEntity & {
export type BlockchainId = string

export enum BlockchainChain {
Ethereum = 'ethereum',
Base = 'base',
Bsc = 'bsc',
Ethereum = 'ethereum',
Solana = 'solana',
Oasys = 'oasys',
OasysVerse = 'oasys-verse',
Expand Down
Loading

0 comments on commit c94d81e

Please sign in to comment.