@calp-pro/dex-db 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dex_db.js CHANGED
@@ -3,84 +3,69 @@
3
3
  const rl = require('readline')
4
4
 
5
5
  function dex_db(pairs = []) {
6
- const trie = {
7
- pairs: {},
8
- tokens: {}
9
- }
10
- const P = []//pairs
11
- const T = []//tokens
12
- const p2tt = []// [[it0, it1], ...]
6
+ const aP = []
7
+ const aT = []
8
+ const P = new Map()//pairs
9
+ const T = new Map()//tokens
10
+ const p2tt = []// [p1.it0, p1.it1, p2.it0, p2, it1, ...]
13
11
  const t2pt = []// [ [[ip, it], [ip, it], ...], ...]
14
12
 
15
- const set = (address, index, trie) =>
16
- address.split('').reduce(
17
- (node, letter, i, a, last = i == a.length - 1) => {
18
- if (last)
19
- node[letter] = index
20
- else
21
- node[letter] ??= {}
22
- return node[letter]
23
- },
24
- trie
25
- )
26
-
27
- const get = (address, trie) => {
28
- const letters = address.split('')
29
- let node = trie
30
- for (let i = 0, letter; i < letters.length; i++) {
31
- letter = letters[i]
32
- if (node[letter] == undefined) return -1
33
- node = node[letter]
34
- }
35
- return node
36
- }
37
-
38
13
  const index = ([pair, token0, token1]) => {
39
- var ip = get(pair, trie.pairs)
40
- if (ip == -1) {
41
- ip = P.length
42
- P[ip] = pair
43
- set(pair, ip, trie.pairs)
14
+ var ip = P.get(pair)
15
+ if (ip == undefined) {
16
+ P.set(pair, ip = P.size)
17
+ aP.push(pair)
44
18
  }
45
- var it0 = get(token0, trie.tokens)
46
- if (it0 == -1) {
47
- it0 = T.length
48
- T[it0] = token0
49
- set(token0, it0, trie.tokens)
19
+
20
+ var it0 = T.get(token0)
21
+ if (it0 == undefined) {
22
+ T.set(token0, it0 = T.size)
23
+ aT.push(token1)
50
24
  }
51
- var it1 = get(token1, trie.tokens)
52
- if (it1 == -1) {
53
- it1 = T.length
54
- T[it1] = token1
55
- set(token1, it1, trie.tokens)
25
+
26
+ var it1 = T.get(token1)
27
+ if (it1 == undefined) {
28
+ T.set(token1, it1 = T.size)
29
+ aT.push(token1)
56
30
  }
57
- p2tt[ip] ??= []
58
- p2tt[ip][0] = it0
59
- p2tt[ip][1] = it1
31
+
32
+ p2tt[ip * 2] = it0
33
+ p2tt[ip * 2 + 1] = it1
60
34
 
61
35
  if (t2pt[it0])
62
- t2pt[it0].push([ip, it1])
36
+ t2pt[it0].push(ip, it1)
63
37
  else
64
- t2pt[it0] = [[ip, it1]]
38
+ t2pt[it0] = [ip, it1]
65
39
 
66
40
  if (t2pt[it1])
67
- t2pt[it1].push([ip, it0])
41
+ t2pt[it1].push(ip, it0)
68
42
  else
69
- t2pt[it1] = [[ip, it0]]
43
+ t2pt[it1] = [ip, it0]
70
44
  }
71
45
 
72
46
  const find_pairs_with_token = token => {
73
- const it = get(token, trie.tokens)
74
- if (it == -1) return []
75
- return t2pt[it].map(pt => P[pt[0]])
47
+ const pairs = []
48
+ const it = T.get(token)
49
+ if (it == undefined) return pairs
50
+ for (var i = 0; i < t2pt[it].length; i += 2)
51
+ pairs.push(
52
+ aP[t2pt[it][i]]
53
+ )
54
+ return pairs
76
55
  }
77
56
 
78
57
  const find_pairs_with_tokens = (token0, token1) => {
79
- const it0 = get(token0, trie.tokens)
80
- if (it0 == -1) return []
81
- const it1 = get(token1, trie.tokens)
82
- if (it1 == -1) return []
83
- return t2pt[it0].filter(pt => pt[1] == it1).map(pt => P[pt[0]])
58
+ const pairs = []
59
+ const it0 = T.get(token0)
60
+ if (it0 == undefined) return pairs
61
+ const it1 = T.get(token1)
62
+ if (it1 == undefined) return pairs
63
+ for (var i = 0; i < t2pt[it0].length; i += 2)
64
+ if (t2pt[it0][i + 1] == it1)
65
+ pairs.push(
66
+ aP[t2pt[it0][i]]
67
+ )
68
+ return pairs
84
69
  }
85
70
 
86
71
  pairs.forEach(index)
@@ -89,7 +74,7 @@ function dex_db(pairs = []) {
89
74
  index,
90
75
  find_pairs_with_token,
91
76
  find_pairs_with_tokens,
92
- //save, TODO
77
+ //save, TODO
93
78
  //load, TODO
94
79
  }
95
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calp-pro/dex-db",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Database designed for handle relation pair/pool address to token0 & token1 addresses",
5
5
  "keywords": [
6
6
  "DEX",
@@ -18,12 +18,23 @@
18
18
  "author": "Vladimir Spirin (spirin.vladimir@gmail.com)",
19
19
  "type": "commonjs",
20
20
  "main": "dex_db.js",
21
+ "module": "src/index.mjs",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./index.d.ts",
25
+ "require": "./dex_db.js",
26
+ "import": "./index.mjs",
27
+ "default": "./dex_db.js"
28
+ }
29
+ },
30
+ "types": "index.d.ts",
21
31
  "scripts": {
22
32
  "test": "node --test test.*"
23
33
  },
24
34
  "devDependencies": {
25
35
  "pancakeswap-dump": "^1.0.8",
26
- "sushiswap-dump": "^1.0.3"
36
+ "sushiswap-dump": "^1.0.3",
37
+ "uniswap-v2-dump": "^2.0.24"
27
38
  },
28
39
  "publishConfig": {
29
40
  "provenance": true
@@ -0,0 +1,32 @@
1
+ const { describe, before, it } = require('node:test')
2
+ const assert = require('node:assert/strict')
3
+ const uniswap_v2_dump = require('uniswap-v2-dump')
4
+ const dex_db = require('./dex_db')
5
+
6
+ describe('Uniswap v2', () => {
7
+ var db
8
+
9
+ before(() =>
10
+ uniswap_v2_dump.load({workers: 0})
11
+ .then(pairs =>
12
+ db = dex_db(
13
+ pairs.map(({pair, token0, token1}) =>
14
+ [pair, token0, token1]
15
+ )
16
+ )
17
+ )
18
+ )
19
+
20
+ it('Get all pairs with BAT at Uniswap v2', () => {
21
+ const pairs = db.find_pairs_with_token(
22
+ '0x0d8775f648430679a709e98d2b0cb6250d2887ef'/*BAT*/
23
+ )
24
+ assert.ok(pairs.length >= 40)
25
+ assert.equal(pairs[0], '0xb6909b960dbbe7392d405429eb2b3649752b4838', 'BAT/WETH https://etherscan.io/address/0xaf4b3145ca0cadbd9454f5815ef5d221f828507e')
26
+ assert.equal(pairs[1], '0x6929abd7931d0243777d3cd147fe863646a752ba', 'BAT/DAI https://etherscan.io/address/0x6929abd7931d0243777d3cd147fe863646a752ba')
27
+ })
28
+
29
+ })
30
+
31
+
32
+