message: feat(market-matcher): startup account check
diff: @@ -9,6 +9,7 @@ import { IOffChainDataClient } from '@energyweb/origin-backend-client';
  import { Configuration } from '@energyweb/utils-general';
  import Web3 from 'web3';
  +import { Role } from '@energyweb/user-registry';
  import { EntityFetcher } from './EntityFetcher';
  import { EntityStore } from './EntityStore';
  import { logger } from './Logger';
  @@ -24,19 +25,19 @@ export interface IMatcherConfig {
  matcherInterval: number;
  }
  -const createBlockchainConf = async (
  +const createBlockchainConfig = async (
  matcherConfig: IMatcherConfig
  ): Promise<Configuration.Entity> => {
  const web3 = new Web3(matcherConfig.web3Url);
  - const marketConf = await marketCreateBlockchainProperties(
  + const blockchainProperties = await marketCreateBlockchainProperties(
  web3,
  matcherConfig.marketLogicAddress
  );
  - marketConf.activeUser = matcherConfig.matcherAccount;
  + blockchainProperties.activeUser = matcherConfig.matcherAccount;
  return {
  - blockchainProperties: marketConf,
  + blockchainProperties,
  logger,
  offChainDataSource: {
  baseUrl: matcherConfig.offChainDataSourceUrl,
  @@ -45,6 +46,29 @@ const createBlockchainConf = async (
  };
  };
  +const testMatcherAccount = async (config: Configuration.Entity) => {
  + const { userLogicInstance, web3, activeUser } = config.blockchainProperties;
  +
  + const { address } = web3.eth.accounts.privateKeyToAccount(activeUser.privateKey);
  + const hasMatcherRole = await userLogicInstance.isRole(Role.Matcher, address);
  + const balance = web3.utils.fromWei(await web3.eth.getBalance(address));
  +
  + if (!hasMatcherRole) {
  + logger.error(`Matcher account: ${address} does not have Matcher role set in User Registry`);
  + throw new Error('Missing Matcher role');
  + }
  + if (parseFloat(balance) < 0.5) {
  + logger.error(
  + `Matcher account: ${address} should have at least 0.5EWT tokens. Current balance is ${parseFloat(
  + balance
  + )}EWT`
  + );
  + throw new Error('Insufficient balance');
  + }
  + logger.info(`Matcher account: ${address} - Matcher role: OK`);
  + logger.info(`Matcher account: ${address} - Matcher balance ${balance}EWT: OK`);
  +};
  +
  export async function startMatcher(matcherConfig: IMatcherConfig) {
  logger.info('Matcher application is starting.');
  @@ -53,7 +77,10 @@ export async function startMatcher(matcherConfig: IMatcherConfig) {
  }
  try {
  - const config = await createBlockchainConf(matcherConfig);
  + const config = await createBlockchainConfig(matcherConfig);
  +
  + await testMatcherAccount(config);
  +
  const entityFetcher = new EntityFetcher(config);
  const entityStore = new EntityStore(config, logger, entityFetcher);
  