github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/angular-app/src/app/TransactionRR/TransactionRR.component.ts (about) 1 import { Component, OnInit, Input } from '@angular/core'; 2 import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms'; 3 import { Observable } from 'rxjs/Observable'; 4 import { TransactionRRService } from './TransactionRR.service'; 5 import 'rxjs/add/operator/toPromise'; 6 7 //provide associated components 8 @Component({ 9 selector: 'app-TransactionRR', 10 templateUrl: './TransactionRR.component.html', 11 styleUrls: ['./TransactionRR.component.css'], 12 providers: [TransactionRRService] 13 }) 14 15 //TransactionRRComponent class 16 export class TransactionRRComponent { 17 18 //define rate of conversion 19 private residentCoinsPerEnergy = 1; 20 private residentEnergyPerCoin = (1 / this.residentCoinsPerEnergy).toFixed(2); 21 22 //define variables 23 private coinsExchanged; 24 private checkResultProducerEnergy = true; 25 private checkResultConsumerCoins = true; 26 27 myForm: FormGroup; 28 private errorMessage; 29 private transactionFrom; 30 31 private allResidents; 32 private producerResident; 33 private consumerResident; 34 35 private energyToCoinsObj; 36 private transactionID; 37 38 //initialize form variables 39 producerResidentID = new FormControl("", Validators.required); 40 consumerResidentID = new FormControl("", Validators.required); 41 energyValue = new FormControl("", Validators.required); 42 coinsValue = new FormControl("", Validators.required); 43 44 constructor(private serviceTransaction:TransactionRRService, fb: FormBuilder) { 45 //intialize form 46 this.myForm = fb.group({ 47 producerResidentID:this.producerResidentID, 48 consumerResidentID:this.consumerResidentID, 49 energyValue:this.energyValue, 50 coinsValue:this.coinsValue, 51 }); 52 53 }; 54 55 //on page initialize, load all residents 56 ngOnInit(): void { 57 this.transactionFrom = false; 58 this.loadAllResidents() 59 .then(() => { 60 this.transactionFrom = true; 61 }); 62 63 } 64 65 //get all Residents 66 loadAllResidents(): Promise<any> { 67 68 //retrieve all residents in the tempList array 69 let tempList = []; 70 71 //call serviceTransaction to get all resident objects 72 return this.serviceTransaction.getAllResidents() 73 .toPromise() 74 .then((result) => { 75 this.errorMessage = null; 76 77 //append tempList with the resident objects returned 78 result.forEach(resident => { 79 tempList.push(resident); 80 }); 81 82 //assign tempList to allResidents 83 this.allResidents = tempList; 84 }) 85 .catch((error) => { 86 if(error == 'Server error'){ 87 this.errorMessage = "Could not connect to REST server. Please check your configuration details"; 88 } 89 else if(error == '404 - Not Found'){ 90 this.errorMessage = "404 - Could not find API route. Please check your available APIs." 91 } 92 else{ 93 this.errorMessage = error; 94 } 95 }); 96 } 97 98 //execute transaction 99 execute(form: any): Promise<any> { 100 101 //loop through all residents, and get producer and consumer resident from user input 102 for (let resident of this.allResidents) { 103 if(resident.residentID == this.producerResidentID.value){ 104 this.producerResident = resident; 105 } 106 if(resident.residentID == this.consumerResidentID.value){ 107 this.consumerResident = resident; 108 } 109 } 110 111 //identify energy and coins id which will be debited 112 var splitted_energyID = this.producerResident.energy.split("#", 2); 113 var energyID = String(splitted_energyID[1]); 114 115 var splitted_coinsID = this.consumerResident.coins.split("#", 2); 116 var coinsID = String(splitted_coinsID[1]); 117 118 //calculate coins exchanges from the rate 119 this.coinsExchanged = this.residentCoinsPerEnergy * this.energyValue.value; 120 121 //create transaction object 122 this.energyToCoinsObj = { 123 $class: "org.decentralized.energy.network.EnergyToCoins", 124 "energyRate": this.residentCoinsPerEnergy, 125 "energyValue": this.energyValue.value, 126 "coinsInc": this.producerResident.coins, 127 "coinsDec": this.consumerResident.coins, 128 "energyInc": this.consumerResident.energy, 129 "energyDec": this.producerResident.energy, 130 }; 131 132 //check consumer coins and producer energy assets for enough balance before creating transaction 133 //call serviceTransaction to get energy asset 134 return this.serviceTransaction.getEnergy(energyID) 135 .toPromise() 136 .then((result) => { 137 this.errorMessage = null; 138 //check if enough value 139 if(result.value) { 140 if ((result.value - this.energyValue.value) < 0 ){ 141 this.checkResultProducerEnergy = false; 142 this.errorMessage = "Insufficient energy in producer account"; 143 return false; 144 } 145 return true; 146 } 147 }) 148 .then((checkProducerEnergy) => { 149 //if positive on sufficient energy, then check coins asset whether sufficient coins 150 if(checkProducerEnergy) 151 { 152 //call serviceTransaction to get coins asset 153 this.serviceTransaction.getCoins(coinsID) 154 .toPromise() 155 .then((result) => { 156 this.errorMessage = null; 157 //check if enough value 158 if(result.value) { 159 if ((result.value - this.coinsExchanged) < 0 ){ 160 this.checkResultConsumerCoins = false; 161 this.errorMessage = "Insufficient coins in consumer account"; 162 return false; 163 } 164 return true; 165 } 166 }) 167 .then((checkConsumerCoins) => { 168 //if positive on sufficient coins, then call transaction 169 if(checkConsumerCoins) 170 { 171 //call serviceTransaction call the energyToCoins transaction with energyToCoinsObj as parameter 172 this.serviceTransaction.energyToCoins(this.energyToCoinsObj) 173 .toPromise() 174 .then((result) => { 175 this.errorMessage = null; 176 this.transactionID = result.transactionId; 177 console.log(result) 178 }) 179 .catch((error) => { 180 if(error == 'Server error'){ 181 this.errorMessage = "Could not connect to REST server. Please check your configuration details"; 182 } 183 else if(error == '404 - Not Found'){ 184 this.errorMessage = "404 - Could not find API route. Please check your available APIs." 185 } 186 else{ 187 this.errorMessage = error; 188 } 189 }).then(() => { 190 this.transactionFrom = false; 191 }); 192 } 193 }); 194 } 195 }); 196 } 197 198 }