98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import {HttpClient} from '@angular/common/http';
|
|
import {first, map} from 'rxjs/operators';
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
|
import {environment} from '@src/environments/environment';
|
|
import {User} from 'cic-client-meta';
|
|
import {UserService} from '@app/_services/user.service';
|
|
import {parse} from '@src/assets/js/parse-vcard';
|
|
import { AccountIndex } from '@app/_helpers';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class TransactionService {
|
|
transactions: any[] = [];
|
|
private transactionList = new BehaviorSubject<any[]>(this.transactions);
|
|
transactionsSubject = this.transactionList.asObservable();
|
|
userInfo: any;
|
|
request = new AccountIndex(environment.contractAddress, environment.signerAddress);
|
|
|
|
constructor(
|
|
private http: HttpClient,
|
|
private userService: UserService
|
|
) { }
|
|
|
|
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;
|
|
}));
|
|
}
|
|
|
|
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(async () => {
|
|
transaction.sender = this.userInfo;
|
|
console.log('Sender address ', transaction.from, ' status: ', await this.request.addToAccountRegistry(transaction.from));
|
|
this.getUser(transaction.to).then(async () => {
|
|
transaction.recipient = this.userInfo;
|
|
console.log('Recipient address ', transaction.to, ' status: ', await this.request.addToAccountRegistry(transaction.to));
|
|
await this.addTransaction(transaction, cacheSize);
|
|
});
|
|
});
|
|
}
|
|
|
|
setConversion(conversion, cacheSize): void {
|
|
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;
|
|
console.log('Trader address ', conversion.trader, ' status: ', await this.request.addToAccountRegistry(conversion.trader));
|
|
await this.addTransaction(conversion, cacheSize);
|
|
});
|
|
}
|
|
|
|
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> {
|
|
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);
|
|
});
|
|
}
|
|
|
|
transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Observable<any> {
|
|
return this.http.post(
|
|
`${environment.cicEthUrl}/transfer`,
|
|
{
|
|
tokenAddress: tokenAddress,
|
|
from: senderAddress,
|
|
to: recipientAddress,
|
|
value: value,
|
|
});
|
|
}
|
|
}
|