cic-staff-client/src/app/_services/token.service.ts

55 lines
1.9 KiB
TypeScript

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',
})
export class TokenService {
registry: CICRegistry;
tokenRegistry: TokenRegistry;
onload: (status: boolean) => void;
constructor(private httpClient: HttpClient) {}
async init(): Promise<void> {
this.registry = await RegistryService.getRegistry();
this.registry.onload = async (address: string): Promise<void> => {
this.tokenRegistry = new TokenRegistry(
await this.registry.getContractAddressByName('TokenRegistry')
);
this.onload(this.tokenRegistry !== undefined);
};
}
async getTokens(): Promise<Array<Token>> {
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();
tokens.push(token);
}
return tokens;
}
async getTokenBySymbol(symbol: string): Promise<Token> {
const tokens: Array<Token> = await this.getTokens();
return tokens.find(token => token.symbol === symbol);
}
async getTokenBalance(address: string): Promise<(address: string) => Promise<number>> {
const sarafuToken = await this.registry.addToken(await this.tokenRegistry.entry(0));
return await sarafuToken.methods.balanceOf(address).call();
}
}