diff --git a/src/app/_services/block-sync.service.ts b/src/app/_services/block-sync.service.ts index cf63667..60f5670 100644 --- a/src/app/_services/block-sync.service.ts +++ b/src/app/_services/block-sync.service.ts @@ -16,17 +16,20 @@ export class BlockSyncService { constructor( private transactionService: TransactionService, - private loggingService: LoggingService, - private registryService: RegistryService + private loggingService: LoggingService ) {} - blockSync(address: string = null, offset: number = 0, limit: number = 100): void { + async init(): Promise { + await this.transactionService.init(); + } + + async blockSync(address: string = null, offset: number = 0, limit: number = 100): Promise { this.transactionService.resetTransactionsList(); const settings: Settings = new Settings(this.scan); const readyStateElements: { network: number } = { network: 2 }; settings.w3.provider = environment.web3Provider; - settings.w3.engine = this.registryService.web3; - settings.registry = this.registryService.registry; + settings.w3.engine = RegistryService.web3; + settings.registry = await RegistryService.getRegistry(); settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry); settings.txHelper.ontransfer = async (transaction: any): Promise => { diff --git a/src/app/_services/registry.service.ts b/src/app/_services/registry.service.ts index 7e845ea..0346f05 100644 --- a/src/app/_services/registry.service.ts +++ b/src/app/_services/registry.service.ts @@ -8,18 +8,24 @@ import { HttpGetter } from '@app/_helpers'; providedIn: 'root', }) export class RegistryService { - web3: Web3 = new Web3(environment.web3Provider); - fileGetter: FileGetter = new HttpGetter(); - registry: CICRegistry = new CICRegistry( - this.web3, - environment.registryAddress, - 'Registry', - this.fileGetter, - ['../../assets/js/block-sync/data'] - ); + static web3: Web3 = new Web3(environment.web3Provider); + static fileGetter: FileGetter = new HttpGetter(); + private static registry: CICRegistry; - constructor() { - this.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); - this.registry.load(); + constructor() {} + + public static async getRegistry(): Promise { + if (!RegistryService.registry) { + RegistryService.registry = new CICRegistry( + RegistryService.web3, + environment.registryAddress, + 'Registry', + RegistryService.fileGetter, + ['../../assets/js/block-sync/data'] + ); + RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); + await RegistryService.registry.load(); + } + return RegistryService.registry; } } diff --git a/src/app/_services/token.service.ts b/src/app/_services/token.service.ts index b644b4d..1444458 100644 --- a/src/app/_services/token.service.ts +++ b/src/app/_services/token.service.ts @@ -14,8 +14,10 @@ export class TokenService { tokenRegistry: TokenRegistry; onload: (status: boolean) => void; - constructor(private httpClient: HttpClient, private registryService: RegistryService) { - this.registry = this.registryService.registry; + constructor(private httpClient: HttpClient) {} + + async init(): Promise { + this.registry = await RegistryService.getRegistry(); this.registry.onload = async (address: string): Promise => { this.tokenRegistry = new TokenRegistry( await this.registry.getContractAddressByName('TokenRegistry') diff --git a/src/app/_services/transaction.service.ts b/src/app/_services/transaction.service.ts index 6a4897d..3d554e7 100644 --- a/src/app/_services/transaction.service.ts +++ b/src/app/_services/transaction.service.ts @@ -34,11 +34,15 @@ export class TransactionService { private httpClient: HttpClient, private authService: AuthService, private userService: UserService, - private loggingService: LoggingService, - private registryService: RegistryService + private loggingService: LoggingService ) { - this.web3 = this.registryService.web3; - this.registry = registryService.registry; + this.web3 = RegistryService.web3; + } + + async init(): Promise { + await this.authService.init(); + await this.userService.init(); + this.registry = await RegistryService.getRegistry(); } getAllTransactions(offset: number, limit: number): Observable { diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 85ffc32..ff1adf0 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -39,14 +39,15 @@ export class UserService { private httpClient: HttpClient, private loggingService: LoggingService, private tokenService: TokenService, - private registryService: RegistryService, private authService: AuthService - ) { - this.authService.init().then(() => { - this.keystore = authService.mutableKeyStore; - this.signer = new PGPSigner(this.keystore); - }); - this.registry = registryService.registry; + ) {} + + async init(): Promise { + await this.authService.init(); + await this.tokenService.init(); + this.keystore = this.authService.mutableKeyStore; + this.signer = new PGPSigner(this.keystore); + this.registry = await RegistryService.getRegistry(); } resetPin(phone: string): Observable { diff --git a/src/app/app.component.ts b/src/app/app.component.ts index dfda722..4ac8e8b 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -27,28 +27,22 @@ export class AppComponent implements OnInit { private errorDialogService: ErrorDialogService, private swUpdate: SwUpdate ) { - (async () => { - try { - await this.authService.init(); - // this.authService.getPublicKeys() - // .pipe(catchError(async (error) => { - // this.loggingService.sendErrorLevelMessage('Unable to load trusted public keys.', this, {error}); - // this.errorDialogService.openDialog({message: 'Trusted keys endpoint can\'t be reached. Please try again later.'}); - // })).subscribe(this.authService.mutableKeyStore.importPublicKey); - const publicKeys = await this.authService.getPublicKeys(); - await this.authService.mutableKeyStore.importPublicKey(publicKeys); - } catch (error) { - this.errorDialogService.openDialog({ - message: 'Trusted keys endpoint cannot be reached. Please try again later.', - }); - // TODO do something to halt user progress...show a sad cicada page 🦗? - } - })(); this.mediaQuery.addEventListener('change', this.onResize); this.onResize(this.mediaQuery); } - ngOnInit(): void { + async ngOnInit(): Promise { + await this.authService.init(); + await this.transactionService.init(); + try { + const publicKeys = await this.authService.getPublicKeys(); + await this.authService.mutableKeyStore.importPublicKey(publicKeys); + } catch (error) { + this.errorDialogService.openDialog({ + message: 'Trusted keys endpoint cannot be reached. Please try again later.', + }); + // TODO do something to halt user progress...show a sad cicada page 🦗? + } if (!this.swUpdate.isEnabled) { this.swUpdate.available.subscribe(() => { if (confirm('New Version available. Load New Version?')) { diff --git a/src/app/pages/accounts/account-details/account-details.component.ts b/src/app/pages/accounts/account-details/account-details.component.ts index 39cdcbf..dedcc42 100644 --- a/src/app/pages/accounts/account-details/account-details.component.ts +++ b/src/app/pages/accounts/account-details/account-details.component.ts @@ -78,6 +78,14 @@ export class AccountDetailsComponent implements OnInit { private cdr: ChangeDetectorRef, private snackBar: MatSnackBar ) { + this.route.paramMap.subscribe((params: Params) => { + this.accountAddress = add0x(params.get('id')); + this.bloxbergLink = + 'https://blockexplorer.bloxberg.org/address/' + this.accountAddress + '/transactions'; + }); + } + + async ngOnInit(): Promise { this.accountInfoForm = this.formBuilder.group({ name: ['', Validators.required], phoneNumber: ['', Validators.required], @@ -90,15 +98,11 @@ export class AccountDetailsComponent implements OnInit { location: ['', Validators.required], locationType: ['', Validators.required], }); - this.route.paramMap.subscribe((params: Params) => { - this.accountAddress = add0x(params.get('id')); - this.bloxbergLink = - 'https://blockexplorer.bloxberg.org/address/' + this.accountAddress + '/transactions'; - this.blockSyncService.blockSync(this.accountAddress); - }); - } - - async ngOnInit(): Promise { + await this.blockSyncService.init(); + await this.tokenService.init(); + await this.transactionService.init(); + await this.userService.init(); + await this.blockSyncService.blockSync(this.accountAddress); (await this.userService.getAccountByAddress(this.accountAddress, 100)).subscribe( async (res) => { if (res !== undefined) { diff --git a/src/app/pages/accounts/account-search/account-search.component.ts b/src/app/pages/accounts/account-search/account-search.component.ts index ef665f8..800b0ce 100644 --- a/src/app/pages/accounts/account-search/account-search.component.ts +++ b/src/app/pages/accounts/account-search/account-search.component.ts @@ -30,7 +30,8 @@ export class AccountSearchComponent implements OnInit { private router: Router ) {} - ngOnInit(): void { + async ngOnInit(): Promise { + await this.userService.init(); this.nameSearchForm = this.formBuilder.group({ name: ['', Validators.required], }); diff --git a/src/app/pages/accounts/accounts.component.ts b/src/app/pages/accounts/accounts.component.ts index 853997a..c009f46 100644 --- a/src/app/pages/accounts/accounts.component.ts +++ b/src/app/pages/accounts/accounts.component.ts @@ -32,28 +32,26 @@ export class AccountsComponent implements OnInit { private userService: UserService, private loggingService: LoggingService, private router: Router - ) { - (async () => { - try { - // TODO it feels like this should be in the onInit handler - await this.userService.loadAccounts(100); - } catch (error) { - this.loggingService.sendErrorLevelMessage('Failed to load accounts', this, { error }); - } - })(); - this.userService - .getAccountTypes() - .pipe(first()) - .subscribe((res) => (this.accountTypes = res)); - } + ) {} - ngOnInit(): void { + async ngOnInit(): Promise { + await this.userService.init(); + try { + // TODO it feels like this should be in the onInit handler + await this.userService.loadAccounts(100); + } catch (error) { + this.loggingService.sendErrorLevelMessage('Failed to load accounts', this, { error }); + } this.userService.accountsSubject.subscribe((accounts) => { this.dataSource = new MatTableDataSource(accounts); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; this.accounts = accounts; }); + this.userService + .getAccountTypes() + .pipe(first()) + .subscribe((res) => (this.accountTypes = res)); } doFilter(value: string): void { diff --git a/src/app/pages/accounts/create-account/create-account.component.ts b/src/app/pages/accounts/create-account/create-account.component.ts index 2f196e7..30692ef 100644 --- a/src/app/pages/accounts/create-account/create-account.component.ts +++ b/src/app/pages/accounts/create-account/create-account.component.ts @@ -26,7 +26,8 @@ export class CreateAccountComponent implements OnInit { private userService: UserService ) {} - ngOnInit(): void { + async ngOnInit(): Promise { + await this.userService.init(); this.createForm = this.formBuilder.group({ accountType: ['', Validators.required], idNumber: ['', Validators.required], diff --git a/src/app/pages/admin/admin.component.ts b/src/app/pages/admin/admin.component.ts index cace112..d33be3c 100644 --- a/src/app/pages/admin/admin.component.ts +++ b/src/app/pages/admin/admin.component.ts @@ -30,7 +30,10 @@ export class AdminComponent implements OnInit { @ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatSort) sort: MatSort; - constructor(private userService: UserService, private loggingService: LoggingService) { + constructor(private userService: UserService, private loggingService: LoggingService) {} + + async ngOnInit(): Promise { + await this.userService.init(); this.userService.getActions(); this.userService.actionsSubject.subscribe((actions) => { this.dataSource = new MatTableDataSource(actions); @@ -40,8 +43,6 @@ export class AdminComponent implements OnInit { }); } - ngOnInit(): void {} - doFilter(value: string): void { this.dataSource.filter = value.trim().toLocaleLowerCase(); } diff --git a/src/app/pages/settings/settings.component.ts b/src/app/pages/settings/settings.component.ts index b8ff67d..2b6825f 100644 --- a/src/app/pages/settings/settings.component.ts +++ b/src/app/pages/settings/settings.component.ts @@ -24,7 +24,8 @@ export class SettingsComponent implements OnInit { constructor(private authService: AuthService) {} - ngOnInit(): void { + async ngOnInit(): Promise { + await this.authService.init(); const d = new Date(); this.date = `${d.getDate()}/${d.getMonth()}/${d.getFullYear()}`; this.trustedUsers = this.authService.getTrustedUsers(); diff --git a/src/app/pages/tokens/token-details/token-details.component.ts b/src/app/pages/tokens/token-details/token-details.component.ts index bb0ece6..795bf30 100644 --- a/src/app/pages/tokens/token-details/token-details.component.ts +++ b/src/app/pages/tokens/token-details/token-details.component.ts @@ -13,7 +13,10 @@ import { Token } from '../../../_models'; export class TokenDetailsComponent implements OnInit { token: Token; - constructor(private route: ActivatedRoute, private tokenService: TokenService) { + constructor(private route: ActivatedRoute, private tokenService: TokenService) {} + + async ngOnInit(): Promise { + await this.tokenService.init(); this.route.paramMap.subscribe((params: Params) => { this.tokenService .getTokenBySymbol(params.get('id')) @@ -23,6 +26,4 @@ export class TokenDetailsComponent implements OnInit { }); }); } - - ngOnInit(): void {} } diff --git a/src/app/pages/tokens/tokens.component.ts b/src/app/pages/tokens/tokens.component.ts index b4b4012..3e76575 100644 --- a/src/app/pages/tokens/tokens.component.ts +++ b/src/app/pages/tokens/tokens.component.ts @@ -26,6 +26,7 @@ export class TokensComponent implements OnInit { ) {} async ngOnInit(): Promise { + await this.tokenService.init(); this.tokenService.onload = async (status: boolean): Promise => { this.tokens = await this.tokenService.getTokens(); this.loggingService.sendInfoLevelMessage(this.tokens); diff --git a/src/app/pages/transactions/transaction-details/transaction-details.component.ts b/src/app/pages/transactions/transaction-details/transaction-details.component.ts index b99ac13..fded333 100644 --- a/src/app/pages/transactions/transaction-details/transaction-details.component.ts +++ b/src/app/pages/transactions/transaction-details/transaction-details.component.ts @@ -23,7 +23,8 @@ export class TransactionDetailsComponent implements OnInit { private snackBar: MatSnackBar ) {} - ngOnInit(): void { + async ngOnInit(): Promise { + await this.transactionService.init(); if (this.transaction?.type === 'conversion') { this.traderBloxbergLink = 'https://blockexplorer.bloxberg.org/address/' + this.transaction?.trader + '/transactions'; diff --git a/src/app/pages/transactions/transactions.component.ts b/src/app/pages/transactions/transactions.component.ts index 56e1f8d..b0b3543 100644 --- a/src/app/pages/transactions/transactions.component.ts +++ b/src/app/pages/transactions/transactions.component.ts @@ -36,17 +36,19 @@ export class TransactionsComponent implements OnInit, AfterViewInit { private blockSyncService: BlockSyncService, private transactionService: TransactionService, private userService: UserService - ) { - this.blockSyncService.blockSync(); - } + ) {} - ngOnInit(): void { + async ngOnInit(): Promise { this.transactionService.transactionsSubject.subscribe((transactions) => { this.transactionDataSource = new MatTableDataSource(transactions); this.transactionDataSource.paginator = this.paginator; this.transactionDataSource.sort = this.sort; this.transactions = transactions; }); + await this.blockSyncService.init(); + await this.transactionService.init(); + await this.userService.init(); + await this.blockSyncService.blockSync(); this.userService .getTransactionTypes() .pipe(first())