kunickyd преди 3 години
родител
ревизия
856cf21bb7
променени са 4 файла, в които са добавени 77 реда и са изтрити 41 реда
  1. 32 0
      src/app/app.config.service.ts
  2. 15 2
      src/app/app.module.ts
  3. 24 39
      src/app/bc-info/bc-info.service.ts
  4. 6 0
      src/assets/config.json

+ 32 - 0
src/app/app.config.service.ts

@@ -0,0 +1,32 @@
+import { HttpClient } from '@angular/common/http';
+import { Injectable } from '@angular/core';
+
+@Injectable({
+  providedIn: 'root'
+})
+export class AppConfigService {
+
+  private appConfig: any;
+
+  constructor(private http: HttpClient) { }
+
+  loadAppConfig() {
+    return this.http.get('/assets/config.json')
+      .toPromise()
+      .then(data => {
+        this.appConfig = data;
+      });
+  }
+
+  get config() : BcConfig {
+      return this.appConfig as BcConfig;
+  }
+  
+}
+
+export interface BcConfig {
+  readonly CHAIN4ALL_SERVICE_URL: string
+  readonly IROHA_ADDRESS: string
+  readonly ASSET: string
+  readonly ADMIN_USER: string
+}

+ 15 - 2
src/app/app.module.ts

@@ -1,15 +1,28 @@
 import {BrowserModule} from '@angular/platform-browser';
-import {NgModule} from '@angular/core';
+import {NgModule, APP_INITIALIZER} from '@angular/core';
 
 import {HslayersModule} from 'hslayers-ng';
 
 import {AppComponent} from './app.component';
+import { AppConfigService } from './app.config.service';
 import {BcInfoModule} from './bc-info/bc-info.module';
 
 @NgModule({
   declarations: [AppComponent],
   imports: [BrowserModule, HslayersModule, BcInfoModule],
-  providers: [],
+  providers: [
+    {
+      provide: APP_INITIALIZER,
+      multi: true,
+      deps: [AppConfigService],
+      useFactory: (appConfigService: AppConfigService) => {
+        return () => {
+          //Make sure to return a promise!
+          return appConfigService.loadAppConfig();
+        };
+      }
+    }
+  ],
   bootstrap: [AppComponent],
 })
 export class AppModule {}

+ 24 - 39
src/app/bc-info/bc-info.service.ts

@@ -5,22 +5,20 @@ import {
   QueryService_v1Client as QueryService
 } from 'iroha-helpers/lib/proto/endpoint_pb_service'
 import { queries, commands } from 'iroha-helpers'
+import { AppConfigService, BcConfig } from '../app.config.service';
+import { Buffer } from 'buffer';
 
 
 
