@le-space/chains 0.4.0 → 0.5.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 +13 -10
- package/package.json +6 -3
- package/src/aleph.js +82 -20
- package/src/filecoin-pay.js +938 -0
- package/src/near-wallet.js +182 -0
- package/src/near.js +47 -0
- package/src/registry.js +41 -2
- package/src/wallets.js +22 -14
- package/test/support/aleph-fetch.js +3 -1
- package/test/support/aleph-sample.js +10 -6
- package/test/support/fake-near.js +121 -0
- package/test/support/filecoin-pay-fetch.js +278 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// A NEAR account as an own wallet, read only, by its account id: the reader
|
|
3
|
+
// from Eigenbeleg (near.js) handed out as WalletEntry records, like every
|
|
4
|
+
// other chain of the wallet service (wallets.js).
|
|
5
|
+
//
|
|
6
|
+
// NearBlocks /v3/accounts/<id>/txns and /ft-txns, the whole history
|
|
7
|
+
// the RPC what a storage deposit kept (EXPERIMENTAL_tx_status), and
|
|
8
|
+
// the balance now (view_account, ft_balance_of)
|
|
9
|
+
//
|
|
10
|
+
// Both see the account id and this machine's IP – the consent screen says so.
|
|
11
|
+
//
|
|
12
|
+
// Per transaction: the NEAR it moved an entry `sent` or `received`, the gas
|
|
13
|
+
// of a transaction this account signed an entry `fee` (`<hash>:fee`), each
|
|
14
|
+
// movement of a listed token an entry of its own. The hash is the
|
|
15
|
+
// transaction's, base58, the same an exchange names for a withdrawal – so a
|
|
16
|
+
// withdrawal from an exchange pairs with what arrives here.
|
|
17
|
+
//
|
|
18
|
+
// Only NEAR and the tokens listed for the chain (registry.js) are booked: a
|
|
19
|
+
// token's symbol is its own claim, and airdropped tokens are common on NEAR.
|
|
20
|
+
// What near.js does not read – NEAR that reaches the account inside another
|
|
21
|
+
// transaction's inner receipts, a staking pool paying out, say – is missing
|
|
22
|
+
// here too; the balance comes from the RPC and so shows the difference.
|
|
23
|
+
//
|
|
24
|
+
// Error messages name a host at most, never the account: the bridge logs them.
|
|
25
|
+
|
|
26
|
+
import { balancesNow, fetchRaw, isAccountId, mapRaw, storageKept } from './near.js';
|
|
27
|
+
import { addressUrl, hostOf, unitsToDecimal, WalletError } from './core.js';
|
|
28
|
+
|
|
29
|
+
/** NearBlocks gives 25 rows a page: 5 000 transactions, and as many token movements. */
|
|
30
|
+
const MAX_PAGES = 200;
|
|
31
|
+
/** Before NEAR's mainnet began (2020). */
|
|
32
|
+
const FROM = '2020-01-01';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {object} options
|
|
36
|
+
* @param {typeof fetch} options.fetch
|
|
37
|
+
* @param {number} [options.timeoutMs]
|
|
38
|
+
* @param {() => Date} [options.now]
|
|
39
|
+
*/
|
|
40
|
+
export function createNearClient({ fetch: f, timeoutMs = 20_000, now = () => new Date() }) {
|
|
41
|
+
/** One request, with a timeout and no redirect followed. @type {typeof fetch} */
|
|
42
|
+
const timed = (url, init = {}) =>
|
|
43
|
+
f(url, { ...init, signal: AbortSignal.timeout(timeoutMs), redirect: 'error' });
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
/**
|
|
47
|
+
* @param {{ chain: import('./registry.js').NearChain, address: string, endpoints: { api: string, rpc: string } }} params
|
|
48
|
+
*/
|
|
49
|
+
async history({ chain, address, endpoints }) {
|
|
50
|
+
const account = String(address ?? '')
|
|
51
|
+
.trim()
|
|
52
|
+
.toLowerCase();
|
|
53
|
+
if (!isAccountId(account)) {
|
|
54
|
+
throw new WalletError(
|
|
55
|
+
'not a NEAR account id (name.near, or 64 hex characters)',
|
|
56
|
+
'WALLET_ADDRESS',
|
|
57
|
+
400
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const own = { api: endpoints.api, rpc: endpoints.rpc };
|
|
61
|
+
/** @type {import('@le-space/ledger').LedgerEntry[]} */
|
|
62
|
+
let ledger;
|
|
63
|
+
/** @type {Awaited<ReturnType<typeof balancesNow>>} */
|
|
64
|
+
let held;
|
|
65
|
+
try {
|
|
66
|
+
const raw = await fetchRaw({
|
|
67
|
+
account,
|
|
68
|
+
network: 'mainnet',
|
|
69
|
+
from: FROM,
|
|
70
|
+
to: now().toISOString().slice(0, 10),
|
|
71
|
+
fetch: timed,
|
|
72
|
+
maxPages: MAX_PAGES,
|
|
73
|
+
endpoints: { ...own, explorer: chain.explorer.tx.replace('{tx}', '') }
|
|
74
|
+
});
|
|
75
|
+
ledger = await storageKept(mapRaw(raw), {
|
|
76
|
+
network: 'mainnet',
|
|
77
|
+
fetch: timed,
|
|
78
|
+
endpoints: own
|
|
79
|
+
});
|
|
80
|
+
const contracts = [
|
|
81
|
+
...new Set(ledger.map((e) => e.contract).filter((c) => Object.hasOwn(chain.tokens, c)))
|
|
82
|
+
];
|
|
83
|
+
held = await balancesNow({
|
|
84
|
+
account,
|
|
85
|
+
network: 'mainnet',
|
|
86
|
+
contracts,
|
|
87
|
+
fetch: timed,
|
|
88
|
+
endpoints: own
|
|
89
|
+
});
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw walletError(error, endpoints);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @type {Set<string>} */
|
|
95
|
+
const unknown = new Set();
|
|
96
|
+
/** @type {import('@le-space/ledger/wallet-entry').WalletEntry[]} */
|
|
97
|
+
const entries = [];
|
|
98
|
+
for (const e of ledger) {
|
|
99
|
+
const token = e.contract === 'native' ? null : chain.tokens[e.contract];
|
|
100
|
+
if (e.contract !== 'native' && !token) {
|
|
101
|
+
unknown.add(e.contract);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const fee = e.type === 'fee';
|
|
105
|
+
entries.push({
|
|
106
|
+
// `<hash>:fee`, `<hash>:deposit`, `<hash>:ft:<receipt>:<event>`
|
|
107
|
+
id: e.id.slice(`near:${account}:`.length),
|
|
108
|
+
hash: e.hash,
|
|
109
|
+
height: e.height,
|
|
110
|
+
time: e.time,
|
|
111
|
+
date: e.date,
|
|
112
|
+
type: /** @type {'sent' | 'received' | 'fee'} */ (e.type),
|
|
113
|
+
kind: fee ? 'fee' : e.kind === 'stake' ? 'stake' : 'transfer',
|
|
114
|
+
asset: token?.symbol ?? chain.native.symbol,
|
|
115
|
+
amount: e.amount,
|
|
116
|
+
decimals: token?.decimals ?? chain.native.decimals,
|
|
117
|
+
counterparty: fee ? '' : e.counterparty,
|
|
118
|
+
counterpartyLabel: '',
|
|
119
|
+
memo: '',
|
|
120
|
+
success: e.success,
|
|
121
|
+
explorerUrl: e.explorerUrl
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
entries.sort((x, y) =>
|
|
125
|
+
x.height === y.height ? (x.id < y.id ? -1 : 1) : x.height - y.height
|
|
126
|
+
);
|
|
127
|
+
return {
|
|
128
|
+
entries,
|
|
129
|
+
balances: [
|
|
130
|
+
{
|
|
131
|
+
asset: chain.native.symbol,
|
|
132
|
+
amount: unitsToDecimal(held.native, chain.native.decimals),
|
|
133
|
+
decimals: chain.native.decimals
|
|
134
|
+
},
|
|
135
|
+
...Object.entries(held.tokens).map(([contract, units]) => ({
|
|
136
|
+
asset: chain.tokens[contract].symbol,
|
|
137
|
+
amount: unitsToDecimal(units, chain.tokens[contract].decimals),
|
|
138
|
+
decimals: chain.tokens[contract].decimals
|
|
139
|
+
}))
|
|
140
|
+
],
|
|
141
|
+
transactions: new Set(entries.map((e) => e.hash)).size,
|
|
142
|
+
unknownAssets: unknown.size,
|
|
143
|
+
history: { earliestHeight: 0, earliestTime: null, pruned: false },
|
|
144
|
+
addressUrl: addressUrl(chain.explorer, account)
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* What near.js threw, as the wallet service's error: a code, and a message
|
|
152
|
+
* that names the hosts asked and nothing of the account.
|
|
153
|
+
*
|
|
154
|
+
* @param {any} error
|
|
155
|
+
* @param {{ api: string, rpc: string }} endpoints
|
|
156
|
+
*/
|
|
157
|
+
function walletError(error, endpoints) {
|
|
158
|
+
if (error instanceof WalletError) return error;
|
|
159
|
+
const hosts = `${hostOf(endpoints.api)} / ${hostOf(endpoints.rpc)}`;
|
|
160
|
+
const message = String(error?.message ?? '');
|
|
161
|
+
if (error?.name === 'TimeoutError' || error?.name === 'AbortError') {
|
|
162
|
+
return new WalletError(`${hosts} did not answer in time`, 'WALLET_TIMEOUT', 504);
|
|
163
|
+
}
|
|
164
|
+
if (error instanceof TypeError) {
|
|
165
|
+
return new WalletError(`${hosts} is not reachable`, 'WALLET_UNREACHABLE');
|
|
166
|
+
}
|
|
167
|
+
if (/^more than \d+ pages/.test(message)) {
|
|
168
|
+
return new WalletError(
|
|
169
|
+
`more than ${MAX_PAGES * 25} transactions on this account`,
|
|
170
|
+
'WALLET_TOO_LARGE',
|
|
171
|
+
413
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
if (/answered 429/.test(message)) {
|
|
175
|
+
return new WalletError(`${hosts} limits requests (HTTP 429)`, 'WALLET_RATE_LIMIT', 429);
|
|
176
|
+
}
|
|
177
|
+
const status = /answered (\d{3})/.exec(message)?.[1];
|
|
178
|
+
return new WalletError(
|
|
179
|
+
status ? `${hosts} answered ${status}` : `${hosts} could not be read`,
|
|
180
|
+
'WALLET_NODE'
|
|
181
|
+
);
|
|
182
|
+
}
|
package/src/near.js
CHANGED
|
@@ -576,6 +576,53 @@ export async function balancesBefore({
|
|
|
576
576
|
return { ...block, balances };
|
|
577
577
|
}
|
|
578
578
|
|
|
579
|
+
/**
|
|
580
|
+
* What an account holds now (the last final block): NEAR (liquid plus staked
|
|
581
|
+
* by a validator) and the listed tokens asked for, in base units. An account
|
|
582
|
+
* that does not exist yet holds nothing.
|
|
583
|
+
*
|
|
584
|
+
* @param {{ account: string, network?: 'mainnet' | 'testnet', contracts?: string[], fetch?: typeof fetch, endpoints?: Partial<{ rpc: string }> }} p
|
|
585
|
+
* @returns {Promise<{ native: bigint, tokens: Record<string, bigint> }>}
|
|
586
|
+
*/
|
|
587
|
+
export async function balancesNow({
|
|
588
|
+
account,
|
|
589
|
+
network = 'mainnet',
|
|
590
|
+
contracts = [],
|
|
591
|
+
fetch = globalThis.fetch,
|
|
592
|
+
endpoints
|
|
593
|
+
}) {
|
|
594
|
+
if (!isAccountId(account)) throw new Error('not a NEAR account id');
|
|
595
|
+
const url = endpointsOf(network, endpoints).rpc;
|
|
596
|
+
let native = 0n;
|
|
597
|
+
try {
|
|
598
|
+
const a = await rpc(fetch, url, 'query', {
|
|
599
|
+
request_type: 'view_account',
|
|
600
|
+
finality: 'final',
|
|
601
|
+
account_id: account
|
|
602
|
+
});
|
|
603
|
+
native = BigInt(a.amount) + BigInt(a.locked ?? '0');
|
|
604
|
+
} catch (e) {
|
|
605
|
+
if (!/UNKNOWN_ACCOUNT/.test(String(/** @type {any} */ (e).cause))) throw e;
|
|
606
|
+
}
|
|
607
|
+
/** @type {Record<string, bigint>} */
|
|
608
|
+
const tokens = {};
|
|
609
|
+
for (const c of contracts) {
|
|
610
|
+
try {
|
|
611
|
+
const r = await rpc(fetch, url, 'query', {
|
|
612
|
+
request_type: 'call_function',
|
|
613
|
+
finality: 'final',
|
|
614
|
+
account_id: c,
|
|
615
|
+
method_name: 'ft_balance_of',
|
|
616
|
+
args_base64: toBase64(utf8(JSON.stringify({ account_id: account })))
|
|
617
|
+
});
|
|
618
|
+
tokens[c] = BigInt(JSON.parse(fromUtf8(Uint8Array.from(r.result))));
|
|
619
|
+
} catch {
|
|
620
|
+
tokens[c] = 0n; // not registered with the token yet
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return { native, tokens };
|
|
624
|
+
}
|
|
625
|
+
|
|
579
626
|
/** `{ native: { asset: 'NEAR', amount } … }` → `{ NEAR: amount … }`. @param {Record<string, { asset: string, amount: string }>} b */
|
|
580
627
|
const bySymbol = (b) => Object.fromEntries(Object.values(b).map((x) => [x.asset, x.amount]));
|
|
581
628
|
|
package/src/registry.js
CHANGED
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
// from a zpub kept in the bridge's keychain (bitcoin.js).
|
|
14
14
|
// filecoin Filfox's public API, by the address (filecoin.js); FIL and the
|
|
15
15
|
// FEVM tokens listed for it.
|
|
16
|
+
// near NearBlocks' API for the history and an archival RPC for the
|
|
17
|
+
// balance, by the account id (near.js, near-wallet.js); NEAR and
|
|
18
|
+
// the NEP-141 tokens listed for it.
|
|
16
19
|
//
|
|
17
20
|
// Only assets listed here are booked. A Cosmos denom or an ERC-20 contract
|
|
18
21
|
// that is not listed is counted and left out: a token contract can name
|
|
@@ -92,7 +95,21 @@ import { KNOWN_EVM_CONTRACTS } from './evm-contracts.js';
|
|
|
92
95
|
* @property {Explorer} explorer
|
|
93
96
|
*/
|
|
94
97
|
|
|
95
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* @typedef {object} NearChain
|
|
100
|
+
* @property {string} id
|
|
101
|
+
* @property {'near'} kind
|
|
102
|
+
* @property {string} name
|
|
103
|
+
* @property {string} shortName
|
|
104
|
+
* @property {string} caip2
|
|
105
|
+
* @property {ChainAsset} native
|
|
106
|
+
* @property {Record<string, ChainAsset>} tokens NEP-141 contract (an account id) → asset
|
|
107
|
+
* @property {{ api: string, rpc: string }} endpoints NearBlocks' API and an archival RPC
|
|
108
|
+
* @property {{ api: string[], rpc: string[] }} alternatives
|
|
109
|
+
* @property {Explorer} explorer
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/** @typedef {CosmosChain | EvmChain | BitcoinChain | FilecoinChain | NearChain | MoneroChain} Chain */
|
|
96
113
|
|
|
97
114
|
const USDC = { symbol: 'USDC', decimals: 6 };
|
|
98
115
|
const ETH = { symbol: 'ETH', decimals: 18 };
|
|
@@ -207,6 +224,28 @@ export const CHAINS = Object.freeze({
|
|
|
207
224
|
// Read through Filfox, by address; a message CID is the hash
|
|
208
225
|
// (filecoin-chain.js holds this description).
|
|
209
226
|
filecoin: FILECOIN,
|
|
227
|
+
// Read through NearBlocks, by the account id; the balance from an archival
|
|
228
|
+
// RPC (near-wallet.js). USDC is Circle's own contract on NEAR (near.js
|
|
229
|
+
// TOKENS); the bridged USDC.e, USDT and wNEAR are counted and left out.
|
|
230
|
+
near: {
|
|
231
|
+
id: 'near',
|
|
232
|
+
kind: 'near',
|
|
233
|
+
name: 'NEAR',
|
|
234
|
+
shortName: 'NEAR',
|
|
235
|
+
caip2: 'near:mainnet',
|
|
236
|
+
native: { symbol: 'NEAR', decimals: 24 },
|
|
237
|
+
tokens: { '17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1': USDC },
|
|
238
|
+
endpoints: {
|
|
239
|
+
api: 'https://api.nearblocks.io',
|
|
240
|
+
rpc: 'https://archival-rpc.mainnet.fastnear.com'
|
|
241
|
+
},
|
|
242
|
+
alternatives: { api: [], rpc: [] },
|
|
243
|
+
explorer: {
|
|
244
|
+
name: 'NearBlocks',
|
|
245
|
+
tx: 'https://nearblocks.io/txns/{tx}',
|
|
246
|
+
address: 'https://nearblocks.io/address/{address}'
|
|
247
|
+
}
|
|
248
|
+
},
|
|
210
249
|
// Not read here: Monero hides amounts and parties on its chain. The app
|
|
211
250
|
// imports the wallet's own export (app/src/lib/wallets/monero-import.js);
|
|
212
251
|
// listed so both tables name the same chains, and for its explorer.
|
|
@@ -250,7 +289,7 @@ export function publicChains() {
|
|
|
250
289
|
assets: Object.values(c.denoms).map((a) => a.symbol),
|
|
251
290
|
nativeSymbol: c.denoms[c.nativeDenom].symbol
|
|
252
291
|
}
|
|
253
|
-
: c.kind === 'evm' || c.kind === 'filecoin'
|
|
292
|
+
: c.kind === 'evm' || c.kind === 'filecoin' || c.kind === 'near'
|
|
254
293
|
? {
|
|
255
294
|
assets: [c.native.symbol, ...Object.values(c.tokens).map((a) => a.symbol)],
|
|
256
295
|
nativeSymbol: c.native.symbol
|
package/src/wallets.js
CHANGED
|
@@ -18,6 +18,7 @@ import { isCosmosAddress } from './bech32.js';
|
|
|
18
18
|
import { createEvmClient } from './evm.js';
|
|
19
19
|
import { createBitcoinClient } from './bitcoin.js';
|
|
20
20
|
import { createFilecoinClient } from './filecoin.js';
|
|
21
|
+
import { createNearClient } from './near-wallet.js';
|
|
21
22
|
import { checkEndpoint, WalletError } from './http.js';
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -50,6 +51,7 @@ export function createWalletService({
|
|
|
50
51
|
alchemy: { key: alchemyKey, baseUrl: alchemyBaseUrl }
|
|
51
52
|
});
|
|
52
53
|
const filecoin = createFilecoinClient({ fetch: f, timeoutMs, sleep });
|
|
54
|
+
const near = createNearClient({ fetch: f, timeoutMs });
|
|
53
55
|
const deployments = createAkashDeploymentsClient({ fetch: f, timeoutMs, sleep });
|
|
54
56
|
const bitcoin = createBitcoinClient({
|
|
55
57
|
fetch: f,
|
|
@@ -119,30 +121,36 @@ export function createWalletService({
|
|
|
119
121
|
const address = typeof request?.address === 'string' ? request.address.trim() : '';
|
|
120
122
|
const { endpoints, ownEndpoint } = endpointsOf(chain, request);
|
|
121
123
|
let result =
|
|
122
|
-
chain.kind === '
|
|
123
|
-
? await
|
|
124
|
+
chain.kind === 'near'
|
|
125
|
+
? await near.history({
|
|
124
126
|
chain,
|
|
125
127
|
address,
|
|
126
|
-
endpoints: /** @type {{ api: string }} */ (endpoints)
|
|
128
|
+
endpoints: /** @type {{ api: string, rpc: string }} */ (endpoints)
|
|
127
129
|
})
|
|
128
|
-
: chain.kind === '
|
|
129
|
-
? await
|
|
130
|
+
: chain.kind === 'filecoin'
|
|
131
|
+
? await filecoin.history({
|
|
130
132
|
chain,
|
|
131
133
|
address,
|
|
132
134
|
endpoints: /** @type {{ api: string }} */ (endpoints)
|
|
133
135
|
})
|
|
134
|
-
: chain.kind === '
|
|
135
|
-
? await
|
|
136
|
+
: chain.kind === 'bitcoin'
|
|
137
|
+
? await bitcoin.history({
|
|
136
138
|
chain,
|
|
137
139
|
address,
|
|
138
|
-
endpoints: /** @type {{
|
|
140
|
+
endpoints: /** @type {{ api: string }} */ (endpoints)
|
|
139
141
|
})
|
|
140
|
-
:
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
142
|
+
: chain.kind === 'cosmos'
|
|
143
|
+
? await cosmos.history({
|
|
144
|
+
chain,
|
|
145
|
+
address,
|
|
146
|
+
endpoints: /** @type {{ rpc: string, rest: string }} */ (endpoints)
|
|
147
|
+
})
|
|
148
|
+
: await evm.history({
|
|
149
|
+
chain,
|
|
150
|
+
address,
|
|
151
|
+
endpoints: /** @type {{ api: string }} */ (endpoints),
|
|
152
|
+
ownEndpoint
|
|
153
|
+
});
|
|
146
154
|
// A pruned node: what it no longer knows, from the chain's indexer. If
|
|
147
155
|
// that fails, the sync keeps what the node gave and says the history
|
|
148
156
|
// is short, as without an indexer.
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* @typedef {object} Row
|
|
8
8
|
* @property {string} at ISO
|
|
9
9
|
* @property {number} amount credits; negative leaves
|
|
10
|
-
* @property {string} [price]
|
|
10
|
+
* @property {string} [price] a purchase's: units of the payment token per credit paid for
|
|
11
|
+
* @property {number} [bonus] of `amount`, the credits given on top
|
|
11
12
|
* @property {string} [txHash]
|
|
12
13
|
* @property {string} [token]
|
|
13
14
|
* @property {string} [chain]
|
|
@@ -66,6 +67,7 @@ export function alephFetch({ accounts, messages = {} }) {
|
|
|
66
67
|
message_timestamp: r.at,
|
|
67
68
|
amount: r.amount,
|
|
68
69
|
price: r.price ?? null,
|
|
70
|
+
bonus_amount: r.bonus ?? null,
|
|
69
71
|
tx_hash: r.txHash ?? null,
|
|
70
72
|
token: r.token ?? null,
|
|
71
73
|
chain: r.chain ?? null,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
2
|
// A made-up Aleph account and month for the tests (Node and browser): a
|
|
3
|
-
// purchase before August
|
|
3
|
+
// purchase before August paid in ALEPH – 1,200,000 credits came in, 200,000
|
|
4
|
+
// of them the bonus, at 0.00008 ALEPH per credit paid for, so 80 ALEPH for
|
|
5
|
+
// 1 USD of credits at the list price – then storage, an
|
|
4
6
|
// instance, a transfer in and one out, and a line in September.
|
|
5
7
|
|
|
6
8
|
export const ACCOUNT = `0x${'a1'.repeat(20)}`;
|
|
@@ -13,11 +15,13 @@ export function sampleRows() {
|
|
|
13
15
|
return [
|
|
14
16
|
{
|
|
15
17
|
at: '2026-07-20T08:00:00.000Z',
|
|
16
|
-
amount:
|
|
17
|
-
|
|
18
|
+
amount: 1_200_000,
|
|
19
|
+
bonus: 200_000,
|
|
20
|
+
// Units of ALEPH per credit paid for – not dollars, as Aleph's API has it.
|
|
21
|
+
price: '0.00008',
|
|
18
22
|
txHash: `0x${'ab'.repeat(32)}`,
|
|
19
23
|
token: 'ALEPH',
|
|
20
|
-
chain: '
|
|
24
|
+
chain: 'ethereum'
|
|
21
25
|
},
|
|
22
26
|
{
|
|
23
27
|
at: '2026-08-01T00:30:00.000Z',
|
|
@@ -60,8 +64,8 @@ export function sampleRows() {
|
|
|
60
64
|
];
|
|
61
65
|
}
|
|
62
66
|
|
|
63
|
-
/** Balance now:
|
|
64
|
-
export const sampleBalance = () =>
|
|
67
|
+
/** Balance now: 1,800,000 before July, then every row. */
|
|
68
|
+
export const sampleBalance = () => 1_800_000 + sampleRows().reduce((n, r) => n + r.amount, 0);
|
|
65
69
|
|
|
66
70
|
/** EUR per USD, the same every day. */
|
|
67
71
|
export const flatUsd = {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// A made-up NearBlocks and NEAR RPC as a `fetch`, for the tests of a NEAR
|
|
2
|
+
// account as an own wallet (near-wallet.js). Every account, hash and amount
|
|
3
|
+
// is made up.
|
|
4
|
+
|
|
5
|
+
export const NEAR = {
|
|
6
|
+
wallet: 'wolkenfabrik.near',
|
|
7
|
+
exchange: 'boerse-auszahlung.near',
|
|
8
|
+
customer: 'stromwerk.near',
|
|
9
|
+
/** Circle's USDC on NEAR, the one listed token (registry.js). */
|
|
10
|
+
usdc: '17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1',
|
|
11
|
+
/** base58, 44 characters, as a NEAR transaction hash. */
|
|
12
|
+
hashIn: '9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin',
|
|
13
|
+
hashOut: '4vJ9JU1bJJE96FWSJKvHsmmFADCg4gpZQff4P3bkLKi1',
|
|
14
|
+
hashToken: 'Us11111111111111111111111111111111111111112',
|
|
15
|
+
hashAirdrop: 'So11111111111111111111111111111111111111112'
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** `1.5` → yoctoNEAR. @param {string} near */
|
|
19
|
+
export const yocto = (near) => (BigInt(Math.round(Number(near) * 1e6)) * 10n ** 18n).toString();
|
|
20
|
+
/** ISO → NearBlocks' nanosecond timestamp. @param {string} iso */
|
|
21
|
+
const ts = (iso) => (BigInt(Date.parse(iso)) * 1_000_000n).toString();
|
|
22
|
+
|
|
23
|
+
/** What NearBlocks lists for the wallet, newest first. */
|
|
24
|
+
export const NEAR_TXNS = [
|
|
25
|
+
{
|
|
26
|
+
transaction_hash: NEAR.hashOut,
|
|
27
|
+
block_timestamp: ts('2026-11-15T10:00:00Z'),
|
|
28
|
+
block: { block_height: '200' },
|
|
29
|
+
signer_account_id: NEAR.wallet,
|
|
30
|
+
receiver_account_id: NEAR.customer,
|
|
31
|
+
actions: [{ action: 'TRANSFER', method: null }],
|
|
32
|
+
actions_agg: { deposit: yocto('1.5') },
|
|
33
|
+
outcomes: { status: true },
|
|
34
|
+
outcomes_agg: { transaction_fee: '446365125000000000000' }
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
transaction_hash: NEAR.hashIn,
|
|
38
|
+
block_timestamp: ts('2026-11-14T08:22:00Z'),
|
|
39
|
+
block: { block_height: '100' },
|
|
40
|
+
signer_account_id: NEAR.exchange,
|
|
41
|
+
receiver_account_id: NEAR.wallet,
|
|
42
|
+
actions: [{ action: 'TRANSFER', method: null }],
|
|
43
|
+
actions_agg: { deposit: yocto('21.25') },
|
|
44
|
+
outcomes: { status: true },
|
|
45
|
+
outcomes_agg: { transaction_fee: '446365125000000000000' }
|
|
46
|
+
}
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
/** Its token movements: one of a token that only calls itself USDC, one of the listed USDC. */
|
|
50
|
+
export const NEAR_FT_TXNS = [
|
|
51
|
+
{
|
|
52
|
+
transaction_hash: NEAR.hashAirdrop,
|
|
53
|
+
block_timestamp: ts('2026-11-14T09:00:00Z'),
|
|
54
|
+
block: { block_height: '110' },
|
|
55
|
+
affected_account_id: NEAR.wallet,
|
|
56
|
+
involved_account_id: 'geschenk.near',
|
|
57
|
+
delta_amount: '100000000',
|
|
58
|
+
contract_account_id: 'usdc-gratis.near',
|
|
59
|
+
meta: { symbol: 'USDC', decimals: 6 },
|
|
60
|
+
receipt_id: 'r1',
|
|
61
|
+
event_index: '0'
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
transaction_hash: NEAR.hashToken,
|
|
65
|
+
block_timestamp: ts('2026-11-14T09:30:00Z'),
|
|
66
|
+
block: { block_height: '120' },
|
|
67
|
+
affected_account_id: NEAR.wallet,
|
|
68
|
+
involved_account_id: NEAR.customer,
|
|
69
|
+
delta_amount: '12340000',
|
|
70
|
+
contract_account_id: NEAR.usdc,
|
|
71
|
+
meta: { symbol: 'USDC', decimals: 6 },
|
|
72
|
+
receipt_id: 'r2',
|
|
73
|
+
event_index: '0'
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {object} [o]
|
|
79
|
+
* @param {any[]} [o.txns]
|
|
80
|
+
* @param {any[]} [o.ftTxns]
|
|
81
|
+
* @param {{ amount: string, locked: string }} [o.account] yoctoNEAR, as view_account answers
|
|
82
|
+
* @param {string} [o.usdc] the wallet's USDC in base units
|
|
83
|
+
*/
|
|
84
|
+
export function fakeNear({
|
|
85
|
+
txns = NEAR_TXNS,
|
|
86
|
+
ftTxns = NEAR_FT_TXNS,
|
|
87
|
+
account = { amount: yocto('19.5'), locked: yocto('0.25') },
|
|
88
|
+
usdc = '12340000'
|
|
89
|
+
} = {}) {
|
|
90
|
+
const state = {
|
|
91
|
+
/** @type {{ url: string, method: string }[]} */
|
|
92
|
+
asked: [],
|
|
93
|
+
/** NearBlocks' answer to everything, when not 200. */
|
|
94
|
+
nearBlocksStatus: 200
|
|
95
|
+
};
|
|
96
|
+
const json = (/** @type {any} */ body, status = 200) =>
|
|
97
|
+
new Response(JSON.stringify(body), {
|
|
98
|
+
status,
|
|
99
|
+
headers: { 'content-type': 'application/json' }
|
|
100
|
+
});
|
|
101
|
+
/**
|
|
102
|
+
* @param {RequestInfo | URL} url
|
|
103
|
+
* @param {RequestInit} [init]
|
|
104
|
+
*/
|
|
105
|
+
const answer = async (url, init) => {
|
|
106
|
+
const u = new URL(String(url));
|
|
107
|
+
state.asked.push({ url: String(url), method: init?.method ?? 'GET' });
|
|
108
|
+
if (u.pathname.startsWith('/v3/accounts/')) {
|
|
109
|
+
if (state.nearBlocksStatus !== 200) return json({ message: 'no' }, state.nearBlocksStatus);
|
|
110
|
+
if (!u.pathname.startsWith(`/v3/accounts/${NEAR.wallet}/`)) return json({ data: [] });
|
|
111
|
+
return json({ data: u.pathname.endsWith('/ft-txns') ? ftTxns : txns, meta: {} });
|
|
112
|
+
}
|
|
113
|
+
const call = JSON.parse(String(init?.body));
|
|
114
|
+
if (call.params?.request_type === 'view_account') return json({ result: account });
|
|
115
|
+
if (call.params?.request_type === 'call_function' && call.params.account_id === NEAR.usdc) {
|
|
116
|
+
return json({ result: { result: [...new TextEncoder().encode(`"${usdc}"`)] } });
|
|
117
|
+
}
|
|
118
|
+
return json({ error: { message: 'unexpected' } });
|
|
119
|
+
};
|
|
120
|
+
return { fetch: /** @type {typeof fetch} */ (answer), state };
|
|
121
|
+
}
|