-
Notifications
You must be signed in to change notification settings - Fork 12
/
db.js
268 lines (243 loc) · 7.43 KB
/
db.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
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
const mongoose = require('mongoose');
let config = require('./config.json')
const { Schema } = mongoose;
/***
* Author: Luke.Nguyen
* Company: sotatek
* Country: Vietnam
* PhoneNumber: +84 386743836
*
* Patch date: 04/05/2021
*
* Newly addded schema:
*
* TokenHolder - Schema for checking token balance of each address that has the token in it
*
*
* Newly updated schema:
*
*
*
*
* **/
const TokenHolder = new Schema(
{
'address': { type: String, lowercase: true},
'tokenContract': {type: String, lowercase: true},
'tokenName': {type: String},
'symbol': {type: String},
'balance': {type: String},
}, { collection: 'TokenHolder' },
);
TokenHolder.index({ "address": 1, "tokenContract": 1} , {unique: true});
TokenHolder.index({ "address": 1})
TokenHolder.index({"tokenContract": 1})
mongoose.model('TokenHolder', TokenHolder);
module.exports.TokenHolder = mongoose.model('TokenHolder');
/**
* End patching
*
* **/
const Block = new Schema(
{
'number': { type: Number, index: { unique: true } },
'hash': String,
'parentHash': String,
'nonce': String,
'sha3Uncles': String,
'logsBloom': String,
'transactionsRoot': String,
'stateRoot': String,
'receiptRoot': String,
'miner': { type: String, lowercase: true },
'difficulty': String,
'totalDifficulty': String,
'size': Number,
'extraData': String,
'gasLimit': Number,
'gasUsed': Number,
'timestamp': Number,
'blockTime': Number,
'uncles': [String],
}, { collection: 'Block' },
);
//master node Info
var Witness = new Schema(
{
"blocksNum": Number,//mine block count
"lastCountTo": Number,//block height
"witness": {type: String, index: {unique: true}},
"status":Boolean,
"hash":String,
"reward":Number,
"miner":String,
"timestamp": Number
});
const Contract = new Schema(
{
'address': { type: String, index: { unique: true } },
'blockNumber': Number,
'ERC': { type: Number, index: true }, //0:normal contract, 2:ERC20, 3:ERC223
'creationTransaction': String,
'contractName': String,
'tokenName': String,
'symbol': String,
'owner': String,
'decimals': Number,
'totalSupply': Number,
'compilerVersion': String,
'optimization': Boolean,
'sourceCode': String,
'abi': String,
'byteCode': String,
}, { collection: 'Contract' },
);
const TokenMetadata = new Schema(
{
'address': { type: String, index: { unique: true } },
'officialWebsite': String,
'socialLinks': Object,
},
{ collection: 'TokenMetadata' },
)
const Account = new Schema(
{
'address': { type: String, index: { unique: true } },
'balance': Number,
'blockNumber': Number,
'type': { type: Number, default: 0 }, // address: 0x0, contract: 0x1
}, { collection: 'Account' },
);
const Transaction = new Schema(
{
'hash': { type: String, index: { unique: true }, lowercase: true },
'nonce': Number,
'blockHash': String,
'blockNumber': Number,
'transactionIndex': Number,
'status': Number,
'from': { type: String, lowercase: true },
'to': { type: String, lowercase: true },
'creates': { type: String, lowercase: true },
'value': String,
'gas': Number,
'gasUsed': Number,
'gasPrice': String,
'timestamp': Number,
'input': String,
}, { collection: 'Transaction' },
);
const TokenTransfer = new Schema(
{
'hash': { type: String, index: { unique: true }, lowercase: true },
'blockNumber': Number,
'method': String,
'from': { type: String, lowercase: true },
'to': { type: String, lowercase: true },
'contract': { type: String, lowercase: true },
'value': String,
'timestamp': Number,
}, { collection: 'TokenTransfer' },
);
const BlockStat = new Schema(
{
'number': { type: Number, index: { unique: true } },
'timestamp': Number,
'difficulty': String,
'hashrate': String,
'txCount': Number,
'gasUsed': Number,
'gasLimit': Number,
'miner': String,
'blockTime': Number,
'uncleCount': Number,
}, { collection: 'BlockStat' },
);
var ActiveAddressesStat = new Schema({
"blockNumber": String,
"count": Number
})
const Market = new Schema(
{
"symbol": String,
"timestamp": Number,
"quoteBTC": Number,
"quoteEUR": Number,
"quoteUSD": Number,
"quoteINR": Number,
"percent_change_24h": Number,
"volume_24h":Number
}, { collection: 'Market' },
);
var LogEvent = new Schema(
{
"address": String,
"txHash": {type: String, index: true},
"blockNumber": Number,
"contractAdd": String,//same with address
"timestamp": Number,
"methodName": String,
"eventName": String,
"from": String,
"to": String,
"logIndex": Number,
"topics": Array,
"data": String
});
mongoose.model('LogEvent', LogEvent);
//all address
var Address = new Schema(
{
"addr": {type: String, index: {unique: true}},
"type": {type: Number, index: true},//0:normal 1:contract 2:masternode
"balance": Number
});
mongoose.model('Address', Address);
// create indexes
Transaction.index({ blockNumber: -1 });
Transaction.index({ from: 1, blockNumber: -1 });
Transaction.index({ to: 1, blockNumber: -1 });
Transaction.index({ creates: 1, blockNumber: -1 });
Account.index({ balance: -1 });
Account.index({ balance: -1, blockNumber: -1 });
Account.index({ type: -1, balance: -1 });
Block.index({ miner: 1 });
Block.index({ miner: 1, blockNumber: -1 });
Block.index({ hash: 1, number: -1 });
Market.index({ timestamp: -1 });
TokenTransfer.index({ blockNumber: -1 });
TokenTransfer.index({ from: 1, blockNumber: -1 });
TokenTransfer.index({ to: 1, blockNumber: -1 });
TokenTransfer.index({ contract: 1, blockNumber: -1 });
ActiveAddressesStat.index({blockNumber: -1});
mongoose.model('BlockStat', BlockStat);
mongoose.model('Block', Block);
mongoose.model('Account', Account);
mongoose.model('Contract', Contract);
mongoose.model('TokenMetadata', TokenMetadata);
mongoose.model('Transaction', Transaction);
mongoose.model('Market', Market);
mongoose.model('TokenTransfer', TokenTransfer);
mongoose.model('ActiveAddressesStat', ActiveAddressesStat);
mongoose.model('Witness', Witness);
module.exports.BlockStat = mongoose.model('BlockStat');
module.exports.Block = mongoose.model('Block');
module.exports.Contract = mongoose.model('Contract');
module.exports.TokenMetadata = mongoose.model('TokenMetadata');
module.exports.Transaction = mongoose.model('Transaction');
module.exports.Witness = Witness;
module.exports.LogEvent = mongoose.model('LogEvent');
module.exports.Address = mongoose.model('Address');
module.exports.Account = mongoose.model('Account');
module.exports.Market = mongoose.model('Market');
module.exports.TokenTransfer = mongoose.model('TokenTransfer');
module.exports.ActiveAddressesStat = mongoose.model('ActiveAddressesStat');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URI || config.MONGO_URI || 'mongodb://localhost/BlockScanDB', {
useMongoClient: true
// poolSize: 5,
// rs_name: 'myReplicaSetName',
// user: 'explorer',
// pass: 'yourdbpasscode'
});
// mongoose.set('debug', true);