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

41 lines
1.5 KiB
TypeScript
Raw Normal View History

import { EventEmitter, Injectable } from '@angular/core';
2021-05-10 18:15:25 +02:00
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({
2021-05-10 18:15:25 +02:00
providedIn: 'root',
})
export class TokenService {
2021-04-20 10:28:40 +02:00
registry: CICRegistry;
tokenRegistry: TokenRegistry;
LoadEvent: EventEmitter<number> = new EventEmitter<number>();
2021-05-10 18:15:25 +02:00
constructor(private httpClient: HttpClient, private registryService: RegistryService) {
2021-05-18 11:38:27 +02:00
this.registry = registryService.registry;
2021-04-20 10:28:40 +02:00
this.registry.onload = async (address: string): Promise<void> => {
2021-05-10 18:15:25 +02:00
this.tokenRegistry = new TokenRegistry(
await this.registry.getContractAddressByName('TokenRegistry')
);
this.LoadEvent.next(Date.now());
2021-04-20 10:28:40 +02:00
};
}
async getTokens(): Promise<Array<Promise<string>>> {
const count: number = await this.tokenRegistry.totalTokens();
2021-05-10 18:15:25 +02:00
return Array.from({ length: count }, async (v, i) => await this.tokenRegistry.entry(i));
2020-12-05 07:29:18 +01:00
}
getTokenBySymbol(symbol: string): Observable<any> {
2021-03-21 12:02:18 +01:00
return this.httpClient.get(`${environment.cicCacheUrl}/tokens/${symbol}`);
}
async getTokenBalance(address: string): Promise<number> {
2021-04-20 10:28:40 +02:00
const sarafuToken = await this.registry.addToken(await this.tokenRegistry.entry(0));
return await sarafuToken.methods.balanceOf(address).call();
}
}