2021-10-15 19:14:49 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-10-20 20:51:14 +00:00
|
|
|
import {Subject, Subscription, BehaviorSubject, Observable} from 'rxjs';
|
2021-11-09 18:39:16 +00:00
|
|
|
import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
|
2021-10-15 19:14:49 +00:00
|
|
|
import {UserService} from './user.service';
|
|
|
|
|
|
|
|
//import {User} from './user.model';
|
|
|
|
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
export class FullnodeService{
|
2021-11-08 20:37:26 +00:00
|
|
|
beUrl = 'http://localhost:3000/';
|
2021-10-22 22:20:36 +00:00
|
|
|
private dataStore: { height: number, memoList: string[], addr: string, price: number } = { height: 0, memoList: [], addr: '', price:0 };
|
2021-10-21 15:23:58 +00:00
|
|
|
private _heightUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.height);
|
|
|
|
private _memoUpdated: BehaviorSubject<string[]> = new BehaviorSubject(this.dataStore.memoList);
|
|
|
|
private _addrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.dataStore.addr);
|
2021-10-22 22:20:36 +00:00
|
|
|
private _priceUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.price);
|
2021-10-20 20:51:14 +00:00
|
|
|
public readonly addrUpdate: Observable<string> = this._addrUpdated.asObservable();
|
|
|
|
public readonly heightUpdate: Observable<number> = this._heightUpdated.asObservable();
|
|
|
|
public readonly memoUpdate: Observable<string[]> = this._memoUpdated.asObservable();
|
2021-10-22 22:20:36 +00:00
|
|
|
public readonly priceUpdate: Observable<number> = this._priceUpdated.asObservable();
|
2021-10-20 20:51:14 +00:00
|
|
|
private UserSub: Subscription = new Subscription();
|
2021-11-09 18:39:16 +00:00
|
|
|
private apiKey = 'Le2adeic8Thah4Aeng4daem6i';
|
|
|
|
private reqHeaders: HttpHeaders;
|
2021-10-15 19:14:49 +00:00
|
|
|
|
|
|
|
constructor(private http: HttpClient, public userService: UserService){
|
2021-11-09 18:39:16 +00:00
|
|
|
this.reqHeaders = new HttpHeaders().set('Authorization', this.apiKey);
|
2021-10-21 15:23:58 +00:00
|
|
|
this.getAddr();
|
|
|
|
this.getHeight();
|
2021-10-22 22:20:36 +00:00
|
|
|
this.getPrice();
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
getHeight(){
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, height: number}>(this.beUrl+'api/blockheight', { headers: this.reqHeaders });
|
2021-10-20 20:51:14 +00:00
|
|
|
obs.subscribe((BlockData) => {
|
2021-10-21 15:23:58 +00:00
|
|
|
this.dataStore.height = BlockData.height;
|
|
|
|
this._heightUpdated.next(Object.assign({}, this.dataStore).height);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 22:20:36 +00:00
|
|
|
getPrice(){
|
2021-11-02 21:13:24 +00:00
|
|
|
var currency = 'usd';
|
|
|
|
const params = new HttpParams().append('currency', currency);
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, price: any}>(this.beUrl+'api/price', { headers:this.reqHeaders, params: params, observe: 'response'});
|
2021-10-22 22:20:36 +00:00
|
|
|
obs.subscribe((PriceData) => {
|
2021-11-02 21:13:24 +00:00
|
|
|
if (PriceData.status == 200) {
|
|
|
|
this.dataStore.price = PriceData.body!.price.price;
|
|
|
|
console.log("price", this.dataStore.price);
|
|
|
|
this._priceUpdated.next(Object.assign({},this.dataStore).price);
|
|
|
|
} else {
|
|
|
|
console.log('No price found for currency', currency);
|
|
|
|
}
|
2021-10-22 22:20:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
2021-10-20 20:51:14 +00:00
|
|
|
|
2021-10-15 19:14:49 +00:00
|
|
|
hexToString(hexString: string) {
|
|
|
|
var str = '';
|
|
|
|
for (var n=0; n < hexString.length; n +=2) {
|
|
|
|
str += String.fromCharCode(parseInt(hexString.substr(n, 2), 16));
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getAddr() {
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, addr: string}>(this.beUrl+'api/getaddr', { headers: this.reqHeaders });
|
2021-10-21 15:23:58 +00:00
|
|
|
|
|
|
|
obs.subscribe((AddrData) => {
|
|
|
|
this.dataStore.addr = AddrData.addr;
|
|
|
|
this._addrUpdated.next(Object.assign({}, this.dataStore).addr);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
2021-10-21 15:23:58 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|