Add fetching of transaction participants from cic-meta.
Add token registry to query tokens.
This commit is contained in:
@@ -3,11 +3,18 @@ import {HttpClient} from '@angular/common/http';
|
||||
import {environment} from '@src/environments/environment';
|
||||
import {first} from 'rxjs/operators';
|
||||
import {BehaviorSubject} from 'rxjs';
|
||||
import {HttpGetter, Registry, TokenRegistry} from '@app/_helpers';
|
||||
import {CICRegistry} from 'cic-client';
|
||||
import Web3 from 'web3';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TokenService {
|
||||
web3 = new Web3(environment.web3Provider);
|
||||
fileGetter = new HttpGetter();
|
||||
registry = new Registry(environment.registryAddress);
|
||||
cicRegistry = new CICRegistry(this.web3, environment.registryAddress, this.fileGetter, ['../../assets/js/block-sync/data']);
|
||||
tokens: any = '';
|
||||
private tokensList = new BehaviorSubject<any>(this.tokens);
|
||||
tokensSubject = this.tokensList.asObservable();
|
||||
@@ -16,11 +23,19 @@ export class TokenService {
|
||||
private http: HttpClient
|
||||
) { }
|
||||
|
||||
getTokens(): any {
|
||||
this.http.get(`${environment.cicCacheUrl}/tokens`).pipe(first()).subscribe(tokens => this.tokensList.next(tokens));
|
||||
async getTokens(): Promise<any> {
|
||||
const tokenRegistryQuery = new TokenRegistry(await this.registry.addressOf('TokenRegistry'));
|
||||
const count = await tokenRegistryQuery.totalTokens();
|
||||
return Array.from({length: count}, async (v, i) => await tokenRegistryQuery.entry(i));
|
||||
}
|
||||
|
||||
getTokenBySymbol(symbol: string): any {
|
||||
return this.http.get(`${environment.cicCacheUrl}/tokens/${symbol}`);
|
||||
}
|
||||
|
||||
async getTokenBalance(address: string): Promise<number> {
|
||||
const tokenRegistryQuery = new TokenRegistry(await this.registry.addressOf('TokenRegistry'));
|
||||
const sarafuToken = await this.cicRegistry.addToken(await tokenRegistryQuery.entry(0));
|
||||
return await sarafuToken.methods.balanceOf(address).call();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import {toValue} from '@src/assets/js/ethtx/dist/tx';
|
||||
import * as secp256k1 from 'secp256k1';
|
||||
import {AuthService} from '@app/_services/auth.service';
|
||||
import {Registry} from '@app/_helpers';
|
||||
import {defaultAccount} from '@app/_models';
|
||||
const Web3 = require('web3');
|
||||
const vCard = require('vcard-parser');
|
||||
|
||||
@@ -48,20 +49,39 @@ export class TransactionService {
|
||||
}
|
||||
|
||||
async setTransaction(transaction, cacheSize: number): Promise<void> {
|
||||
const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash);
|
||||
if (cachedTransaction) { return; }
|
||||
transaction.value = Number(transaction.value);
|
||||
transaction.type = 'transaction';
|
||||
transaction.sender = await this.getUser(transaction.from);
|
||||
transaction.recipient = await this.getUser(transaction.to);
|
||||
await this.addTransaction(transaction, cacheSize);
|
||||
if (this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash)) { return; }
|
||||
try {
|
||||
this.userService.getAccountDetailsFromMeta(await User.toKey(transaction.from)).pipe(first()).subscribe(async (res) => {
|
||||
transaction.sender = this.getAccountInfo(res);
|
||||
}, error => {
|
||||
transaction.sender = defaultAccount;
|
||||
});
|
||||
this.userService.getAccountDetailsFromMeta(await User.toKey(transaction.to)).pipe(first()).subscribe(async (res) => {
|
||||
transaction.recipient = this.getAccountInfo(res);
|
||||
}, error => {
|
||||
transaction.recipient = defaultAccount;
|
||||
});
|
||||
} finally {
|
||||
await this.addTransaction(transaction, cacheSize);
|
||||
}
|
||||
}
|
||||
|
||||
async setConversion(conversion, cacheSize): Promise<void> {
|
||||
const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash);
|
||||
if (cachedConversion) { return; }
|
||||
if (this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash)) { return; }
|
||||
conversion.type = 'conversion';
|
||||
conversion.sender = conversion.recipient = await this.getUser(conversion.trader);
|
||||
await this.addTransaction(conversion, cacheSize);
|
||||
conversion.fromValue = Number(conversion.fromValue);
|
||||
conversion.toValue = Number(conversion.toValue);
|
||||
try {
|
||||
this.userService.getAccountDetailsFromMeta(await User.toKey(conversion.trader)).pipe(first()).subscribe(async (res) => {
|
||||
conversion.sender = conversion.recipient = this.getAccountInfo(res);
|
||||
}, error => {
|
||||
conversion.sender = conversion.recipient = defaultAccount;
|
||||
});
|
||||
} finally {
|
||||
await this.addTransaction(conversion, cacheSize);
|
||||
}
|
||||
}
|
||||
|
||||
async addTransaction(transaction, cacheSize: number): Promise<void> {
|
||||
@@ -72,14 +92,10 @@ export class TransactionService {
|
||||
this.transactionList.next(this.transactions);
|
||||
}
|
||||
|
||||
async getUser(address: string): Promise<any> {
|
||||
return this.userService.getAccountDetailsFromMeta(await User.toKey(address)).pipe(first()).subscribe(res => {
|
||||
const account = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
||||
const accountInfo = account.m.data;
|
||||
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
|
||||
this.userInfo = accountInfo;
|
||||
return accountInfo;
|
||||
});
|
||||
getAccountInfo(account: string): any {
|
||||
let accountInfo = Envelope.fromJSON(JSON.stringify(account)).unwrap().m.data;
|
||||
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
|
||||
return accountInfo;
|
||||
}
|
||||
|
||||
async transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Promise<any> {
|
||||
|
||||
@@ -134,7 +134,7 @@ export class UserService {
|
||||
}
|
||||
|
||||
getUser(userKey: string): any {
|
||||
this.http.get(`${environment.cicMetaUrl}/${userKey}`, { headers: this.headers }).pipe(first()).subscribe(async res => {
|
||||
return this.http.get(`${environment.cicMetaUrl}/${userKey}`, { headers: this.headers }).pipe(first()).subscribe(async res => {
|
||||
return Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
||||
});
|
||||
}
|
||||
@@ -154,19 +154,27 @@ export class UserService {
|
||||
}
|
||||
|
||||
async loadAccounts(limit: number, offset: number = 0): Promise<void> {
|
||||
this.resetAccountsList();
|
||||
const accountIndexAddress = await this.registry.addressOf('AccountRegistry');
|
||||
const accountIndexQuery = new AccountIndex(accountIndexAddress);
|
||||
const accountAddresses = await accountIndexQuery.last(await accountIndexQuery.totalAccounts());
|
||||
console.log(accountAddresses);
|
||||
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
|
||||
this.getAccountDetailsFromMeta(await User.toKey(accountAddress)).pipe(first()).subscribe(res => {
|
||||
const account = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
||||
this.accountsMeta.push(account);
|
||||
const accountInfo = account.m.data;
|
||||
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
|
||||
this.accounts.push(accountInfo);
|
||||
this.accounts.unshift(accountInfo);
|
||||
if (this.accounts.length > limit) {
|
||||
this.accounts.length = limit;
|
||||
}
|
||||
this.accountsList.next(this.accounts);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
resetAccountsList(): void {
|
||||
this.accounts = [];
|
||||
this.accountsList.next(this.accounts);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user