forked from chainstacklabs/quorum-iot-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic-externalSign.js
95 lines (82 loc) · 2.35 KB
/
public-externalSign.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
const { compileContract } = require('./utils/compiler.js');
const { getNonce } = require("./utils/jsonRPC.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(node3);
console.log(`Contract deployed at address: ${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);
};
async function deployContract(node) {
// encode contract
const contract = new node.web3.eth.Contract(temperatureMonitor.interface);
const encodedABI = contract
.deploy({
data: temperatureMonitor.bytecode,
})
.encodeABI();
const nonce = await getNonce(node.WALLET_ADDRESS, node.RPC);
return node.web3.eth.accounts.signTransaction({
nonce,
gasPrice: 0,
gasLimit: 4300000,
value: 0,
data: encodedABI,
}, node.WALLET_KEY)
.then(payload => {
return node.web3.eth.sendSignedTransaction(payload.rawTransaction)
.then(receipt => receipt.contractAddress)
.catch(error => error.message);
});
}
async function setTemperature({ node, contractAddress, temp }) {
const encodedABI = node.web3.eth.abi.encodeFunctionCall(
temperatureMonitor.interface.find(x => x.name === 'set'),
[temp],
);
const nonce = await getNonce(node.WALLET_ADDRESS, node.RPC);
return node.web3.eth.accounts.signTransaction({
nonce,
to: contractAddress,
gasLimit: '0x47b760',
gasPrice: "0x0",
data: encodedABI,
}, node.WALLET_KEY)
.then(payload => {
return node.web3.eth.sendSignedTransaction(payload.rawTransaction)
.then(receipt => receipt.status)
.catch(error => error.message);
});
}
async function getTemperature({ contractAddress, node }) {
const contract = new node.web3.eth.Contract(
temperatureMonitor.interface,
contractAddress,
);
return contract.methods
.get().call({
from: node.WALLET_ADDRESS,
})
.then(data => data)
.catch(error => error.message);
}
main();