Skip to content

Instantly share code, notes, and snippets.

import { Injectable } from '@angular/core';
import { createEffect } from '@ngrx/effects';
import { tap, map } from 'rxjs/operators';
import * as fromStore from '../reducers';
import { Store, select } from '@ngrx/store';
import { SpinnerOverlayService } from '../../services/spinner-overlay.service';
@Injectable()
export class SpinnerEffects {
import { Injectable } from '@angular/core';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { MatSpinner } from '@angular/material';
import { Observable, Subject } from 'rxjs';
import { scan, map, distinctUntilChanged } from 'rxjs/operators';
@Injectable({
providedIn: 'root',
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',
})
metaMaskEnabled$ = createEffect(() =>
this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
map(() => {
const ethereum = (window as any).ethereum;
// Returns true or false, representing whether the user has MetaMask installed.
if (!ethereum || !ethereum.isMetaMask) {
return ErrorActions.errorMessage({ errorMsg: `Please install MetaMask.` });
}
accountChanged$ = createEffect(
() =>
fromEvent(this.web3Token, 'accountsChanged').pipe(
withLatestFrom(this.store$.pipe(select(fromStore.getAccount))),
filter(([accounts, currentAccount]) => !!currentAccount && (currentAccount !== accounts[0])),
map(([accounts, currentAccount]) => {
console.log('new account', accounts[0]);
// to reload browser
// based onhttps://medium.com/metamask/no-longer-reloading-pages-on-network-change-fbf041942b44
this.document.location.reload();
getAccountInfo$ = createEffect(() =>
this.actions$.pipe(
ofType(Web3ProviderActions.metamaskConnectSuccess),
switchMap(() => {
return [Web3ProviderActions.getNetwork(), Web3ProviderActions.getAddress(), Web3ProviderActions.getBalance()];
})
)
);
getAddress$ = createEffect(() =>
public confirmDelivery(contractAddress: string): Observable<string> {
const contract: Contract = new ethers.Contract(contractAddress,
this.abi, this.provider.getSigner());
const token = contract.buyerConfirmReceived();
return from(token)
.pipe(
switchMap((tx: any) => {
confirmDelivery$ = createEffect(
() =>
this.actions$.pipe(
ofType(PurchaseContractActions.confirmDelivery),
withLatestFrom(this.store$.pipe(select(fromStore.getSelectedPurchaseContract))),
switchMap(([payload, contract]) => {
const dialogConfig = new MatDialogConfig();
dialogConfig.width = '420px';
dialogConfig.disableClose = true;
public confirmPurchase(contractAddress: string,
etherValue: string): Observable<string> {
const contract: Contract =
new ethers.Contract(contractAddress, this.abi,
this.provider.getSigner());
const wei = utils.parseEther(etherValue);
const token = contract.buyerPurchase({
value: wei
confirmBuy$ = createEffect(
() =>
this.actions$.pipe(
ofType(PurchaseContractActions.confirmBuy),
withLatestFrom(
this.store$.pipe(select(fromStore.getSelectedPurchaseContract))),
switchMap(([payload, contract]) => {
const dialogConfig = new MatDialogConfig();
dialogConfig.width = '420px';