packageDocumentation module hidden import { hexlify , isHexString , keccak256 , toChecksumAddress } from '@harmony-js/crypto' ; import { numberToHex , isArray , isString , isAddress , hexToBN , } from '@harmony-js/utils' ; import { toUtf8Bytes } from '../abi/abiCoder' ; export const inputLogFormatter = ( options ) => { if ( options . fromBlock ) { options . fromBlock = inputBlockNumberFormatter ( options . fromBlock ) ; } if ( options . toBlock ) { options . toBlock = inputBlockNumberFormatter ( options . toBlock ) ; } options . topics = options . topics || [ ] ; options . topics = options . topics . map ( ( topic ) => { return isArray ( topic ) ? topic . map ( toTopic ) : toTopic ( topic ) ; } ) ; if ( options . address ) { if ( isArray ( options . address ) ) { options . address = options . address . map ( ( addr ) => { return inputAddressFormatter ( addr ) ; } ) ; } else { options . address = inputAddressFormatter ( options . address ) ; } } return options ; } ; method param { Object } log returns { Object } export const outputLogFormatter = ( log ) => { if ( typeof log . blockHash === 'string' && typeof log . transactionHash === 'string' && typeof log . logIndex === 'string' ) { const shaId = keccak256 ( '0x' + log . blockHash . replace ( '0x' , '' ) + log . transactionHash . replace ( '0x' , '' ) + log . logIndex . replace ( '0x' , '' ) ) ; shaId . replace ( '0x' , '' ) . substr ( 0 , 8 ) ; log . id = `log_${ shaId }` ; } else if ( ! log . id ) { log . id = null ; } if ( log . blockNumber !== null ) { log . blockNumber = hexToBN ( log . blockNumber ) . toNumber ( ) ; } if ( log . transactionIndex !== null ) { log . transactionIndex = hexToBN ( log . transactionIndex ) . toNumber ( ) ; } if ( log . logIndex !== null ) { log . logIndex = hexToBN ( log . logIndex ) . toNumber ( ) ; } if ( log . address ) { log . address = toChecksumAddress ( log . address ) ; } return log ; } ; export const inputBlockNumberFormatter = ( blockNumber ) => { if ( blockNumber === undefined || blockNumber === null || isPredefinedBlockNumber ( blockNumber ) ) { return blockNumber ; } if ( isHexString ( blockNumber ) ) { if ( isString ( blockNumber ) ) { return blockNumber . toLowerCase ( ) ; } return blockNumber ; } return numberToHex ( blockNumber ) ; } ; export const isPredefinedBlockNumber = ( blockNumber ) => { return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest' ; } ; export const inputAddressFormatter = ( address ) => { if ( isAddress ( address ) ) { return `0x${ address . toLowerCase ( ) . replace ( '0x' , '' ) }` ; } throw new Error ( `Provided address "${ address }" is invalid, the capitalization checksum test failed, or its an indrect IBAN address which can't be converted.` ) ; } ; export const toTopic = ( value ) => { if ( value === null || typeof value === 'undefined' ) { return null ; } value = String ( value ) ; if ( value . indexOf ( '0x' ) === 0 ) { return value ; } return hexlify ( toUtf8Bytes ( value ) ) ; } ;