# CICADA

### File

`src/app/_services/token.service.ts`

### Index

###### Properties

- load
- registry
- tokenRegistry
- tokens
- Private tokensList
- tokensSubject

###### Methods

- addToken
- Async getTokenBalance
- Async getTokenByAddress
- Async getTokenBySymbol
- Async getTokenName
- Async getTokens
- Async getTokenSymbol
- Async init

### Constructor

`constructor()`
Defined in src/app/_services/token.service.ts:19

### Methods

addToken
`addToken(token: Token)`
Defined in src/app/_services/token.service.ts:31

Parameters :

Name | Type | Optional
token | `Token` | No

Returns : `void`

Async getTokenBalance
`getTokenBalance(address: string)`
Defined in src/app/_services/token.service.ts:72

Parameters :

Name | Type | Optional
address | `string` | No

Returns : `Promise<Promise<number>>`

Async getTokenByAddress
`getTokenByAddress(address: string)`
Defined in src/app/_services/token.service.ts:51

Parameters :

Name | Type | Optional
address | `string` | No

Returns : `Promise<Token>`

Async getTokenBySymbol
`getTokenBySymbol(symbol: string)`
Defined in src/app/_services/token.service.ts:62

Parameters :

Name | Type | Optional
symbol | `string` | No

Returns : `Promise<Observable<Token>>`

Async getTokenName
`getTokenName()`
Defined in src/app/_services/token.service.ts:77
Returns : `Promise<string>`

Async getTokens
`getTokens()`
Defined in src/app/_services/token.service.ts:43
Returns : `Promise<void>`

Async getTokenSymbol
`getTokenSymbol()`
Defined in src/app/_services/token.service.ts:82
Returns : `Promise<string>`

Async init
`init()`
Defined in src/app/_services/token.service.ts:23
Returns : `Promise<void>`

### Properties

load
Type : `BehaviorSubject<any>`
Default value : `new BehaviorSubject<any>(false)`
Defined in src/app/_services/token.service.ts:19

registry
Type : `CICRegistry`
Defined in src/app/_services/token.service.ts:12

tokenRegistry
Type : `TokenRegistry`
Defined in src/app/_services/token.service.ts:13

tokens
Type : `Array<Token>`
Default value : `[]`
Defined in src/app/_services/token.service.ts:14

Private tokensList
Type : `BehaviorSubject<Array<Token>>`
Default value : `new BehaviorSubject<Array<Token>>( this.tokens )`
Defined in src/app/_services/token.service.ts:15

tokensSubject
Type : `Observable<Array<Token>>`
Default value : `this.tokensList.asObservable()`
Defined in src/app/_services/token.service.ts:18

    import { Injectable } from '@angular/core';
    import { CICRegistry } from '@cicnet/cic-client';
    import { TokenRegistry } from '@app/_eth';
    import { RegistryService } from '@app/_services/registry.service';
    import { Token } from '@app/_models';
    import { BehaviorSubject, Observable, Subject } from 'rxjs';

    @Injectable({
      providedIn: 'root',
    })
    export class TokenService {
      registry: CICRegistry;
      tokenRegistry: TokenRegistry;
      tokens: Array<Token> = [];
      private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
        this.tokens
      );
      tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
      load: BehaviorSubject<any> = new BehaviorSubject<any>(false);

      constructor() {}

      async init(): Promise<void> {
        this.registry = await RegistryService.getRegistry();
        this.tokenRegistry = new TokenRegistry(
          await this.registry.getContractAddressByName('TokenRegistry')
        );
        this.load.next(true);
      }

      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> {
        const count: number = await this.tokenRegistry.totalTokens();
        for (let i = 0; i < count; i++) {
          const token: Token = await this.getTokenByAddress(await this.tokenRegistry.entry(i));
          this.addToken(token);
        }
      }

      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;
      }

      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();
      }

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

      async getTokenName(): Promise<string> {
        const token = await this.registry.addToken(await this.tokenRegistry.entry(0));
        return await token.methods.name().call();
      }

      async getTokenSymbol(): Promise<string> {
        const token = await this.registry.addToken(await this.tokenRegistry.entry(0));
        return await token.methods.symbol().call();
      }
    }

# result-matching ""

# No results matching ""