import fetch from 'node-fetch' ; import { TextEncoder , TextDecoder } from 'util' ; import { Api , JsonRpc } from 'eosjs' ; import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig' ; import { Account } from './accounts' ; import { ConfigManager , LamingtonDebugLevel } from './configManager' ; import { convertLegacyPublicKey } from 'eosjs/dist/eosjs-numeric' ; import { timer } from './utils' ; import * as chalk from 'chalk' ; interface InitArgs { adminAccount : Account ; chainId ? : string ; httpEndpoint : string ; } /**
 * Manages client connection and communication with a local EOSIO node
 */ export class EOSManager { /** Defaults to `eosio` administration account */ static adminAccount : Account ; /** Development signature provider */ static signatureProvider : JsSignatureProvider ; /** Configured EOSjs client */ static api : Api ; /** RPC connection with the local EOSIO node at `http://127.0.0.1:8888` */ static rpc : JsonRpc ; author Kevin Brown <github.com/thekevinbrown>
      static initWithDefaults = ( ) => { const adminAccount = new Account ( 'eosio' , '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3' ) ; EOSManager . init ( { httpEndpoint : 'http://127.0.0.1:8888' , adminAccount } ) ; } ; author Kevin Brown <github.com/thekevinbrown>
      static init = ( { httpEndpoint , adminAccount , chainId } ) => { EOSManager . adminAccount = adminAccount ; if ( adminAccount . privateKey ) { EOSManager . signatureProvider = new JsSignatureProvider ( [ adminAccount . privateKey ] ) ; } EOSManager . rpc = new JsonRpc ( httpEndpoint , { fetch : fetch as any } ) ; EOSManager . api = new Api ( { rpc : EOSManager . rpc , chainId , signatureProvider : EOSManager . signatureProvider , textDecoder : new TextDecoder ( ) as any , textEncoder : new TextEncoder ( ) as any , } ) ; } ; author Kevin Brown <github.com/thekevinbrown>
     *  param account static addSigningAccountIfMissing = ( account ) => { if ( account . publicKey && account . privateKey ) { const nonLegacyPublicKey = convertLegacyPublicKey ( account . publicKey ) ; if ( ! EOSManager . signatureProvider . keys . get ( nonLegacyPublicKey ) ) { EOSManager . signatureProvider . keys . set ( nonLegacyPublicKey , account . privateKey ) ; EOSManager . signatureProvider . availableKeys . push ( nonLegacyPublicKey ) ; } } } ; author Kevin Brown <github.com/thekevinbrown>
     *  param transaction param eos param options static transact = async ( transaction , eos = EOSManager . api , options ? ) => { const flattenedOptions = Object . assign ( { blocksBehind : 1 , expireSeconds : 30 } , options ) ; const transactionTimer = timer ( ) ; async function logOutput ( verboseOutput ) { const legacyDebugOption = ConfigManager . debugTransactions || flattenedOptions . debug ; if ( ConfigManager . debugLevelMin || ConfigManager . debugLevelVerbose || legacyDebugOption ) { let consoleHeader = '' ; const calls = transaction . actions . map ( ( action ) => `${ action . account }.${ action . name }` ) ; consoleHeader += `========== Calling ${ calls . join ( ', ' ) } ==========` ; console . log ( chalk . cyan ( consoleHeader ) + chalk . blue ( ' (%s)' ) , transactionTimer . ms ) ; } if ( ConfigManager . debugLevelVerbose ) { console . log ( verboseOutput ) ; console . log ( 'Options: ' , options ) ; } } return await eos . transact ( transaction , flattenedOptions ) . then ( ( value ) => { logOutput ( chalk . green ( 'Succeeded: ' ) + JSON . stringify ( value , null , 4 ) ) ; return value ; } ) . catch ( ( error ) => { logOutput ( chalk . red ( 'Threw error: ' ) + error + '\n' + chalk . cyan ( 'Payload causing the above error: ' ) + JSON . stringify ( transaction , null , 4 ) ) ; throw error ; } ) ; } ; }