-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartcontracts.js
More file actions
78 lines (67 loc) · 3.17 KB
/
smartcontracts.js
File metadata and controls
78 lines (67 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
*/
'use strict';
/**
* Creates a new Company
* @param {org.example.empty.SubmitHouseholdNotProducingEnergy} householdNPESubmission - the SubmitHouseholdNotProducingEnergy transaction to be processed
* @transaction
*/
async function addHouseholdNPE(householdSubmission){
// create Company asset
var factory = getFactory();
// to create a resource; give the identifier, here name, as an argument
var household = factory.newResource('org.example.empty', 'Household', householdSubmission.id);
household.adress = householdSubmission.adress;
household.powerCoinBalance = 0;
household.pendingEnergyBalance = 0;
household.canProduceEnergy = false;
// save the new company on the blockchain
let householdRegistry = await getAssetRegistry('org.example.empty.Household');
// for participants, use getParticipantRegistry
await householdRegistry.add(household);
}
/**
* Creates a new Company
* @param {org.example.empty.SubmitHouseholdProducingEnergy} householdPESubmission - the SubmitHouseholdProducingEnergy transaction to be processed
* @transaction
*/
async function addHouseholdPE(householdSubmission){
// create Company asset
var factory = getFactory();
// to create a resource; give the identifier, here name, as an argument
var household = factory.newResource('org.example.empty', 'Household', householdSubmission.id);
household.adress = householdSubmission.adress;
household.powerCoinBalance = 0;
household.pendingEnergyBalance = 0;
household.canProduceEnergy = true;
// save the new company on the blockchain
let householdRegistry = await getAssetRegistry('org.example.empty.Household');
// for participants, use getParticipantRegistry
await householdRegistry.add(household);
}
/**
* Creates a new Company
* @param {org.example.empty.NewTransaction} transactionSubmission - the NewTransaction transaction to be processed
* @transaction
*/
async function addTransaction(transactionSubmission){
// create Company asset
var factory = getFactory();
// to create a resource; give the identifier, here name, as an argument
var transaction = factory.newResource('org.example.empty', 'Transaction', transactionSubmission.id);
transaction.amount = transactionSubmission.amount;
transaction.date = new Date();
transaction.energyPrice = transactionSubmission.energyPrice;
transaction.gridFee = transactionSubmission.gridFee;
transaction.consumer = transactionSubmission.consumer;
transaction.supplier = transactionSubmission.supplier;
// save the new company on the blockchain
let transactionRegistry = await getAssetRegistry('org.example.empty.Transaction');
// for participants, use getParticipantRegistry
await transactionRegistry.add(transaction);
transactionSubmission.supplier.powerCoinBalance += transactionSubmission.amount * transactionSubmission.energyPrice;
transactionSubmission.consumer.powerCoinBalance -= transactionSubmission.amount * transactionSubmission.energyPrice + transactionSubmission.gridFee;
let HouseholdRegistry = await getAssetRegistry('org.example.empty.Household');
await HouseholdRegistry.update(transactionSubmission.supplier);
await HouseholdRegistry.update(transactionSubmission.consumer);
}