zgo/src/app/fullnode.service.ts

99 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-10-15 19:14:49 +00:00
import {Injectable} from '@angular/core';
2023-05-08 17:02:03 +00:00
import {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 { ConfigData } from './configdata';
2022-05-18 20:51:39 +00:00
var Buffer = require('buffer/').Buffer;
2021-10-15 19:14:49 +00:00
//import {User} from './user.model';
@Injectable({providedIn: 'root'})
export class FullnodeService{
beUrl = ConfigData.Be_URL;
2023-05-08 17:02:03 +00:00
private session: null|string = '';
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-11-09 18:39:16 +00:00
private reqHeaders: HttpHeaders;
2023-05-08 17:02:03 +00:00
private params : HttpParams;
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-18 22:40:20 +00:00
paid: false,
2022-05-18 20:51:39 +00:00
zats: false,
invoices: false,
2022-07-14 16:11:04 +00:00
expiration: new Date(Date.now()).toISOString(),
2022-07-18 20:29:27 +00:00
payconf: false,
crmToken: '',
2023-10-20 13:26:37 +00:00
viewkey: '',
tips: false
2021-11-22 20:37:45 +00:00
};
2021-10-15 19:14:49 +00:00
constructor(private http: HttpClient, public userService: UserService){
2023-05-08 17:02:03 +00:00
this.session = localStorage.getItem('s4z_token');
this.params = new HttpParams().append('session', this.session!);
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
2022-05-18 20:51:39 +00:00
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
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(){
let obs = this.http.get<{message: string, height: number}>(this.beUrl+'blockheight', { headers: this.reqHeaders, params: this.params });
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';
2023-05-08 17:02:03 +00:00
const params = this.params.append('currency', currency);
let obs = this.http.get<{message: string, price: any}>(this.beUrl+'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() {
let obs = this.http.get<{message: string, addr: string}>(this.beUrl+'getaddr', { headers: this.reqHeaders, params: this.params });
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
}
}