Created
September 2, 2018 13:15
-
-
Save GrandSchtroumpf/8e68461b6a6b861fc7cf106ce23d8592 to your computer and use it in GitHub Desktop.
The provider service to manage RPC calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Inject } from '@angular/core'; | |
import { URL } from './provider.module'; | |
@Injectable({ providedIn: 'root' }) | |
export class Provider { | |
private rpcId: number; | |
constructor(@Inject(URL) private url: string) {} | |
/** JSON RPC Request */ | |
private req(method: string, params?: any[]) { | |
return { | |
jsonrpc: '2.0', | |
id: this.rpcId, | |
method: method, | |
params: params || [] | |
}; | |
} | |
/** Send a request to the node */ | |
public rpc<T>(method: string, params?: any[]): Observable<T> { | |
const payload = this.req(method, params); | |
this.rpcId++; | |
return this.http.post(this.url, payload).pipe( | |
map(res => { | |
if (res.error) throw res.error; | |
return res.result; | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment