2024-02-09 15:49:28 +01:00
|
|
|
import { Router, NavigationStart } from '@angular/router';
|
2023-12-12 17:24:58 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
|
|
|
import { StateService } from './state.service';
|
|
|
|
import { StorageService } from './storage.service';
|
|
|
|
import { MenuGroup } from '../interfaces/services.interface';
|
2024-02-10 10:31:56 +01:00
|
|
|
import { Observable, of, ReplaySubject, tap, catchError, share, filter, switchMap } from 'rxjs';
|
2023-12-12 17:24:58 +01:00
|
|
|
import { IBackendInfo } from '../interfaces/websocket.interface';
|
|
|
|
import { Acceleration, AccelerationHistoryParams } from '../interfaces/node-api.interface';
|
2024-02-27 15:20:15 +01:00
|
|
|
import { AccelerationStats } from '../components/acceleration/acceleration-stats/acceleration-stats.component';
|
2023-12-12 17:24:58 +01:00
|
|
|
|
|
|
|
export type ProductType = 'enterprise' | 'community' | 'mining_pool' | 'custom';
|
|
|
|
export interface IUser {
|
|
|
|
username: string;
|
|
|
|
email: string | null;
|
|
|
|
passwordIsSet: boolean;
|
|
|
|
snsId: string;
|
|
|
|
type: ProductType;
|
|
|
|
subscription_tag: string;
|
|
|
|
status: 'pending' | 'verified' | 'disabled';
|
|
|
|
features: string | null;
|
|
|
|
fullName: string | null;
|
|
|
|
countryCode: string | null;
|
|
|
|
imageMd5: string;
|
2023-12-15 14:48:17 +01:00
|
|
|
ogRank: number | null;
|
2023-12-12 17:24:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Todo - move to config.json
|
|
|
|
const SERVICES_API_PREFIX = `/api/v1/services`;
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ServicesApiServices {
|
2024-02-09 15:49:28 +01:00
|
|
|
apiBaseUrl: string; // base URL is protocol, hostname, and port
|
|
|
|
apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
|
2023-12-12 17:24:58 +01:00
|
|
|
|
|
|
|
userSubject$ = new ReplaySubject<IUser | null>(1);
|
2024-02-09 15:49:28 +01:00
|
|
|
currentAuth = null;
|
2023-12-12 17:24:58 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private httpClient: HttpClient,
|
|
|
|
private stateService: StateService,
|
2024-02-09 15:49:28 +01:00
|
|
|
private storageService: StorageService,
|
|
|
|
private router: Router,
|
2023-12-12 17:24:58 +01:00
|
|
|
) {
|
2024-02-09 15:49:28 +01:00
|
|
|
this.currentAuth = localStorage.getItem('auth');
|
|
|
|
|
2023-12-12 17:24:58 +01:00
|
|
|
this.apiBaseUrl = ''; // use relative URL by default
|
|
|
|
if (!stateService.isBrowser) { // except when inside AU SSR process
|
|
|
|
this.apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT;
|
|
|
|
}
|
|
|
|
this.apiBasePath = ''; // assume mainnet by default
|
|
|
|
this.stateService.networkChanged$.subscribe((network) => {
|
|
|
|
this.apiBasePath = network ? '/' + network : '';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.stateService.env.GIT_COMMIT_HASH_MEMPOOL_SPACE) {
|
|
|
|
this.getServicesBackendInfo$().subscribe(version => {
|
|
|
|
this.stateService.servicesBackendInfo$.next(version);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getUserInfo$().subscribe();
|
2024-02-10 10:31:56 +01:00
|
|
|
this.router.events.pipe(
|
|
|
|
filter((event) => event instanceof NavigationStart && this.currentAuth !== localStorage.getItem('auth')),
|
|
|
|
switchMap(() => this.getUserInfo$()),
|
|
|
|
).subscribe();
|
2023-12-12 17:24:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not call directly, userSubject$ instead
|
|
|
|
*/
|
|
|
|
private getUserInfo$() {
|
|
|
|
return this.getUserInfoApi$().pipe(
|
|
|
|
tap((user) => {
|
|
|
|
this.userSubject$.next(user);
|
|
|
|
}),
|
|
|
|
catchError((e) => {
|
|
|
|
if (e.error === 'User does not exists') {
|
|
|
|
this.userSubject$.next(null);
|
|
|
|
this.logout$().subscribe();
|
|
|
|
return of(null);
|
|
|
|
}
|
|
|
|
this.userSubject$.next(null);
|
|
|
|
return of(null);
|
|
|
|
}),
|
|
|
|
share(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not call directly, userSubject$ instead
|
|
|
|
*/
|
|
|
|
private getUserInfoApi$(): Observable<any> {
|
|
|
|
const auth = this.storageService.getAuth();
|
|
|
|
if (!auth) {
|
|
|
|
return of(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.httpClient.get<any>(`${SERVICES_API_PREFIX}/account`);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserMenuGroups$(): Observable<MenuGroup[]> {
|
|
|
|
const auth = this.storageService.getAuth();
|
|
|
|
if (!auth) {
|
|
|
|
return of(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.httpClient.get<MenuGroup[]>(`${SERVICES_API_PREFIX}/account/menu`);
|
|
|
|
}
|
|
|
|
|
|
|
|
logout$(): Observable<any> {
|
|
|
|
const auth = this.storageService.getAuth();
|
|
|
|
if (!auth) {
|
|
|
|
return of(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.removeItem('auth');
|
|
|
|
return this.httpClient.post(`${SERVICES_API_PREFIX}/auth/logout`, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
getServicesBackendInfo$(): Observable<IBackendInfo> {
|
|
|
|
return this.httpClient.get<IBackendInfo>(`${SERVICES_API_PREFIX}/version`);
|
|
|
|
}
|
|
|
|
|
|
|
|
estimate$(txInput: string) {
|
|
|
|
return this.httpClient.post<any>(`${SERVICES_API_PREFIX}/accelerator/estimate`, { txInput: txInput }, { observe: 'response' });
|
|
|
|
}
|
|
|
|
|
2024-04-08 21:43:06 +09:00
|
|
|
accelerate$(txInput: string, userBid: number, accelerationUUID: string) {
|
|
|
|
return this.httpClient.post<any>(`${SERVICES_API_PREFIX}/accelerator/accelerate`, { txInput: txInput, userBid: userBid, accelerationUUID: accelerationUUID });
|
2023-12-12 17:24:58 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 23:07:19 +09:00
|
|
|
accelerateWithCashApp$(txInput: string, token: string, cashtag: string, referenceId: string, accelerationUUID: string) {
|
|
|
|
return this.httpClient.post<any>(`${SERVICES_API_PREFIX}/accelerator/accelerate/cashapp`, { txInput: txInput, token: token, cashtag: cashtag, referenceId: referenceId, accelerationUUID: accelerationUUID });
|
2024-03-21 16:44:07 +09:00
|
|
|
}
|
|
|
|
|
2023-12-12 17:24:58 +01:00
|
|
|
getAccelerations$(): Observable<Acceleration[]> {
|
|
|
|
return this.httpClient.get<Acceleration[]>(`${SERVICES_API_PREFIX}/accelerator/accelerations`);
|
|
|
|
}
|
|
|
|
|
2024-03-08 18:00:57 +09:00
|
|
|
getAggregatedAccelerationHistory$(params: AccelerationHistoryParams): Observable<any> {
|
|
|
|
return this.httpClient.get<any>(`${SERVICES_API_PREFIX}/accelerator/accelerations/history/aggregated`, { params: { ...params }, observe: 'response' });
|
2024-03-01 12:53:29 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 17:24:58 +01:00
|
|
|
getAccelerationHistory$(params: AccelerationHistoryParams): Observable<Acceleration[]> {
|
|
|
|
return this.httpClient.get<Acceleration[]>(`${SERVICES_API_PREFIX}/accelerator/accelerations/history`, { params: { ...params } });
|
|
|
|
}
|
2024-02-26 15:54:10 +01:00
|
|
|
|
|
|
|
getAccelerationHistoryObserveResponse$(params: AccelerationHistoryParams): Observable<any> {
|
|
|
|
return this.httpClient.get<any>(`${SERVICES_API_PREFIX}/accelerator/accelerations/history`, { params: { ...params }, observe: 'response'});
|
|
|
|
}
|
2024-02-27 15:20:15 +01:00
|
|
|
|
2024-04-10 06:27:48 +00:00
|
|
|
getAccelerationStats$(params: AccelerationHistoryParams): Observable<AccelerationStats> {
|
|
|
|
return this.httpClient.get<AccelerationStats>(`${SERVICES_API_PREFIX}/accelerator/accelerations/stats`, { params: { ...params } });
|
2024-02-27 15:20:15 +01:00
|
|
|
}
|
2024-03-21 16:44:07 +09:00
|
|
|
|
|
|
|
setupSquare$(): Observable<{squareAppId: string, squareLocationId: string}> {
|
|
|
|
return this.httpClient.get<{squareAppId: string, squareLocationId: string}>(`${SERVICES_API_PREFIX}/square/setup`);
|
|
|
|
}
|
2023-12-12 17:24:58 +01:00
|
|
|
}
|