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

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-05-18 12:24:41 +02:00
import { Injectable } from '@angular/core';
2021-05-10 18:15:25 +02:00
import { environment } from '@src/environments/environment';
2021-05-18 12:24:41 +02:00
import { Observable } from 'rxjs';
2021-05-10 18:15:25 +02:00
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;
2021-05-18 12:24:41 +02:00
onload: (status: boolean) => void;
2021-05-19 18:57:10 +02:00
constructor(private httpClient: HttpClient) {}
async init(): Promise<void> {
this.registry = await RegistryService.getRegistry();
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')
);
2021-05-18 12:24:41 +02:00
this.onload(this.tokenRegistry !== undefined);
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}`);
}
2021-05-18 12:24:41 +02:00
async getTokenBalance(address: string): Promise<(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();
}
}