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';
|
2020-11-25 08:51:15 +01:00
|
|
|
|
|
|
|
@Injectable({
|
2021-05-10 18:15:25 +02:00
|
|
|
providedIn: 'root',
|
2020-11-25 08:51:15 +01:00
|
|
|
})
|
|
|
|
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;
|
2020-11-25 08:51:15 +01:00
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-04-29 19:10:39 +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
|
|
|
}
|
|
|
|
|
2021-03-14 09:19:25 +01:00
|
|
|
getTokenBySymbol(symbol: string): Observable<any> {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicCacheUrl}/tokens/${symbol}`);
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
2021-03-10 10:47:01 +01:00
|
|
|
|
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));
|
2021-03-10 10:47:01 +01:00
|
|
|
return await sarafuToken.methods.balanceOf(address).call();
|
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|