This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
storeValueFromNode1.js
130 lines (115 loc) · 3.11 KB
/
storeValueFromNode1.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
120
121
122
123
124
125
126
127
128
129
130
const Web3 = require("web3");
const EEAClient = require("../../src");
const EventEmitterAbi = require("../solidity/EventEmitter/EventEmitter.json")
.output.abi;
const { chainID, orion, besu } = require("../keys.js");
const storeValueFromNode1 = (address, value) => {
const web3 = new EEAClient(new Web3(besu.node1.url), chainID, 0, 3000000);
const contract = new web3.eth.Contract(EventEmitterAbi);
// eslint-disable-next-line no-underscore-dangle
const functionAbi = contract._jsonInterface.find(e => {
return e.name === "store";
});
const functionArgs = web3.eth.abi
.encodeParameters(functionAbi.inputs, [value])
.slice(2);
const functionCall = {
to: address,
data: functionAbi.signature + functionArgs,
privateFrom: orion.node1.publicKey,
privateFor: [orion.node2.publicKey],
privateKey: besu.node1.privateKey
};
return web3.eea
.sendRawTransaction(functionCall)
.then(transactionHash => {
console.log("Transaction Hash:", transactionHash);
return web3.priv.getTransactionReceipt(
transactionHash,
orion.node1.publicKey
);
})
.then(result => {
console.log("Event Emitted:", result.logs[0].data);
return result;
});
};
const getValue = (url, address, privateFrom, privateFor, privateKey) => {
const web3 = new EEAClient(new Web3(url), chainID);
const contract = new web3.eth.Contract(EventEmitterAbi);
// eslint-disable-next-line no-underscore-dangle
const functionAbi = contract._jsonInterface.find(e => {
return e.name === "value";
});
const functionCall = {
to: address,
data: functionAbi.signature,
privateFrom,
privateFor,
privateKey
};
return web3.eea
.sendRawTransaction(functionCall)
.then(transactionHash => {
return web3.priv.getTransactionReceipt(
transactionHash,
orion.node1.publicKey
);
})
.then(result => {
console.log(`Get Value from ${url}:`, result.output);
return result;
});
};
const getValueFromNode1 = address => {
return getValue(
besu.node1.url,
address,
orion.node1.publicKey,
[orion.node2.publicKey],
besu.node1.privateKey
);
};
const getValueFromNode2 = address => {
return getValue(
besu.node2.url,
address,
orion.node2.publicKey,
[orion.node1.publicKey],
besu.node2.privateKey
);
};
const getValueFromNode3 = address => {
return getValue(
besu.node3.url,
address,
orion.node3.publicKey,
[orion.node1.publicKey],
besu.node3.privateKey
);
};
module.exports = {
storeValueFromNode1,
getValueFromNode1,
getValueFromNode2,
getValueFromNode3
};
if (require.main === module) {
if (!process.env.CONTRACT_ADDRESS) {
throw Error(
"You need to export the following variable in your shell environment: CONTRACT_ADDRESS="
);
}
const address = process.env.CONTRACT_ADDRESS;
storeValueFromNode1(address, 1000)
.then(() => {
return getValueFromNode1(address);
})
.then(() => {
return getValueFromNode2(address);
})
.then(() => {
return getValueFromNode3(address);
})
.catch(console.log);
}