|
|
@@ -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"];
|