From 2b99dd8c62162096fe315060fd4240f49e865a64 Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Thu, 4 Mar 2021 18:56:14 +0300 Subject: [PATCH] Query transaction participants from meta service. --- src/app/_services/transaction.service.ts | 38 +++++++++---------- src/app/app.component.ts | 4 +- .../transaction-details.component.html | 6 +-- .../transactions/transactions.component.html | 8 ++-- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/app/_services/transaction.service.ts b/src/app/_services/transaction.service.ts index 3bba720..c3e08f6 100644 --- a/src/app/_services/transaction.service.ts +++ b/src/app/_services/transaction.service.ts @@ -1,9 +1,9 @@ import { Injectable } from '@angular/core'; import {HttpClient} from '@angular/common/http'; -import {map} from 'rxjs/operators'; +import {first, map} from 'rxjs/operators'; import {BehaviorSubject, Observable} from 'rxjs'; 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 { AccountIndex } from '@app/_helpers'; import { Keccak } from 'sha3'; @@ -14,6 +14,7 @@ import {toValue} from '@src/assets/js/ethtx/dist/tx'; import * as secp256k1 from 'secp256k1'; import {AuthService} from '@app/_services/auth.service'; const Web3 = require('web3'); +const vCard = require('vcard-parser'); @Injectable({ providedIn: 'root' @@ -46,28 +47,21 @@ export class TransactionService { })); } - setTransaction(transaction, cacheSize: number): Promise { + async setTransaction(transaction, cacheSize: number): Promise { const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash); if (cachedTransaction) { return; } transaction.type = 'transaction'; - this.getUser(transaction.from).then(async () => { - transaction.sender = this.userInfo; - this.getUser(transaction.to).then(async () => { - transaction.recipient = this.userInfo; - await this.addTransaction(transaction, cacheSize); - }); - }); + transaction.sender = await this.getUser(transaction.from); + transaction.recipient = await this.getUser(transaction.to); + await this.addTransaction(transaction, cacheSize); } - setConversion(conversion, cacheSize): void { + async setConversion(conversion, cacheSize): Promise { const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash); if (cachedConversion) { return; } conversion.type = 'conversion'; - this.getUser(conversion.trader).then(async () => { - conversion.sender = this.userInfo; - conversion.recipient = this.userInfo; - await this.addTransaction(conversion, cacheSize); - }); + conversion.sender = conversion.recipient = await this.getUser(conversion.trader); + await this.addTransaction(conversion, cacheSize); } async addTransaction(transaction, cacheSize: number): Promise { @@ -76,12 +70,16 @@ export class TransactionService { 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 { - this.userInfo = this.userService.getUser(await User.toKey(address)); + async getUser(address: string): Promise { + 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 { diff --git a/src/app/app.component.ts b/src/app/app.component.ts index cface6a..0e797c9 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -50,12 +50,12 @@ export class AppComponent { @HostListener('window:cic_transfer', ['$event']) cicTransfer(event: CustomEvent): void { const transaction = event.detail.tx; - this.transactionService.setTransaction(transaction, 100); + this.transactionService.setTransaction(transaction, 100).then(); } @HostListener('window:cic_convert', ['$event']) cicConvert(event: CustomEvent): void { const conversion = event.detail.tx; - this.transactionService.setConversion(conversion, 100); + this.transactionService.setConversion(conversion, 100).then(); } } diff --git a/src/app/pages/transactions/transaction-details/transaction-details.component.html b/src/app/pages/transactions/transaction-details/transaction-details.component.html index 041d01b..65d6822 100644 --- a/src/app/pages/transactions/transaction-details/transaction-details.component.html +++ b/src/app/pages/transactions/transaction-details/transaction-details.component.html @@ -12,12 +12,12 @@

Exchange:

  • - Sender: {{transaction.sender?.vcard.fn}}

    + Sender: {{transaction.sender?.vcard.fn[0].value}}

    Sender Address: {{transaction.from}}

  • - Recipient: {{transaction.recipient?.vcard.fn}}

    + Recipient: {{transaction.recipient?.vcard.fn[0].value}}

    Recipient Address: {{transaction.to}}

  • @@ -70,7 +70,7 @@

    Exchange:

    • - Trader: {{transaction.sender?.vcard.fn}} + Trader: {{transaction.sender?.vcard.fn[0].value}}
    • Trader Address: {{transaction.trader}} diff --git a/src/app/pages/transactions/transactions.component.html b/src/app/pages/transactions/transactions.component.html index 9466570..1047b54 100644 --- a/src/app/pages/transactions/transactions.component.html +++ b/src/app/pages/transactions/transactions.component.html @@ -68,22 +68,22 @@ Sender - {{transaction.sender?.vcard.fn}} + {{transaction.sender?.vcard.fn[0].value}} Sender Location - Miyani + {{transaction.sender?.location.area_name}} Recipient - {{transaction.recipient?.vcard.fn}} + {{transaction.recipient?.vcard.fn[0].value}} Recipient Location - Kayaba + {{transaction.recipient?.location.area_name}}