import { Injectable } from '@angular/core'; import { CICRegistry } from '@cicnet/cic-client'; import { TokenRegistry } from '@app/_eth'; import { RegistryService } from '@app/_services/registry.service'; import { Token } from '@app/_models'; import { BehaviorSubject, Observable, Subject } from 'rxjs'; @Injectable({ providedIn: 'root', }) export class TokenService { registry: CICRegistry; tokenRegistry: TokenRegistry; tokens: Array = []; private tokensList: BehaviorSubject> = new BehaviorSubject>( this.tokens ); tokensSubject: Observable> = this.tokensList.asObservable(); load: BehaviorSubject = new BehaviorSubject(false); constructor() {} async init(): Promise { this.registry = await RegistryService.getRegistry(); this.tokenRegistry = await RegistryService.getTokenRegistry(); this.load.next(true); } addToken(token: Token): void { const savedIndex = this.tokens.findIndex((tk) => tk.address === token.address); if (savedIndex === 0) { return; } if (savedIndex > 0) { this.tokens.splice(savedIndex, 1); } this.tokens.unshift(token); this.tokensList.next(this.tokens); } async getTokens(): Promise { const count: number = await this.tokenRegistry.totalTokens(); for (let i = 0; i < count; i++) { const token: Token = await this.getTokenByAddress(await this.tokenRegistry.entry(i)); this.addToken(token); } } async getTokenByAddress(address: string): Promise { const token: any = {}; const tokenContract = await this.registry.addToken(address); token.address = address; token.name = await tokenContract.methods.name().call(); token.symbol = await tokenContract.methods.symbol().call(); token.supply = await tokenContract.methods.totalSupply().call(); token.decimals = await tokenContract.methods.decimals().call(); return token; } async getTokenBySymbol(symbol: string): Promise> { const tokenSubject: Subject = new Subject(); await this.getTokens(); this.tokensSubject.subscribe((tokens) => { const queriedToken = tokens.find((token) => token.symbol === symbol); tokenSubject.next(queriedToken); }); return tokenSubject.asObservable(); } async getTokenBalance(address: string): Promise<(address: string) => Promise> { const token = await this.registry.addToken(await this.tokenRegistry.entry(0)); return await token.methods.balanceOf(address).call(); } async getTokenName(): Promise { const token = await this.registry.addToken(await this.tokenRegistry.entry(0)); return await token.methods.name().call(); } async getTokenSymbol(): Promise { const token = await this.registry.addToken(await this.tokenRegistry.entry(0)); return await token.methods.symbol().call(); } }