From d94f7e8e1553551dc8d830c4f9bdd50b28fd960b Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Fri, 4 Jun 2021 09:23:05 +0300 Subject: [PATCH] Add a dedicated get token by address method. --- src/app/_services/token.service.ts | 34 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/app/_services/token.service.ts b/src/app/_services/token.service.ts index f4ee1fb..f5b69f1 100644 --- a/src/app/_services/token.service.ts +++ b/src/app/_services/token.service.ts @@ -1,9 +1,9 @@ -import {Injectable} from '@angular/core'; -import {CICRegistry} from 'cic-client'; -import {TokenRegistry} from '@app/_eth'; -import {HttpClient} from '@angular/common/http'; -import {RegistryService} from '@app/_services/registry.service'; -import {Token} from '@app/_models'; +import { Injectable } from '@angular/core'; +import { CICRegistry } from 'cic-client'; +import { TokenRegistry } from '@app/_eth'; +import { HttpClient } from '@angular/common/http'; +import { RegistryService } from '@app/_services/registry.service'; +import { Token } from '@app/_models'; @Injectable({ providedIn: 'root', @@ -29,22 +29,26 @@ export class TokenService { const count: number = await this.tokenRegistry.totalTokens(); const tokens: Array = []; for (let i = 0; i < count; i++) { - const token: any = {}; - token.address = await this.tokenRegistry.entry(i); - const tokenContract = await this.registry.addToken(token.address); - token.name = await tokenContract.methods.name().call(); - token.symbol = await tokenContract.methods.symbol().call(); - token.address = await this.tokenRegistry.entry(i); - token.supply = await tokenContract.methods.totalSupply().call(); - token.decimals = await tokenContract.methods.decimals().call(); + const token: Token = await this.getTokenByAddress(await this.tokenRegistry.entry(i)); tokens.push(token); } return tokens; } + 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 tokens: Array = await this.getTokens(); - return tokens.find(token => token.symbol === symbol); + return tokens.find((token) => token.symbol === symbol); } async getTokenBalance(address: string): Promise<(address: string) => Promise> {