@le-space/chains 0.1.0 → 0.3.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.
- package/README.md +7 -3
- package/package.json +12 -4
- package/src/aleph.js +454 -0
- package/src/bitcoin.js +1 -1
- package/src/evm-address.js +36 -0
- package/src/evm-contracts.js +27 -0
- package/src/evm.js +6 -33
- package/src/http.js +7 -0
- package/src/index.js +3 -0
- package/src/registry.js +15 -27
- package/test/support/aleph-fetch.js +80 -0
- package/test/support/aleph-sample.js +74 -0
- package/test/support/aleph.js +6 -0
- package/test/support/fake-alchemy.js +749 -0
- package/test/support/fake-blockscout.js +279 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// A fake Blockscout on 127.0.0.1 for tests (Node): the Etherscan-compatible
|
|
3
|
+
// `/api` (txlist, txlistinternal, tokentx, balances, paging as strict as
|
|
4
|
+
// Blockscout's), its JSON-RPC proxy's eth_chainId and the v2 coin balance
|
|
5
|
+
// history, with a made-up EVM wallet's history. Every address, hash and
|
|
6
|
+
// amount here is made up: addresses are derived from test phrases, hashes
|
|
7
|
+
// from counters. fake-alchemy.js serves the same kind of chain.
|
|
8
|
+
import http from 'node:http';
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
|
|
11
|
+
import { fakeHash } from './fake-cosmos.js';
|
|
12
|
+
|
|
13
|
+
/** A synthetic EVM address, lower case. */
|
|
14
|
+
export const fakeEvmAddress = (/** @type {string} */ phrase) =>
|
|
15
|
+
`0x${createHash('sha256').update(`belege test ${phrase}`).digest('hex').slice(0, 40)}`;
|
|
16
|
+
|
|
17
|
+
export const EVM = {
|
|
18
|
+
wallet: fakeEvmAddress('evm wallet'),
|
|
19
|
+
exchange: fakeEvmAddress('exchange deposit address'),
|
|
20
|
+
friend: fakeEvmAddress('someone else on evm'),
|
|
21
|
+
contract: fakeEvmAddress('a contract'),
|
|
22
|
+
usdc: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
|
|
23
|
+
spamToken: fakeEvmAddress('a token calling itself USDC')
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const evmTime = (/** @type {number} */ block) =>
|
|
27
|
+
String(Math.floor(Date.parse('2026-09-01T00:00:00Z') / 1000) + (block - 100) * 12);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A made-up history of EVM.wallet on Ethereum: ETH in, USDC in and out to
|
|
31
|
+
* the exchange, a failed send, ETH from a contract, a spam token.
|
|
32
|
+
*/
|
|
33
|
+
export function sampleEvmHistory({ fillerTokens = 0 } = {}) {
|
|
34
|
+
const w = EVM.wallet;
|
|
35
|
+
const tx = (
|
|
36
|
+
/** @type {string} */ seed,
|
|
37
|
+
/** @type {number} */ block,
|
|
38
|
+
/** @type {Record<string, string>} */ fields
|
|
39
|
+
) => ({
|
|
40
|
+
blockNumber: String(block),
|
|
41
|
+
timeStamp: evmTime(block),
|
|
42
|
+
hash: fakeHash(seed, true),
|
|
43
|
+
transactionIndex: '0',
|
|
44
|
+
gas: '60000',
|
|
45
|
+
...fields
|
|
46
|
+
});
|
|
47
|
+
const normal = [
|
|
48
|
+
tx('eth-in', 100, {
|
|
49
|
+
from: EVM.friend,
|
|
50
|
+
to: w,
|
|
51
|
+
value: '500000000000000000',
|
|
52
|
+
gasUsed: '21000',
|
|
53
|
+
gasPrice: '1000000000',
|
|
54
|
+
isError: '0',
|
|
55
|
+
txreceipt_status: '1'
|
|
56
|
+
}),
|
|
57
|
+
// USDC to the exchange: the token moves in tokentx; here only the gas.
|
|
58
|
+
tx('usdc-out', 200, {
|
|
59
|
+
from: w,
|
|
60
|
+
to: EVM.usdc,
|
|
61
|
+
value: '0',
|
|
62
|
+
gasUsed: '50000',
|
|
63
|
+
gasPrice: '2000000000',
|
|
64
|
+
isError: '0',
|
|
65
|
+
txreceipt_status: '1'
|
|
66
|
+
}),
|
|
67
|
+
tx('failed', 300, {
|
|
68
|
+
from: w,
|
|
69
|
+
to: EVM.friend,
|
|
70
|
+
value: '100000000000000000',
|
|
71
|
+
gasUsed: '21000',
|
|
72
|
+
gasPrice: '3000000000',
|
|
73
|
+
isError: '1',
|
|
74
|
+
txreceipt_status: '0'
|
|
75
|
+
}),
|
|
76
|
+
tx('eth-out', 400, {
|
|
77
|
+
from: w,
|
|
78
|
+
to: EVM.friend,
|
|
79
|
+
value: '100000000000000000',
|
|
80
|
+
gasUsed: '21000',
|
|
81
|
+
gasPrice: '1000000000',
|
|
82
|
+
isError: '0',
|
|
83
|
+
txreceipt_status: '1'
|
|
84
|
+
})
|
|
85
|
+
];
|
|
86
|
+
const internal = [
|
|
87
|
+
{
|
|
88
|
+
blockNumber: '500',
|
|
89
|
+
timeStamp: evmTime(500),
|
|
90
|
+
transactionHash: fakeHash('contract-pays', true),
|
|
91
|
+
index: '1',
|
|
92
|
+
from: EVM.contract,
|
|
93
|
+
to: w,
|
|
94
|
+
value: '20000000000000000',
|
|
95
|
+
isError: '0',
|
|
96
|
+
type: 'call'
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
const tokens = [
|
|
100
|
+
tx('usdc-in', 150, {
|
|
101
|
+
from: EVM.friend,
|
|
102
|
+
to: w,
|
|
103
|
+
value: '750000000',
|
|
104
|
+
contractAddress: EVM.usdc,
|
|
105
|
+
tokenSymbol: 'USDC',
|
|
106
|
+
tokenDecimal: '6'
|
|
107
|
+
}),
|
|
108
|
+
tx('usdc-out', 200, {
|
|
109
|
+
from: w,
|
|
110
|
+
to: EVM.exchange,
|
|
111
|
+
value: '300000000',
|
|
112
|
+
contractAddress: EVM.usdc,
|
|
113
|
+
tokenSymbol: 'USDC',
|
|
114
|
+
tokenDecimal: '6'
|
|
115
|
+
}),
|
|
116
|
+
tx('spam', 250, {
|
|
117
|
+
from: EVM.friend,
|
|
118
|
+
to: w,
|
|
119
|
+
value: '1000000000000',
|
|
120
|
+
contractAddress: EVM.spamToken,
|
|
121
|
+
tokenSymbol: 'USDC',
|
|
122
|
+
tokenDecimal: '6'
|
|
123
|
+
})
|
|
124
|
+
];
|
|
125
|
+
for (let i = 0; i < fillerTokens; i++) {
|
|
126
|
+
tokens.push(
|
|
127
|
+
tx(`filler-${i}`, 1000 + Math.floor(i / 3), {
|
|
128
|
+
from: EVM.friend,
|
|
129
|
+
to: w,
|
|
130
|
+
value: '1',
|
|
131
|
+
contractAddress: EVM.usdc,
|
|
132
|
+
tokenSymbol: 'USDC',
|
|
133
|
+
tokenDecimal: '6'
|
|
134
|
+
})
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return { normal, internal, tokens };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {object} [options]
|
|
142
|
+
* @param {{ normal: Record<string, string>[], internal: Record<string, string>[], tokens: Record<string, string>[] }} [options.history]
|
|
143
|
+
* as sampleEvmHistory, or fake-alchemy.js's blockscoutView
|
|
144
|
+
* @param {Record<string, string>} [options.balances] `native` and contract → units
|
|
145
|
+
* @param {number} [options.rateLimitedCalls] this many answer Blockscout's "rate limit" first
|
|
146
|
+
* @param {number} [options.maxOffset] page × offset beyond this is refused, as Blockscout does
|
|
147
|
+
* @param {string | null} [options.chainId] what `…/api/eth-rpc` answers to eth_chainId; null: 404
|
|
148
|
+
* @param {{ block_number: number, block_timestamp: string, value: string }[] | null} [options.coinBalanceHistory]
|
|
149
|
+
* `…/api/v2/addresses/<a>/coin-balance-history`, newest first; null: 404
|
|
150
|
+
*/
|
|
151
|
+
export async function startFakeBlockscout({
|
|
152
|
+
history = sampleEvmHistory(),
|
|
153
|
+
balances = { native: '310000000000000000', [EVM.usdc]: '450000000' },
|
|
154
|
+
rateLimitedCalls = 0,
|
|
155
|
+
maxOffset = 10_000,
|
|
156
|
+
chainId = '0x1',
|
|
157
|
+
// The native coin's balance after each change (#287), newest first as Blockscout gives it;
|
|
158
|
+
// null: the v2 endpoint is not there.
|
|
159
|
+
coinBalanceHistory = [
|
|
160
|
+
{
|
|
161
|
+
block_number: 19_500_000,
|
|
162
|
+
block_timestamp: '2026-03-20T09:00:00.000000Z',
|
|
163
|
+
value: '310000000000000000'
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
block_number: 19_400_000,
|
|
167
|
+
block_timestamp: '2026-02-10T12:00:00.000000Z',
|
|
168
|
+
value: '500000000000000000'
|
|
169
|
+
}
|
|
170
|
+
]
|
|
171
|
+
} = {}) {
|
|
172
|
+
/** @type {Record<string, string>[]} */
|
|
173
|
+
const calls = [];
|
|
174
|
+
let limited = rateLimitedCalls;
|
|
175
|
+
const server = http.createServer((req, res) => {
|
|
176
|
+
const url = new URL(req.url ?? '/', 'http://127.0.0.1');
|
|
177
|
+
if (url.pathname === '/api/eth-rpc' && req.method === 'POST') {
|
|
178
|
+
let raw = '';
|
|
179
|
+
req.on('data', (c) => (raw += c));
|
|
180
|
+
req.on('end', () => {
|
|
181
|
+
calls.push({ rpc: JSON.parse(raw).method });
|
|
182
|
+
if (chainId === null) {
|
|
183
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
184
|
+
return res.end(JSON.stringify({ message: 'Not found' }));
|
|
185
|
+
}
|
|
186
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
187
|
+
res.end(JSON.stringify({ jsonrpc: '2.0', id: 1, result: chainId }));
|
|
188
|
+
});
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const reply = (/** @type {unknown} */ body) => {
|
|
192
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
193
|
+
res.end(JSON.stringify(body));
|
|
194
|
+
};
|
|
195
|
+
const v2 = /^\/api\/v2\/addresses\/(0x[0-9a-f]{40})\/coin-balance-history$/.exec(url.pathname);
|
|
196
|
+
if (v2) {
|
|
197
|
+
calls.push({
|
|
198
|
+
v2: 'coin-balance-history',
|
|
199
|
+
address: v2[1],
|
|
200
|
+
...Object.fromEntries(url.searchParams)
|
|
201
|
+
});
|
|
202
|
+
if (coinBalanceHistory === null) {
|
|
203
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
204
|
+
return res.end(JSON.stringify({ message: 'Not found' }));
|
|
205
|
+
}
|
|
206
|
+
// One item a page, to prove the paging.
|
|
207
|
+
const from = Number(url.searchParams.get('n') ?? 0);
|
|
208
|
+
const item = coinBalanceHistory[from];
|
|
209
|
+
return reply({
|
|
210
|
+
items: item ? [item] : [],
|
|
211
|
+
next_page_params: from + 1 < coinBalanceHistory.length ? { n: from + 1 } : null
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const p = Object.fromEntries(url.searchParams);
|
|
215
|
+
calls.push(p);
|
|
216
|
+
if (url.pathname !== '/api')
|
|
217
|
+
return reply({ status: '0', message: 'Unknown path', result: null });
|
|
218
|
+
if (limited > 0) {
|
|
219
|
+
limited--;
|
|
220
|
+
return reply({ status: '0', message: 'NOTOK', result: 'Max rate limit reached' });
|
|
221
|
+
}
|
|
222
|
+
if (!/^0x[0-9a-f]{40}$/.test(p.address ?? '')) {
|
|
223
|
+
return reply({ status: '0', message: 'Invalid address format', result: null });
|
|
224
|
+
}
|
|
225
|
+
if (p.action === 'balance')
|
|
226
|
+
return reply({ status: '1', message: 'OK', result: balances.native ?? '0' });
|
|
227
|
+
if (p.action === 'tokenbalance') {
|
|
228
|
+
return reply({
|
|
229
|
+
status: '1',
|
|
230
|
+
message: 'OK',
|
|
231
|
+
result: balances[String(p.contractaddress).toLowerCase()] ?? '0'
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const lists = /** @type {Record<string, any[]>} */ ({
|
|
235
|
+
txlist: history.normal,
|
|
236
|
+
txlistinternal: history.internal,
|
|
237
|
+
tokentx: history.tokens
|
|
238
|
+
});
|
|
239
|
+
const list = lists[p.action];
|
|
240
|
+
if (!list) return reply({ status: '0', message: 'Unknown action', result: null });
|
|
241
|
+
const page = Number(p.page ?? 1);
|
|
242
|
+
const offset = Number(p.offset ?? 10);
|
|
243
|
+
if (page * offset > maxOffset) {
|
|
244
|
+
return reply({
|
|
245
|
+
status: '0',
|
|
246
|
+
message: `Result window is too large, PageNo x Offset size must be less than or equal to ${maxOffset}`,
|
|
247
|
+
result: null
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const start = Number(p.startblock ?? 0);
|
|
251
|
+
const mine = list
|
|
252
|
+
.filter((r) => [r.from, r.to].includes(p.address) && Number(r.blockNumber) >= start)
|
|
253
|
+
.sort((a, b) => Number(a.blockNumber) - Number(b.blockNumber));
|
|
254
|
+
const slice = mine.slice((page - 1) * offset, page * offset);
|
|
255
|
+
// Blockscout's tokentx carries no logIndex.
|
|
256
|
+
const shaped = slice.map(({ logIndex: _l, ...r }) => r);
|
|
257
|
+
if (!shaped.length) {
|
|
258
|
+
return reply({
|
|
259
|
+
status: '0',
|
|
260
|
+
message: p.action === 'tokentx' ? 'No token transfers found' : 'No transactions found',
|
|
261
|
+
result: []
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
return reply({ status: '1', message: 'OK', result: shaped });
|
|
265
|
+
});
|
|
266
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(undefined)));
|
|
267
|
+
const { port } = /** @type {import('node:net').AddressInfo} */ (server.address());
|
|
268
|
+
const url = `http://127.0.0.1:${port}`;
|
|
269
|
+
return {
|
|
270
|
+
url,
|
|
271
|
+
endpoints: { api: `${url}/api` },
|
|
272
|
+
calls,
|
|
273
|
+
close: () =>
|
|
274
|
+
new Promise((resolve) => {
|
|
275
|
+
server.close(() => resolve(undefined));
|
|
276
|
+
server.closeAllConnections?.();
|
|
277
|
+
})
|
|
278
|
+
};
|
|
279
|
+
}
|