import { CeloTransactionObject , CeloTx , EventLog , parseDecodedParams } from '@celo/connect' ; import { CLIError } from '@oclif/errors' ; import BigNumber from 'bignumber.js' ; import chalk from 'chalk' ; import Table from 'cli-table' ; import { cli } from 'cli-ux' ; export async function displayWeb3Tx ( name , txObj , tx ? ) { cli . action . start ( `Sending Transaction: ${ name }` ) ; const result = await txObj . send ( tx ) ; console . log ( result ) ; cli . action . stop ( ) ; } export async function displaySendTx < A > ( name , txObj , tx ? , displayEventName ? ) { cli . action . start ( `Sending Transaction: ${ name }` ) ; try { const txResult = await txObj . send ( tx ) ; const txHash = await txResult . getHash ( ) ; console . log ( chalk `SendTransaction: {red.bold ${ name }}` ) ; printValueMap ( { txHash } ) ; const txReceipt = await txResult . waitReceipt ( ) ; cli . action . stop ( ) ; if ( displayEventName && txReceipt . events ) { Object . entries ( txReceipt . events ) . filter ( ( [ eventName ] ) => ( typeof displayEventName === 'string' && eventName === displayEventName ) || displayEventName . includes ( eventName ) ) . forEach ( ( [ eventName , log ] ) => { const { params } = parseDecodedParams ( ( log as EventLog ) . returnValues ) ; console . log ( chalk . magenta . bold ( `${ eventName }:` ) ) ; printValueMap ( params , chalk . magenta ) ; } ) ; } } catch ( e ) { cli . action . stop ( `failed: ${ e . message }` ) ; throw e ; } } export function printValueMap ( valueMap , color = chalk . yellowBright . bold ) { console . log ( Object . keys ( valueMap ) . map ( ( key ) => color ( `${ key }: ` ) + valueMap [ key ] ) . join ( '\n' ) ) ; } export function printValueMap2 ( valueMap , color = chalk . yellowBright . bold ) { valueMap . forEach ( ( value , key ) => console . log ( color ( `${ key }: ` ) + value ) ) ; } export function printValueMapRecursive ( valueMap ) { console . log ( toStringValueMapRecursive ( valueMap , '' ) ) ; } function toStringValueMapRecursive ( valueMap , prefix ) { const printValue = ( v ) : string => { if ( typeof v === 'object' && v != null ) { if ( BigNumber . isBigNumber ( v ) ) { const extra = v . isGreaterThan ( new BigNumber ( 10 ) . pow ( 3 ) ) ? `(~${ v . toExponential ( 3 ) })` : '' ; return `${ v . toFixed ( ) } ${ extra }` ; } else if ( v instanceof Error ) { return '\n' + chalk . red ( v . message ) ; } return '\n' + toStringValueMapRecursive ( v , prefix + '  ' ) ; } return chalk `${ v }` ; } ; return Object . keys ( valueMap ) . map ( ( key ) => prefix + chalk . yellowBright . bold ( `${ key }: ` ) + printValue ( valueMap [ key ] ) ) . join ( '\n' ) ; } export function printVTable ( valueMap ) { const table = new Table ( ) ; Object . keys ( valueMap ) . forEach ( ( key ) => { table . push ( { [ key ] : valueMap [ key ] } ) ; } ) ; console . log ( table . toString ( ) ) ; } export function failWith ( msg ) { throw new CLIError ( msg ) ; } export async function binaryPrompt ( promptMessage , defaultToNo ? ) { const resp = await cli . prompt ( promptMessage + ` [y/yes, n/no${ defaultToNo ? ' (default)' : '' }]` , { required : ! defaultToNo } ) ; return [ 'y' , 'yes' ] . includes ( resp . toLowerCase ( ) ) ; }