cic-staff-client/src/app/_services/block-sync.service.ts
Spencer Ofwiti e372921439 Bug fix.
2021-02-06 17:08:37 +03:00

125 lines
4.3 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Settings} from '../_models';
import Web3 from 'web3';
import {abi, Registry, TransactionHelper} from 'cic-client';
import {first} from 'rxjs/operators';
import {TransactionService} from './transaction.service';
import {environment} from '../../environments/environment';
const cic = require('../../assets/js/block-sync/cic-client.web');
@Injectable({
providedIn: 'root'
})
export class BlockSyncService {
// registryAddress: string = '0xb708175e3f6Cd850643aAF7B32212AFad50e2549';
// registryAddress: string = '0x4f8af296202Bff3B8589DA4Af87A8cfe74ad4d3A';
trustedDeclaratorAddress: string = '0x5567139c7a1C2977A391f51D8cA45B1D6700f5F6';
readyStateTarget: number = 2;
readyState: number = 0;
constructor(private transactionService: TransactionService) { }
blockSync(): any {
const settings = new Settings(this.scan);
// const provider = 'ws://localhost:8545';
const provider = environment.web3Provider;
const readyStateElements = {
token: 1,
network: 2,
};
// @ts-ignore
// const fileGetter = new httpGetter();
settings.w3.provider = provider;
settings.w3.engine = new Web3(provider);
settings.registry = new Registry(settings.w3.engine, environment.registryAddress, abi);
// settings.registry = new cic.Registry(settings.w3.engine, environment.registryAddress, fileGetter, ['../../assets/js/block-sync/data']);
// settings.registry.addTrust(this.trustedDeclaratorAddress);
settings.txHelper = new TransactionHelper(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.ontokensload = (tokenCount: number): void => {
// console.debug('loaded tokens', tokenCount);
this.readyStateProcessor(settings, readyStateElements.token);
};
settings.registry.onregistryload = (addressReturned: number): void => {
// console.debug('loaded network contracts', addressReturned);
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);
const wHeadSync = new Worker('./../assets/js/block-sync/head.js');
wHeadSync.onmessage = (m) => {
settings.txHelper.processReceipt(m.data);
};
wHeadSync.postMessage({
w3_provider: settings.w3.provider,
});
this.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,
});
}
fetcher(settings: any, offset: number = 0, limit: number = 100): void {
this.transactionService.getAllTransactions(offset, limit).pipe(first()).subscribe(data => {
const blockFilterBinstr = window.atob(data.block_filter);
const bOne = new Uint8Array(blockFilterBinstr.length);
bOne.map((e, i, v) => v[i] = blockFilterBinstr.charCodeAt(i));
const blocktxFilterBinstr = window.atob(data.blocktx_filter);
const bTwo = new Uint8Array(blocktxFilterBinstr.length);
bTwo.map((e, i, v) => v[i] = blocktxFilterBinstr.charCodeAt(i));
for (let i = 0; i < blockFilterBinstr.length; i++) {
if (bOne[i] > 0 ) {
// console.debug('blocktx value on', i);
}
}
settings.scanFilter(settings, data.low, data.high, bOne, bTwo, data.filter_rounds);
});
}
}