Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active April 27, 2019 01:10
Show Gist options
  • Save alexytiger/00f811eec5ce2f9b8ef28db766f12afa to your computer and use it in GitHub Desktop.
Save alexytiger/00f811eec5ce2f9b8ef28db766f12afa to your computer and use it in GitHub Desktop.
import { Injectable, Inject } from '@angular/core';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of, from, EMPTY as empty } from 'rxjs';
import { exhaustMap, switchMap, map, tap, catchError } from 'rxjs/operators';
import { MetamaskWeb3Provider } from '../../services/tokens';
import { ProviderService } from '../../services/provider.services';
import { Web3ProviderActions, SpinnerActions, ErrorActions } from '../actions';
@Injectable()
export class Web3ProviderEffects {
constructor(
@Inject(MetamaskWeb3Provider) private web3Provider,
private providerSrv: ProviderService,
private readonly actions$: Actions
) {}
metaMaskEnable$ = createEffect(() =>
this.actions$.pipe(
ofType(Web3ProviderActions.init),
exhaustMap(() => {
if ('enable' in this.web3Provider) {
return from(this.web3Provider.enable()).pipe(
tap((ethAccounts: string[]) =>
console.log(
'Ethereum provider has been granted access to the following accounts',
ethAccounts
)
),
map((ethAccounts: string[]) => {
if (ethAccounts.length === 0) {
return ErrorActions.errorMessage({
errorMsg: 'Can not get any user accounts'
});
}
return Web3ProviderActions.initSuccess();
}),
// User denied account access
catchError((err: Error) =>
of(ErrorActions.errorMessage({ errorMsg: err.message }))
)
);
}
return empty;
})
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment