Skip to content

Instantly share code, notes, and snippets.

import { Injectable } from '@angular/core';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { tap, map } from 'rxjs/operators';
import { ErrorActions } from '../actions';
@Injectable()
export class ErrorEffects {
constructor(private readonly actions$: Actions) {}
handleError$ = createEffect(
export const reducers: ActionReducerMap<AppState> = {
router: routerReducer,
spinner: fromSpinner.reducer,
error: fromError.reducer,
web3Provider: fromWeb3Provider.reducer,
};
import { Injectable, InjectionToken, Inject } from '@angular/core';
import { providers } from 'ethers';
export const MetamaskWeb3Provider = new InjectionToken('Metamask Web3 provider', {
providedIn: 'root',
factory: () => (window as any).ethereum
});
@Injectable({ providedIn: 'root' })
export class Provider extends providers.Web3Provider {
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';
export interface Web3ProviderState {
metamaskEnable: boolean;
account: string;
balance: string;
}
@alexytiger
alexytiger / eth-init.guard.ts
Last active December 30, 2019 01:14
e-book
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { take, tap, filter, switchMap, catchError } from 'rxjs/operators';
import * as fromRoot from '../store';
@Injectable({
providedIn: 'root',
})
export const init = createAction('[Web3/Provider] Init');
export const initSuccess = createAction('[Web3/Provider] Init Success');
export const account = createAction('[Web3/Provider] Account', props<{ address: string }>());
export const balance = createAction('[Web3/Provider] Balance');
export const balanceSuccess = createAction('[Web3/Provider] Balance Success', props<{ balance: string }>());
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';
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 const ipfsToken = new InjectionToken('The IPFS Token', {
providedIn: 'root',
factory: () => {
try {
return new (window as any).IpfsHttpClient('ipfs.infura.io', '5001', {
protocol: 'https'
});
} catch (err) {
console.log('Error', err);
throw new Error('Unable to access IPFS node daemon on Infura network');