Skip to content

Commit

Permalink
Merge branch 'binance-scanner' into 'dev'
Browse files Browse the repository at this point in the history
Binance scanner

Closes #463

See merge request ergo/rosen-bridge/ui!386
  • Loading branch information
zargarzadehm committed Dec 12, 2024
2 parents dbafbb9 + 9f84dd1 commit df17a94
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-carpets-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosen-service': minor
---

Add binance chain scanner with observation and event trigger extractors
10 changes: 10 additions & 0 deletions apps/rosen-service/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ ethereum:
rwt:
initialHeight:
rpcUrl: https://eth-mainnet.public.blastapi.io
binance:
addresses:
lock:
eventTrigger:
permit:
fraud:
tokens:
rwt:
initialHeight:
rpcUrl: https://bsc-mainnet.public.blastapi.io/
# rpcAuthToken:
postgres:
url: # postgresql://username:password@host:port/databasename
Expand Down
24 changes: 20 additions & 4 deletions apps/rosen-service/src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getConfig = () => {
addresses: {
lock: nodeConfig.get<string>('cardano.addresses.lock'),
eventTrigger: nodeConfig.get<string>(
'cardano.addresses.eventTrigger',
'cardano.addresses.eventTrigger'
),
permit: nodeConfig.get<string>('cardano.addresses.permit'),
fraud: nodeConfig.get<string>('cardano.addresses.fraud'),
Expand All @@ -49,7 +49,7 @@ const getConfig = () => {
addresses: {
lock: nodeConfig.get<string>('bitcoin.addresses.lock'),
eventTrigger: nodeConfig.get<string>(
'bitcoin.addresses.eventTrigger',
'bitcoin.addresses.eventTrigger'
),
permit: nodeConfig.get<string>('bitcoin.addresses.permit'),
fraud: nodeConfig.get<string>('bitcoin.addresses.fraud'),
Expand All @@ -67,7 +67,7 @@ const getConfig = () => {
addresses: {
lock: nodeConfig.get<string>('ethereum.addresses.lock'),
eventTrigger: nodeConfig.get<string>(
'ethereum.addresses.eventTrigger',
'ethereum.addresses.eventTrigger'
),
permit: nodeConfig.get<string>('ethereum.addresses.permit'),
fraud: nodeConfig.get<string>('ethereum.addresses.fraud'),
Expand All @@ -79,6 +79,22 @@ const getConfig = () => {
rpcUrl: nodeConfig.get<string>('ethereum.rpcUrl'),
rpcAuthToken: getOptionalString('ethereum.rpcAuthToken'),
},
binance: {
addresses: {
lock: nodeConfig.get<string>('binance.addresses.lock'),
eventTrigger: nodeConfig.get<string>(
'binance.addresses.eventTrigger'
),
permit: nodeConfig.get<string>('binance.addresses.permit'),
fraud: nodeConfig.get<string>('binance.addresses.fraud'),
},
initialHeight: nodeConfig.get<number>('binance.initialHeight'),
tokens: {
rwt: nodeConfig.get<string>('binance.tokens.rwt'),
},
rpcUrl: nodeConfig.get<string>('binance.rpcUrl'),
rpcAuthToken: getOptionalString('binance.rpcAuthToken'),
},
postgres: {
url: nodeConfig.get<string>('postgres.url'),
logging: nodeConfig.get<boolean>('postgres.logging'),
Expand All @@ -98,7 +114,7 @@ const getConfig = () => {
`an error occurred reading some service configs: ${error}`,
false,
'error',
error instanceof Error ? error.stack : undefined,
error instanceof Error ? error.stack : undefined
);
}
};
Expand Down
2 changes: 2 additions & 0 deletions apps/rosen-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ export const ERGO_SCANNER_INTERVAL = 2 * 60 * 1000;
export const CARDANO_SCANNER_INTERVAL = 30 * 1000;
export const BITCOIN_SCANNER_INTERVAL = 10 * 60 * 1000;
export const ETHEREUM_SCANNER_INTERVAL = 60 * 1000;
export const BINANCE_SCANNER_INTERVAL = 10 * 1000;
export const ASSET_CALCULATOR_INTERVAL = 30 * 1000;

export const ERGO_SCANNER_LOGGER_NAME = 'ergo-scanner';
export const CARDANO_SCANNER_LOGGER_NAME = 'cardano-scanner';
export const BITCOIN_SCANNER_LOGGER_NAME = 'bitcoin-scanner';
export const ETHEREUM_SCANNER_LOGGER_NAME = 'ethereum-scanner';
export const BINANCE_SCANNER_LOGGER_NAME = 'binance-scanner';
15 changes: 15 additions & 0 deletions apps/rosen-service/src/event-trigger/event-trigger-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const bitcoinEventTriggerExtractorLogger =
WinstonLogger.getInstance().getLogger('bitcoin-event-trigger-extractor');
const ethereumEventTriggerExtractorLogger =
WinstonLogger.getInstance().getLogger('ethereum-event-trigger-extractor');
const binanceEventTriggerExtractorLogger =
WinstonLogger.getInstance().getLogger('binance-event-trigger-extractor');

/**
* register event trigger extractors for all chains
Expand Down Expand Up @@ -68,11 +70,23 @@ export const registerExtractors = (scanner: ErgoScanner) => {
configs.ethereum.addresses.fraud,
ethereumEventTriggerExtractorLogger,
);
const binanceEventTriggerExtractor = new EventTriggerExtractor(
'binance-extractor',
dataSource,
ErgoNetworkType.Explorer,
configs.ergo.explorerUrl,
configs.binance.addresses.eventTrigger,
configs.binance.tokens.rwt,
configs.binance.addresses.permit,
configs.binance.addresses.fraud,
binanceEventTriggerExtractorLogger,
);

scanner.registerExtractor(ergoEventTriggerExtractor);
scanner.registerExtractor(cardanoEventTriggerExtractor);
scanner.registerExtractor(bitcoinEventTriggerExtractor);
scanner.registerExtractor(ethereumEventTriggerExtractor);
scanner.registerExtractor(binanceEventTriggerExtractor);

logger.debug('event trigger extractors registered', {
scannerName: scanner.name(),
Expand All @@ -81,6 +95,7 @@ export const registerExtractors = (scanner: ErgoScanner) => {
cardanoEventTriggerExtractor.getId(),
bitcoinEventTriggerExtractor.getId(),
ethereumEventTriggerExtractor.getId(),
binanceEventTriggerExtractor.getId(),
],
});
} catch (error) {
Expand Down
41 changes: 41 additions & 0 deletions apps/rosen-service/src/observation/chains/binance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { BinanceRpcObservationExtractor } from '@rosen-bridge/evm-observation-extractor';
import { EvmRpcScanner } from '@rosen-bridge/evm-rpc-scanner';
import WinstonLogger from '@rosen-bridge/winston-logger';

import config from '../../configs';
import dataSource from '../../data-source';
import AppError from '../../errors/AppError';
import { getRosenTokens } from '../../utils';

const logger = WinstonLogger.getInstance().getLogger(import.meta.url);

/**
* register an observation extractor for the provided scanner
* @param scanner
*/
export const registerBinanceExtractor = (scanner: EvmRpcScanner) => {
try {
const observationExtractor = new BinanceRpcObservationExtractor(
config.binance.addresses.lock,
dataSource,
getRosenTokens(),
logger
);

scanner.registerExtractor(observationExtractor);

logger.debug('binance observation extractor registered', {
scannerName: scanner.name(),
});
} catch (error) {
throw new AppError(
`cannot create or register binance observation extractor due to error: ${error}`,
false,
'error',
error instanceof Error ? error.stack : undefined,
{
scannerName: scanner.name(),
}
);
}
};
2 changes: 2 additions & 0 deletions apps/rosen-service/src/observation/observation-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { registerBinanceExtractor } from './chains/binance';
import { registerBitcoinExtractor } from './chains/bitcoin';
import { registerCardanoExtractor } from './chains/cardano';
import { registerErgoExtractor } from './chains/ergo';
Expand All @@ -8,6 +9,7 @@ const observationService = {
registerCardanoExtractor,
registerErgoExtractor,
registerEthereumExtractor,
registerBinanceExtractor,
};

export default observationService;
53 changes: 53 additions & 0 deletions apps/rosen-service/src/scanner/chains/binance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { EvmRpcScanner } from '@rosen-bridge/evm-rpc-scanner';
import WinstonLogger from '@rosen-bridge/winston-logger';

import config from '../../configs';
import {
BINANCE_SCANNER_INTERVAL,
BINANCE_SCANNER_LOGGER_NAME,
SCANNER_API_TIMEOUT,
} from '../../constants';
import dataSource from '../../data-source';
import AppError from '../../errors/AppError';
import observationService from '../../observation/observation-service';
import { startScanner } from '../scanner-utils';

const logger = WinstonLogger.getInstance().getLogger(import.meta.url);
const scannerLogger = WinstonLogger.getInstance().getLogger(
BINANCE_SCANNER_LOGGER_NAME
);

/**
* create a binance scanner, initializing it and calling its update method
* periodically
*/
export const startBinanceScanner = async () => {
try {
const scanner = new EvmRpcScanner(
'binance',
{
RpcUrl: config.binance.rpcUrl,
dataSource,
initialHeight: config.binance.initialHeight,
timeout: SCANNER_API_TIMEOUT,
},
scannerLogger,
config.binance.rpcAuthToken
);

observationService.registerBinanceExtractor(scanner);

await startScanner(scanner, import.meta.url, BINANCE_SCANNER_INTERVAL);

logger.debug('binance scanner started');

return scanner;
} catch (error) {
throw new AppError(
`cannot create or start binance scanner due to error: ${error}`,
false,
'error',
error instanceof Error ? error.stack : undefined
);
}
};
22 changes: 15 additions & 7 deletions apps/rosen-service/src/scanner/scanner-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import WinstonLogger from '@rosen-bridge/winston-logger/dist/WinstonLogger';

import { handleError } from '../utils';
import { startBinanceScanner } from './chains/binance';
import { startBitcoinScanner } from './chains/bitcoin';
import { startCardanoScanner } from './chains/cardano';
import { startErgoScanner } from './chains/ergo';
Expand All @@ -13,20 +14,27 @@ const logger = WinstonLogger.getInstance().getLogger(import.meta.url);
*/
const start = async () => {
try {
const [ergoScanner, cardanoScanner, bitcoinScanner, ethereumScanner] =
await Promise.all([
startErgoScanner(),
startCardanoScanner(),
startBitcoinScanner(),
startEthereumScanner(),
]);
const [
ergoScanner,
cardanoScanner,
bitcoinScanner,
ethereumScanner,
binanceScanner,
] = await Promise.all([
startErgoScanner(),
startCardanoScanner(),
startBitcoinScanner(),
startEthereumScanner(),
startBinanceScanner(),
]);

logger.debug('all scanners started and their extractors registered', {
scannerNames: [
ergoScanner.name(),
cardanoScanner.name(),
bitcoinScanner.name(),
ethereumScanner.name(),
binanceScanner.name(),
],
});
} catch (error) {
Expand Down

0 comments on commit df17a94

Please sign in to comment.