cic-staff-client/src/app/app.component.ts

107 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-11-08 07:31:52 +01:00
import {Component, HostListener, OnInit} from '@angular/core';
import {TransactionService} from './_services/transaction.service';
import {fetcher} from './../assets/js/plugin';
import {Registry, TransactionHelper} from './../assets/js/bancor/registry';
import Web3 from 'web3';
import { Settings } from './_models';
2020-10-30 16:16:05 +01:00
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
2020-11-08 07:31:52 +01:00
export class AppComponent implements OnInit {
2020-10-30 16:16:05 +01:00
title = 'cic-staff-client';
2020-11-08 07:31:52 +01:00
registryAddress: string = '0xb708175e3f6Cd850643aAF7B32212AFad50e2549';
readyStateTarget: number = 3;
readyState: number = 0;
constructor(private transactionService: TransactionService) {}
ngOnInit(): void {
const settings = new Settings(this.scan);
const provider = 'ws://localhost:8545';
const readyStateElements = {
token: 1,
network: 2,
};
settings.w3.provider = provider;
settings.w3.engine = new Web3(provider);
settings.registry = new Registry(settings.w3.engine, this.registryAddress);
settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry);
settings.txHelper.ontransfer = async (transaction: any): Promise<void> => {
window.dispatchEvent(this.newTransferEvent(transaction));
};
settings.txHelper.onconversion = async (transaction: any): Promise<any> => {
window.dispatchEvent(this.newConversionEvent(transaction));
};
settings.registry.ontokenload = (tokenCount: number): void => {
console.debug('loaded tokens', tokenCount);
this.readyStateProcessor(settings, readyStateElements.token);
};
settings.registry.onregistryload = (tokenCount: number): void => {
console.debug('loaded network contracts', tokenCount);
this.readyStateProcessor(settings, readyStateElements.network);
};
settings.registry.load();
}
readyStateProcessor(settings, bit): void {
this.readyState |= bit;
if (this.readyStateTarget === this.readyState && this.readyStateTarget) {
console.log('reached readyState target', this.readyStateTarget);
fetcher(settings);
}
}
newTransferEvent(tx): any {
return new CustomEvent('cic_transfer', {
detail: {
tx,
},
});
}
newConversionEvent(tx): any {
return new CustomEvent('cic_convert', {
detail: {
tx,
},
});
}
async scan(settings, lo, hi, bloomBlockBytes, bloomBlocktxBytes, bloomRounds): Promise<void> {
const w = new Worker('./../assets/js/worker.js');
w.onmessage = (m) => {
settings.txHelper.processReceipt(m.data);
};
w.postMessage({
w3_provider: settings.w3.provider,
lo,
hi,
filters: [
bloomBlockBytes,
bloomBlocktxBytes,
],
filter_rounds: bloomRounds,
});
}
@HostListener('window:cic_transfer', ['$event'])
cicTransfer(event: CustomEvent): void {
console.log('have tx ', event.detail.tx);
const transaction = event.detail.tx;
this.transactionService.setTransaction(transaction);
}
@HostListener('window:cic_convert', ['$event'])
cicConvert(event: CustomEvent): void {
console.log('have convert ', event.detail.tx);
const conversion = event.detail.tx;
this.transactionService.setConversion(conversion);
}
2020-10-30 16:16:05 +01:00
}