-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl
executable file
·134 lines (118 loc) · 4.79 KB
/
curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
const { strict: assert } = require('assert');
const { appendFileSync } = require('fs');
const { join } = require('path');
const { tokens } = require('../data');
const LONG_ZERO_PREFIX = '0x000000000000';
const { endpoint, searchParams } = function () {
const urlarg = process.argv.at(-1);
assert(urlarg !== undefined);
const url = new URL(urlarg);
assert(url.pathname.startsWith('/api/v1/'));
appendFileSync(join(__dirname, 'curl.log'), `${urlarg}\n`, 'utf-8');
return {
endpoint: url.pathname.replace('/api/v1/', ''),
searchParams: url.searchParams
};
}();
for (const [re, fn] of /** @type {const} */([
[/^blocks\/([0-9]{1,10})$/, blockNumber => {
return require(`./blocks_${blockNumber}.json`);
}],
[/^tokens\/([\w.]+)$/, tokenId => {
assert(typeof tokenId === 'string');
const token = tokens[tokenId];
if (token === undefined) return undefined;
return require(`../data/${token.symbol}/getToken.json`);
}],
[/^tokens\/([\w.]+)\/balances$/, tokenId => {
assert(typeof tokenId === 'string');
const token = tokens[tokenId];
if (token === undefined)
return { timestamp: null, balances: [], links: { next: null } };
const accountId = searchParams.get('account.id');
assert(accountId !== null);
const timestamp = searchParams.get('timestamp');
assert(timestamp !== null);
return require(`../data/${token.symbol}/getBalanceOfToken_${accountId}.json`);
}],
[/^tokens\/([\w.]+)\/nfts\/(\d+)$/, (tokenId, nftNumber) => {
assert(typeof tokenId === 'string');
const token = tokens[tokenId];
if (token === undefined) return undefined;
return require(`../data/${token.symbol}/getNonFungibleToken_${nftNumber}.json`);
}],
[/^accounts\/((0x[0-9a-fA-F]{40})|(0\.0\.\d+))$/, idOrAliasOrEvmAddress => {
assert(typeof idOrAliasOrEvmAddress === 'string');
try {
return require(`../data/getAccount_${idOrAliasOrEvmAddress.toLowerCase()}.json`);
} catch {
if (idOrAliasOrEvmAddress.startsWith(LONG_ZERO_PREFIX)) {
return {
account: '0.0.' + parseInt(idOrAliasOrEvmAddress, 16),
evm_address: idOrAliasOrEvmAddress,
};
}
if (idOrAliasOrEvmAddress.startsWith('0.0.')) {
const accountNumber = parseInt(idOrAliasOrEvmAddress.slice(4));
return {
account: idOrAliasOrEvmAddress,
evm_address: `0x${accountNumber.toString(16).padStart(40, '0')}`
};
}
return undefined;
}
}],
[/^accounts\/(0\.0\.\d+)\/allowances\/tokens$/, accountId => {
assert(typeof accountId === 'string');
const tokenId = searchParams.get('token.id');
assert(tokenId !== null);
const spenderId = searchParams.get('spender.id');
assert(spenderId !== null);
const token = tokens[tokenId];
if (token === undefined)
return { allowances: [], links: { next: null } };
try {
return require(`../data/${token.symbol}/getAllowanceForToken_${accountId}_${spenderId}.json`);
} catch {
return undefined;
}
}],
[/^accounts\/(0\.0\.\d+)\/allowances\/nfts/, accountId => {
assert(typeof accountId === 'string');
const tokenId = searchParams.get('token.id');
assert(tokenId !== null);
const spenderId = searchParams.get('account.id');
assert(spenderId !== null);
const token = tokens[tokenId];
if (token === undefined)
return { allowances: [], links: { next: null } };
return require(`../data/${token.symbol}/getAllowanceForToken_${accountId}_${spenderId}.json`);
}],
[/^accounts\/((0x[0-9a-fA-F]{40})|(0\.0\.\d+))\/tokens/, idOrAliasOrEvmAddress => {
assert(typeof idOrAliasOrEvmAddress === 'string');
const tokenId = searchParams.get('token.id');
assert(tokenId !== null);
const token = tokens[tokenId];
if (token === undefined)
return { tokens: [], links: { next: null } };
try {
return require(`../data/${token.symbol}/getTokenRelationship_${idOrAliasOrEvmAddress.toLowerCase()}.json`);
} catch {
return undefined;
}
}],
])) {
const match = endpoint.match(re);
if (match !== null) {
const response = fn(...match.slice(1));
if (response !== undefined) {
console.info(JSON.stringify(response));
console.info('200');
return;
}
break;
}
}
console.info(JSON.stringify({ "_status": { "messages": [{ "message": "Not found" }] } }));
console.info('404');