43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { EventEmitter, Injectable } from '@angular/core';
|
|
import {environment} from '@src/environments/environment';
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
|
import {CICRegistry} from 'cic-client';
|
|
import {TokenRegistry} from '@app/_eth';
|
|
import {HttpClient} from '@angular/common/http';
|
|
import {RegistryService} from '@app/_services/registry.service';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class TokenService {
|
|
registry: CICRegistry;
|
|
tokenRegistry: TokenRegistry;
|
|
LoadEvent: EventEmitter<number> = new EventEmitter<number>();
|
|
|
|
constructor(
|
|
private httpClient: HttpClient,
|
|
private registryService: RegistryService,
|
|
) {
|
|
this.registry = registryService.getRegistry();
|
|
this.registry.load();
|
|
this.registry.onload = async (address: string): Promise<void> => {
|
|
this.tokenRegistry = new TokenRegistry(await this.registry.getContractAddressByName('TokenRegistry'));
|
|
this.LoadEvent.next(Date.now());
|
|
};
|
|
}
|
|
|
|
async getTokens(): Promise<Array<Promise<string>>> {
|
|
const count: number = await this.tokenRegistry.totalTokens();
|
|
return Array.from({length: count}, async (v, i) => await this.tokenRegistry.entry(i));
|
|
}
|
|
|
|
getTokenBySymbol(symbol: string): Observable<any> {
|
|
return this.httpClient.get(`${environment.cicCacheUrl}/tokens/${symbol}`);
|
|
}
|
|
|
|
async getTokenBalance(address: string): Promise<number> {
|
|
const sarafuToken = await this.registry.addToken(await this.tokenRegistry.entry(0));
|
|
return await sarafuToken.methods.balanceOf(address).call();
|
|
}
|
|
}
|