-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
105 lines (91 loc) · 3.13 KB
/
main.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
'use strict';
const express = require("express");
const bodyParser = require('body-parser');
const minimist = require('minimist');
const block = require("./block");
const tx = require("./transaction");
const config = require("./config");
const geth = require("./geth");
const initHttpServer = (http_port) => {
const app = express();
app.use(bodyParser.json());
// Block related
app.get('/blocks', (req, res) => {
res.send(JSON.stringify(block.getBlocks().map(b => b.printBlock())));
});
app.post('/mineBlock', async (req, res) => {
try {
const newBlock = await block.generateNextBlock(geth);
res.send(JSON.stringify(newBlock.printBlock()));
} catch (e) {
res.send(JSON.stringify(e));
}
});
// Transaction related
app.post('/transact', async (req, res) => {
try {
const rawTx = await tx.createTransaction(req.body, geth);
console.log('New transaction created: ' + JSON.stringify(rawTx));
res.send(JSON.stringify(rawTx.toString(true)));
} catch (e) {
res.send(JSON.stringify(e));
}
});
// Deposit related
app.post('/deposit', async (req, res) => {
try{
await geth.deposit(req.body.address, req.body.amount);
res.send();
}catch(e){
console.log(e)
}
});
// Withdrawal related
app.post('/withdraw/create', async (req, res) => {
const p = block.getTransactionProofInBlock(req.body.blkNum,
req.body.txIndex);
try{
const withdrawalId = await geth.startWithdrawal(req.body.blkNum,
req.body.txIndex, req.body.oIndex, p.tx, p.proof, req.body.from);
res.send(withdrawalId);
}catch(e){
console.log(e)
}
});
app.post('/withdraw/challenge', async (req, res) => {
const p = block.getTransactionProofInBlock(req.body.blkNum,
req.body.txIndex);
try{
await geth.challengeWithdrawal(req.body.withdrawalId, req.body.blkNum,
req.body.txIndex, req.body.oIndex, p.tx, p.proof, req.body.from);
res.send();
}catch(e){
console.log(e)
}
});
app.post('/withdraw/finalize', async (req, res) => {
try{
await geth.finalizeWithdrawal(req.body.from);
res.send();
}catch(e){
console.log(e)
}
});
// Debug function
app.get('/utxo', (req, res) => {
res.send(tx.getUTXO());
});
app.get('/pool', (req, res) => {
res.send(tx.getPool());
});
app.listen(http_port, () => console.log('Listening http on port: ' + http_port));
};
const main = () => {
const argv = minimist(process.argv.slice(2), { string: ["port", "contract", "operator"]});
const http_port = argv.port || 3001;
const contract_address = argv.contract || config.plasmaContractAddress;
const operator_address = argv.operator || config.plasmaOperatorAddress;
geth.init(contract_address, operator_address);
initHttpServer(http_port);
};
main();