2021-04-29 19:10:39 +02:00
|
|
|
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';
|
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-04-29 19:10:39 +02:00
|
|
|
LoadEvent: EventEmitter<number> = new EventEmitter<number>();
|
2020-11-25 08:51:15 +01:00
|
|
|
|
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')
|
|
|
|
);
|
2021-04-29 19:10:39 +02:00
|
|
|
this.LoadEvent.next(Date.now());
|
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
|
|
|
|
|
|
|
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));
|
2021-03-10 10:47:01 +01:00
|
|
|
return await sarafuToken.methods.balanceOf(address).call();
|
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|