Created
July 31, 2018 19:21
-
-
Save GrandSchtroumpf/c59f9f67665a852763f60a97190318ce to your computer and use it in GitHub Desktop.
A simple appcomponent that print the network on which you are connected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, Inject } from '@angular/core'; | |
import { WEB3 } from './web3'; | |
import Web3 from 'web3'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.scss'] | |
}) | |
export class AppComponent implements OnInit { | |
constructor(@Inject(WEB3) private web3: Web3) {} | |
ngOnInit() { | |
this.web3.eth.net.getId() | |
.then(id => console.log(`You are connected on ${this.getNet(id)}`)); | |
} | |
private getNet(id: number): string { | |
const networks = { | |
1: 'mainnet', | |
3: 'ropsten', | |
4: 'rinkeby', | |
42: 'koven' | |
}; | |
return networks[id]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment