diff --git a/src/app/_services/registry.service.ts b/src/app/_services/registry.service.ts index 07b61b6..ecb076a 100644 --- a/src/app/_services/registry.service.ts +++ b/src/app/_services/registry.service.ts @@ -1,14 +1,10 @@ import { Injectable } from '@angular/core'; import { environment } from '@src/environments/environment'; import { CICRegistry, FileGetter } from '@cicnet/cic-client'; -import { TokenRegistry } from '@app/_eth'; +import { AccountIndex, TokenRegistry } from '@app/_eth'; import { HttpGetter } from '@app/_helpers'; import { Web3Service } from '@app/_services/web3.service'; - -// export interface RegistryCollection { -// cicRegistry: CICRegistry; -// tokenRegistry: TokenRegistry; -// } +import { BehaviorSubject } from 'rxjs'; @Injectable({ providedIn: 'root', @@ -17,7 +13,8 @@ export class RegistryService { static fileGetter: FileGetter = new HttpGetter(); private static registry: CICRegistry; private static tokenRegistry: TokenRegistry; - // private static registries: RegistryCollection; + private static accountRegistry: AccountIndex; + private static load: BehaviorSubject = new BehaviorSubject(false); public static async getRegistry(): Promise { if (!RegistryService.registry) { @@ -30,6 +27,8 @@ export class RegistryService { ); RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); await RegistryService.registry.load(); + RegistryService.load.next(true); + return RegistryService.registry; } return RegistryService.registry; } @@ -38,16 +37,33 @@ export class RegistryService { if (!RegistryService.tokenRegistry) { // then initial it const registry = await RegistryService.getRegistry(); - const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName( - 'TokenRegistry' - ); - RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress); - return new Promise((resolve, reject) => { - resolve(RegistryService.tokenRegistry); + RegistryService.load.subscribe(async (status: boolean) => { + if (status) { + const tokenRegistryAddress = await registry.getContractAddressByName('TokenRegistry'); + RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress); + return RegistryService.tokenRegistry; + } + }); + // return new Promise((resolve, reject) => { + // resolve(RegistryService.tokenRegistry); + // }); + } + return RegistryService.tokenRegistry; + } + + public static async getAccountRegistry(): Promise { + if (!RegistryService.accountRegistry) { + const registry = await RegistryService.getRegistry(); + RegistryService.load.subscribe(async (status: boolean) => { + if (status) { + const accountIndexAddress: string = await registry.getContractAddressByName( + 'AccountRegistry' + ); + RegistryService.accountRegistry = new AccountIndex(accountIndexAddress); + return RegistryService.accountRegistry; + } }); } - return new Promise((resolve, reject) => { - resolve(RegistryService.tokenRegistry); - }); + return RegistryService.accountRegistry; } } diff --git a/src/app/_services/token.service.ts b/src/app/_services/token.service.ts index f219706..ebefece 100644 --- a/src/app/_services/token.service.ts +++ b/src/app/_services/token.service.ts @@ -1,7 +1,6 @@ import { Injectable } from '@angular/core'; import { CICRegistry } from '@cicnet/cic-client'; import { TokenRegistry } from '@app/_eth'; -import { HttpClient } from '@angular/common/http'; import { RegistryService } from '@app/_services/registry.service'; import { Token } from '@app/_models'; import { BehaviorSubject, Observable, Subject } from 'rxjs'; @@ -12,24 +11,19 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs'; export class TokenService { registry: CICRegistry; tokenRegistry: TokenRegistry; - onload: (status: boolean) => void; tokens: Array = []; private tokensList: BehaviorSubject> = new BehaviorSubject>( this.tokens ); tokensSubject: Observable> = this.tokensList.asObservable(); + load: BehaviorSubject = new BehaviorSubject(false); - constructor(private httpClient: HttpClient) {} + constructor() {} async init(): Promise { this.registry = await RegistryService.getRegistry(); this.tokenRegistry = await RegistryService.getTokenRegistry(); - // this.registry.onload = async (address: string): Promise => { - // this.tokenRegistry = new TokenRegistry( - // await this.registry.getContractAddressByName('TokenRegistry') - // ); - // this.onload(this.tokenRegistry !== undefined); - // }; + this.load.next(true); } addToken(token: Token): void { diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 82b6551..681b7e6 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -7,7 +7,6 @@ import { ArgPair, Envelope, Phone, Syncable, User } from 'cic-client-meta'; import { AccountDetails } from '@app/_models'; import { LoggingService } from '@app/_services/logging.service'; import { TokenService } from '@app/_services/token.service'; -import { AccountIndex } from '@app/_eth'; import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp'; import { RegistryService } from '@app/_services/registry.service'; import { CICRegistry } from '@cicnet/cic-client'; @@ -179,11 +178,8 @@ export class UserService { async loadAccounts(limit: number = 100, offset: number = 0): Promise { this.resetAccountsList(); - const accountIndexAddress: string = await this.registry.getContractAddressByName( - 'AccountRegistry' - ); - const accountIndexQuery = new AccountIndex(accountIndexAddress); - const accountAddresses: Array = await accountIndexQuery.last(limit); + const accountRegistry = await RegistryService.getAccountRegistry(); + const accountAddresses: Array = await accountRegistry.last(limit); this.loggingService.sendInfoLevelMessage(accountAddresses); for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { await this.getAccountByAddress(accountAddress, limit); @@ -201,11 +197,13 @@ export class UserService { const account: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap(); const accountInfo = account.m.data; await personValidation(accountInfo); - this.tokenService.onload = async (status: boolean): Promise => { - accountInfo.balance = await this.tokenService.getTokenBalance( - accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] - ); - }; + this.tokenService.load.subscribe(async (status: boolean) => { + if (status) { + accountInfo.balance = await this.tokenService.getTokenBalance( + accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] + ); + } + }); accountInfo.vcard = vCard.parse(atob(accountInfo.vcard)); await vcardValidation(accountInfo.vcard); this.addAccount(accountInfo, limit);