File

src/app/_services/token.service.ts

Index

Properties
Methods

Constructor

constructor(httpClient: HttpClient, registryService: RegistryService)
Parameters :
Name Type Optional
httpClient HttpClient No
registryService RegistryService No

Methods

Async getTokenBalance
getTokenBalance(address: string)
Parameters :
Name Type Optional
address string No
Returns : Promise<number>
getTokenBySymbol
getTokenBySymbol(symbol: string)
Parameters :
Name Type Optional
symbol string No
Returns : Observable<any>
Async getTokens
getTokens()
Returns : Promise<Array<Promise<string>>>

Properties

LoadEvent
Type : EventEmitter<number>
Default value : new EventEmitter<number>()
registry
Type : CICRegistry
tokenRegistry
Type : TokenRegistry
import { EventEmitter, Injectable } from '@angular/core';
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';

@Injectable({
  providedIn: 'root',
})
export class TokenService {
  registry: CICRegistry;
  tokenRegistry: TokenRegistry;
  LoadEvent: EventEmitter<number> = new EventEmitter<number>();

  constructor(private httpClient: HttpClient, 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')
      );
      this.LoadEvent.next(Date.now());
    };
  }

  async getTokens(): Promise<Array<Promise<string>>> {
    const count: number = await this.tokenRegistry.totalTokens();
    return Array.from({ length: count }, async (v, i) => await this.tokenRegistry.entry(i));
  }

  getTokenBySymbol(symbol: string): Observable<any> {
    return this.httpClient.get(`${environment.cicCacheUrl}/tokens/${symbol}`);
  }

  async getTokenBalance(address: string): Promise<number> {
    const sarafuToken = await this.registry.addToken(await this.tokenRegistry.entry(0));
    return await sarafuToken.methods.balanceOf(address).call();
  }
}

result-matching ""

    No results matching ""