-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a main runner class as the entry point for the indexing t…
…ool (#1058) * chore: updated artifacts Signed-off-by: Logan Nguyen <[email protected]> * Feat: added a main runner class as the entry point for the indexing tool Signed-off-by: Logan Nguyen <[email protected]> * docs: added README file Signed-off-by: Logan Nguyen <[email protected]> * fix: updated log message Signed-off-by: Logan Nguyen <[email protected]> * fix: added MIRROR_NODE_URL to README.md Signed-off-by: Logan Nguyen <[email protected]> --------- Signed-off-by: Logan Nguyen <[email protected]>
- Loading branch information
1 parent
f8c21fb
commit 78fb1ee
Showing
6 changed files
with
202 additions
and
20 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
94 changes: 94 additions & 0 deletions
94
tools/erc-repository-indexer/erc-contract-indexer/README.md
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,94 @@ | ||
# ERC Contract Indexer for Hedera Hashgraph | ||
|
||
## Overview | ||
|
||
The ERC Contract Indexer is a tool designed to facilitate the indexing and management of ERC20 and ERC721 smart contracts on the Hedera Hashgraph network. This project provides a set of services to fetch, analyze, and store contract data efficiently, enabling developers and users to interact with smart contracts seamlessly. | ||
|
||
## Features | ||
|
||
- **Contract Fetching**: Retrieve contract details from the Hedera mirror node. | ||
- **Bytecode Analysis**: Analyze smart contract bytecode to categorize contracts as ERC20 or ERC721. | ||
- **Registry Management**: Generate and update registries for ERC20 and ERC721 contracts. | ||
- **Next Pointer Handling**: Manage pagination for contract indexing using a next pointer. | ||
|
||
## Getting Started | ||
|
||
### Prerequisites | ||
|
||
- Node.js (version 18 or higher) | ||
- npm (Node Package Manager) | ||
|
||
### Installation | ||
|
||
1. Clone the repository: | ||
|
||
```bash | ||
git clone https://github.com/hashgraph/hedera-smart-contracts.git | ||
cd tools/erc-repository-indexer/erc-contract-indexer | ||
``` | ||
|
||
2. Install the dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
3. Create a `.env` file in the root directory and configure your environment variables: | ||
|
||
| Variable | Description | Accepted Values | | ||
| ----------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `HEDERA_NETWORK` | The network to connect to. | `previewnet`, `testnet`, or `mainnet` | | ||
| `MIRROR_NODE_URL` | The URL for the Hedera mirror node API. | A valid URL pointing to the Hedera mirror node (e.g., `https://{previewnet\|testnet\|mainnet}.mirrornode.hedera.com`) | | ||
| `STARTING_POINT` | The starting point for contract indexing. | A Hedera contract ID (e.g., `0.0.369`), an EVM 20-byte address (e.g., `0x0000000000000000000000000000000000000369`), or a get contract list next pointer (e.g., `/api/v1/contracts?limit=100&order=asc&contract.id=gt:0.0.369`) | | ||
|
||
Example configuration: | ||
|
||
```plaintext | ||
HEDERA_NETWORK=testnet | ||
MIRROR_NODE_URL=https://testnet.mirrornode.hedera.com | ||
STARTING_POINT=0.0.1013 | ||
``` | ||
|
||
> **Note:** If a `STARTING_POINT` is not set, the tool will first look for a saved `next` pointer in the registry storage to continue indexing. If no pointer is available, the tool will start from the genesis block. | ||
### Usage | ||
|
||
To start the indexing process, run the following command: | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
This will initiate the indexing process, fetching contracts from the specified starting point and categorizing them into ERC20 and ERC721 contracts. The end goal is to generate a comprehensive registry that contains JSON files for both ERC-20 and ERC-721 tokens. The expected format for these registries is as follows: | ||
|
||
- **ERC20**: | ||
```json | ||
[ | ||
{ | ||
"address": "0xevm_address", | ||
"contractId": "0.0.hedera_contract_id" | ||
} | ||
] | ||
``` | ||
- **ERC721**: | ||
```json | ||
[ | ||
{ | ||
"address": "0xevm_address", | ||
"contractId": "0.0.hedera_contract_id" | ||
} | ||
] | ||
``` | ||
These JSON files will serve as a reliable registry of ERC tokens on the Hedera network, enhancing visibility and usability for developers and users alike. | ||
|
||
> **Note:** Future updates will enhance the JSON file format to include additional details about the ERC tokens. | ||
### Testing | ||
|
||
To run the tests for the project, use the following command: | ||
|
||
```bash | ||
npm test | ||
``` | ||
|
||
This will execute the test suite and provide feedback on the functionality of the services. |
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
81 changes: 81 additions & 0 deletions
81
tools/erc-repository-indexer/erc-contract-indexer/src/index.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,81 @@ | ||
/*- | ||
* | ||
* Hedera Smart Contracts | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import dotenv from 'dotenv'; | ||
import { ByteCodeAnalyzer } from './services/byteCodeAnalyzer'; | ||
import { ConfigService } from './services/config'; | ||
import { ContractScannerService } from './services/contractScanner'; | ||
import { RegistryGenerator } from './services/registryGenerator'; | ||
|
||
dotenv.config(); | ||
|
||
export const main = async () => { | ||
const configService = new ConfigService(); | ||
const registryGenerator = new RegistryGenerator(); | ||
const contractScannerService = new ContractScannerService( | ||
configService.getMirrorNodeUrl() | ||
); | ||
const byteCodeAnalyzer = new ByteCodeAnalyzer(); | ||
|
||
try { | ||
let next = await configService.resolveStartingPoint(registryGenerator); | ||
await processContracts( | ||
next, | ||
contractScannerService, | ||
byteCodeAnalyzer, | ||
registryGenerator | ||
); | ||
} catch (error) { | ||
console.error('Error during the indexing process:', error); | ||
} | ||
}; | ||
|
||
const processContracts = async ( | ||
next: string | null, | ||
contractScannerService: ContractScannerService, | ||
byteCodeAnalyzer: ByteCodeAnalyzer, | ||
registryGenerator: RegistryGenerator | ||
) => { | ||
do { | ||
const fetchContractsResponse = | ||
await contractScannerService.fetchContracts(next); | ||
|
||
if (!fetchContractsResponse || !fetchContractsResponse.contracts.length) { | ||
console.warn('No contracts found.'); | ||
return; | ||
} | ||
|
||
next = fetchContractsResponse.links.next; | ||
|
||
const ercContracts = await byteCodeAnalyzer.categorizeERCContracts( | ||
contractScannerService, | ||
fetchContractsResponse.contracts | ||
); | ||
|
||
// let the registry update process to run asynchronously in the background | ||
registryGenerator.generateErcRegistry( | ||
ercContracts.erc20Contracts, | ||
ercContracts.erc721Contracts | ||
); | ||
registryGenerator.updateNextPointer(next); | ||
} while (next); | ||
}; | ||
|
||
main().then(); |
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