@calp-pro/dex-db 1.0.1

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/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/npm/uniswap-v2-loader@5.0.1/logo-dark.svg"><img alt="calp.pro icon" src="https://cdn.jsdelivr.net/npm/uniswap-v2-loader@5.0.1/logo-light.svg" height="32" align="absmiddle"></picture>&nbsp;&nbsp;DEX DB
2
+
3
+ Database for decentralize exchangers.<br>
4
+ Low level data base based on [Radix-tree](https://en.wikipedia.org/wiki/Radix_tree) (same way as SQL).<br>
5
+ Each address (pair or token) presented as an integer number after indexing.<br>
6
+ Then [factory](https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Factory.sol) information can be presented as:
7
+ [[token0, token1], ...]<br>
8
+ Example:<br>
9
+ `[[0, 4], [0, 1], [5, 3], ...]`<br>
10
+ where:<br>
11
+ - `[0, 4]`
12
+ * is first element at array at index `0` (pair index `0`)
13
+ * `0` index of `token0` of pair 0
14
+ * `4` index of `token1` of pair 0
15
+ - `[0, 1]`
16
+ * is second element at array at index `1` (pair index `1`)
17
+ * `0` index of `token0` of pair 1 (same token can be at different pairs)
18
+ * `1` index of `token1` of pair 1
19
+ - `[5, 3]`
20
+ * is third element at array at index `2` (pair index `2`)
21
+ * `5` index of `token0` of pair 2
22
+ * `4` index of `token1` of pair 2
23
+
24
+ ## Install
25
+ ```bash
26
+ npm i --save dex-db
27
+ ```
28
+
29
+ ## Use
30
+ ```js
31
+ import dex_db from 'dex-db'
32
+
33
+ const db = dex_db(
34
+ [
35
+ ['0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc', '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'],
36
+ ['0x12ede161c702d1494612d19f05992f43aa6a26fb', '0x06af07097c9eeb7fd685c692751d5c66db49c215', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'],
37
+ ['0xa478c2975ab1ea89e8196811f51a7b7ade33eb11', '0x6b175474e89094c44da98b954eedeac495271d0f', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'],
38
+ ['0x07f068ca326a469fc1d87d85d448990c8cba7df9', '0x408e41876cccdc0f92210600ef50372656052a38', '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'],
39
+ ['0xae461ca67b15dc8dc81ce7615e0320da1a9ab8d5', '0x6b175474e89094c44da98b954eedeac495271d0f', '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'],
40
+ ['0xce407cd7b95b39d3b4d53065e711e713dd5c5999', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0xfa3e941d1f6b7b10ed84a0c211bfa8aee907965e']
41
+ ]
42
+ )
43
+
44
+ const pairs = dex_db.find_pairs_with_token('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'/*USDC*/)
45
+
46
+ //pairs[0] == '0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc'/*WETH/USDC*/)
47
+ //pairs[1] == '0x07f068ca326a469fc1d87d85d448990c8cba7df9'/*REN/USDC*/)
48
+ //pairs[2] == '0xae461ca67b15dc8dc81ce7615e0320da1a9ab8d5'/*DAI/USDC*/)
49
+ ```
package/dex_db.js ADDED
@@ -0,0 +1,97 @@
1
+ // DexDB
2
+ // written 13 March 2026 by Vladimir Spirin at Danang, Vietnam
3
+ const rl = require('readline')
4
+
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], ...]
13
+ const t2pt = []// [ [[ip, it], [ip, it], ...], ...]
14
+
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
+ 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)
44
+ }
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)
50
+ }
51
+ var it1 = get(token1, trie.tokens)
52
+ if (it1 == -1) {
53
+ it1 = T.length
54
+ T[it1] = token1
55
+ get(token1, it1, trie.tokens)
56
+ }
57
+ p2tt[ip] ??= []
58
+ p2tt[ip][0] = it0
59
+ p2tt[ip][1] = it1
60
+
61
+ if (t2pt[it0])
62
+ t2pt[it0].push([ip, it1])
63
+ else
64
+ t2pt[it0] = [[ip, it1]]
65
+
66
+ if (t2pt[it1])
67
+ t2pt[it1].push([ip, it0])
68
+ else
69
+ t2pt[it1] = [[ip, it0]]
70
+ }
71
+
72
+ const find_pairs_with_token = token => {
73
+ const it = get(token, trie.tokens)
74
+ if (it == -1) return -1
75
+ return t2pt[it].map(pt => P[pt[0]])
76
+ }
77
+
78
+ const find_pairs_with_tokens = (token0, token1) => {
79
+ const it0 = get(token0, trie.tokens)
80
+ if (it0 == -1) return -1
81
+ const it1 = get(token1, trie.tokens)
82
+ if (it1 == -1) return -1
83
+ return t2pt[it0].filter(pt => pt[1] == it1).map(pt => P[pt[0]])
84
+ }
85
+
86
+ pairs.forEach(index)
87
+
88
+ return {
89
+ index,
90
+ find_pairs_with_token,
91
+ find_pairs_with_tokens,
92
+ //save, TODO
93
+ //load, TODO
94
+ }
95
+ }
96
+
97
+ module.exports = dex_db
package/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import dex_db from './dex_db.js'
2
+ export default dex_db
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@calp-pro/dex-db",
3
+ "version": "1.0.1",
4
+ "description": "Database designed for handle relation pair/pool address to token0 & token1 addresses",
5
+ "keywords": [
6
+ "DEX",
7
+ "db"
8
+ ],
9
+ "homepage": "https://github.com/calp-pro/dex_db#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/calp-pro/dex_db/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/calp-pro/dex_db.git"
16
+ },
17
+ "license": "MIT",
18
+ "author": "Vladimir Spirin (spirin.vladimir@gmail.com)",
19
+ "type": "commonjs",
20
+ "main": "dex_db.js",
21
+ "scripts": {
22
+ "test": "node --test test.*"
23
+ },
24
+ "devDependencies": {
25
+ "pancakeswap-dump": "^1.0.8",
26
+ "sushiswap-dump": "^1.0.3"
27
+ }
28
+ }