Add support for account index.
This commit is contained in:
47
src/app/_helpers/accountIndex.ts
Normal file
47
src/app/_helpers/accountIndex.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { AccountRegistry } from '../../assets/js/eth_account_index';
|
||||
import * as accountIndex from '../../assets/json/accountIndex.abi.json';
|
||||
import {environment} from '../../environments/environment';
|
||||
const Web3 = require('web3');
|
||||
|
||||
const web3 = new Web3(environment.web3Provider);
|
||||
// @ts-ignore
|
||||
const abi = accountIndex.default;
|
||||
|
||||
export class AccountIndex {
|
||||
contractAddress: string;
|
||||
signerAddress: string;
|
||||
contract: any;
|
||||
|
||||
constructor(contractAddress: string, signerAddress?: string) {
|
||||
this.contractAddress = contractAddress;
|
||||
if (signerAddress) {
|
||||
this.signerAddress = signerAddress;
|
||||
}
|
||||
this.contract = new AccountRegistry(web3, abi, this.contractAddress, this.signerAddress);
|
||||
}
|
||||
|
||||
async totalAccounts(): Promise<number> {
|
||||
return await this.contract.count();
|
||||
}
|
||||
|
||||
async haveAccount(address: string): Promise<boolean> {
|
||||
return await this.contract.have(address);
|
||||
}
|
||||
|
||||
async addAccount(address: string): Promise<boolean> {
|
||||
return await this.contract.add(address);
|
||||
}
|
||||
|
||||
async last(numberOfAccounts: number): Promise<Array<string>> {
|
||||
return await this.contract.last(numberOfAccounts);
|
||||
}
|
||||
|
||||
async addToAccountRegistry(address: string): Promise<boolean> {
|
||||
if (!await this.contract.have(address)) {
|
||||
await this.contract.add(address);
|
||||
return await this.contract.have(address);
|
||||
} else {
|
||||
return await this.contract.have(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/app/_helpers/http-getter.ts
Normal file
34
src/app/_helpers/http-getter.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// function httpGetter(): any {
|
||||
// }
|
||||
//
|
||||
// httpGetter().prototype.get = (filename: string) => new Promise((whohoo, doh) => {
|
||||
// const xhr = new XMLHttpRequest();
|
||||
// xhr.addEventListener('load', (e) => {
|
||||
// if (xhr.status === 200) {
|
||||
// whohoo(xhr.responseText);
|
||||
// return;
|
||||
// }
|
||||
// doh('failed with status ' + xhr.status + ': ' + xhr.statusText);
|
||||
// });
|
||||
// xhr.open('GET', filename);
|
||||
// xhr.send();
|
||||
// });
|
||||
//
|
||||
// class HttpGetter {
|
||||
// async get(filename: string): Promise<any> {
|
||||
// const xhr = new XMLHttpRequest();
|
||||
// xhr.addEventListener('load', (e) => {
|
||||
// if (xhr.status === 200) {
|
||||
// // whohoo(xhr.responseText);
|
||||
// return;
|
||||
// }
|
||||
// // doh('failed with status ' + xhr.status + ': ' + xhr.statusText);
|
||||
// });
|
||||
// xhr.open('GET', filename);
|
||||
// xhr.send();
|
||||
// }
|
||||
// }
|
||||
// export {
|
||||
// httpGetter,
|
||||
// HttpGetter
|
||||
// };
|
||||
@@ -1,2 +1,6 @@
|
||||
export * from './mock-backend';
|
||||
export * from './array-sum';
|
||||
export * from './accountIndex';
|
||||
export {AccountIndex} from './accountIndex';
|
||||
export * from './http-getter';
|
||||
|
||||
|
||||
2
src/app/_models/staff.ts
Normal file
2
src/app/_models/staff.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export interface Staff {
|
||||
}
|
||||
15
src/app/_models/token.ts
Normal file
15
src/app/_models/token.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface Token {
|
||||
name: string;
|
||||
symbol: string;
|
||||
address: string;
|
||||
supply: string;
|
||||
decimals: string;
|
||||
reserves: {
|
||||
'0xa686005CE37Dce7738436256982C3903f2E4ea8E'?: {
|
||||
weight: string;
|
||||
balance: string;
|
||||
}
|
||||
};
|
||||
reserveRatio?: string;
|
||||
owner?: string;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {Component, HostListener, OnInit} from '@angular/core';
|
||||
import {BlockSyncService, TransactionService} from './_services';
|
||||
import {User} from 'cic-client-meta';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
@@ -9,7 +8,6 @@ import {User} from 'cic-client-meta';
|
||||
})
|
||||
export class AppComponent implements OnInit {
|
||||
title = 'cic-staff-client';
|
||||
registryAddress: string = '0xb708175e3f6Cd850643aAF7B32212AFad50e2549';
|
||||
readyStateTarget: number = 3;
|
||||
readyState: number = 0;
|
||||
mediaQuery = window.matchMedia('(max-width: 768px)');
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
<mat-option value="group">GROUPACCOUNT</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
<button mat-raised-button color="primary" routerLink="/accounts/create" type="button" class="btn btn-outline-primary ml-auto">
|
||||
<mat-icon>add</mat-icon>
|
||||
<button mat-raised-button color="primary" routerLink="/accounts/create" type="button" class="btn btn-outline-primary ml-auto">
|
||||
<mat-icon>add</mat-icon>
|
||||
ADD NEW </button>
|
||||
<button mat-raised-button color="primary" routerLink="/accounts/export" type="button" class="btn btn-outline-primary ml-2"> EXPORT </button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user