diff --git a/src/services/cw20/cw20.md b/src/services/cw20/cw20.md new file mode 100644 index 000000000..0d4018a06 --- /dev/null +++ b/src/services/cw20/cw20.md @@ -0,0 +1,50 @@ +```markdown +# CW20 Service Utilities + +## Introduction + +This folder contains a set of services and utilities for handling CW20 token contracts within the Horoscope v2 application. It includes services for indexing, updating, and reindexing CW20 token data based on blockchain events. These services use BullMQ for job processing and interact with a database via Knex.js. + +## File Descriptions + +* **`cw20.service.ts`**: This file defines the main `Cw20Service`, responsible for processing CW20 contract events, including instantiation, transfers, minting, and burning. It also handles updating total supply and holder statistics. It uses a queue to process new blocks of events. +* **`cw20_reindexing.service.ts`**: This file contains the `Cw20ReindexingContract` service that provides functionality to reindex all data related to a specific CW20 contract by deleting existing data and re-creating it from scratch by fetching events from start block in config till last event block height of this contract.. It also uses a queue to perform reindexing jobs +* **`cw20_update_by_contract.service.ts`**: This file defines the `Cw20UpdateByContractService`, which is responsible for updating the balance of holders of a specific CW20 contract based on events occurring within specified blocks range.. It uses a queue to process update jobs per contract.. + +## Usage Instructions + +### `cw20.service.ts` +* **Execution**: This service is designed to run within the Moleculer framework as part of the broader Horoscope v2 application and doesn't have direct command-line execution instructions +* It processes blockchain events related to CW20 contracts using BullMQ queues defined in `@QueueHandler`. The queue is started automatically when the service starts up +* It will automatically create a job in queue with repeat option base on config every time it starts up if not in test environment +* The service listens on `BULL_JOB_NAME.HANDLE_CW20`. To trigger processing, ensure that this job is scheduled correctly within your BullMQ setup according to config . + +### `cw20_reindexing.service.ts` +* **Execution**: This service is designed to run within the Moleculer framework as part of the broader Horoscope v2 application and doesn't have direct command-line execution instructions +* **Triggering Reindexing**: Call action `SERVICE.V1.Cw20ReindexingService.Reindexing`. The action requires one parameter: `{ contractAddress: string }`. For example: ```js broker.call(SERVICE.V1.Cw20ReindexingService.Reindexing,{contractAddress:"contract_address"}) ``` + * This action will create job in queue name `BULL_JOB_NAME.REINDEX_CW20_CONTRACT`. + * After this job done, will create another job in queue name `BULL_JOB_NAME.REINDEX_CW20_HISTORY` to handle histories data for this contract . + +### `cw20_update_by_contract.service.ts` + * **Execution:** This service runs as part of Moleculer framework and does not require direct execution via command line . it uses BullMQ queues defined in `@QueueHandler`. The queue is started automatically when the service starts up + * This service listens on `BULL_JOB_NAME.CW20_UPDATE_BY_CONTRACT`. To trigger processing, call action `SERVICE.V1.Cw20UpdateByContractService`. For example:  ```js broker call( SERVICE . V1 . Cw20UpdateByContract . UpdateByContract , { cwContracts : [{ id : 1 , lastUpdatedHeight : 1 } ] , startBlock : 1 , endBlock : 1 } ) ``` where cwContracts is an array with element `{id:number, lastUpdatedHeight: number}` , startBlock, endBlock are numbers representing block height range you want update holder balance for each contact id . + + +## Dependencies + +* **@ourparentcenter/moleculer-decorators-extended**: A library providing decorators for Moleculer services including actions definitions.. +* **bullmq**: A library used for managing message queues with Redis backend (or other supported backends). Used by all three files here.. +* **knex**: A SQL query builder used for database interactions.. Used by all three files here.. +* **lodash**: A utility library providing helper functions; used by all three files here... +* **moleculer**: The core microservices framework that these services are built on top of; used by all three files here... +*. config file (`../../../config`) contains essential configurations such as database connection details & job settings; used by all three files here... + + +## Additional Notes + +* These services are designed to work together as part of a larger system and do not operate independently well without whole system running fully & proper configuration set up correctly . All three files here need each other running correctly before working correctly .. Note that they use common constants from `/src/common/index`, models from `/src/models`, utils from `/src/common/utils`, bullable base class from `/src/base/bullable`. Please make sure all those dependencies are available when running those code snippets .. + + +*The code assumes that appropriate database schemas already exist.* + +