Skip to content

Commit ee6ba8f

Browse files
authored
v0.1.7 (#16)
* chore: Bump api to 1.5.1 * feat: Metadata endpoint * feat: Update readme
1 parent 3c476db commit ee6ba8f

File tree

5 files changed

+159
-102
lines changed

5 files changed

+159
-102
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ yarn start
2020
+ `/block/NUMBER` fetch block details at block height `NUMBER`.
2121
+ `/balance/ADDRESS` fetch balances for `ADDRESS` at latest finalized block.
2222
+ `/balance/ADDRESS/NUMBER` fetch balances for `ADDRESS` at block height `NUMBER`.
23+
+ `/metadata` fetch chain metadata at latest finalized block.
24+
+ `/metadata/NUMBER` fetch chain metadata at block height `NUMBER`.
2325

2426
### Configuration
2527

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polkadot-rpc-proxy",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -11,10 +11,10 @@
1111
"author": "",
1212
"license": "GPL-3.0-or-later",
1313
"dependencies": {
14-
"@polkadot/api": "1.4.0-beta.45",
15-
"@polkadot/metadata": "1.4.0-beta.45",
16-
"@polkadot/rpc-provider": "1.4.0-beta.45",
17-
"@polkadot/types": "1.4.0-beta.45",
14+
"@polkadot/api": "1.5.1",
15+
"@polkadot/metadata": "1.5.1",
16+
"@polkadot/rpc-provider": "1.5.1",
17+
"@polkadot/types": "1.5.1",
1818
"@types/express": "^4.17.2",
1919
"express": "^4.17.1",
2020
"typescript": "^3.8.2"

src/ApiHandler.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ export default class ApiHandler {
3838
}
3939

4040
async fetchBlock(hash: BlockHash) {
41-
await this.ensureMeta(hash);
42-
43-
const { api } = this;
41+
const api = await this.ensureMeta(hash);
4442
const [{ block }, events] = await Promise.all([
4543
api.rpc.chain.getBlock(hash),
46-
this.fetchEvents(hash),
44+
this.fetchEvents(api, hash),
4745
]);
4846

4947
const { parentHash, number, stateRoot, extrinsicsRoot } = block.header;
@@ -126,9 +124,7 @@ export default class ApiHandler {
126124
}
127125

128126
async fetchBalance(hash: BlockHash, address: string) {
129-
await this.ensureMeta(hash);
130-
131-
const { api } = this;
127+
const api = await this.ensureMeta(hash);
132128

133129
const [header, free, reserved, locks, nonce] = await Promise.all([
134130
api.rpc.chain.getHeader(hash),
@@ -146,15 +142,23 @@ export default class ApiHandler {
146142
return { at, nonce, free, reserved, locks };
147143
}
148144

149-
async fetchEvents(hash: BlockHash): Promise<EventRecord[] | string> {
145+
async fetchMetadata(hash: BlockHash) {
146+
const api = await this.ensureMeta(hash);
147+
148+
const metadata = await api.rpc.state.getMetadata(hash);
149+
150+
return metadata;
151+
}
152+
153+
async fetchEvents(api: ApiPromise, hash: BlockHash): Promise<EventRecord[] | string> {
150154
try {
151-
return await await this.api.query.system.events.at(hash);
155+
return await await api.query.system.events.at(hash);
152156
} catch (_) {
153157
return 'Unable to fetch Events, cannot confirm extrinsic status. Check pruning settings on the node.';
154158
}
155159
}
156160

157-
async ensureMeta(hash: BlockHash) {
161+
async ensureMeta(hash: BlockHash): Promise<ApiPromise> {
158162
const { api } = this;
159163

160164
try {
@@ -167,13 +171,15 @@ export default class ApiHandler {
167171
const meta = await api.rpc.state.getMetadata(hash);
168172
const chain = await api.rpc.system.chain();
169173

170-
api.registry.register(getSpecTypes(chain, runtimeVersion));
174+
api.registry.register(getSpecTypes(api.registry, chain, runtimeVersion));
171175
api.registry.setMetadata(meta);
172176
}
173177
} catch (err) {
174178
console.error(`Failed to get Metadata for block ${hash}, using latest.`);
175179
console.error(err);
176180
this.specVersion = api.createType('u32', -1);
177181
}
182+
183+
return api;
178184
}
179185
}

src/main.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ const WS_URL = process.env.NODE_WS_URL || 'ws://127.0.0.1:9944';
2727

2828
type Params = { [key: string]: string };
2929

30+
interface Error {
31+
error: string;
32+
}
33+
34+
function parseNumber(n: string): [Error | null, number] {
35+
const num = Number(n);
36+
37+
if (!Number.isInteger(num)) {
38+
return [{ error: 'Invalid block number' }, 0];
39+
}
40+
41+
return [null, num];
42+
}
43+
3044
async function main() {
3145
const api = await ApiPromise.create({ provider: new WsProvider(WS_URL) });
3246
const handler = new ApiHandler(api);
@@ -47,7 +61,12 @@ async function main() {
4761
get('/', async (req) => 'Sidecar is running, go to /block to get latest finalized block');
4862

4963
get('/block/:number', async (params) => {
50-
const number = Number(params.number) || 0;
64+
const [error, number] = parseNumber(params.number);
65+
66+
if (error) {
67+
return error;
68+
}
69+
5170
const hash = await api.rpc.chain.getBlockHash(number);
5271

5372
return await handler.fetchBlock(hash);
@@ -68,12 +87,35 @@ async function main() {
6887

6988
get('/balance/:address/:number', async (params) => {
7089
const { address } = params;
71-
const number = Number(params.number) || 0;
90+
const [error, number] = parseNumber(params.number);
91+
92+
if (error) {
93+
return error;
94+
}
95+
7296
const hash = await api.rpc.chain.getBlockHash(number);
7397

7498
return await handler.fetchBalance(hash, address);
7599
});
76100

101+
get('/metadata/', async () => {
102+
const hash = await api.rpc.chain.getFinalizedHead();
103+
104+
return await handler.fetchMetadata(hash);
105+
});
106+
107+
get('/metadata/:number', async (params) => {
108+
const [error, number] = parseNumber(params.number);
109+
110+
if (error) {
111+
return error;
112+
}
113+
114+
const hash = await api.rpc.chain.getBlockHash(number);
115+
116+
return await handler.fetchMetadata(hash);
117+
});
118+
77119
app.listen(PORT, HOST, () => console.log(`Running on http://${HOST}:${PORT}/`))
78120
}
79121

0 commit comments

Comments
 (0)