zgo/src/app/fullnode.service.ts

96 lines
3.2 KiB
TypeScript
Raw Normal View History

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';
2021-11-22 20:37:45 +00:00
import { Owner } from './owner.model';
2021-10-15 19:14:49 +00:00
//import {User} from './user.model';
@Injectable({providedIn: 'root'})
export class FullnodeService{
2021-11-08 20:37:26 +00:00
beUrl = 'http://localhost:3000/';
private dataStore: { height: number, memoList: string[], addr: string, price: number } = { height: 0, memoList: [], addr: '', price:0 };
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);
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();
public readonly priceUpdate: Observable<number> = this._priceUpdated.asObservable();
2021-11-22 20:37:45 +00:00
public readonly ownerUpdate: Observable<Owner>;
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-11-22 20:37:45 +00:00
private owner: Owner = {
_id: '',
name: '',
address: '',
currency: 'usd',
tax: false,
taxValue: 0,
vat: false,
2022-01-17 20:49:08 +00:00
vatValue: 0,
2022-01-28 20:03:35 +00:00
first: '',
last: '',
2022-01-17 20:49:08 +00:00
email: '',
street: '',
city: '',
state: '',
postal: '',
phone: '',
2022-01-18 22:40:20 +00:00
paid: false,
2022-01-19 16:26:25 +00:00
website: '',
country: ''
2021-11-22 20:37:45 +00:00
};
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-11-22 20:37:45 +00:00
this.ownerUpdate = userService.ownerUpdate;
this.getAddr();
this.getHeight();
2021-11-22 20:37:45 +00:00
this.ownerUpdate.subscribe((owner) => {
this.owner = owner;
this.getPrice(this.owner.currency);
});
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) => {
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-11-22 20:37:45 +00:00
getPrice(currency: string){
//var currency = 'usd';
2021-11-02 21:13:24 +00:00
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'});
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);
}
});
return obs;
}
2021-10-20 20:51:14 +00:00
2021-10-15 19:14:49 +00:00
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 });
obs.subscribe((AddrData) => {
this.dataStore.addr = AddrData.addr;
this._addrUpdated.next(Object.assign({}, this.dataStore).addr);
2021-10-15 19:14:49 +00:00
});
return obs;
2021-10-15 19:14:49 +00:00
}
}