Add a dedicated get token by address method.

This commit is contained in:
Spencer Ofwiti 2021-06-04 09:23:05 +03:00
parent 701605be31
commit d94f7e8e15
1 changed files with 19 additions and 15 deletions

View File

@ -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<Token> = [];
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<Token> {
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<Token> {
const tokens: Array<Token> = 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<number>> {