2021-06-04 08:23:05 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-06-10 17:40:45 +02:00
|
|
|
import { CICRegistry } from '@cicnet/cic-client';
|
2021-06-04 08:23:05 +02:00
|
|
|
import { TokenRegistry } from '@app/_eth';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { RegistryService } from '@app/_services/registry.service';
|
|
|
|
import { Token } from '@app/_models';
|
2021-06-09 17:04:29 +02:00
|
|
|
import { BehaviorSubject, Observable, Subject } from 'rxjs';
|
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-06-04 10:01:20 +02:00
|
|
|
tokens: Array<Token> = [];
|
2021-06-09 17:04:29 +02:00
|
|
|
private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
|
|
|
|
this.tokens
|
|
|
|
);
|
2021-06-04 10:01:20 +02:00
|
|
|
tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
|
2021-06-09 17:04:29 +02:00
|
|
|
load: BehaviorSubject<any> = new BehaviorSubject<any>(false);
|
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-06-10 17:40:45 +02:00
|
|
|
this.tokenRegistry = new TokenRegistry(
|
|
|
|
await this.registry.getContractAddressByName('TokenRegistry')
|
|
|
|
);
|
|
|
|
this.load.next(true);
|
2021-04-20 10:28:40 +02:00
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-06-04 10:01:20 +02:00
|
|
|
addToken(token: Token): void {
|
|
|
|
const savedIndex = this.tokens.findIndex((tk) => tk.address === token.address);
|
|
|
|
if (savedIndex === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (savedIndex > 0) {
|
|
|
|
this.tokens.splice(savedIndex, 1);
|
|
|
|
}
|
|
|
|
this.tokens.unshift(token);
|
|
|
|
this.tokensList.next(this.tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTokens(): Promise<void> {
|
2021-04-29 19:10:39 +02:00
|
|
|
const count: number = await this.tokenRegistry.totalTokens();
|
2021-06-01 19:34:41 +02:00
|
|
|
for (let i = 0; i < count; i++) {
|
2021-06-04 08:23:05 +02:00
|
|
|
const token: Token = await this.getTokenByAddress(await this.tokenRegistry.entry(i));
|
2021-06-04 10:01:20 +02:00
|
|
|
this.addToken(token);
|
2021-06-01 19:34:41 +02:00
|
|
|
}
|
2020-12-05 07:29:18 +01:00
|
|
|
}
|
|
|
|
|
2021-06-04 08:23:05 +02:00
|
|
|
async getTokenByAddress(address: string): Promise<Token> {
|
|
|
|
const token: any = {};
|
|
|
|
const tokenContract = await this.registry.addToken(address);
|
|
|
|
token.address = address;
|
|
|
|
token.name = await tokenContract.methods.name().call();
|
|
|
|
token.symbol = await tokenContract.methods.symbol().call();
|
|
|
|
token.supply = await tokenContract.methods.totalSupply().call();
|
|
|
|
token.decimals = await tokenContract.methods.decimals().call();
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2021-06-04 10:01:20 +02:00
|
|
|
async getTokenBySymbol(symbol: string): Promise<Observable<Token>> {
|
|
|
|
const tokenSubject: Subject<Token> = new Subject<Token>();
|
|
|
|
await this.getTokens();
|
|
|
|
this.tokensSubject.subscribe((tokens) => {
|
|
|
|
const queriedToken = tokens.find((token) => token.symbol === symbol);
|
|
|
|
tokenSubject.next(queriedToken);
|
|
|
|
});
|
|
|
|
return tokenSubject.asObservable();
|
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-06-10 17:40:45 +02:00
|
|
|
const token = await this.registry.addToken(await this.tokenRegistry.entry(0));
|
2021-06-09 17:04:29 +02:00
|
|
|
return await token.methods.balanceOf(address).call();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTokenName(): Promise<string> {
|
2021-06-10 17:40:45 +02:00
|
|
|
const token = await this.registry.addToken(await this.tokenRegistry.entry(0));
|
2021-06-09 17:04:29 +02:00
|
|
|
return await token.methods.name().call();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTokenSymbol(): Promise<string> {
|
2021-06-10 17:40:45 +02:00
|
|
|
const token = await this.registry.addToken(await this.tokenRegistry.entry(0));
|
2021-06-09 17:04:29 +02:00
|
|
|
return await token.methods.symbol().call();
|
2021-03-10 10:47:01 +01:00
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|