Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active April 27, 2019 13:47
Show Gist options
  • Save alexytiger/6386c6a6ceab5e3f17ad3812de1bef90 to your computer and use it in GitHub Desktop.
Save alexytiger/6386c6a6ceab5e3f17ad3812de1bef90 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
) {}
getAccount$ = createEffect(() =>
this.actions$.pipe(
ofType(Web3ProviderActions.initSuccess),
switchMap(() =>
this.providerSrv.getAccount().pipe(
map((address: string) => Web3ProviderActions.account({ address })),
catchError((err: Error) =>
of(ErrorActions.errorMessage({ errorMsg: err.message }))
)
)
)
)
);
getBalance$ = createEffect(() =>
this.actions$.pipe(
ofType(Web3ProviderActions.initSuccess),
switchMap(() =>
this.providerSrv.getBalance().pipe(
map((balance: string) =>
Web3ProviderActions.balanceSuccess({ balance })
),
catchError((err: Error) =>
of(ErrorActions.errorMessage({ errorMsg: err.message }))
)
)
)
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment