Skip to content

Instantly share code, notes, and snippets.

@GrandSchtroumpf
Created September 2, 2018 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GrandSchtroumpf/8e68461b6a6b861fc7cf106ce23d8592 to your computer and use it in GitHub Desktop.
Save GrandSchtroumpf/8e68461b6a6b861fc7cf106ce23d8592 to your computer and use it in GitHub Desktop.
The provider service to manage RPC calls
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