Skip to content

Commit

Permalink
#7 allowed networks are kovan and mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzhak committed Oct 25, 2018
1 parent dbdba06 commit ee10cf3
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { MetamaskNotAllowedNetworkComponent } from './metamask-not-allowed-network';
import { MetamaskNotInstalledComponent } from './metamask-not-installed';
import { MetamaskNotLoggedInComponent } from './metamask-not-logged-in';
import { PageNotFoundComponent } from './page-not-found';
Expand All @@ -11,6 +12,10 @@ const routes: Routes = [
redirectTo: '/dashboard/home',
pathMatch: 'full'
},
{
path: 'metamask-not-allowed-network',
component: MetamaskNotAllowedNetworkComponent
},
{
path: 'metamask-not-installed',
component: MetamaskNotInstalledComponent
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { MessageService } from 'primeng/api';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DashboardModule } from './dashboard';
import { MetamaskNotAllowedNetworkComponent } from './metamask-not-allowed-network';
import { MetamaskNotInstalledComponent } from './metamask-not-installed';
import { MetamaskNotLoggedInComponent } from './metamask-not-logged-in';
import { PageNotFoundComponent } from './page-not-found';

@NgModule({
declarations: [
AppComponent,
MetamaskNotAllowedNetworkComponent,
MetamaskNotInstalledComponent,
MetamaskNotLoggedInComponent,
PageNotFoundComponent
Expand Down
3 changes: 3 additions & 0 deletions src/app/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class DashboardComponent implements OnInit {
if(errorCode == this.web3Service.CONNECTION_NOT_LOGGED_IN) {
this.router.navigate(['metamask-not-logged-in']);
}
if(errorCode == this.web3Service.CONNECTION_NOT_ALLOWED_NETWORK) {
this.router.navigate(['metamask-not-allowed-network']);
}
}
},
(err) => {
Expand Down
1 change: 1 addition & 0 deletions src/app/metamask-not-allowed-network/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MetamaskNotAllowedNetworkComponent } from './metamask-not-allowed-network.component';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div style="width: 100%; height: 100%; background: #393836; position: fixed; left: 0; top: 0;">
<span style="display: flex; height: 100%; justify-content: center; align-items: center;">
<span style="color: #fff; font-size: 1.5em;">Please switch your MetaMask to Kovan or Mainnet</span>
</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { MetamaskNotAllowedNetworkComponent } from './metamask-not-allowed-network.component';

describe('MetamaskNotAllowedNetworkComponent', () => {
let component: MetamaskNotAllowedNetworkComponent;
let fixture: ComponentFixture<MetamaskNotAllowedNetworkComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MetamaskNotAllowedNetworkComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(MetamaskNotAllowedNetworkComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-metamask-not-allowed-network',
templateUrl: './metamask-not-allowed-network.component.html',
styleUrls: ['./metamask-not-allowed-network.component.css']
})
export class MetamaskNotAllowedNetworkComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}
19 changes: 15 additions & 4 deletions src/app/shared/services/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Web3Service {
CONNECTION_NO_ERROR = 0;
CONNECTION_NO_PROVIDER = 1;
CONNECTION_NOT_LOGGED_IN = 2;
CONNECTION_NOT_ALLOWED_NETWORK = 3;

constructor() { }

Expand Down Expand Up @@ -95,10 +96,20 @@ export class Web3Service {
return Observable.create((observer) => {
this.getAccounts().subscribe(
(accounts) => {
observer.next(accounts.length > 0 ? this.CONNECTION_NO_ERROR : this.CONNECTION_NOT_LOGGED_IN);
},
(err) => { observer.error(err); },
() => { observer.complete(); }
if(accounts.length == 0) {
observer.next(this.CONNECTION_NOT_LOGGED_IN);
} else {
// check that user is connected to the allowed network
this.getNetwork().subscribe(
networkName => {
const allowedNetworks = ["main", "kovan", "private"];
observer.next(allowedNetworks.includes(networkName) ? this.CONNECTION_NO_ERROR : this.CONNECTION_NOT_ALLOWED_NETWORK);
},
(err) => { observer.error(err); },
() => { observer.complete(); }
);
}
}
);
});
}
Expand Down

0 comments on commit ee10cf3

Please sign in to comment.