Skip to content

Commit dc15db7

Browse files
committed
Utils & Fixes
1 parent 8041c0b commit dc15db7

File tree

8 files changed

+43
-74
lines changed

8 files changed

+43
-74
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
# 0.1.5 -- Utils & Fixes
3+
4+
- Exposing serialize and unserialize functions via utils
5+
- Fixed file store error when no index exists
6+
27
# 0.1.5 -- NPM Package Tweaks
38

49
- Including all stores in lib

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Syncs events from Ethereum and indexes them for fast retrieval. This is useful a
1313
- [x] Keep track of blockNumbers in persistence for fast-resume
1414
- [x] Benchmark documentation
1515
- [x] Support going forward syncing for Ethereum
16-
- [ ] NPM package
16+
- [x] NPM package
1717
- [ ] Improved failsafes for data consistency
1818
- [ ] Split up stores into separate dependencies
1919

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eth-indexer",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "High performance Ethereum smart contract event indexing for fast local retrieval",
55
"author": "Dominiek Ter Heide <[email protected]>",
66
"license": "MIT",

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import redisStore from './stores/redis';
88
import mongodbStore from './stores/mongodb';
99
import elasticsearchStore from './stores/elasticsearch';
1010
import levelStore from './stores/level';
11+
import { serialize, unserialize } from './utils';
1112

1213
export const stores = {
1314
memory: memoryStore,
@@ -18,6 +19,10 @@ export const stores = {
1819
level: levelStore,
1920
};
2021

22+
export const utils = {
23+
serialize, unserialize,
24+
};
25+
2126
const waitForBlockchainSync = client => new Promise((accept) => {
2227
let i = 0;
2328
const getAndCheckStatus = (callback) => {

src/stores/elasticsearch.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11

22
import elasticsearch from 'elasticsearch';
3-
import BigNumber from 'bignumber.js';
4-
5-
const serialize = (event) => {
6-
const doc = Object.assign({}, event);
7-
for (const key in event.args) {
8-
if (event.args[key] instanceof BigNumber) {
9-
doc.args[key] = {
10-
type: 'BigNumber',
11-
value: event.args[key].toString(),
12-
};
13-
}
14-
}
15-
return doc;
16-
};
17-
18-
const unserialize = (doc) => {
19-
const event = Object.assign({}, doc);
20-
for (const key in doc.args) {
21-
if (doc.args[key] && doc.args[key].type === 'BigNumber') {
22-
event.args[key] = new BigNumber(doc.args[key].value);
23-
}
24-
}
25-
return event;
26-
};
3+
import { serialize, unserialize } from '../utils';
274

285
const createElasticsearchIndex = async (client, eventType, config) => {
296
const index = eventType.toLowerCase();

src/stores/file.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,7 @@
22
import fs from 'fs';
33
import path from 'path';
44
import rimraf from 'rimraf';
5-
import BigNumber from 'bignumber.js';
6-
7-
const serialize = (event) => {
8-
const doc = Object.assign({}, event);
9-
for (const key in event.args) {
10-
if (event.args[key] instanceof BigNumber) {
11-
doc.args[key] = {
12-
type: 'BigNumber',
13-
value: event.args[key].toString(),
14-
};
15-
}
16-
}
17-
return doc;
18-
};
19-
20-
const unserialize = (doc) => {
21-
const event = Object.assign({}, doc);
22-
for (const key in doc.args) {
23-
if (doc.args[key] && doc.args[key].type === 'BigNumber') {
24-
event.args[key] = new BigNumber(doc.args[key].value);
25-
}
26-
}
27-
return event;
28-
};
5+
import { serialize, unserialize } from '../utils';
296

307
export default class FileStore {
318
constructor(indexing, dbPath) {
@@ -69,6 +46,9 @@ export default class FileStore {
6946
get(eventType, indexId, value) {
7047
const indexKey = `${eventType}-${indexId}-${value}`;
7148
const filePath = `${this.dbPath}/${indexKey}.jsons`;
49+
if (!fs.existsSync(filePath)) {
50+
return [];
51+
}
7252
return fs.readFileSync(filePath)
7353
.toString().split('\n')
7454
.filter(line => line.length > 0)

src/stores/mongodb.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11

22
import { promisify } from 'util';
33
import { MongoClient } from 'mongodb';
4-
import BigNumber from 'bignumber.js';
5-
6-
const serialize = (event) => {
7-
const doc = Object.assign({}, event);
8-
for (const key in event.args) {
9-
if (event.args[key] instanceof BigNumber) {
10-
doc.args[key] = {
11-
type: 'BigNumber',
12-
value: event.args[key].toString(),
13-
};
14-
}
15-
}
16-
return doc;
17-
};
18-
19-
const unserialize = (doc) => {
20-
const event = Object.assign({}, doc);
21-
for (const key in doc.args) {
22-
if (doc.args[key] && doc.args[key].type === 'BigNumber') {
23-
event.args[key] = new BigNumber(doc.args[key].value);
24-
}
25-
}
26-
return event;
27-
};
4+
import { serialize, unserialize } from '../utils';
285

296
const mongodbInsertMany = (collection, events) => new Promise((accept, reject) => {
307
collection.insertMany(events, (err) => {

src/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
import BigNumber from 'bignumber.js';
3+
4+
export const serialize = (event) => {
5+
const doc = Object.assign({}, event);
6+
for (const key in event.args) {
7+
if (event.args[key] instanceof BigNumber) {
8+
doc.args[key] = {
9+
type: 'BigNumber',
10+
value: event.args[key].toString(),
11+
};
12+
}
13+
}
14+
return doc;
15+
};
16+
17+
export const unserialize = (doc) => {
18+
const event = Object.assign({}, doc);
19+
for (const key in doc.args) {
20+
if (doc.args[key] && doc.args[key].type === 'BigNumber') {
21+
event.args[key] = new BigNumber(doc.args[key].value);
22+
}
23+
}
24+
return event;
25+
};

0 commit comments

Comments
 (0)