github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/lib/transactions.js (about)

     1  /*
     2   * Licensed under the Apache License, Version 2.0 (the "License");
     3   * you may not use this file except in compliance with the License.
     4   * You may obtain a copy of the License at
     5   *
     6   * http://www.apache.org/licenses/LICENSE-2.0
     7   *
     8   * Unless required by applicable law or agreed to in writing, software
     9   * distributed under the License is distributed on an "AS IS" BASIS,
    10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11   * See the License for the specific language governing permissions and
    12   * limitations under the License.
    13   */
    14  
    15  
    16  /**
    17   * Energy to Coins transaction
    18   * @param {org.decentralized.energy.network.EnergyToCoins} UpdateValues 
    19   * @transaction
    20   */
    21  function EnergyToCoins(UpdateValues) {
    22  
    23      //determine change in coins value from the rate
    24      var coinsChange = (UpdateValues.energyRate * UpdateValues.energyValue);
    25  
    26      //update values of the assets
    27      UpdateValues.coinsInc.value = UpdateValues.coinsInc.value + coinsChange;
    28      UpdateValues.coinsDec.value = UpdateValues.coinsDec.value - coinsChange;
    29      UpdateValues.energyInc.value = UpdateValues.energyInc.value + UpdateValues.energyValue;
    30      UpdateValues.energyDec.value = UpdateValues.energyDec.value - UpdateValues.energyValue;
    31  
    32      //get asset registry for Coins and Energy, and update on the ledger
    33      return getAssetRegistry('org.decentralized.energy.network.Coins')
    34          .then(function (assetRegistry) {
    35              return assetRegistry.updateAll([UpdateValues.coinsInc,UpdateValues.coinsDec]);
    36          })                
    37          .then(function () {
    38              return  getAssetRegistry('org.decentralized.energy.network.Energy')
    39              .then(function (assetRegistry) {
    40                  return assetRegistry.updateAll([UpdateValues.energyInc,UpdateValues.energyDec]);
    41              });            
    42          });        
    43     
    44  }
    45  
    46  
    47  /**
    48   * Resident to bank transaction
    49   * @param {org.decentralized.energy.network.CashToCoins} UpdateValues
    50   * @transaction
    51   */
    52  function CashToCoins(UpdateValues) {
    53  
    54      //determine change in coins value from the rate
    55      var coinsChange = (UpdateValues.cashRate * UpdateValues.cashValue);
    56  
    57      //update values of the assets
    58      UpdateValues.coinsInc.value = UpdateValues.coinsInc.value + coinsChange;
    59      UpdateValues.coinsDec.value = UpdateValues.coinsDec.value - coinsChange;
    60      UpdateValues.cashInc.value = UpdateValues.cashInc.value + UpdateValues.cashValue;
    61      UpdateValues.cashDec.value = UpdateValues.cashDec.value - UpdateValues.cashValue;
    62  
    63      //get asset registry for Coins and Cash, and update the ledger
    64      return getAssetRegistry('org.decentralized.energy.network.Coins')
    65          .then(function (assetRegistry) {
    66              return assetRegistry.updateAll([UpdateValues.coinsInc,UpdateValues.coinsDec]);
    67          })                
    68          .then(function () {
    69              return  getAssetRegistry('org.decentralized.energy.network.Cash')
    70              .then(function (assetRegistry) {
    71                  return assetRegistry.updateAll([UpdateValues.cashInc,UpdateValues.cashDec]);
    72              });            
    73          });     
    74  }
    75  
    76