-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (114 loc) · 4.73 KB
/
index.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
131
132
133
const express = require('express');
const app = express();
const fs = require('fs');
const solc = require('solc');
var wallet = require('ethereumjs-wallet')
var ethereumjs = require('ethereumjs-tx')
var Web3 = require('web3');
// Generate a new Ethereum account to use for deployment and sending messages to contracts
var account = wallet.generate();
var accountAddress = account.getAddressString()
var privateKey = account.getPrivateKey();
// The address where you can find the contract at on the blockchain.
// This is an output of the contract deployment on the blockchain. Once you deploy your contract,
// you will get a transaction hash. Check the details of that transaction using the /getTx method below.
// The contract address will be the "creates" value in the result.
var contractAddress = "0xa13270efed99e6f01878f99d3ba142e465214097";
// Our miner node's RPC endpoint
var rpcUrl = "http://eth53hgra-dns-reg1.westeurope.cloudapp.azure.com:8540";
var web3 = new Web3(rpcUrl);
app.use(express.json())
var port = 5000;
app.listen(port, () => {
console.log("App is starting. Listening on port: " + port);
});
// Get the details of a transaction
app.get('/getTx', async (req, res) => {
var tx = await web3.eth.getTransaction(req.query.txHash);
return res.send(tx);
})
// Depoy the contract defined in ./MyContract.sol
app.get('/deploy', async (req, res) => {
try {
var source = fs.readFileSync("./MyContract.sol", 'utf8');
var compiledContract = solc.compile(source, 1);
var bytecode = compiledContract.contracts[':MyContract'].bytecode;
var data = '0x' + bytecode;
// Get the current nonce of the account. We are not using the transaction count.
// It's just a way to get the nonce so ignore that.
web3.eth.getTransactionCount(accountAddress, function (err, nonce) {
var rawTx = {
nonce: nonce,
gasPrice: '0x00',
gasLimit: '0x2FAF080',
value: '0x00',
data: data
}
var tx = new ethereumjs(rawTx);
tx.sign(privateKey);
var raw = '0x' + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(raw, function (txErr, transactionHash) {
if (txErr) {
return res.send("something went wrong: " + txErr);
}
return res.send(transactionHash);
});
});
} catch (error) {
console.error(error);
return res.send("nok");
}
});
// Read data from a contract on the blockchain
// Call the getValue method in the contract.
app.get('/getValue', async (req, res) => {
var source = fs.readFileSync("./MyContract.sol", 'utf8');
var compiledContract = solc.compile(source, 1);
var abi = compiledContract.contracts[':MyContract'].interface;
var contract = new web3.eth.Contract(JSON.parse(abi), contractAddress);
var currentValue = await contract.methods.getValue().call();
return res.send(currentValue);
});
// Write data to a contract on the blockchain.
// Call the setValue method in the contract.
// "changing" a value stored on the blockchain requires sending a transaction.
// This method returns the hash of the transaction we send.
app.get('/setValue', async (req, res) => {
try {
var source = fs.readFileSync("./MyContract.sol", 'utf8');
var compiledContract = solc.compile(source, 1);
var abi = compiledContract.contracts[':MyContract'].interface;
var contract = new web3.eth.Contract(JSON.parse(abi), contractAddress);
var data = contract.methods.setValue(req.query.newValue).encodeABI();
web3.eth.getTransactionCount(accountAddress, function (err, nonce) {
var rawTx = {
nonce: nonce,
to: contractAddress,
gasPrice: '0x00',
gasLimit: '0x2FAF080',
value: '0x00',
data: data
}
var tx = new ethereumjs(rawTx);
tx.sign(privateKey);
var raw = '0x' + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(raw)
.on('transactionHash', hash => {
return res.send(hash);
})
.then(receipt => {
console.log('Tx Mined', receipt)
if(receipt.status == '0x1' || receipt.status == 1){
console.log('Transaction Success')
}
else
console.log('Transaction Failed')
})
.catch( err => {
console.error('Something went wrong: ' + err)
})
});
} catch(error) {
console.error('Something went wrong: ' + err)
}
})