2021-05-10 18:15:25 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Settings } from '@app/_models';
|
|
|
|
import { TransactionHelper } from 'cic-client';
|
|
|
|
import { first } from 'rxjs/operators';
|
|
|
|
import { TransactionService } from '@app/_services/transaction.service';
|
|
|
|
import { environment } from '@src/environments/environment';
|
|
|
|
import { LoggingService } from '@app/_services/logging.service';
|
|
|
|
import { RegistryService } from '@app/_services/registry.service';
|
2020-11-25 08:51:15 +01:00
|
|
|
|
|
|
|
@Injectable({
|
2021-05-10 18:15:25 +02:00
|
|
|
providedIn: 'root',
|
2020-11-25 08:51:15 +01:00
|
|
|
})
|
|
|
|
export class BlockSyncService {
|
2021-01-15 05:30:19 +01:00
|
|
|
readyStateTarget: number = 2;
|
2020-11-25 08:51:15 +01:00
|
|
|
readyState: number = 0;
|
|
|
|
|
2021-03-14 09:19:25 +01:00
|
|
|
constructor(
|
|
|
|
private transactionService: TransactionService,
|
2021-04-20 10:28:40 +02:00
|
|
|
private loggingService: LoggingService,
|
2021-05-10 18:15:25 +02:00
|
|
|
private registryService: RegistryService
|
|
|
|
) {}
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-04-29 19:10:39 +02:00
|
|
|
blockSync(address: string = null, offset: number = 0, limit: number = 100): void {
|
2021-03-16 11:08:18 +01:00
|
|
|
this.transactionService.resetTransactionsList();
|
2021-04-29 19:10:39 +02:00
|
|
|
const settings: Settings = new Settings(this.scan);
|
|
|
|
const readyStateElements: { network: number } = { network: 2 };
|
2021-04-20 10:28:40 +02:00
|
|
|
settings.w3.provider = environment.web3Provider;
|
2021-05-18 11:38:27 +02:00
|
|
|
settings.w3.engine = this.registryService.web3;
|
|
|
|
settings.registry = this.registryService.registry;
|
2021-02-25 11:46:24 +01:00
|
|
|
settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry);
|
2020-11-25 08:51:15 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
};
|
2021-05-18 11:38:27 +02:00
|
|
|
settings.registry.onload = (addressReturned: string): void => {
|
2021-03-14 09:19:25 +01:00
|
|
|
this.loggingService.sendInfoLevelMessage(`Loaded network contracts ${addressReturned}`);
|
2021-03-16 11:08:18 +01:00
|
|
|
this.readyStateProcessor(settings, readyStateElements.network, address, offset, limit);
|
2020-11-25 08:51:15 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
readyStateProcessor(
|
|
|
|
settings: Settings,
|
|
|
|
bit: number,
|
|
|
|
address: string,
|
|
|
|
offset: number,
|
|
|
|
limit: number
|
|
|
|
): void {
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
2020-11-25 08:51:15 +01:00
|
|
|
this.readyState |= bit;
|
|
|
|
if (this.readyStateTarget === this.readyState && this.readyStateTarget) {
|
2021-04-29 19:10:39 +02:00
|
|
|
const wHeadSync: Worker = new Worker('./../assets/js/block-sync/head.js');
|
2021-01-15 05:30:19 +01:00
|
|
|
wHeadSync.onmessage = (m) => {
|
|
|
|
settings.txHelper.processReceipt(m.data);
|
|
|
|
};
|
|
|
|
wHeadSync.postMessage({
|
|
|
|
w3_provider: settings.w3.provider,
|
|
|
|
});
|
2021-03-16 11:08:18 +01:00
|
|
|
if (address === null) {
|
2021-05-10 18:15:25 +02:00
|
|
|
this.transactionService
|
|
|
|
.getAllTransactions(offset, limit)
|
|
|
|
.pipe(first())
|
|
|
|
.subscribe((res) => {
|
|
|
|
this.fetcher(settings, res);
|
|
|
|
});
|
2021-03-16 11:08:18 +01:00
|
|
|
} else {
|
2021-05-10 18:15:25 +02:00
|
|
|
this.transactionService
|
|
|
|
.getAddressTransactions(address, offset, limit)
|
|
|
|
.pipe(first())
|
|
|
|
.subscribe((res) => {
|
|
|
|
this.fetcher(settings, res);
|
|
|
|
});
|
2021-03-16 11:08:18 +01:00
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:10:39 +02:00
|
|
|
newTransferEvent(tx: any): any {
|
2020-11-25 08:51:15 +01:00
|
|
|
return new CustomEvent('cic_transfer', {
|
|
|
|
detail: {
|
|
|
|
tx,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:10:39 +02:00
|
|
|
newConversionEvent(tx: any): any {
|
2020-11-25 08:51:15 +01:00
|
|
|
return new CustomEvent('cic_convert', {
|
|
|
|
detail: {
|
|
|
|
tx,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
async scan(
|
|
|
|
settings: Settings,
|
|
|
|
lo: number,
|
|
|
|
hi: number,
|
|
|
|
bloomBlockBytes: Uint8Array,
|
|
|
|
bloomBlocktxBytes: Uint8Array,
|
|
|
|
bloomRounds: any
|
|
|
|
): Promise<void> {
|
2021-04-29 19:10:39 +02:00
|
|
|
const w: Worker = new Worker('./../assets/js/block-sync/ondemand.js');
|
2020-11-25 08:51:15 +01:00
|
|
|
w.onmessage = (m) => {
|
|
|
|
settings.txHelper.processReceipt(m.data);
|
|
|
|
};
|
|
|
|
w.postMessage({
|
|
|
|
w3_provider: settings.w3.provider,
|
|
|
|
lo,
|
|
|
|
hi,
|
2021-05-10 18:15:25 +02:00
|
|
|
filters: [bloomBlockBytes, bloomBlocktxBytes],
|
2020-11-25 08:51:15 +01:00
|
|
|
filter_rounds: bloomRounds,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:08:18 +01:00
|
|
|
fetcher(settings: Settings, transactionsInfo: any): void {
|
2021-04-29 19:10:39 +02:00
|
|
|
const blockFilterBinstr: string = window.atob(transactionsInfo.block_filter);
|
|
|
|
const bOne: Uint8Array = new Uint8Array(blockFilterBinstr.length);
|
2021-05-10 18:15:25 +02:00
|
|
|
bOne.map((e, i, v) => (v[i] = blockFilterBinstr.charCodeAt(i)));
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-04-29 19:10:39 +02:00
|
|
|
const blocktxFilterBinstr: string = window.atob(transactionsInfo.blocktx_filter);
|
|
|
|
const bTwo: Uint8Array = new Uint8Array(blocktxFilterBinstr.length);
|
2021-05-10 18:15:25 +02:00
|
|
|
bTwo.map((e, i, v) => (v[i] = blocktxFilterBinstr.charCodeAt(i)));
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
settings.scanFilter(
|
|
|
|
settings,
|
|
|
|
transactionsInfo.low,
|
|
|
|
transactionsInfo.high,
|
|
|
|
bOne,
|
|
|
|
bTwo,
|
|
|
|
transactionsInfo.filter_rounds
|
|
|
|
);
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
}
|