import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/distinctUntilChanged' ; import 'rxjs/add/operator/share' ; import { Web3Service } from '../../web3.service' ; import { SmartContract , emptyPromise } from '../smart-contract.abstract' ; interface BasicTokenConstants { name : string ; symbol : string ; decimals : number ; totalSupply : number ; balanceOf : number ; } interface BasicTokenActions { transfer : boolean ; } interface BasicTokenEvents { Transfer : { from : string ; to : string ; value : number ; } ; } export type BasicToken = BasicTokenActions & BasicTokenConstants & BasicTokenEvents ; @ Injectable ( ) export class BasicTokenMixin extends SmartContract < BasicTokenConstants , { } , BasicTokenActions , BasicTokenEvents > { get Transfer$ ( ) { return this . generateEventObservable ( 'Transfer' ) ; } get name ( ) { return ( ) => this . generateConstant ( 'name' ) ( ) ; } get symbol ( ) { return ( ) => this . generateConstant ( 'symbol' ) ( ) ; } get decimals ( ) { return ( ) => this . generateConstant ( 'decimals' , this . n ) ( ) ; } get totalSupply ( ) { return ( ) => this . generateConstant ( 'totalSupply' , this . n ) ( ) ; } get balanceOf ( ) { return ( address ) => this . generateConstant ( 'balanceOf' , this . n ) ( address ) ; } get transfer ( ) { return ( to , value ) => this . generateAction ( 'transfer' ) ( to , value ) ; } constructor ( protected web3Service ) { super ( web3Service , undefined ) ; } getTokenInfo ( ) { return this . checkUpdates ( async ( contract ) => ( { name : await ( ( < any > contract ) . name || emptyPromise ) ( ) , symbol : await ( ( < any > contract ) . symbol || emptyPromise ) ( ) , decimals : await ( ( < any > contract ) . decimals || emptyPromise ) ( ) , } ) ) . distinctUntilChanged ( ( a , b ) => JSON . stringify ( a ) === JSON . stringify ( b ) ) . share ( ) ; } async myBalance ( ) { const account = await this . web3Service . getAccount ( ) . toPromise ( ) ; return await this . balanceOf ( account ) ; } }