2020-11-04 13:35:20 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2020-11-25 08:51:15 +01:00
|
|
|
import {first, map} from 'rxjs/operators';
|
|
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
2020-11-04 13:35:20 +01:00
|
|
|
import {environment} from '../../environments/environment';
|
2020-11-08 07:31:52 +01:00
|
|
|
import {Conversion, Transaction} from '../_models';
|
2020-11-25 08:51:15 +01:00
|
|
|
import {User} from 'cic-client-meta';
|
|
|
|
import {UserService} from './user.service';
|
|
|
|
import {parse} from '../../assets/js/parse-vcard';
|
2020-11-04 13:35:20 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class TransactionService {
|
2020-11-08 07:31:52 +01:00
|
|
|
transactions: Transaction[] = [];
|
|
|
|
conversions: Conversion[] = [];
|
2020-11-25 08:51:15 +01:00
|
|
|
private transactionList = new BehaviorSubject<Transaction[]>(this.transactions);
|
|
|
|
transactionsSubject = this.transactionList.asObservable();
|
|
|
|
private conversionList = new BehaviorSubject<Conversion[]>(this.conversions);
|
|
|
|
conversionsSubject = this.conversionList.asObservable();
|
|
|
|
userInfo: any;
|
2020-11-04 13:35:20 +01:00
|
|
|
|
2020-11-25 08:51:15 +01:00
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
|
|
|
private userService: UserService
|
|
|
|
) { }
|
2020-11-04 13:35:20 +01:00
|
|
|
|
|
|
|
getAllTransactions(offset: number, limit: number): Observable<any> {
|
|
|
|
return this.http.get(`${environment.cicCacheUrl}/tx/${offset}/${limit}`)
|
|
|
|
.pipe(map(response => {
|
|
|
|
return response;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddressTransactions(address: string, offset: number, limit: number): Observable<any> {
|
|
|
|
return this.http.get(`${environment.cicCacheUrl}/tx/${address}/${offset}/${limit}`)
|
|
|
|
.pipe(map(response => {
|
|
|
|
return response;
|
|
|
|
}));
|
|
|
|
}
|
2020-11-08 07:31:52 +01:00
|
|
|
|
2020-11-25 08:51:15 +01:00
|
|
|
setTransaction(transaction, cacheSize: number): void {
|
|
|
|
const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash);
|
|
|
|
if (cachedTransaction) { return; }
|
|
|
|
this.getUser(transaction.from).then(() => {
|
|
|
|
transaction.sender = this.userInfo;
|
|
|
|
this.getUser(transaction.to).then(() => {
|
|
|
|
transaction.recipient = this.userInfo;
|
|
|
|
this.transactions.unshift(transaction);
|
|
|
|
if (this.transactions.length > cacheSize) {
|
|
|
|
this.transactions.length = cacheSize;
|
|
|
|
}
|
|
|
|
this.transactionList.next(this.transactions);
|
|
|
|
});
|
|
|
|
});
|
2020-11-08 07:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setConversion(conversion): void {
|
2020-11-25 08:51:15 +01:00
|
|
|
const cachedConversion = this.conversions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash);
|
|
|
|
if (cachedConversion) { return; }
|
|
|
|
this.getUser(conversion.trader).then(() => {
|
|
|
|
conversion.user = this.userInfo;
|
|
|
|
this.conversions.push(conversion);
|
|
|
|
this.conversionList.next(this.conversions);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUser(address: string): Promise<void> {
|
|
|
|
this.userService.getUser(await User.toKey(address)).pipe(first()).subscribe(res => {
|
|
|
|
const vcard = parse(atob(res.vcard));
|
|
|
|
res.vcard = vcard;
|
|
|
|
this.userInfo = res;
|
|
|
|
}, error => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2020-11-08 07:31:52 +01:00
|
|
|
}
|
2020-11-04 13:35:20 +01:00
|
|
|
}
|