浏览代码

chain4all-service integration WIP

kunickyd 3 年之前
父节点
当前提交
79bce74941
共有 2 个文件被更改,包括 35 次插入8 次删除
  1. 3 1
      src/app/bc-info/bc-info.component.html
  2. 32 7
      src/app/bc-info/bc-info.service.ts

+ 3 - 1
src/app/bc-info/bc-info.component.html

@@ -2,4 +2,6 @@ My account balance: {{bcInfoService.userBalance}} $$ <button (click)="updateUser
 <br>
 Payment hash: {{bcInfoService.paymentHash}} <button (click)="pay()">SEND TX</button>
 <br>
-Price: $$
+Price: {{bcInfoService.price}}$$
+<br>
+Data URL: {{bcInfoService.dataUrl}}

+ 32 - 7
src/app/bc-info/bc-info.service.ts

@@ -12,6 +12,7 @@ import { queries, commands } from 'iroha-helpers'
 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;
@@ -21,24 +22,34 @@ export class BcInfoService {
   user = 'kunickyd@test';
   // userPrivateKey = 'ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8';
   userBalance = -1;
-  paymentHash = ""
+  price = -1;
+  paymentHash = "";
+  dataUrl = "";
+  extent = [ //bottom left and top right point should be enough
+    [48.5, 17],
+    [49, 17],
+    [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);
     this.getUserBalance();
+    this.getPrice(this.extent).then(price => this.price = price);
   }
 
   async getUserBalance(): Promise<any> {
     if (this._requestInProcess) {
       return;
     }
-    this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET );    
+    this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET);
   }
 
-  async pay(): Promise<any>{
-    this.paymentHash = await this.transferAssets(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET, "extent + data source", 1);
+  async pay(): Promise<any> {
+    this.paymentHash = await this.transferAssets(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.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> {
@@ -83,7 +94,7 @@ export class BcInfoService {
   async transferAssets(user: string, userPrivateKey: string, assetId: string, description: string, amount: number): Promise<string> {
 
     let commandOptions = {
-      privateKeys: [ userPrivateKey ], // Array of private keys in hex format
+      privateKeys: [userPrivateKey], // Array of private keys in hex format
       creatorAccountId: user, // Account id, ex. admin@test
       quorum: 1,
       commandService: this.commandService,
@@ -101,11 +112,25 @@ export class BcInfoService {
       });
 
       console.debug(response);
-      
+
       return response["txHash"][0];
 
-    } catch (err) {     
+    } catch (err) {
       console.error(err)
     }
   }
+
+  async getPrice(extent: Array<Array<number>>) : Promise<number>{
+    let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/price', { extent: extent }).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 }).toPromise();
+
+    console.debug(response);
+    return response["dataUrl"];
+  }
 }