-@Injectable({ providedIn: 'root' })
-export class BcInfoService {
 
-  BASE_URL = 'https://chain4all.lesprojekt.cz' as const;
-  CHAIN4ALL_SERVICE_URL = 'http://localhost:3000' as const;
-  IROHA_ADDRESS = "http://localhost:8080" as const;
-  ASSET = "testcoin#test" as const;
-  ADMIN_USER = "admin@test" as const;
+@Injectable({ providedIn: 'root' })
+export class BcInfoService {  
+  private readonly commandService: CommandService;
+  private readonly queryService: QueryService;
+  private readonly basicAuthHeaders: HttpHeaders;
 
+  private readonly _bcConfig: BcConfig;
 
-  commandService: CommandService;
-  queryService: QueryService;
-  basicAuthHeaders: HttpHeaders;
   user = 'kunickyd@test';
   // userPrivateKey = 'ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8';
   userBalance = -1;
@@ -33,11 +31,14 @@ export class BcInfoService {
     [49, 16.5],
     [48.5, 16.5]
   ];
-  private _requestInProcess = false;
 
-  constructor(public httpClient: HttpClient) {
-    this.commandService = new CommandService(this.IROHA_ADDRESS);
-    this.queryService = new QueryService(this.IROHA_ADDRESS);
+  constructor(private httpClient: HttpClient, private appSettings: AppConfigService ) {
+    this._bcConfig = appSettings.config;
+
+    this.commandService = new CommandService(this._bcConfig.IROHA_ADDRESS);
+    this.queryService = new QueryService(this._bcConfig.IROHA_ADDRESS);
+
+
     this.getUserBalance();
     this.getPrice(this.extent).then(price => this.price = price);
 
@@ -46,21 +47,17 @@ export class BcInfoService {
     this.basicAuthHeaders.append("Authorization", "Basic " + Buffer.from("admin:password", 'base64'));
   }
 
-  async getUserBalance(): Promise<any> {
-    if (this._requestInProcess) {
-      return;
-    }
-    this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET);
+  async getUserBalance(): Promise<any> {    
+    this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this._bcConfig.ASSET);
   }
 
-  async pay(): Promise<any> {
-    this.paymentHash = await this.transferAssets(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET, JSON.stringify({ extent: this.extent }), this.price);
+  async pay(): Promise<any> {    
+    this.paymentHash = await this.transferAssets(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this._bcConfig.ASSET, JSON.stringify({ extent: this.extent }), this.price);
     this.dataUrl = await this.requestData(this.paymentHash, this.user);
   }
 
   async getUserBalanceFromIroha(user: string, userPrivateKey: string, assetId: string): Promise<number> {
 
-    this._requestInProcess = true;
     let queryOptions = {
       privateKey: userPrivateKey,
       creatorAccountId: user,
@@ -69,13 +66,6 @@ export class BcInfoService {
     };
 
     try {
-      // For testing
-      /*data = await new Promise((resolve, reject) => {
-        setTimeout(() => {
-          resolve(Math.random()*100);
-        }, 1500);
-      });*/
-
       let data = await queries.getAccountAssets(queryOptions, {
         accountId: user,
         firstAssetId: assetId,
@@ -84,17 +74,12 @@ export class BcInfoService {
 
       console.debug(data);
 
-      let balance: number = parseFloat(data[0].balance);
-
-      this._requestInProcess = false;
+      let balance: number = parseFloat(data[0].balance);      
       return balance;
 
-    } catch (err) {
-      this._requestInProcess = false
+    } catch (err) {      
       console.error(err)
     }
-
-    this._requestInProcess = false
   }
 
   async transferAssets(user: string, userPrivateKey: string, assetId: string, description: string, amount: number): Promise<string> {
@@ -111,7 +96,7 @@ export class BcInfoService {
 
       let response = await commands.transferAsset(commandOptions, {
         srcAccountId: user,
-        destAccountId: this.ADMIN_USER,
+        destAccountId: this._bcConfig.ADMIN_USER,
         assetId,
         description,
         amount: String(amount),
@@ -127,14 +112,14 @@ export class BcInfoService {
   }
 
   async getPrice(extent: Array<Array<number>>) : Promise<number>{
-    let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/price', { extent: extent }, { headers: this.basicAuthHeaders} ).toPromise();
+    let response = await this.httpClient.post(this._bcConfig.CHAIN4ALL_SERVICE_URL + '/price', { extent: extent }, { headers: this.basicAuthHeaders} ).toPromise();
 
     console.debug(response);
     return response["price"];
   }
 
   async requestData(paymentHash: string, user: string) : Promise<string>{
-    let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders}).toPromise();
+    let response = await this.httpClient.post(this._bcConfig.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders}).toPromise();
 
     console.debug(response);
     return response["dataUrl"];

+ 6 - 0
src/assets/config.json

@@ -0,0 +1,6 @@
+{
+    "CHAIN4ALL_SERVICE_URL": "http://localhost:3000",
+    "IROHA_ADDRESS": "http://localhost:8080",
+    "ASSET": "testcoin#test",
+    "ADMIN_USER": "admin@test"
+}