From 8c9eb4a6a339eb29b1207a2e936a8f66414fe979 Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Sat, 12 Jun 2021 09:57:30 -0700 Subject: [PATCH] added account registry to registries --- src/app/_services/registry.service.ts | 83 ++++++++++++++++----------- src/app/_services/user.service.ts | 23 +++++--- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/src/app/_services/registry.service.ts b/src/app/_services/registry.service.ts index 0f652fb..5e67991 100644 --- a/src/app/_services/registry.service.ts +++ b/src/app/_services/registry.service.ts @@ -1,7 +1,8 @@ import { Injectable } from '@angular/core'; + import { environment } from '@src/environments/environment'; import { CICRegistry, FileGetter } from '@cicnet/cic-client'; -import { TokenRegistry } from '@app/_eth'; +import { TokenRegistry, AccountIndex } from '@app/_eth'; import { HttpGetter } from '@app/_helpers'; import { Web3Service } from '@app/_services/web3.service'; @@ -12,43 +13,59 @@ import { Web3Service } from '@app/_services/web3.service'; //} @Injectable({ - providedIn: 'root', + providedIn: 'root', }) export class RegistryService { - static fileGetter: FileGetter = new HttpGetter(); - private static registry: CICRegistry; - private static tokenRegistry: TokenRegistry; - //private static registries: RegistryCollection; + static fileGetter: FileGetter = new HttpGetter(); + private static registry: CICRegistry; + private static tokenRegistry: TokenRegistry; + private static accountRegistry: AccountIndex; + //private static registries: RegistryCollection; - public static async getRegistry(): Promise { - if (!RegistryService.registry) { - RegistryService.registry = new CICRegistry( - Web3Service.getInstance(), - environment.registryAddress, - 'Registry', - RegistryService.fileGetter, - ['../../assets/js/block-sync/data'] - ); - RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); - await RegistryService.registry.load() + public static async getRegistry(): Promise { + if (!RegistryService.registry) { + RegistryService.registry = new CICRegistry( + Web3Service.getInstance(), + environment.registryAddress, + 'Registry', + RegistryService.fileGetter, + ['../../assets/js/block-sync/data'] + ); + RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); + await RegistryService.registry.load() + } + return RegistryService.registry; } - return RegistryService.registry; - } - public static async getTokenRegistry(): Promise { - 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) - }) - } - return new Promise((resolve, reject) => { - resolve(RegistryService.tokenRegistry); - }) - } + public static async getTokenRegistry(): Promise { + return new Promise(async (resolve, reject) => { + if (!RegistryService.tokenRegistry) { + const registry = await RegistryService.getRegistry() + const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName('TokenRegistry') + if (!tokenRegistryAddress) { + return reject("Unable to initialize Token Registry") + } + RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress); + return resolve(RegistryService.tokenRegistry) + } + return resolve(RegistryService.tokenRegistry); + }) + } + + public static async getAccountRegistry(): Promise { + return new Promise(async (resolve, reject) => { + if (!RegistryService.accountRegistry) { + const registry = await RegistryService.getRegistry() + const accountRegistryAddress = await RegistryService.registry.getContractAddressByName('AccountRegistry') + if (!accountRegistryAddress) { + return reject("Unable to initialize Account Registry") + } + RegistryService.accountRegistry = new AccountIndex(accountRegistryAddress); + return resolve(RegistryService.accountRegistry) + } + return resolve(RegistryService.accountRegistry); + }) + } } diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 82b6551..9dd604f 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -179,14 +179,21 @@ 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); - this.loggingService.sendInfoLevelMessage(accountAddresses); - for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { - await this.getAccountByAddress(accountAddress, limit); + //const accountIndexAddress: string = await this.registry.getContractAddressByName( + // 'AccountRegistry' + //); + //const accountIndexQuery = new AccountIndex(accountIndexAddress); + //const accountAddresses: Array = await accountIndexQuery.last(limit); + try{ + 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); + } + } catch (error) { + this.loggingService.sendErrorLevelMessage("Unable to load accounts.", "user.service", error) + throw error } }