Created
April 25, 2019 02:44
-
-
Save alexytiger/633653802523152bcec2205ec1161dcd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { 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