-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_write.js
More file actions
287 lines (270 loc) · 12.5 KB
/
Copy pathhandler_write.js
File metadata and controls
287 lines (270 loc) · 12.5 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
var path = require('path')
var config = require('./config')
var logger = config.logger()
const {
Bussiness
} = require('./bussiness')
const db = config.db
const substrate = require('oo7-substrate');
const {
decode
} = require('oo7-substrate/src/codec.js')
const {
toBtcAddress,
bytesToRIHex,
bytesToHex
} = require('oo7-substrate/src/utils');
const {
parseRedeemScript
} = require('./bitcoin')
exports.handler = function (response, request) {
var body = ''
request.on('data', function (chunk) {
body += chunk
})
request.on('end', function () {
try {
// {"data":[{"key":["k1","k2"...],"prefix":"block","value":["v1","v2"]}],"height":0}
logger.debug('Receive write data:' + JSON.stringify(body))
body = JSON.parse(body)
for (var i = 0; i < body.data.length; i++) {
let tail_sql = ''
let value = body.data[i].value
if ("null" == value || !value) { //已被删除
if ("XBridgeOfBTC PendingDepositMap" == body.data[i].prefix) {
let delete_sql = "delete from \"XBridgeOfBTC_PendingDepositMap\" where address='" + JSON.stringify(body.data[i].key) + "';"
logger.debug(delete_sql)
db.query(delete_sql, (err, res) => {
if (err && err.code != 23505) {
logger.error(delete_sql, err)
// throw err
}
})
}
continue;
}
if ("XAssets AssetBalance" == body.data[i].prefix || "XAssets TotalAssetBalance" == body.data[i].prefix) {
value = [
value['Free'] ? value['Free'] : 0,
value['ReservedStaking'] ? value['ReservedStaking'] : 0,
value['ReservedStakingRevocation'] ? value['ReservedStakingRevocation'] : 0,
value['ReservedWithdrawal'] ? value['ReservedWithdrawal'] : 0,
value['ReservedDexSpot'] ? value['ReservedDexSpot'] : 0,
value['ReservedDexFuture'] ? value['ReservedDexFuture'] : 0,
value['ReservedErc20'] ? value['ReservedErc20'] : 0,
]
} else if ("XTokens PseduIntentions" == body.data[i].prefix) {
value = [JSON.stringify(value)];
} else if ("XTokens PseduIntentionProfiles" == body.data[i].prefix || "XTokens PseduIntentionProfilesV1" == body.data[i].prefix) {
value = [
value.last_total_deposit_weight,
value.last_total_deposit_weight_update
]
} else if ("XTokens DepositRecords" == body.data[i].prefix || "XTokens DepositRecordsV1" == body.data[i].prefix) {
value = [
value.last_deposit_weight,
value.last_deposit_weight_update
]
} else if ("XStaking IntentionProfiles" == body.data[i].prefix) {
value = [
value.total_nomination,
value.last_total_vote_weight,
value.last_total_vote_weight_update
]
} else if ("XSpot TradingPairOf" == body.data[i].prefix) {
value = [
JSON.stringify(value.currency_pair),
value.precision,
value.unit_precision,
value.online
]
} else if ("XSpot QuotationsOf" == body.data[i].prefix) {
value = [
JSON.stringify(value)
]
} else if ("XAccounts IntentionPropertiesOf" == body.data[i].prefix) {
value = [
value.url,
value.is_active,
value.about,
value.session_key != null ? value.session_key : '',
]
} else if ("XBridgeFeatures BitcoinTrusteeIntentionPropertiesOf" == body.data[i].prefix) {
value = [
value.about,
JSON.stringify(value.hot_entity),
JSON.stringify(value.cold_entity)
]
} else if ("XStaking Intentions" == body.data[i].prefix || "XStaking IntentionsV1" == body.data[i].prefix) {
value = [
JSON.stringify(value)
]
} else if ("XStaking SlashedPerSession" == body.data[i].prefix) {
value = [
JSON.stringify(value)
]
} else if ("XBridgeFeatures BitcoinCrossChainOf" == body.data[i].prefix) {
body.data[i].key = JSON.stringify(body.data[i].key)
value = [
value[0],
value[1] ? value[1] : ''
]
} else if ("XBridgeFeatures EthereumCrossChainOf" == body.data[i].prefix) {
value = [
value[0],
value[1] ? value[1] : ''
]
} else if ("XBridgeFeatures BitcoinTrusteeSessionInfoOf" == body.data[i].prefix) {
let hot_redeem_script = parseRedeemScript(value.hot_address.redeem_script.replace(/^0x/, ''))
let cold_redeem_script = parseRedeemScript(value.cold_address.redeem_script.replace(/^0x/, ''))
logger.debug('redeem_script ', hot_redeem_script, cold_redeem_script)
value = [
JSON.stringify(value.trustee_list),
JSON.stringify(value.hot_address),
JSON.stringify(value.cold_address),
JSON.stringify(hot_redeem_script.keys),
JSON.stringify(cold_redeem_script.keys)
]
} else if ("XStaking NominationRecords" == body.data[i].prefix || "XStaking NominationRecordsV1" == body.data[i].prefix) {
value = [
value.nomination,
value.last_vote_weight,
value.last_vote_weight_update,
JSON.stringify(value.revocations)
]
} else if ("XAssets AssetInfo" == body.data[i].prefix) {
value = [
value[0].token_name,
value[0].chain,
value[0].precision,
value[0].desc.replace(/\'/, ' '),
value[1],
value[2]
]
} else if ("XSpot OrderInfoOf" == body.data[i].prefix) {
value = [
value.props[1],
value.props[4],
value.props[5],
value.props[0],
value.props[6],
value.props[2],
value.props[3],
value.already_filled,
value.props[7],
value.last_update_at,
value.status,
value.remaining,
JSON.stringify(value.executed_indices)
]
} else if ("XSpot HandicapOf" == body.data[i].prefix) {
value = [
value.highest_bid,
value.lowest_offer
]
} else if ("XAssetsRecords ApplicationMap" == body.data[i].prefix) {
value = [
value.data.applicant,
value.data.token,
value.data.balance,
value.data.addr,
value.data.ext,
value.data.time
]
} else if ("XBridgeOfBTC PendingDepositMap" == body.data[i].prefix) {
body.data[i].type = "value"
value = [
JSON.stringify(body.data[i].key),
JSON.stringify(value)
]
} else if ("XBridgeOfBTC BlockHeaderFor" == body.data[i].prefix) {
let hash_b = substrate.hexToBytes(body.data[i].key)
let hash = substrate.bytesToRIHex(hash_b)
body.data[i].key = hash
let previous_b = substrate.hexToBytes(value.header.previous_header_hash)
let previous_hash = substrate.bytesToRIHex(previous_b)
let merkle_b = substrate.hexToBytes(value.header.merkle_root_hash)
let merkle_hash = substrate.bytesToRIHex(merkle_b)
for (var j = 0; value.txid_list && j < value.txid_list.length; j++) {
value.txid_list[j] = substrate.bytesToRIHex(substrate.hexToBytes(value.txid_list[j]))
}
if (value.confirmed == false) {
tail_sql = 'update "public"."XBridgeOfBTC_BlockHeaderFor" set insert_height=' + body.height + ' where header=\'' + body.data[i].key + '\';'
}
value = [
value.height,
value.header.version,
previous_hash,
merkle_hash,
value.header.time,
value.header.bits,
value.header.nonce,
JSON.stringify(value.txid_list),
value.confirmed,
]
} else if ("XBridgeOfBTC TxFor" == body.data[i].prefix) {
body.data[i].key = substrate.bytesToRIHex(substrate.hexToBytes(body.data[i].key))
let balance = 0
if ('Withdrawal' == value.tx_type) {
for (var k = 0; k < value.raw_tx.outputs.length - 1; k++) {
balance += value.raw_tx.outputs[k].value
}
}
value = [
value.tx_type,
value.raw_tx.version,
JSON.stringify(value.raw_tx.inputs),
JSON.stringify(value.raw_tx.outputs),
value.raw_tx.lock_time,
balance
]
} else if (!(value instanceof Array)) {
value = [value]
} else if ("value" == body.data[i].type) {
value = value.map(v => {
if (typeof v == 'object') {
return JSON.stringify(v);
} else
return v.toString();
});
}
let key = body.data[i].key
if (!(key instanceof Array)) {
key = [key]
}
let sql = '';
if (body.data[i].type == "value") {
sql = Bussiness[body.data[i].prefix].insertSql([...value, body.height])
} else {
sql = Bussiness[body.data[i].prefix].insertOrUpdateSql([...key.map(v => v.toString()), ...value, body.height])
}
if (tail_sql != '') {
sql += tail_sql;
}
db.query(sql, (err, res) => {
logger.debug(sql, err)
if (err && err.code != 23505) {
logger.error(sql, err)
// throw err
}
})
}
} catch (e) {
logger.error('write!' + JSON.stringify(body) + e)
response.writeHead(200, {
'Content-Type': 'text/html',
'X-FRAME-OPTIONS': 'DENY',
'Cache-Control': 'no-cache'
})
response.end('{"result":"null"}')
return
}
response.writeHead(200, {
'Content-Type': 'text/html',
'X-FRAME-OPTIONS': 'DENY',
'Cache-Control': 'no-cache'
})
response.end('{"result":"OK"}')
return
})
}