Skip to content

Instantly share code, notes, and snippets.

View GrandSchtroumpf's full-sized avatar
🏠
Working from home

François GrandSchtroumpf

🏠
Working from home
  • Dapps Nation
  • Nantes
View GitHub Profile
@GrandSchtroumpf
GrandSchtroumpf / eth.module.ts
Last active April 12, 2018 10:57
EthModule with web3 provider
import { NgModule, ModuleWithProviders, Type } from '@angular/core';
import { CommonModule } from '@angular/common';
// Web3
import { WEB3 } from './tokens';
const Web3 = require('web3');
// Services
import { AccountsService } from './eth.services';
@NgModule({
@GrandSchtroumpf
GrandSchtroumpf / getAccounts.ts
Created April 12, 2018 10:58
Get accounts wrapper for Angular
/** Return the list of accounts available */
public getAccounts(): Observable<string[]> {
return fromPromise(this.web3.eth.getAccounts());
}
@GrandSchtroumpf
GrandSchtroumpf / currentAccount.ts
Created April 12, 2018 10:59
Get the current account or set it if there is no current account
/** Get the current account */
public currentAccount(): Observable<string | Error> {
if (this.web3.eth.defaultAccount) {
return of(this.web3.eth.defaultAccount);
} else {
return this.getAccounts().pipe(
tap((accounts: string[]) => {
if (accounts.length === 0) { throw new Error('No accounts available'); }
}),
map((accounts: string[]) => accounts[0]),
@GrandSchtroumpf
GrandSchtroumpf / eth.service.ts
Created April 12, 2018 11:02
The Ethereum service to manage accounts
import { Injectable, Inject } from '@angular/core';
// Web3
import { WEB3 } from './tokens';
import Web3 from 'web3';
// RXJS
import { Observable } from 'rxjs/Observable';
import { bindNodeCallback } from 'rxjs/observable/bindNodeCallback';
import { of } from 'rxjs/observable/of';
import { map, tap, catchError } from 'rxjs/operators';
@GrandSchtroumpf
GrandSchtroumpf / web3.token.ts
Created April 12, 2018 11:12
Injectable Token for Web3
import { InjectionToken } from '@angular/core';
import Web3 from ‘web3’;
export const WEB3 = new InjectionToken<Web3>('web3');
@GrandSchtroumpf
GrandSchtroumpf / eth.component.ts
Created April 12, 2018 11:52
The component to interact with Ethereum
import { Component, OnInit } from '@angular/core';
import { EthService } from './ethereum/eth.service';
@Component({
selector: 'app-root',
template: '<p>Current Account: {{address$ | async}}',
styles: []
})
export class AppComponent implements OnInit {
public address$: Observable<string>;
@GrandSchtroumpf
GrandSchtroumpf / eth.component.zone.ts
Created April 12, 2018 11:54
The Angular component with ngZone to interact with Ethereum
import { Component, OnInit, OnDestroy, NgZone } from '@angular/core';
import { EthService } from './ethereum/eth.service';
@Component({
selector: 'app-root',
template: '<p>Current Account: {{ address }}',
styles: []
})
export class AppComponent implements OnInit, OnDestroy {
public address: string;
@GrandSchtroumpf
GrandSchtroumpf / eth.module.ngrx.ts
Created April 12, 2018 11:59
Import ngrx into our module
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { environment } from './../../environments/environment';
// Web3
import { WEB3 } from './tokens';
const Web3 = require('web3');
// Services
import { AccountsService } from './eth.services';
@GrandSchtroumpf
GrandSchtroumpf / eth.actions.ts
Created April 12, 2018 12:09
The NGRX actions to work with Ethereum accounts
import { Action } from '@ngrx/store';
/**
* TYPES
*/
export const GET_ACCOUNTS = '[Eth] Get Accounts';
export const GET_ACCOUNTS_SUCCESS = '[Eth] Get Accounts Success';
export const SELECT_ACCOUNT = '[Eth] Select Account';
@GrandSchtroumpf
GrandSchtroumpf / eth.reducers.ts
Created April 12, 2018 12:13
The NGRX reducers
import { GET_ACCOUNTS_SUCCESS, SELECT_ACCOUNT, AccountsActions } from './eth.actions';
export interface State {
selected: string,
accounts: string[]
}
const initialState: State = {
selected: null,
accounts: []