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 { 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<boolean> = new BehaviorSubject<boolean>(false);
public static async getRegistry(): Promise<CICRegistry> {
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<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) => {
resolve(RegistryService.tokenRegistry);
});
return RegistryService.accountRegistry;
}
}

View File

@ -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<Token> = [];
private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
this.tokens
);
tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
load: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor(private httpClient: HttpClient) {}
constructor() {}
async init(): Promise<void> {
this.registry = await RegistryService.getRegistry();
this.tokenRegistry = await RegistryService.getTokenRegistry();
// this.registry.onload = async (address: string): Promise<void> => {
// this.tokenRegistry = new TokenRegistry(
// await this.registry.getContractAddressByName('TokenRegistry')
// );
// this.onload(this.tokenRegistry !== undefined);
// };
this.load.next(true);
}
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 { 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<void> {
this.resetAccountsList();
const accountIndexAddress: string = await this.registry.getContractAddressByName(
'AccountRegistry'
);
const accountIndexQuery = new AccountIndex(accountIndexAddress);
const accountAddresses: Array<string> = await accountIndexQuery.last(limit);
const accountRegistry = await RegistryService.getAccountRegistry();
const accountAddresses: Array<string> = 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<void> => {
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);