Query transaction participants from meta service.

This commit is contained in:
Spencer Ofwiti 2021-03-04 18:56:14 +03:00
parent 70fe7cf08e
commit 2b99dd8c62
4 changed files with 27 additions and 29 deletions

View File

@ -1,9 +1,9 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {map} from 'rxjs/operators'; import {first, map} from 'rxjs/operators';
import {BehaviorSubject, Observable} from 'rxjs'; import {BehaviorSubject, Observable} from 'rxjs';
import {environment} from '@src/environments/environment'; import {environment} from '@src/environments/environment';
import {User} from 'cic-client-meta'; import {Envelope, User} from 'cic-client-meta';
import {UserService} from '@app/_services/user.service'; import {UserService} from '@app/_services/user.service';
import { AccountIndex } from '@app/_helpers'; import { AccountIndex } from '@app/_helpers';
import { Keccak } from 'sha3'; import { Keccak } from 'sha3';
@ -14,6 +14,7 @@ import {toValue} from '@src/assets/js/ethtx/dist/tx';
import * as secp256k1 from 'secp256k1'; import * as secp256k1 from 'secp256k1';
import {AuthService} from '@app/_services/auth.service'; import {AuthService} from '@app/_services/auth.service';
const Web3 = require('web3'); const Web3 = require('web3');
const vCard = require('vcard-parser');
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -46,28 +47,21 @@ export class TransactionService {
})); }));
} }
setTransaction(transaction, cacheSize: number): Promise<void> { async setTransaction(transaction, cacheSize: number): Promise<void> {
const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash); const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash);
if (cachedTransaction) { return; } if (cachedTransaction) { return; }
transaction.type = 'transaction'; transaction.type = 'transaction';
this.getUser(transaction.from).then(async () => { transaction.sender = await this.getUser(transaction.from);
transaction.sender = this.userInfo; transaction.recipient = await this.getUser(transaction.to);
this.getUser(transaction.to).then(async () => { await this.addTransaction(transaction, cacheSize);
transaction.recipient = this.userInfo;
await this.addTransaction(transaction, cacheSize);
});
});
} }
setConversion(conversion, cacheSize): void { async setConversion(conversion, cacheSize): Promise<void> {
const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash); const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash);
if (cachedConversion) { return; } if (cachedConversion) { return; }
conversion.type = 'conversion'; conversion.type = 'conversion';
this.getUser(conversion.trader).then(async () => { conversion.sender = conversion.recipient = await this.getUser(conversion.trader);
conversion.sender = this.userInfo; await this.addTransaction(conversion, cacheSize);
conversion.recipient = this.userInfo;
await this.addTransaction(conversion, cacheSize);
});
} }
async addTransaction(transaction, cacheSize: number): Promise<void> { async addTransaction(transaction, cacheSize: number): Promise<void> {
@ -76,12 +70,16 @@ export class TransactionService {
this.transactions.length = cacheSize; this.transactions.length = cacheSize;
} }
this.transactionList.next(this.transactions); 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> { async getUser(address: string): Promise<any> {
this.userInfo = this.userService.getUser(await User.toKey(address)); 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;
});
} }
async transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Promise<any> { async transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Promise<any> {

View File

@ -50,12 +50,12 @@ export class AppComponent {
@HostListener('window:cic_transfer', ['$event']) @HostListener('window:cic_transfer', ['$event'])
cicTransfer(event: CustomEvent): void { cicTransfer(event: CustomEvent): void {
const transaction = event.detail.tx; const transaction = event.detail.tx;
this.transactionService.setTransaction(transaction, 100); this.transactionService.setTransaction(transaction, 100).then();
} }
@HostListener('window:cic_convert', ['$event']) @HostListener('window:cic_convert', ['$event'])
cicConvert(event: CustomEvent): void { cicConvert(event: CustomEvent): void {
const conversion = event.detail.tx; const conversion = event.detail.tx;
this.transactionService.setConversion(conversion, 100); this.transactionService.setConversion(conversion, 100).then();
} }
} }

View File

@ -12,12 +12,12 @@
<h4>Exchange: </h4> <h4>Exchange: </h4>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
<li class="list-group-item"> <li class="list-group-item">
<span>Sender: {{transaction.sender?.vcard.fn}}</span><br><br> <span>Sender: {{transaction.sender?.vcard.fn[0].value}}</span><br><br>
<span>Sender Address: {{transaction.from}}</span><br><br> <span>Sender Address: {{transaction.from}}</span><br><br>
<button mat-raised-button color="primary" class="btn btn-outline-info" routerLink="/accounts/1">View Sender</button> <button mat-raised-button color="primary" class="btn btn-outline-info" routerLink="/accounts/1">View Sender</button>
</li> </li>
<li class="list-group-item"> <li class="list-group-item">
<span>Recipient: {{transaction.recipient?.vcard.fn}}</span><br><br> <span>Recipient: {{transaction.recipient?.vcard.fn[0].value}}</span><br><br>
<span>Recipient Address: {{transaction.to}}</span><br><br> <span>Recipient Address: {{transaction.to}}</span><br><br>
<button mat-raised-button color="primary" class="btn btn-outline-info" routerLink="/accounts/1">View Recipient</button> <button mat-raised-button color="primary" class="btn btn-outline-info" routerLink="/accounts/1">View Recipient</button>
</li> </li>
@ -70,7 +70,7 @@
<h3>Exchange: </h3> <h3>Exchange: </h3>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
<li class="list-group-item"> <li class="list-group-item">
<span><strong>Trader: {{transaction.sender?.vcard.fn}}</strong></span> <span><strong>Trader: {{transaction.sender?.vcard.fn[0].value}}</strong></span>
</li> </li>
<li class="list-group-item"> <li class="list-group-item">
<span>Trader Address: {{transaction.trader}}</span> <span>Trader Address: {{transaction.trader}}</span>

View File

@ -68,22 +68,22 @@
<ng-container matColumnDef="sender"> <ng-container matColumnDef="sender">
<mat-header-cell *matHeaderCellDef mat-sort-header> Sender </mat-header-cell> <mat-header-cell *matHeaderCellDef mat-sort-header> Sender </mat-header-cell>
<mat-cell *matCellDef="let transaction"> {{transaction.sender?.vcard.fn}} </mat-cell> <mat-cell *matCellDef="let transaction"> {{transaction.sender?.vcard.fn[0].value}} </mat-cell>
</ng-container> </ng-container>
<ng-container matColumnDef="senderLocation"> <ng-container matColumnDef="senderLocation">
<mat-header-cell *matHeaderCellDef mat-sort-header> Sender Location </mat-header-cell> <mat-header-cell *matHeaderCellDef mat-sort-header> Sender Location </mat-header-cell>
<mat-cell *matCellDef="let transaction"> Miyani </mat-cell> <mat-cell *matCellDef="let transaction"> {{transaction.sender?.location.area_name}} </mat-cell>
</ng-container> </ng-container>
<ng-container matColumnDef="recipient"> <ng-container matColumnDef="recipient">
<mat-header-cell *matHeaderCellDef mat-sort-header> Recipient </mat-header-cell> <mat-header-cell *matHeaderCellDef mat-sort-header> Recipient </mat-header-cell>
<mat-cell *matCellDef="let transaction"> {{transaction.recipient?.vcard.fn}} </mat-cell> <mat-cell *matCellDef="let transaction"> {{transaction.recipient?.vcard.fn[0].value}} </mat-cell>
</ng-container> </ng-container>
<ng-container matColumnDef="recipientLocation"> <ng-container matColumnDef="recipientLocation">
<mat-header-cell *matHeaderCellDef mat-sort-header> Recipient Location </mat-header-cell> <mat-header-cell *matHeaderCellDef mat-sort-header> Recipient Location </mat-header-cell>
<mat-cell *matCellDef="let transaction"> Kayaba </mat-cell> <mat-cell *matCellDef="let transaction"> {{transaction.recipient?.location.area_name}} </mat-cell>
</ng-container> </ng-container>
<ng-container matColumnDef="token"> <ng-container matColumnDef="token">