@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
package/src/index.js
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
// filecoin Filfox
|
|
14
14
|
// near NearBlocks + an archival RPC (from Eigenbeleg);
|
|
15
15
|
// `near` here, or `@le-space/chains/near`
|
|
16
|
+
// aleph Aleph Cloud credits (no chain: Aleph's API, by the
|
|
17
|
+
// Ethereum address); `aleph` here, or `/aleph`
|
|
16
18
|
|
|
17
19
|
export * from './core.js';
|
|
18
20
|
export { CHAINS, KNOWN_EVM_CONTRACTS, chainOf, publicChains } from './registry.js';
|
|
@@ -61,3 +63,4 @@ export {
|
|
|
61
63
|
/** @typedef {import('./registry.js').Chain} Chain */
|
|
62
64
|
/** @typedef {import('@le-space/ledger/wallet-entry').WalletEntry} WalletEntry */
|
|
63
65
|
export * as near from './near.js';
|
|
66
|
+
export * as aleph from './aleph.js';
|
package/src/registry.js
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { addressUrl, txUrl } from './explorer.js';
|
|
25
25
|
import { AKASH_CHAIN, NYX_CHAIN } from './cosmos-chains.js';
|
|
26
26
|
import { FILECOIN } from './filecoin-chain.js';
|
|
27
|
+
import { KNOWN_EVM_CONTRACTS } from './evm-contracts.js';
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* @typedef {object} ChainAsset
|
|
@@ -95,6 +96,11 @@ import { FILECOIN } from './filecoin-chain.js';
|
|
|
95
96
|
|
|
96
97
|
const USDC = { symbol: 'USDC', decimals: 6 };
|
|
97
98
|
const ETH = { symbol: 'ETH', decimals: 18 };
|
|
99
|
+
// Aleph Cloud's token (ALEPH), what Aleph's credits and storage are paid
|
|
100
|
+
// with: on Ethereum and Base. Checked 2026-11-06: CoinGecko's platform list
|
|
101
|
+
// for `aleph`, Blockscout's token page (Ethereum) and the contract's own
|
|
102
|
+
// symbol() and decimals() (Base).
|
|
103
|
+
const ALEPH = { symbol: 'ALEPH', decimals: 18 };
|
|
98
104
|
|
|
99
105
|
/** @param {string} base e.g. https://etherscan.io */
|
|
100
106
|
const etherscanLike = (/** @type {string} */ name, /** @type {string} */ base) => ({
|
|
@@ -103,30 +109,6 @@ const etherscanLike = (/** @type {string} */ name, /** @type {string} */ base) =
|
|
|
103
109
|
address: `${base}/address/{address}`
|
|
104
110
|
});
|
|
105
111
|
|
|
106
|
-
/**
|
|
107
|
-
* Contracts a wallet meets when it swaps (issue #115), by their lower-case
|
|
108
|
-
* address, and what they are called. Routers of the big aggregators and DEXes
|
|
109
|
-
* sit at the same address on most EVM chains; WETH is Ethereum's. Checked
|
|
110
|
-
* against the projects' own documentation; an address not here stays
|
|
111
|
-
* "Vertrag".
|
|
112
|
-
*/
|
|
113
|
-
export const KNOWN_EVM_CONTRACTS = /** @type {Readonly<Record<string, string>>} */ (
|
|
114
|
-
Object.freeze({
|
|
115
|
-
'0x881d40237659c251811cec9c364ef91dc08d300c': 'MetaMask Swap (Router)',
|
|
116
|
-
'0x74de5d4fcbf63e00296fd95d33236b9794016631': 'MetaMask Swap (Spender)',
|
|
117
|
-
'0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad': 'Uniswap (Universal Router)',
|
|
118
|
-
'0x66a9893cc07d91d95644aedd05d03f95e1dba8af': 'Uniswap (Universal Router v4)',
|
|
119
|
-
'0x7a250d5630b4cf539739df2c5dacb4c659f2488d': 'Uniswap V2 (Router)',
|
|
120
|
-
'0xe592427a0aece92de3edee1f18e0157c05861564': 'Uniswap V3 (Router)',
|
|
121
|
-
'0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45': 'Uniswap V3 (Router 2)',
|
|
122
|
-
'0x1111111254eeb25477b68fb85ed929f73a960582': '1inch (Router v5)',
|
|
123
|
-
'0x111111125421ca6dc452d289314280a0f8842a65': '1inch (Router v6)',
|
|
124
|
-
'0xdef1c0ded9bec7f1a1670819833240f027b25eff': '0x (Exchange Proxy)',
|
|
125
|
-
'0x9008d19f58aabd9ed0d60971565aa8510560ab41': 'CoW Protocol (Settlement)',
|
|
126
|
-
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2': 'WETH'
|
|
127
|
-
})
|
|
128
|
-
);
|
|
129
|
-
|
|
130
112
|
/** @type {Readonly<Record<string, Chain>>} */
|
|
131
113
|
export const CHAINS = Object.freeze({
|
|
132
114
|
nyx: NYX_CHAIN,
|
|
@@ -139,7 +121,10 @@ export const CHAINS = Object.freeze({
|
|
|
139
121
|
chainId: 1,
|
|
140
122
|
caip2: 'eip155:1',
|
|
141
123
|
native: ETH,
|
|
142
|
-
tokens: {
|
|
124
|
+
tokens: {
|
|
125
|
+
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48': USDC,
|
|
126
|
+
'0x27702a26126e0b3702af63ee09ac4d1a084ef628': ALEPH
|
|
127
|
+
},
|
|
143
128
|
endpoints: { api: 'https://eth.blockscout.com/api' },
|
|
144
129
|
alternatives: { api: [] },
|
|
145
130
|
explorer: etherscanLike('Etherscan', 'https://etherscan.io'),
|
|
@@ -153,7 +138,10 @@ export const CHAINS = Object.freeze({
|
|
|
153
138
|
chainId: 8453,
|
|
154
139
|
caip2: 'eip155:8453',
|
|
155
140
|
native: ETH,
|
|
156
|
-
tokens: {
|
|
141
|
+
tokens: {
|
|
142
|
+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': USDC,
|
|
143
|
+
'0xc0fbc4967259786c743361a5885ef49380473dcf': ALEPH
|
|
144
|
+
},
|
|
157
145
|
endpoints: { api: 'https://base.blockscout.com/api' },
|
|
158
146
|
alternatives: { api: [] },
|
|
159
147
|
explorer: etherscanLike('Basescan', 'https://basescan.org'),
|
|
@@ -244,7 +232,7 @@ export function chainOf(id) {
|
|
|
244
232
|
return typeof id === 'string' && Object.hasOwn(CHAINS, id) ? CHAINS[id] : null;
|
|
245
233
|
}
|
|
246
234
|
|
|
247
|
-
export { txUrl, addressUrl };
|
|
235
|
+
export { txUrl, addressUrl, KNOWN_EVM_CONTRACTS };
|
|
248
236
|
|
|
249
237
|
/**
|
|
250
238
|
* What the app is told about the chains: everything but code.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// A fake Aleph API as a `fetch` function, for Node and browser tests alike:
|
|
3
|
+
// balance, credit history (filters, pages, newest first), its summary, and
|
|
4
|
+
// messages. Every address, hash and amount handed to it is made up.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {object} Row
|
|
8
|
+
* @property {string} at ISO
|
|
9
|
+
* @property {number} amount credits; negative leaves
|
|
10
|
+
* @property {string} [price] USD per credit, a purchase's
|
|
11
|
+
* @property {string} [txHash]
|
|
12
|
+
* @property {string} [token]
|
|
13
|
+
* @property {string} [chain]
|
|
14
|
+
* @property {string} [paymentMethod]
|
|
15
|
+
* @property {string} [origin]
|
|
16
|
+
* @property {string} [originRef]
|
|
17
|
+
* @property {number} [count]
|
|
18
|
+
* @property {number} [sizeMib]
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {{ accounts: Record<string, { balance: number, rows: Row[] }>, messages?: Record<string, { type: string, name: string }> }} data
|
|
23
|
+
* @returns {typeof fetch}
|
|
24
|
+
*/
|
|
25
|
+
export function alephFetch({ accounts, messages = {} }) {
|
|
26
|
+
const json = (/** @type {unknown} */ body, status = 200) =>
|
|
27
|
+
new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
|
28
|
+
return async (input) => {
|
|
29
|
+
const url = new URL(String(input));
|
|
30
|
+
const q = url.searchParams;
|
|
31
|
+
const message = /^\/api\/v0\/messages\/([0-9a-f]{64})$/.exec(url.pathname);
|
|
32
|
+
if (message) {
|
|
33
|
+
const m = messages[message[1]];
|
|
34
|
+
return m
|
|
35
|
+
? json({ message: { type: m.type, content: { metadata: { name: m.name } } } })
|
|
36
|
+
: json({}, 404);
|
|
37
|
+
}
|
|
38
|
+
const m =
|
|
39
|
+
/^\/api\/v0\/addresses\/(0x[0-9a-fA-F]{40})\/(balance|credit_history)(\/summary)?$/.exec(
|
|
40
|
+
url.pathname
|
|
41
|
+
);
|
|
42
|
+
// Keyed in any case here; the client asks with the EIP-55 checksum, as Aleph needs.
|
|
43
|
+
const key = m
|
|
44
|
+
? Object.keys(accounts).find((a) => a.toLowerCase() === m[1].toLowerCase())
|
|
45
|
+
: undefined;
|
|
46
|
+
const account = key ? accounts[key] : undefined;
|
|
47
|
+
if (!m || !account) return json({}, 404);
|
|
48
|
+
if (m[2] === 'balance') return json({ credit_balance: account.balance });
|
|
49
|
+
const from = q.has('startDate') ? Number(q.get('startDate')) * 1000 : -Infinity;
|
|
50
|
+
const to = q.has('endDate') ? Number(q.get('endDate')) * 1000 : Infinity;
|
|
51
|
+
const direction = q.get('direction');
|
|
52
|
+
const rows = account.rows
|
|
53
|
+
.filter((r) => Date.parse(r.at) >= from && Date.parse(r.at) <= to)
|
|
54
|
+
.filter((r) => !direction || (direction === 'incoming' ? r.amount > 0 : r.amount < 0))
|
|
55
|
+
.sort((a, b) => b.at.localeCompare(a.at));
|
|
56
|
+
if (m[3])
|
|
57
|
+
return json({
|
|
58
|
+
total_amount: rows.reduce((n, r) => n + r.amount, 0),
|
|
59
|
+
entry_count: rows.length
|
|
60
|
+
});
|
|
61
|
+
const per = Number(q.get('pagination') ?? 100);
|
|
62
|
+
const page = Number(q.get('page') ?? 1);
|
|
63
|
+
return json({
|
|
64
|
+
pagination_total: rows.length,
|
|
65
|
+
credit_history: rows.slice((page - 1) * per, page * per).map((r) => ({
|
|
66
|
+
message_timestamp: r.at,
|
|
67
|
+
amount: r.amount,
|
|
68
|
+
price: r.price ?? null,
|
|
69
|
+
tx_hash: r.txHash ?? null,
|
|
70
|
+
token: r.token ?? null,
|
|
71
|
+
chain: r.chain ?? null,
|
|
72
|
+
payment_method: r.paymentMethod ?? null,
|
|
73
|
+
origin: r.origin ?? null,
|
|
74
|
+
origin_ref: r.originRef ?? null,
|
|
75
|
+
expense_count: r.count ?? null,
|
|
76
|
+
expense_size_mib: r.sizeMib ?? null
|
|
77
|
+
}))
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// A made-up Aleph account and month for the tests (Node and browser): a
|
|
3
|
+
// purchase before August at 0.000002 USD per credit, then storage, an
|
|
4
|
+
// instance, a transfer in and one out, and a line in September.
|
|
5
|
+
|
|
6
|
+
export const ACCOUNT = `0x${'a1'.repeat(20)}`;
|
|
7
|
+
export const OTHER = `0x${'b2'.repeat(20)}`;
|
|
8
|
+
export const VM = 'c3'.repeat(32);
|
|
9
|
+
export const BILLING = 'd4'.repeat(32);
|
|
10
|
+
|
|
11
|
+
/** @returns {import('./aleph-fetch.js').Row[]} */
|
|
12
|
+
export function sampleRows() {
|
|
13
|
+
return [
|
|
14
|
+
{
|
|
15
|
+
at: '2026-07-20T08:00:00.000Z',
|
|
16
|
+
amount: 1_000_000,
|
|
17
|
+
price: '0.000002',
|
|
18
|
+
txHash: `0x${'ab'.repeat(32)}`,
|
|
19
|
+
token: 'ALEPH',
|
|
20
|
+
chain: 'ETH'
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
at: '2026-08-01T00:30:00.000Z',
|
|
24
|
+
amount: -100,
|
|
25
|
+
paymentMethod: 'credit_expense',
|
|
26
|
+
originRef: 'storage',
|
|
27
|
+
count: 3,
|
|
28
|
+
sizeMib: 12.5
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
at: '2026-08-01T01:30:00.000Z',
|
|
32
|
+
amount: -100,
|
|
33
|
+
paymentMethod: 'credit_expense',
|
|
34
|
+
originRef: 'storage',
|
|
35
|
+
count: 4,
|
|
36
|
+
sizeMib: 13
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
at: '2026-08-01T02:00:00.000Z',
|
|
40
|
+
amount: -2000,
|
|
41
|
+
paymentMethod: 'credit_expense',
|
|
42
|
+
originRef: BILLING,
|
|
43
|
+
origin: VM
|
|
44
|
+
},
|
|
45
|
+
{ at: '2026-08-15T12:00:00.000Z', amount: 50_000, origin: OTHER },
|
|
46
|
+
{
|
|
47
|
+
at: '2026-08-20T12:00:00.000Z',
|
|
48
|
+
amount: -5_000,
|
|
49
|
+
paymentMethod: 'credit_transfer',
|
|
50
|
+
origin: OTHER
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
at: '2026-09-01T00:30:00.000Z',
|
|
54
|
+
amount: -120,
|
|
55
|
+
paymentMethod: 'credit_expense',
|
|
56
|
+
originRef: 'storage',
|
|
57
|
+
count: 4,
|
|
58
|
+
sizeMib: 13
|
|
59
|
+
}
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Balance now: 2,000,000 before July, then every row. */
|
|
64
|
+
export const sampleBalance = () => 2_000_000 + sampleRows().reduce((n, r) => n + r.amount, 0);
|
|
65
|
+
|
|
66
|
+
/** EUR per USD, the same every day. */
|
|
67
|
+
export const flatUsd = {
|
|
68
|
+
rate: async (/** @type {string} */ asset, /** @type {string} */ date) => ({
|
|
69
|
+
asset,
|
|
70
|
+
date,
|
|
71
|
+
rate: '0.9',
|
|
72
|
+
source: 'ecb'
|
|
73
|
+
})
|
|
74
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// @le-space/chains/testing/aleph: a fake Aleph API as a `fetch` function
|
|
3
|
+
// (Node and browser) and a made-up account and month to feed it. Every
|
|
4
|
+
// address, hash and amount is made up.
|
|
5
|
+
export * from './aleph-fetch.js';
|
|
6
|
+
export * from './aleph-sample.js';
|