import { RPCResponse } from '../providers/common' ; export let messageId = 0 ; export type RPCSendableMessage = { method : string ; params : any [ ] ; } ; /**
 * Should be called to valid json create payload object
 */ export function toJsonRpcRequest ( method , params ) { if ( ! method ) { throw new Error ( 'jsonrpc method should be specified!' ) ; } if ( typeof method !== 'string' ) { throw new Error ( `jsonrpc must be a string, got ${ typeof method }!` ) ; } messageId ++ ; return { jsonrpc : '2.0' , id : messageId , method : method , params : params || [ ] } ; } /**
 * Should be called to check if jsonrpc response is valid
 */ export function isValidResponse ( response ) { return Array . isArray ( response ) ? response . every ( validateSingleMessage ) : validateSingleMessage ( response ) ; function validateSingleMessage ( message ) { return ( ! ! message && ! ( 'error' in message ) && message . jsonrpc === '2.0' && typeof message . id === 'number' && ( message . result != null || message . result !== undefined ) ) ; } } param messages export function toBatchPayload ( messages ) { return messages . map ( function ( message ) { return toJsonRpcRequest ( message . method , message . params ) ; } ) ; }