Skip to content

Commit

Permalink
Merge pull request #71 from sotatek-dev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Sotatek-TanHoang authored Sep 24, 2024
2 parents 126aa09 + d2c403a commit 0a3033d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/constants/entity.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export enum ETableName {
TOKEN_PRICES = 'token_prices',
MULTI_SIGNATURE = 'multi_signature',
}

export const MINA_CRAWL_SAFE_BLOCK = 10;
12 changes: 6 additions & 6 deletions src/modules/crawler/crawler.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CrawlerConsole {
async handleCrawlETHBridge() {
try {
while (true) {
this.blockchainEVMCrawler.handleEventCrawlBlock();
await this.blockchainEVMCrawler.handleEventCrawlBlock();
await sleep(15);
}
} catch (error) {
Expand All @@ -50,7 +50,7 @@ export class CrawlerConsole {
async handleValidateEthLockTx() {
try {
while (true) {
this.senderEVMBridge.unlockEVMTransaction();
await this.senderEVMBridge.unlockEVMTransaction();
await sleep(15);
}
} catch (error) {
Expand All @@ -65,7 +65,7 @@ export class CrawlerConsole {
async handleValidateMinaLockTx() {
try {
while (true) {
this.senderMinaBridge.handleValidateUnlockTxMina();
await this.senderMinaBridge.handleValidateUnlockTxMina();
await sleep(1);
}
} catch (error) {
Expand All @@ -80,7 +80,7 @@ export class CrawlerConsole {
async handleSenderETHBridgeUnlock() {
try {
while (true) {
this.senderEVMBridge.handleUnlockEVM();
await this.senderEVMBridge.handleUnlockEVM();
await sleep(15);
}
} catch (error) {
Expand All @@ -95,7 +95,7 @@ export class CrawlerConsole {
async handleCrawlMinaBridge() {
try {
while (true) {
this.scBridgeMinaCrawler.handleEventCrawlBlock();
await this.scBridgeMinaCrawler.handleEventCrawlBlock();
await sleep(15);
}
} catch (error) {
Expand Down Expand Up @@ -139,7 +139,7 @@ export class CrawlerConsole {
async getPriceCoinMarketCap() {
try {
while (true) {
this.jobGetPrice.handleGetPriceToken();
await this.jobGetPrice.handleGetPriceToken();
await sleep(43200);
}
} catch (error) {
Expand Down
54 changes: 29 additions & 25 deletions src/modules/crawler/crawler.minabridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import dayjs from 'dayjs';
import { Logger } from 'log4js';
import { Field, Mina, PublicKey, UInt32 } from 'o1js';
import { fetchLastBlock, Field, Mina, PublicKey, UInt32 } from 'o1js';
import { DataSource, QueryRunner } from 'typeorm';

import { EAsset } from '../../constants/api.constant.js';
import { DEFAULT_ADDRESS_PREFIX, EEventName, EEventStatus, ENetworkName } from '../../constants/blockchain.constant.js';
import { MINA_CRAWL_SAFE_BLOCK } from '../../constants/entity.constant.js';
import { EEnvKey } from '../../constants/env.constant.js';
import { CrawlContractRepository } from '../../database/repositories/crawl-contract.repository.js';
import { TokenPairRepository } from '../../database/repositories/token-pair.repository.js';
Expand All @@ -25,25 +26,29 @@ export class SCBridgeMinaCrawler {
private readonly loggerService: LoggerService,
) {
this.logger = this.loggerService.getLogger('SC_BRIDGE_MINA_CRAWLER');
const Network = Mina.Network({
mina: this.configService.get(EEnvKey.MINA_BRIDGE_RPC_OPTIONS),
archive: this.configService.get(EEnvKey.MINA_BRIDGE_ARCHIVE_RPC_OPTIONS),
});
Mina.setActiveInstance(Network);
}
public async handleEventCrawlBlock() {
const { startBlockNumber, toBlock } = await this.getFromToBlock();
if (startBlockNumber.greaterThanOrEqual(toBlock).toBoolean()) {
this.logger.warn('Already latest block. Skipped.');
return;
}
const zkappAddress = PublicKey.fromBase58(this.configService.get(EEnvKey.MINA_BRIDGE_CONTRACT_ADDRESS));
const zkapp = new Bridge(zkappAddress);

const events = await zkapp.fetchEvents(startBlockNumber.add(1), toBlock);
this.logger.info({ events });

const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const { startBlockNumber } = await this.getFromToBlock();

const Network = Mina.Network({
mina: this.configService.get(EEnvKey.MINA_BRIDGE_RPC_OPTIONS),
archive: this.configService.get(EEnvKey.MINA_BRIDGE_ARCHIVE_RPC_OPTIONS),
});
Mina.setActiveInstance(Network);
const zkappAddress = PublicKey.fromBase58(this.configService.get(EEnvKey.MINA_BRIDGE_CONTRACT_ADDRESS));

const zkapp = new Bridge(zkappAddress);
const events = await zkapp.fetchEvents(UInt32.from(Number(startBlockNumber) + 1));
this.logger.info({ events });

this.logger.info(`[handleCrawlMinaBridge] Crawling from ${startBlockNumber} to ${toBlock}`);
for (const event of events) {
switch (event.type) {
case 'Unlock':
Expand All @@ -56,12 +61,9 @@ export class SCBridgeMinaCrawler {
continue;
}
}
this.logger.info(`[handleCrawlMinaBridge] Crawled from============================= ${startBlockNumber}`);
if (events.length > 0) {
// udpate current latest block
await this.updateLatestBlockCrawl(Number(events.pop().blockHeight.toString()), queryRunner);
}
return await queryRunner.commitTransaction();
// udpate current latest block
await this.updateLatestBlockCrawl(Number(toBlock.toString()), queryRunner);
return queryRunner.commitTransaction();
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
Expand Down Expand Up @@ -135,8 +137,8 @@ export class SCBridgeMinaCrawler {
};
}

private async updateLatestBlockCrawl(blockNumber: number, queryRunner: QueryRunner) {
await queryRunner.manager.update(
private updateLatestBlockCrawl(blockNumber: number, queryRunner: QueryRunner) {
return queryRunner.manager.update(
CrawlContract,
{
contractAddress: this.configService.get(EEnvKey.MINA_BRIDGE_CONTRACT_ADDRESS),
Expand All @@ -148,8 +150,7 @@ export class SCBridgeMinaCrawler {
);
}

private async getFromToBlock(): Promise<{ startBlockNumber; toBlock }> {
let toBlock;
private async getFromToBlock(): Promise<{ startBlockNumber: UInt32; toBlock: UInt32 }> {
const currentCrawledBlock = await this.crawlContractRepository.findOne({
where: {
networkName: ENetworkName.MINA,
Expand All @@ -168,7 +169,10 @@ export class SCBridgeMinaCrawler {
} else {
startBlockNumber = currentCrawledBlock.latestBlock;
}
const latestBlock = await fetchLastBlock(this.configService.get(EEnvKey.MINA_BRIDGE_RPC_OPTIONS));

const toBlock = UInt32.from(latestBlock.blockchainLength.toUInt64().sub(MINA_CRAWL_SAFE_BLOCK).toString());

return { startBlockNumber, toBlock };
return { startBlockNumber: UInt32.from(startBlockNumber), toBlock };
}
}

0 comments on commit 0a3033d

Please sign in to comment.