Clean up constructors.

This commit is contained in:
Spencer Ofwiti 2021-05-19 19:57:10 +03:00
parent 5baffa5fef
commit a4c0e26be9
16 changed files with 107 additions and 86 deletions

View File

@ -16,17 +16,20 @@ export class BlockSyncService {
constructor( constructor(
private transactionService: TransactionService, private transactionService: TransactionService,
private loggingService: LoggingService, private loggingService: LoggingService
private registryService: RegistryService
) {} ) {}
blockSync(address: string = null, offset: number = 0, limit: number = 100): void { async init(): Promise<void> {
await this.transactionService.init();
}
async blockSync(address: string = null, offset: number = 0, limit: number = 100): Promise<void> {
this.transactionService.resetTransactionsList(); this.transactionService.resetTransactionsList();
const settings: Settings = new Settings(this.scan); const settings: Settings = new Settings(this.scan);
const readyStateElements: { network: number } = { network: 2 }; const readyStateElements: { network: number } = { network: 2 };
settings.w3.provider = environment.web3Provider; settings.w3.provider = environment.web3Provider;
settings.w3.engine = this.registryService.web3; settings.w3.engine = RegistryService.web3;
settings.registry = this.registryService.registry; settings.registry = await RegistryService.getRegistry();
settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry); settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry);
settings.txHelper.ontransfer = async (transaction: any): Promise<void> => { settings.txHelper.ontransfer = async (transaction: any): Promise<void> => {

View File

@ -8,18 +8,24 @@ import { HttpGetter } from '@app/_helpers';
providedIn: 'root', providedIn: 'root',
}) })
export class RegistryService { export class RegistryService {
web3: Web3 = new Web3(environment.web3Provider); static web3: Web3 = new Web3(environment.web3Provider);
fileGetter: FileGetter = new HttpGetter(); static fileGetter: FileGetter = new HttpGetter();
registry: CICRegistry = new CICRegistry( private static registry: CICRegistry;
this.web3,
environment.registryAddress,
'Registry',
this.fileGetter,
['../../assets/js/block-sync/data']
);
constructor() { constructor() {}
this.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
this.registry.load(); public static async getRegistry(): Promise<CICRegistry> {
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;
} }
} }

View File

