Created
April 12, 2018 10:59
-
-
Save GrandSchtroumpf/b39acde9b6740805f0a897780419cc45 to your computer and use it in GitHub Desktop.
Get the current account or set it if there is no current account
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
/** Get the current account */ | |
public currentAccount(): Observable<string | Error> { | |
if (this.web3.eth.defaultAccount) { | |
return of(this.web3.eth.defaultAccount); | |
} else { | |
return this.getAccounts().pipe( | |
tap((accounts: string[]) => { | |
if (accounts.length === 0) { throw new Error('No accounts available'); } | |
}), | |
map((accounts: string[]) => accounts[0]), | |
tap((account: string) => this.web3.eth.defaultAccount = account), | |
catchError((err: Error) => of(err)) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment