forked from maarsej/pharmaBlock_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainHelpers.js
119 lines (98 loc) · 4.34 KB
/
chainHelpers.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Import libraries we need.
const Web3 = require('web3');
const contract = require('truffle-contract');
const prescription_artifacts = require('./build/contracts/Prescription.json')
const filledprescription_artifacts = require('./build/contracts/filledPrescription.json')
const EROFS = require('constants').EROFS
// import { default as Web3 } from 'web3';
// import { default as contract } from 'truffle-contract'
// import prescription_artifacts from '../../build/contracts/Prescription.json'
// import { EROFS } from "constants";
// Contract object
const Prescription = contract(prescription_artifacts);
const filledPrescription = contract(filledprescription_artifacts);
let values = { "drugID": "field-1", "dosage": "field-2", "numberOfDoses": "field-3", "frequencyOfDose": "field-4" }
setProvider = function () {
if (typeof web3 !== 'undefined' /*'&& false' used to escape metamask being installed in my browser */) {
// console.warn("Using web3 detected from external source like Metamask")
// Use MetaMask's provider
// window.web3 = new Web3(web3.currentProvider);
web3 = new Web3(web3.currentProvider);
} else {
// console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
// window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
Prescription.setProvider(web3.currentProvider);
filledPrescription.setProvider(web3.currentProvider);
}
findFilled = function (id) {
return new Promise((resolve, reject) => {
setProvider();
const contractInstance = filledPrescription.at(id); //0xd49bDC6802Acc58931591749607ad08cb13F8e67
contractInstance.getInfo.call().then(function (v) {
// console.log(v);
resolve(v);
}).catch((error) => {
reject(error);
});
})
}
find = function (id) {
return new Promise((resolve, reject) => {
setProvider();
const contractInstance = Prescription.at(id); //0xd49bDC6802Acc58931591749607ad08cb13F8e67
contractInstance.getInfo.call().then(function (v) {
// console.log(v);
resolve(v);
}).catch((error) => {
reject(error);
});
})
}
create = function (currentUser, drugID, dosage, numberOfDoses, frequencyOfDose) {
return new Promise((resolve, reject) => {
setProvider();
Prescription.new(drugID, dosage, numberOfDoses, frequencyOfDose, { from: currentUser, gas: 6000000}).then(instance => {
let checkAddress = setInterval(() => {
if (instance.address) {
console.log("Contract address: " + instance.address);
clearInterval(checkAddress)
resolve(instance.address);
}
}, 100);
}).catch((error) => {
reject(error)
});
})
}
sign = function (id, currentUser, costPerDose, startDate, endDate, pharmaPubAddr) {
return new Promise((resolve, reject) => {
setProvider();
const contractInstance = Prescription.at(id); //0xac68dB96A9E756a83AEC20d47DbeE90017a05bF2
contractInstance.getInfo.call().then((output) => {
filledPrescription.new(output[0], output[1], output[2], output[3], costPerDose, startDate, endDate, pharmaPubAddr, { from: currentUser, gas: 6000000 }).then(instance => {
let checkAddress = setInterval(() => {
if (instance.address) {
console.log("Contract address: " + instance.address);
clearInterval(checkAddress)
resolve(instance.address);
}
}, 100);
}).catch((error) => {
console.log(error)
});
}).catch((error) => {
reject(error)
});
})
}
const blockFunc = {
create: create,
find: find,
sign: sign,
findFilled: findFilled,
setProvider: setProvider,
}
module.exports = blockFunc;