-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
187 lines (161 loc) · 4.37 KB
/
app.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fastify from 'fastify'
import Web3 from 'web3'
import utils from 'ethereumjs-util'
import Transaction from 'ethereumjs-tx'
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.WEB3))
const CONSTANTS = {
networkId: 1,
from: process.env.FROM,
privateKey: new Buffer(process.env.PRIVATE_KEY, 'hex'),
gasPrice: parseInt(process.env.GAS_PRICE)
}
const server = fastify()
web3.eth.net.getId((err, result) => {
CONSTANTS.networkId = err || result
})
async function makeTransaction(to, value, data, gasLimit, gasPrice) {
console.log("push1")
const nonce = utils.bufferToHex(await web3.eth.getTransactionCount(CONSTANTS.from))
console.log("push1")
const tx = new Transaction({
to,
value,
data,
gasLimit,
gasPrice,
nonce
}, CONSTANTS.chainId)
console.log(CONSTANTS.privateKey)
tx.sign(CONSTANTS.privateKey)
const raw = `0x${tx.serialize().toString('hex')}`
console.log(raw)
return new Promise((resolve, reject) => {
web3.eth.sendSignedTransaction(raw)
.on('transactionHash', hash => {
resolve(hash)
})
.catch(reject)
})
}
const opts = {
schema: {
body: {
type: 'object',
properties: {
contract: { type: 'string' },
at: { type: 'string' },
method: { type: 'string' },
args: { type: 'array' }
}
}
}
}
server.post('/events', opts, async (request, reply) => {
if (!request.body.contract) {
return reply.send({
error: 'Internal Server Error',
message: `contract name is required`,
statusCode: 500
})
}
if (!request.body.event) {
return reply.send({
error: 'Internal Server Error',
message: `contract event is required`,
statusCode: 500
})
}
const json = require(`./abi/${request.body.contract}.json`)
const address = request.body.at || json.networks[CONSTANTS.networkId].address
const event = request.body.event
let filter = request.body.filter || {}
let fromBlock = request.body.from || 0
const contract = new web3.eth.Contract(json.abi, address)
const events = await contract.getPastEvents(event, {
filter,
fromBlock
})
reply.send({
statusCode: 200,
result: events
})
})
server.post('/contract', opts, async (request, reply) => {
if (!request.body.contract) {
return reply.send({
error: 'Internal Server Error',
message: `contract name is required`,
statusCode: 500
})
}
if (!request.body.method) {
return reply.send({
error: 'Internal Server Error',
message: `contract method is required`,
statusCode: 500
})
}
const json = require(`./abi/${request.body.contract}.json`)
const address = request.body.at || json.networks[CONSTANTS.networkId].address
const method = request.body.method
let args = request.body.args || []
const contract = new web3.eth.Contract(json.abi, address)
const contractMethod = contract.methods[method](...args)
contractMethod.estimateGas({ gas: 5 * 1e6 }, (error, estimateGas) => {
if (error) {
return reply.send(error)
}
if (contractMethod._method.constant) {
contractMethod.call({}, (error, result) => {
if (error) {
return reply.send(error)
}
return reply.send({
result,
statusCode: 200
})
})
} else {
const data = contractMethod.encodeABI()
makeTransaction(address, 0, data, estimateGas, CONSTANTS.gasPrice)
.then(result => reply.send({
result,
statusCode: 200
}))
.catch(error => reply.send({
error: 'Internal Server Error',
message: error,
statusCode: 500
}))
}
})
})
server.post('/*', opts, async (request, reply) => {
const commands = request.params['*'].split('/')
let func = web3.eth
let args = request.body.args || []
for (let part of commands) {
func = func[part]
}
if (!func) {
return reply.send({
'error': 'Internal Server Error',
'message': `web3.eth.${commands.join('.')} is not a function`,
'statusCode': 500
})
}
func(...args, (error, result) => {
if (error) {
return reply.send(error)
}
return reply.send({
result,
'statusCode': 200
})
})
})
// Run the server!
server.listen(3000, function (err) {
if (err) throw err
server.log.info(`server listening on ${server.server.address().port}`)
})