@ -14,8 +14,10 @@ export class TokenService {
tokenRegistry: TokenRegistry; tokenRegistry: TokenRegistry;
onload: (status: boolean) => void; onload: (status: boolean) => void;
constructor(private httpClient: HttpClient, private registryService: RegistryService) { constructor(private httpClient: HttpClient) {}
this.registry = this.registryService.registry;
async init(): Promise<void> {
this.registry = await RegistryService.getRegistry();
this.registry.onload = async (address: string): Promise<void> => { this.registry.onload = async (address: string): Promise<void> => {
this.tokenRegistry = new TokenRegistry( this.tokenRegistry = new TokenRegistry(
await this.registry.getContractAddressByName('TokenRegistry') await this.registry.getContractAddressByName('TokenRegistry')

View File

@ -34,11 +34,15 @@ export class TransactionService {
private httpClient: HttpClient, private httpClient: HttpClient,
private authService: AuthService, private authService: AuthService,
private userService: UserService, private userService: UserService,
private loggingService: LoggingService, private loggingService: LoggingService
private registryService: RegistryService
) { ) {
this.web3 = this.registryService.web3; this.web3 = RegistryService.web3;
this.registry = registryService.registry; }
async init(): Promise<void> {
await this.authService.init();
await this.userService.init();
this.registry = await RegistryService.getRegistry();
} }
getAllTransactions(offset: number, limit: number): Observable<any> { getAllTransactions(offset: number, limit: number): Observable<any> {

View File

@ -39,14 +39,15 @@ export class UserService {
private httpClient: HttpClient, private httpClient: HttpClient,
private loggingService: LoggingService, private loggingService: LoggingService,
private tokenService: TokenService, private tokenService: TokenService,
private registryService: RegistryService,
private authService: AuthService private authService: AuthService
) { ) {}
this.authService.init().then(() => {
this.keystore = authService.mutableKeyStore; async init(): Promise<void> {
this.signer = new PGPSigner(this.keystore); await this.authService.init();
}); await this.tokenService.init();
this.registry = registryService.registry; this.keystore = this.authService.mutableKeyStore;
this.signer = new PGPSigner(this.keystore);
this.registry = await RegistryService.getRegistry();
} }
resetPin(phone: string): Observable<any> { resetPin(phone: string): Observable<any> {

View File

@ -27,28 +27,22 @@ export class AppComponent implements OnInit {
private errorDialogService: ErrorDialogService, private errorDialogService: ErrorDialogService,
private swUpdate: SwUpdate 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.mediaQuery.addEventListener('change', this.onResize);
this.onResize(this.mediaQuery); this.onResize(this.mediaQuery);
} }
ngOnInit(): void { async ngOnInit(): Promise<void> {
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) { if (!this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe(() => { this.swUpdate.available.subscribe(() => {
if (confirm('New Version available. Load New Version?')) { if (confirm('New Version available. Load New Version?')) {

View File

@ -78,6 +78,14 @@ export class AccountDetailsComponent implements OnInit {
private cdr: ChangeDetectorRef, private cdr: ChangeDetectorRef,
private snackBar: MatSnackBar 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<void> {
this.accountInfoForm = this.formBuilder.group({ this.accountInfoForm = this.formBuilder.group({
name: ['', Validators.required], name: ['', Validators.required],
phoneNumber: ['', Validators.required], phoneNumber: ['', Validators.required],
@ -90,15 +98,11 @@ export class AccountDetailsComponent implements OnInit {
location: ['', Validators.required], location: ['', Validators.required],
locationType: ['', Validators.required], locationType: ['', Validators.required],
}); });
this.route.paramMap.subscribe((params: Params) => { await this.blockSyncService.init();
this.accountAddress = add0x(params.get('id')); await this.tokenService.init();
this.bloxbergLink = await this.transactionService.init();
'https://blockexplorer.bloxberg.org/address/' + this.accountAddress + '/transactions'; await this.userService.init();
this.blockSyncService.blockSync(this.accountAddress); await this.blockSyncService.blockSync(this.accountAddress);
});
}
async ngOnInit(): Promise<void> {
(await this.userService.getAccountByAddress(this.accountAddress, 100)).subscribe( (await this.userService.getAccountByAddress(this.accountAddress, 100)).subscribe(
async (res) => { async (res) => {
if (res !== undefined) { if (res !== undefined) {

View File

@ -30,7 +30,8 @@ export class AccountSearchComponent implements OnInit {
private router: Router private router: Router
) {} ) {}
ngOnInit(): void { async ngOnInit(): Promise<void> {
await this.userService.init();
this.nameSearchForm = this.formBuilder.group({ this.nameSearchForm = this.formBuilder.group({
name: ['', Validators.required], name: ['', Validators.required],
}); });

View File

@ -32,28 +32,26 @@ export class AccountsComponent implements OnInit {
private userService: UserService, private userService: UserService,
private loggingService: LoggingService, private loggingService: LoggingService,
private router: Router 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<void> {
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.userService.accountsSubject.subscribe((accounts) => {
this.dataSource = new MatTableDataSource<any>(accounts); this.dataSource = new MatTableDataSource<any>(accounts);
this.dataSource.paginator = this.paginator; this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort; this.dataSource.sort = this.sort;
this.accounts = accounts; this.accounts = accounts;
}); });
this.userService
.getAccountTypes()
.pipe(first())
.subscribe((res) => (this.accountTypes = res));
} }
doFilter(value: string): void { doFilter(value: string): void {

View File

@ -26,7 +26,8 @@ export class CreateAccountComponent implements OnInit {
private userService: UserService private userService: UserService
) {} ) {}
ngOnInit(): void { async ngOnInit(): Promise<void> {
await this.userService.init();
this.createForm = this.formBuilder.group({ this.createForm = this.formBuilder.group({
accountType: ['', Validators.required], accountType: ['', Validators.required],
idNumber: ['', Validators.required], idNumber: ['', Validators.required],

View File

@ -30,7 +30,10 @@ export class AdminComponent implements OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort; @ViewChild(MatSort) sort: MatSort;
constructor(private userService: UserService, private loggingService: LoggingService) { constructor(private userService: UserService, private loggingService: LoggingService) {}
async ngOnInit(): Promise<void> {
await this.userService.init();
this.userService.getActions(); this.userService.getActions();
this.userService.actionsSubject.subscribe((actions) => { this.userService.actionsSubject.subscribe((actions) => {
this.dataSource = new MatTableDataSource<any>(actions); this.dataSource = new MatTableDataSource<any>(actions);
@ -40,8 +43,6 @@ export class AdminComponent implements OnInit {
}); });
} }
ngOnInit(): void {}
doFilter(value: string): void { doFilter(value: string): void {
this.dataSource.filter = value.trim().toLocaleLowerCase(); this.dataSource.filter = value.trim().toLocaleLowerCase();
} }

View File

@ -24,7 +24,8 @@ export class SettingsComponent implements OnInit {
constructor(private authService: AuthService) {} constructor(private authService: AuthService) {}
ngOnInit(): void { async ngOnInit(): Promise<void> {
await this.authService.init();
const d = new Date(); const d = new Date();
this.date = `${d.getDate()}/${d.getMonth()}/${d.getFullYear()}`; this.date = `${d.getDate()}/${d.getMonth()}/${d.getFullYear()}`;
this.trustedUsers = this.authService.getTrustedUsers(); this.trustedUsers = this.authService.getTrustedUsers();

View File

@ -13,7 +13,10 @@ import { Token } from '../../../_models';
export class TokenDetailsComponent implements OnInit { export class TokenDetailsComponent implements OnInit {
token: Token; token: Token;
constructor(private route: ActivatedRoute, private tokenService: TokenService) { constructor(private route: ActivatedRoute, private tokenService: TokenService) {}
async ngOnInit(): Promise<void> {
await this.tokenService.init();
this.route.paramMap.subscribe((params: Params) => { this.route.paramMap.subscribe((params: Params) => {
this.tokenService this.tokenService
.getTokenBySymbol(params.get('id')) .getTokenBySymbol(params.get('id'))
@ -23,6 +26,4 @@ export class TokenDetailsComponent implements OnInit {
}); });
}); });
} }
ngOnInit(): void {}
} }

View File

@ -26,6 +26,7 @@ export class TokensComponent implements OnInit {
) {} ) {}
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
await this.tokenService.init();
this.tokenService.onload = async (status: boolean): Promise<void> => { this.tokenService.onload = async (status: boolean): Promise<void> => {
this.tokens = await this.tokenService.getTokens(); this.tokens = await this.tokenService.getTokens();
this.loggingService.sendInfoLevelMessage(this.tokens); this.loggingService.sendInfoLevelMessage(this.tokens);

View File

@ -23,7 +23,8 @@ export class TransactionDetailsComponent implements OnInit {
private snackBar: MatSnackBar private snackBar: MatSnackBar
) {} ) {}
ngOnInit(): void { async ngOnInit(): Promise<void> {
await this.transactionService.init();
if (this.transaction?.type === 'conversion') { if (this.transaction?.type === 'conversion') {
this.traderBloxbergLink = this.traderBloxbergLink =
'https://blockexplorer.bloxberg.org/address/' + this.transaction?.trader + '/transactions'; 'https://blockexplorer.bloxberg.org/address/' + this.transaction?.trader + '/transactions';

View File

@ -36,17 +36,19 @@ export class TransactionsComponent implements OnInit, AfterViewInit {
private blockSyncService: BlockSyncService, private blockSyncService: BlockSyncService,
private transactionService: TransactionService, private transactionService: TransactionService,
private userService: UserService private userService: UserService
) { ) {}
this.blockSyncService.blockSync();
}
ngOnInit(): void { async ngOnInit(): Promise<void> {
this.transactionService.transactionsSubject.subscribe((transactions) => { this.transactionService.transactionsSubject.subscribe((transactions) => {
this.transactionDataSource = new MatTableDataSource<any>(transactions); this.transactionDataSource = new MatTableDataSource<any>(transactions);
this.transactionDataSource.paginator = this.paginator; this.transactionDataSource.paginator = this.paginator;
this.transactionDataSource.sort = this.sort; this.transactionDataSource.sort = this.sort;
this.transactions = transactions; this.transactions = transactions;
}); });
await this.blockSyncService.init();
await this.transactionService.init();
await this.userService.init();
await this.blockSyncService.blockSync();
this.userService this.userService
.getTransactionTypes() .getTransactionTypes()
.pipe(first()) .pipe(first())