Add support for account index.

This commit is contained in:
Spencer Ofwiti
2021-01-15 07:30:19 +03:00
parent f7f4fe77f6
commit 4d920cdbe9
31 changed files with 1117 additions and 17 deletions

View File

@@ -4,27 +4,37 @@ 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';
import {httpGetter} from '../_helpers';
const cic = require('../../assets/js/block-sync/cic-client.web');
@Injectable({
providedIn: 'root'
})
export class BlockSyncService {
registryAddress: string = '0xb708175e3f6Cd850643aAF7B32212AFad50e2549';
readyStateTarget: number = 3;
// 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 = '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, this.registryAddress, abi);
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> => {
@@ -49,6 +59,13 @@ export class BlockSyncService {
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);
}
}

View File

@@ -6,6 +6,7 @@ import {environment} from '../../environments/environment';
import {User} from 'cic-client-meta';
import {UserService} from './user.service';
import {parse} from '../../assets/js/parse-vcard';
import { AccountIndex } from '../_helpers';
@Injectable({
providedIn: 'root'
@@ -15,6 +16,7 @@ export class TransactionService {
private transactionList = new BehaviorSubject<any[]>(this.transactions);
transactionsSubject = this.transactionList.asObservable();
userInfo: any;
request = new AccountIndex(environment.contractAddress, environment.signerAddress);
constructor(
private http: HttpClient,
@@ -35,15 +37,17 @@ export class TransactionService {
}));
}
setTransaction(transaction, cacheSize: number): void {
setTransaction(transaction, cacheSize: number): Promise<void> {
const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash);
if (cachedTransaction) { return; }
transaction.type = 'transaction';
this.getUser(transaction.from).then(() => {
this.getUser(transaction.from).then(async () => {
transaction.sender = this.userInfo;
this.getUser(transaction.to).then(() => {
console.log('Sender address ', transaction.from, ' status: ', await this.request.addToAccountRegistry(transaction.from));
this.getUser(transaction.to).then(async () => {
transaction.recipient = this.userInfo;
this.addTransaction(transaction, cacheSize);
console.log('Recipient address ', transaction.to, ' status: ', await this.request.addToAccountRegistry(transaction.to));
await this.addTransaction(transaction, cacheSize);
});
});
}
@@ -52,19 +56,22 @@ export class TransactionService {
const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash);
if (cachedConversion) { return; }
conversion.type = 'conversion';
this.getUser(conversion.trader).then(() => {
this.getUser(conversion.trader).then(async () => {
conversion.sender = this.userInfo;
conversion.recipient = this.userInfo;
this.addTransaction(conversion, cacheSize);
console.log('Trader address ', conversion.trader, ' status: ', await this.request.addToAccountRegistry(conversion.trader));
await this.addTransaction(conversion, cacheSize);
});
}
addTransaction(transaction, cacheSize: number): void {
async addTransaction(transaction, cacheSize: number): Promise<void> {
this.transactions.unshift(transaction);
if (this.transactions.length > cacheSize) {
this.transactions.length = cacheSize;
}
this.transactionList.next(this.transactions);
console.log('Last 10 accounts are: ', await this.request.last(10));
console.log('Total number of accounts: ', await this.request.totalAccounts());
}
async getUser(address: string): Promise<void> {