forked from chainstacklabs/quorum-iot-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.js
76 lines (62 loc) · 1.88 KB
/
public.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
const { compileContract } = require('./utils/compiler.js');
const {
node1,
node2,
node3,
} = require('./utils/environment.js');
let temperatureMonitor = {};
const main = async () => {
const {interface, bytecode} = compileContract('temperatureMonitor.sol');
temperatureMonitor = {
interface,
bytecode,
};
const contractAddress = await deployContract(node1);
console.log(`Contract address after deployment: ${contractAddress}`);
const status = await setTemperature({
node: node2,
contractAddress,
temp: 3,
});
console.log(`Transaction status: ${status}`);
const temp = await getTemperature({
node: node3,
contractAddress,
});
console.log('Retrieved contract Temperature', temp);
}
function getContract(web3, contractAddress) {
return new web3.eth.Contract(temperatureMonitor.interface, contractAddress);
}
async function deployContract(node) {
await node.web3.eth.personal.unlockAccount(node.WALLET_ADDRESS, '', 1000);
const contract = new node.web3.eth.Contract(temperatureMonitor.interface);
return contract.deploy({
data: temperatureMonitor.bytecode,
})
.send({
from: node.WALLET_ADDRESS,
gas: '0x1dcd6500',
gasPrice: '0',
})
.on('error', console.error)
.then((newContractInstance) => {
return newContractInstance.options.address;
});
}
async function setTemperature({ node, contractAddress, temp }) {
await node.web3.eth.personal.unlockAccount(node.WALLET_ADDRESS, '', 1000);
const myContract = getContract(node.web3, contractAddress);
return myContract.methods.set(temp).send({
from: node.WALLET_ADDRESS,
})
.on('error', console.error)
.then((receipt) => {
return receipt.status;
});
}
async function getTemperature({ node, contractAddress }) {
const myContract = getContract(node.web3, contractAddress);
return myContract.methods.get().call().then(result => result);
}
main()