like-robinhood 0.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/index.js +211 -0
  4. package/package.json +26 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Lucas Barrena
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/README.md ADDED
@@ -0,0 +1 @@
1
+ # like-robinhood
package/index.js ADDED
@@ -0,0 +1,211 @@
1
+ const { zeroPadValue } = require('ethers')
2
+ const { programs, tokenInfoEvents, tokenActivityEvents } = require('./programs/index.js')
3
+
4
+ const RPC_URL = 'https://rpc.mainnet.chain.robinhood.com'
5
+
6
+ module.exports = class Robinhood {
7
+ static programs = programs
8
+
9
+ constructor (opts = {}) {
10
+ this.rpc = opts.rpc || RPC_URL
11
+ this.id = 1
12
+ }
13
+
14
+ async getTokenCreation (tokenAddress) {
15
+ const queries = getTokenInfoQueries(tokenAddress, { fromBlock: blockTag(0), toBlock: 'latest' })
16
+ const logs = []
17
+
18
+ // Parallelism is not needed for now to avoid rate limits
19
+ // We aim for one query here and the returned value can be cached
20
+ for (const query of queries) {
21
+ const result = await this.request('eth_getLogs', [query])
22
+
23
+ logs.push(...result)
24
+ }
25
+
26
+ const tokenInfos = parseTokenInfoLogs(tokenAddress, logs)
27
+
28
+ return tokenInfos
29
+ }
30
+
31
+ async getTokenEvents (address, opts = {}) {
32
+ const eventNames = opts.eventNames || [...tokenActivityEvents.values()].map(e => e.event.name)
33
+ const selectedEvents = [...tokenActivityEvents.values()].filter(e => {
34
+ return eventNames.includes(e.event.name)
35
+ })
36
+
37
+ const start = opts.after || opts.start
38
+ const startBlock = start ? Number(start.blockNumber) : 0
39
+ const startLogIndex = start
40
+ ? Number(start.logIndex || 0) + (opts.after ? 1 : 0)
41
+ : 0
42
+ const endBlock = opts.end ? Number(opts.end.blockNumber) : null
43
+ const endLogIndex = opts.end ? Number(opts.end.logIndex || 0) : null
44
+ const limit = typeof opts.limit === 'number' ? (opts.limit >= 0 ? opts.limit : 0) : Infinity
45
+
46
+ const logs = await this.request('eth_getLogs', [{
47
+ address,
48
+ fromBlock: blockTag(startBlock),
49
+ toBlock: blockTag(endBlock === null ? 'latest' : endBlock),
50
+ topics: [
51
+ selectedEvents.map(e => e.event.topicHash)
52
+ ]
53
+ }])
54
+
55
+ const eligibleLogs = logs.filter(log => {
56
+ const blockNumber = Number(log.blockNumber)
57
+ const logIndex = Number(log.logIndex)
58
+
59
+ const afterStart = blockNumber > startBlock || (blockNumber === startBlock && logIndex >= startLogIndex)
60
+ const beforeEnd = endBlock === null || blockNumber < endBlock || (blockNumber === endBlock && logIndex < endLogIndex)
61
+
62
+ return afterStart && beforeEnd
63
+ })
64
+
65
+ eligibleLogs.sort((left, right) => {
66
+ const leftBlock = Number(left.blockNumber)
67
+ const rightBlock = Number(right.blockNumber)
68
+
69
+ if (leftBlock !== rightBlock) {
70
+ return leftBlock < rightBlock ? -1 : 1
71
+ }
72
+
73
+ return Number(left.logIndex) - Number(right.logIndex)
74
+ })
75
+
76
+ const pageLogs = eligibleLogs.slice(0, limit)
77
+
78
+ const events = pageLogs.map(log => {
79
+ const key = log.topics[0].toLowerCase()
80
+ const e = tokenActivityEvents.get(key)
81
+ const event = e.contract.interface.parseLog({
82
+ topics: log.topics,
83
+ data: log.data
84
+ })
85
+
86
+ return {
87
+ program: e.programId,
88
+ contract: e.contractId,
89
+ name: event.name,
90
+ data: event.args.toObject(),
91
+ blockNumber: Number(log.blockNumber),
92
+ transactionHash: log.transactionHash,
93
+ transactionIndex: Number(log.transactionIndex),
94
+ logIndex: Number(log.logIndex)
95
+ }
96
+ })
97
+
98
+ return events
99
+ }
100
+
101
+ async request (method, params) {
102
+ for (let retry = 1; retry <= 3; retry++) {
103
+ try {
104
+ const response = await fetch(RPC_URL, {
105
+ method: 'POST',
106
+ headers: {
107
+ 'content-type': 'application/json'
108
+ },
109
+ body: JSON.stringify({
110
+ jsonrpc: '2.0',
111
+ id: this.id++,
112
+ method,
113
+ params
114
+ })
115
+ })
116
+
117
+ const data = await response.json()
118
+
119
+ if (data.error) {
120
+ throw new Error(data.error.message)
121
+ }
122
+
123
+ return data.result
124
+ } catch (err) {
125
+ if (retry === 3) {
126
+ throw err
127
+ }
128
+
129
+ await new Promise(resolve => setTimeout(resolve, 1000))
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ function getTokenInfoQueries (tokenAddress, opts = {}) {
136
+ const tokenTopic = zeroPadValue(tokenAddress, 32)
137
+ const queries = []
138
+
139
+ for (const e of tokenInfoEvents.values()) {
140
+ let query = queries[e.indexedTokenPosition]
141
+
142
+ if (!query) {
143
+ query = {
144
+ fromBlock: blockTag(opts.fromBlock),
145
+ toBlock: blockTag(opts.toBlock),
146
+ blockHash: opts.blockHash,
147
+ address: [],
148
+ topics: [[]]
149
+ }
150
+
151
+ queries[e.indexedTokenPosition] = query
152
+
153
+ if (e.indexedTokenPosition >= 1) {
154
+ for (let i = 1; i < e.indexedTokenPosition; i++) {
155
+ query.topics[i] = null // Allows any value at this position
156
+ }
157
+
158
+ query.topics[e.indexedTokenPosition] = tokenTopic
159
+ }
160
+ }
161
+
162
+ query.address.push(e.contract.address)
163
+ query.topics[0].push(e.event.topicHash)
164
+ }
165
+
166
+ return queries.filter(Boolean)
167
+ }
168
+
169
+ function parseTokenInfoLogs (tokenAddress, logs) {
170
+ const tokenInfo = []
171
+
172
+ for (const log of logs) {
173
+ const e = tokenInfoEvents.get(log.address.toLowerCase() + ':' + log.topics[0].toLowerCase())
174
+
175
+ if (!e) {
176
+ continue
177
+ }
178
+
179
+ const event = e.contract.interface.parseLog({ topics: log.topics, data: log.data })
180
+ const args = event.args.toObject()
181
+
182
+ if (args[e.argument].toLowerCase() !== tokenAddress.toLowerCase()) {
183
+ continue
184
+ }
185
+
186
+ tokenInfo.push({
187
+ program: e.programId,
188
+ contract: e.contractId,
189
+ name: event.name,
190
+ data: args,
191
+ blockNumber: Number(log.blockNumber),
192
+ transactionHash: log.transactionHash,
193
+ transactionIndex: Number(log.transactionIndex),
194
+ logIndex: Number(log.logIndex)
195
+ })
196
+ }
197
+
198
+ return tokenInfo
199
+ }
200
+
201
+ function blockTag (value) {
202
+ if (value === 'latest' || value === Infinity) {
203
+ return 'latest'
204
+ }
205
+
206
+ if (typeof value === 'number' || typeof value === 'string'/* || typeof value === 'bigint' */) {
207
+ return '0x' + BigInt(value).toString(16)
208
+ }
209
+
210
+ throw new Error('Invalid block tag value: ' + value + ' (' + (typeof value) + ')')
211
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "like-robinhood",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "files": [],
7
+ "scripts": {
8
+ "test": "standard && brittle test.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/lukks/like-robinhood.git"
13
+ },
14
+ "author": "Lucas Barrena (LuKks)",
15
+ "license": "UNLICENSED",
16
+ "bugs": {
17
+ "url": "https://github.com/lukks/like-robinhood/issues"
18
+ },
19
+ "homepage": "https://github.com/lukks/like-robinhood#readme",
20
+ "devDependencies": {
21
+ "require-npm-global": "github:lukks/require-npm-global"
22
+ },
23
+ "dependencies": {
24
+ "ethers": "^6.17.0"
25
+ }
26
+ }