import { BaseProvider } from './base' ; import { RPCMethod , RPCRequest , RPCResponse , performRPC , performBatchRPC , RPCRequestPayload , } from '../net' ; import { composeMiddleware } from '../util' ; import { Provider , Subscriber } from '../types' ; export class HTTPProvider extends BaseProvider implements Provider { buildPayload < T extends any [ ] > ( method , params ) { return { url : this . nodeURL , payload : { id : 1 , jsonrpc : '2.0' , method , params } , } ; } buildBatchPayload < T extends any [ ] > ( method , paramsList ) { const payloads = [ ] ; for ( let i = 0 ; i < paramsList . length ; i ++ ) { const payloadParams = paramsList [ i ] ; let params ; if ( Array . isArray ( payloadParams ) ) { params = payloadParams ; } else { params = [ payloadParams ] ; } payloads . push ( { id : i + 1 , jsonrpc : '2.0' , method , params , } ) ; } return { url : this . nodeURL , payload : payloads , } ; } send < P extends any [ ] , R = any , E = string > ( method , ... params ) { const [ tReq , tRes ] = this . getMiddleware ( method ) ; const reqMiddleware = composeMiddleware ( ... tReq ) ; const resMiddleware = composeMiddleware ( ... tRes ) ; const req = reqMiddleware ( this . buildPayload ( method , params ) ) ; return performRPC ( req , resMiddleware ) ; } sendBatch < P extends any [ ] , R = any , E = string > ( method , params ) { const [ tReq , tRes ] = this . getMiddleware ( method ) ; const reqMiddleware = composeMiddleware ( ... tReq ) ; const resMiddleware = composeMiddleware ( ... tRes ) ; const batchPayload = this . buildBatchPayload ( method , params ) ; const req = reqMiddleware ( batchPayload ) ; return performBatchRPC ( req , resMiddleware ) ; } subscribe ( event , subscriber ) { throw new Error ( 'HTTPProvider does not support subscriptions.' ) ; } unsubscribe ( token ) { throw new Error ( 'HTTPProvider does not support subscriptions.' ) ; } }