Add services.
- Add block sync service. - Add mock token service. - Add mock user service. - Add user info to transactions.
This commit is contained in:
108
src/app/_services/block-sync.service.ts
Normal file
108
src/app/_services/block-sync.service.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
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';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class BlockSyncService {
|
||||
registryAddress: string = '0xb708175e3f6Cd850643aAF7B32212AFad50e2549';
|
||||
readyStateTarget: number = 3;
|
||||
readyState: number = 0;
|
||||
|
||||
constructor(private transactionService: TransactionService) { }
|
||||
|
||||
blockSync(): any {
|
||||
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, abi);
|
||||
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);
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user