Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Created April 25, 2019 02:44
Show Gist options
  • Save alexytiger/633653802523152bcec2205ec1161dcd to your computer and use it in GitHub Desktop.
Save alexytiger/633653802523152bcec2205ec1161dcd to your computer and use it in GitHub Desktop.
import { Injectable, Inject } from '@angular/core';
import { Observable, of, from } from 'rxjs';
import { map, tap, catchError } from 'rxjs/operators';
import { Provider } from './tokens';
import { ethers, Signer } from 'ethers';
@Injectable({
providedIn: 'root'
})
export class ProviderService {
private signer: Signer;
constructor(private provider: Provider) {
this.signer = provider.getSigner();
}
public getAccount(): Observable<string> {
return from(this.signer.getAddress()).pipe(
tap(address => console.log('address', address))
);
}
public getBalance(): Observable<string> {
return from(this.provider.getBalance(this.signer.getAddress())).pipe(
tap(wei_balance => console.log('wei balance', wei_balance)),
map(wei_balance => ethers.utils.formatEther(wei_balance)),
tap(balance => console.log('eth balance', balance)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment