import os from 'os' ; import fs from 'fs' ; import { join } from 'path' ; import { jsonc } from 'jsonc' ; import _ from 'lodash' ; import { Keccak } from 'sha3' ; import { BlockEvent , EventType , FortaConfig , Network , Trace , TransactionEvent } from '.' ; import { Transaction } from './transaction' ; import { Receipt } from './receipt' ; import { TxEventBlock } from './transaction.event' ; import { Block } from './block' ; import { ethers } from '.' ; export const getEthersProvider = ( ) => { return new ethers . providers . JsonRpcProvider ( getJsonRpcUrl ( ) ) ; } ; export const getEthersBatchProvider = ( ) => { return new ethers . providers . JsonRpcBatchProvider ( getJsonRpcUrl ( ) ) ; } ; const getFortaConfig = ( ) => { let config = { } ; const globalConfigPath = join ( os . homedir ( ) , '.forta' , 'forta.config.json' ) ; if ( fs . existsSync ( globalConfigPath ) ) { config = Object . assign ( config , jsonc . parse ( fs . readFileSync ( globalConfigPath , 'utf8' ) ) ) ; } const configFlagIndex = process . argv . indexOf ( '--config' ) ; const configFile = configFlagIndex == - 1 ? undefined : process . argv [ configFlagIndex + 1 ] ; const localConfigPath = join ( process . cwd ( ) , configFile || 'forta.config.json' ) ; if ( fs . existsSync ( localConfigPath ) ) { config = Object . assign ( config , jsonc . parse ( fs . readFileSync ( localConfigPath , 'utf8' ) ) ) ; } return config ; } ; export const getJsonRpcUrl = ( ) => { if ( process . env . JSON_RPC_HOST ) { return `http://${ process . env . JSON_RPC_HOST }${ process . env . JSON_RPC_PORT ? `:${ process . env . JSON_RPC_PORT }` : '' }` ; } const { jsonRpcUrl } = getFortaConfig ( ) ; if ( ! jsonRpcUrl ) throw new Error ( 'no jsonRpcUrl found' ) ; if ( ! jsonRpcUrl . startsWith ( "http" ) ) throw new Error ( 'jsonRpcUrl must begin with http(s)' ) ; return jsonRpcUrl ; } ; export const createTransactionEvent = ( { type = EventType . BLOCK , network = Network . MAINNET , transaction , receipt , traces = [ ] , addresses = { } , block } ) => { return new TransactionEvent ( type , network , transaction , receipt , traces , addresses , block ) ; } ; export const createBlockEvent = ( { type = EventType . BLOCK , network = Network . MAINNET , block } ) => { return new BlockEvent ( type , network , block ) ; } ; export const assertIsNonEmptyString = ( str , varName ) => { if ( ! _ . isString ( str ) || str . length === 0 ) { throw new Error ( `${ varName } must be non-empty string` ) ; } } ; export const assertIsFromEnum = ( value , Enum , varName ) => { if ( ! Object . values ( Enum ) . includes ( value ) ) { throw new Error ( `${ varName } must be valid enum value` ) ; } } ; export const keccak256 = ( str ) => { const hash = new Keccak ( 256 ) ; hash . update ( str ) ; return `0x${ hash . digest ( 'hex' ) }` ; } ;