uniswap-v2-loader 1.0.0

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.
Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/index.js +69 -0
  3. package/loader.js +95 -0
  4. package/package.json +30 -0
  5. package/test.js +17 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 calp.pro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.js ADDED
@@ -0,0 +1,69 @@
1
+ const { spawn } = require('child_process')
2
+ const os = require('os')
3
+ const fs = require('fs')
4
+ const { parseAbiItem, createPublicClient, http } = require('viem')
5
+ const { mainnet } = require('viem/chains')
6
+ const workers = os.cpus().length - 1
7
+ const missed = Array(workers).fill(null).map(() => [])
8
+ const key = process.env.KEY || process.argv[2] || 'FZBvlPrOxtgaKBBkry3SH0W1IqH4Y5tu'
9
+ const factory = '0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f'
10
+ const client = createPublicClient({
11
+ chain: mainnet,
12
+ transport: http('https://eth-mainnet.g.alchemy.com/v2/' + key)
13
+ })
14
+
15
+ const all = () => {
16
+ const filename = 'pairs.csv'
17
+ const pairs_ids = fs.existsSync(filename)
18
+ ? fs.readFileSync(filename).toString().trim().split('\n')
19
+ .map(line => +line.split(',')[0])
20
+ : []
21
+
22
+ return client.readContract({
23
+ address: factory,
24
+ abi: [parseAbiItem('function allPairsLength() view returns (uint256)')],
25
+ functionName: 'allPairsLength'
26
+ }).then(allPairsLength => {
27
+ allPairsLength = Number(allPairsLength)
28
+
29
+ missed.forEach(_ => _.length = 0)
30
+
31
+ for (var i = 0, rr = 0; i < allPairsLength; i++) {
32
+ if (pairs_ids.includes(i)) continue
33
+ missed[rr].push(i)
34
+ rr = (rr + 1) % workers
35
+ }
36
+
37
+ const jobs_data = {
38
+ missed,
39
+ factory,
40
+ filename,
41
+ key
42
+ }
43
+
44
+ fs.writeFileSync('jobs_data.json', JSON.stringify(jobs_data), 'utf8')
45
+
46
+ return Promise.all(
47
+ jobs_data.missed
48
+ .filter(_ => _.length)
49
+ .map((_, i) => new Promise(y =>
50
+ spawn('node', ['loader.js', i.toString()])
51
+ .on('close', y)
52
+ ))
53
+ )
54
+ .then(() =>
55
+ fs.readFileSync(filename).toString().trim().split('\n')
56
+ .map(line => {
57
+ line = line.split(',')
58
+ return {
59
+ id: +line[0],
60
+ pair: line[1],
61
+ token0: line[2],
62
+ token1: line[3]
63
+ }
64
+ })
65
+ )
66
+ })
67
+ }
68
+
69
+ module.exports.all = all
package/loader.js ADDED
@@ -0,0 +1,95 @@
1
+ const { parseAbiItem, createPublicClient, http } = require('viem')
2
+ const { mainnet } = require('viem/chains')
3
+ const fs = require('fs')
4
+
5
+ const POOL = {
6
+ ID: 0,
7
+ ADDRESS: 1,
8
+ TOKEN0: 2,
9
+ TOKEN1: 3
10
+ }
11
+
12
+ async function load(client, missed, filename, factory, key) {
13
+ let loaded = 0
14
+ const chunk_size = 50
15
+
16
+ for (let i = 0; i < missed.length; i += chunk_size) {
17
+ const chunk = missed.slice(i, i + chunk_size)
18
+
19
+ try {
20
+ const pool_addresses = await client.multicall({
21
+ contracts: chunk.map(i => ({
22
+ address: factory,
23
+ abi: [parseAbiItem('function allPairs(uint256) view returns (address)')],
24
+ functionName: 'allPairs',
25
+ args: [BigInt(i)]
26
+ }))
27
+ })
28
+
29
+ const pools_ok = []
30
+ const token_calls = []
31
+
32
+ pool_addresses.forEach((result, i) => {
33
+ if (result.status == 'success') {
34
+ const pool_address = result.result
35
+ pools_ok.push({
36
+ id: chunk[i],
37
+ address: pool_address
38
+ })
39
+ token_calls.push({
40
+ address: pool_address,
41
+ abi: [parseAbiItem('function token0() view returns (address)')],
42
+ functionName: 'token0'
43
+ })
44
+ token_calls.push({
45
+ address: pool_address,
46
+ abi: [parseAbiItem('function token1() view returns (address)')],
47
+ functionName: 'token1'
48
+ })
49
+ } else {
50
+ console.error('Failed loading pair', chunk[i], result)
51
+ }
52
+ })
53
+
54
+ if (token_calls.length === 0) continue
55
+
56
+ const token_results = await client.multicall({
57
+ contracts: token_calls
58
+ })
59
+
60
+ for (let j = 0, pool = []; j < pools_ok.length; j++) {
61
+ const token0_result = token_results[j * 2]
62
+ const token1_result = token_results[j * 2 + 1]
63
+
64
+ if (token0_result.status == 'success' && token1_result.status == 'success') {
65
+ pool[POOL.ID] = pools_ok[j].id
66
+ pool[POOL.ADDRESS] = pools_ok[j].address.toLowerCase()
67
+ pool[POOL.TOKEN0] = token0_result.result.toLowerCase()
68
+ pool[POOL.TOKEN1] = token1_result.result.toLowerCase()
69
+
70
+ fs.appendFileSync(filename, pool.join(',') + '\n')
71
+ }
72
+ }
73
+
74
+ } catch (error) {
75
+ console.error(error.message)
76
+ }
77
+ }
78
+ }
79
+
80
+ const job_index = +process.argv[2]
81
+
82
+ if (isNaN(job_index)) console.error('Required pass an index of job via argument') || process.exit(1)
83
+
84
+ const jobs_data = JSON.parse(fs.readFileSync('jobs_data.json', 'utf8'))
85
+ const missed = jobs_data.missed[job_index]
86
+ const filename = jobs_data.filename
87
+ const factory = jobs_data.factory
88
+ const key = jobs_data.key
89
+
90
+ const client = createPublicClient({
91
+ chain: mainnet,
92
+ transport: http(`https://eth-mainnet.g.alchemy.com/v2/${key}`)
93
+ })
94
+
95
+ load(client, missed, filename, factory, key)
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "uniswap-v2-loader",
3
+ "version": "1.0.0",
4
+ "description": "Uniswap v2 protocol loader",
5
+ "keywords": [
6
+ "uniswap-v2",
7
+ "protocol",
8
+ "loader",
9
+ "crypto",
10
+ "ethereum"
11
+ ],
12
+ "homepage": "https://github.com/calp-pro/uniswap-v2#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/calp-pro/uniswap-v2/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/calp-pro/uniswap-v2.git"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Vladimir Spirin (spirin.vladimir@gmail.com)",
22
+ "type": "commonjs",
23
+ "main": "index.js",
24
+ "scripts": {
25
+ "test": "node --test test.js"
26
+ },
27
+ "dependencies": {
28
+ "viem": "^2.46.2"
29
+ }
30
+ }
package/test.js ADDED
@@ -0,0 +1,17 @@
1
+ const { test } = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const uniswap_v2_loader = require('./index')
4
+
5
+ test('Exist USDC/USDP pair', () =>
6
+ uniswap_v2_loader.all().then(pairs => {
7
+ assert.ok(pairs.length > 0)
8
+ const i = pairs.findIndex(({id}) => id == 1)
9
+ assert.ok(i != -1)
10
+ if (i != -1) {
11
+ const {pair, token0, token1} = pairs[i]
12
+ assert.equal(pair, '0x3139ffc91b99aa94da8a2dc13f1fc36f9bdc98ee')
13
+ assert.equal(token0, '0x8e870d67f660d95d5be530380d0ec0bd388289e1')
14
+ assert.equal(token1, '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48')
15
+ }
16
+ })
17
+ )