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

44 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import {environment} from '@src/environments/environment';
import {BehaviorSubject, Observable} from 'rxjs';
import {CICRegistry} from 'cic-client';
2021-04-20 10:28:40 +02:00
import {TokenRegistry} from '@app/_eth';
2021-03-21 12:02:18 +01:00
import {HttpClient} from '@angular/common/http';
2021-04-20 10:28:40 +02:00
import {RegistryService} from '@app/_services/registry.service';
@Injectable({
providedIn: 'root'
})
export class TokenService {
2021-04-20 10:28:40 +02:00
registry: CICRegistry;
tokenRegistry: TokenRegistry;
2020-12-05 07:29:18 +01:00
tokens: any = '';
private tokensList = new BehaviorSubject<any>(this.tokens);
tokensSubject = this.tokensList.asObservable();
2020-12-05 07:29:18 +01:00
constructor(
2021-03-21 12:02:18 +01:00
private httpClient: HttpClient,
2021-04-20 10:28:40 +02:00
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'));
};
}
async getTokens(): Promise<any> {
2021-04-20 10:28:40 +02:00
const count = await this.tokenRegistry.totalTokens();
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();
}
}