Add account registry getter in registry service.

This commit is contained in:
Spencer Ofwiti 2021-06-11 10:56:55 +03:00
parent 190d36254a
commit a2ee2ae071
3 changed files with 44 additions and 36 deletions

View File

@ -1,14 +1,10 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { environment } from '@src/environments/environment'; import { environment } from '@src/environments/environment';
import { CICRegistry, FileGetter } from '@cicnet/cic-client'; import { CICRegistry, FileGetter } from '@cicnet/cic-client';
import { TokenRegistry } from '@app/_eth'; import { AccountIndex, TokenRegistry } from '@app/_eth';
import { HttpGetter } from '@app/_helpers'; import { HttpGetter } from '@app/_helpers';
import { Web3Service } from '@app/_services/web3.service'; import { Web3Service } from '@app/_services/web3.service';
import { BehaviorSubject } from 'rxjs';
// export interface RegistryCollection {
// cicRegistry: CICRegistry;
// tokenRegistry: TokenRegistry;
// }
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -17,7 +13,8 @@ export class RegistryService {
static fileGetter: FileGetter = new HttpGetter(); static fileGetter: FileGetter = new HttpGetter();
private static registry: CICRegistry; private static registry: CICRegistry;
private static tokenRegistry: TokenRegistry; private static tokenRegistry: TokenRegistry;
// private static registries: RegistryCollection; private static accountRegistry: AccountIndex;
private static load: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
public static async getRegistry(): Promise<CICRegistry> { public static async getRegistry(): Promise<CICRegistry> {
if (!RegistryService.registry) { if (!RegistryService.registry) {
@ -30,6 +27,8 @@ export class RegistryService {
); );
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
await RegistryService.registry.load(); await RegistryService.registry.load();
RegistryService.load.next(true);
return RegistryService.registry;
} }
return RegistryService.registry; return RegistryService.registry;
} }
@ -38,16 +37,33 @@ export class RegistryService {
if (!RegistryService.tokenRegistry) { if (!RegistryService.tokenRegistry) {
// then initial it // then initial it
const registry = await RegistryService.getRegistry(); const registry = await RegistryService.getRegistry();
const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName( RegistryService.load.subscribe(async (status: boolean) => {
'TokenRegistry' if (status) {
); const tokenRegistryAddress = await registry.getContractAddressByName('TokenRegistry');
RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress); RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress);
return new Promise((resolve, reject) => { return RegistryService.tokenRegistry;
resolve(RegistryService.tokenRegistry); }
});
// return new Promise((resolve, reject) => {
// resolve(RegistryService.tokenRegistry);
// });
}
return RegistryService.tokenRegistry;
}
public static async getAccountRegistry(): Promise<AccountIndex> {
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) => { return RegistryService.accountRegistry;
resolve(RegistryService.tokenRegistry);
});
} }
} }

View File

@ -1,7 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CICRegistry } from '@cicnet/cic-client'; import { CICRegistry } from '@cicnet/cic-client';
import { TokenRegistry } from '@app/_eth'; import { TokenRegistry } from '@app/_eth';
import { HttpClient } from '@angular/common/http';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import { Token } from '@app/_models'; import { Token } from '@app/_models';
import { BehaviorSubject, Observable, Subject } from 'rxjs'; import { BehaviorSubject, Observable, Subject } from 'rxjs';
@ -12,24 +11,19 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs';
export class TokenService { export class TokenService {
registry: CICRegistry; registry: CICRegistry;
tokenRegistry: TokenRegistry; tokenRegistry: TokenRegistry;
onload: (status: boolean) => void;
tokens: Array<Token> = []; tokens: Array<Token> = [];
private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>( private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
this.tokens this.tokens
); );
tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable(); tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
load: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor(private httpClient: HttpClient) {} constructor() {}
async init(): Promise<void> { async init(): Promise<void> {
this.registry = await RegistryService.getRegistry(); this.registry = await RegistryService.getRegistry();
this.tokenRegistry = await RegistryService.getTokenRegistry(); this.tokenRegistry = await RegistryService.getTokenRegistry();
// this.registry.onload = async (address: string): Promise<void> => { this.load.next(true);
// this.tokenRegistry = new TokenRegistry(
// await this.registry.getContractAddressByName('TokenRegistry')
// );
// this.onload(this.tokenRegistry !== undefined);
// };
} }
addToken(token: Token): void { addToken(token: Token): void {

View File

@ -7,7 +7,6 @@ import { ArgPair, Envelope, Phone, Syncable, User } from 'cic-client-meta';
import { AccountDetails } from '@app/_models'; import { AccountDetails } from '@app/_models';
import { LoggingService } from '@app/_services/logging.service'; import { LoggingService } from '@app/_services/logging.service';
import { TokenService } from '@app/_services/token.service'; import { TokenService } from '@app/_services/token.service';
import { AccountIndex } from '@app/_eth';
import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp'; import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import { CICRegistry } from '@cicnet/cic-client'; import { CICRegistry } from '@cicnet/cic-client';
@ -179,11 +178,8 @@ export class UserService {
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> { async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
this.resetAccountsList(); this.resetAccountsList();
const accountIndexAddress: string = await this.registry.getContractAddressByName( const accountRegistry = await RegistryService.getAccountRegistry();
'AccountRegistry' const accountAddresses: Array<string> = await accountRegistry.last(limit);
);
const accountIndexQuery = new AccountIndex(accountIndexAddress);
const accountAddresses: Array<string> = await accountIndexQuery.last(limit);
this.loggingService.sendInfoLevelMessage(accountAddresses); this.loggingService.sendInfoLevelMessage(accountAddresses);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit); await this.getAccountByAddress(accountAddress, limit);
@ -201,11 +197,13 @@ export class UserService {
const account: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap(); const account: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
const accountInfo = account.m.data; const accountInfo = account.m.data;
await personValidation(accountInfo); await personValidation(accountInfo);
this.tokenService.onload = async (status: boolean): Promise<void> => { this.tokenService.load.subscribe(async (status: boolean) => {
accountInfo.balance = await this.tokenService.getTokenBalance( if (status) {
accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] accountInfo.balance = await this.tokenService.getTokenBalance(
); accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
}; );
}
});
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard)); accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
await vcardValidation(accountInfo.vcard); await vcardValidation(accountInfo.vcard);
this.addAccount(accountInfo, limit); this.addAccount(accountInfo, limit);