Newer
Older
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { Request, Response } from 'express';
import { ApiPromise } from '@polkadot/api';
import { BlockHash } from '@polkadot/types/interfaces/rpc';
import { HttpProvider, WsProvider } from '@polkadot/rpc-provider';
const HOST = process.env.BIND_HOST || '127.0.0.1';
const WS_URL = process.env.NODE_WS_URL || 'ws://127.0.0.1:9944';
interface Error {
error: string;
}
function parseNumber(n: string): [Error | null, number] {
const num = Number(n);
if (!Number.isInteger(num)) {
return [{ error: 'Invalid block number' }, 0];
}
return [null, num];
}
const api = await ApiPromise.create({ provider: new WsProvider(WS_URL) });
function get(path: string, cb: (params: Params) => Promise<any>) {
app.get(path, async (req, res) => {
try {
res.send(await cb(req.params));
} catch(err) {
console.error('Internal Error:', err);
res.status(500).send({ error: 'Interal Error' });
}
function post(path: string, cb: (params: Params, body: any) => Promise<any>) {
app.post(path, async (req, res) => {
try {
res.send(await cb(req.params, req.body));
} catch(err) {
console.error('Internal Error:', err);
res.status(500).send({ error: 'Interal Error' });
}
});
}
get('/', async (req) => 'Sidecar is running, go to /block to get latest finalized block');
const [error, number] = parseNumber(params.number);
if (error) {
return error;
}
const hash = await api.rpc.chain.getBlockHash(number);
const hash = await api.rpc.chain.getFinalizedHead();
get('/balance/:address', async (params) => {
const { address } = params;
const hash = await api.rpc.chain.getFinalizedHead();
return await handler.fetchBalance(hash, address);
get('/balance/:address/:number', async (params) => {
const { address } = params;
const [error, number] = parseNumber(params.number);
if (error) {
return error;
}
const hash = await api.rpc.chain.getBlockHash(number);
return await handler.fetchBalance(hash, address);
get('/metadata/', async () => {
const hash = await api.rpc.chain.getFinalizedHead();
return await handler.fetchMetadata(hash);
});
get('/metadata/:number', async (params) => {
const [error, number] = parseNumber(params.number);
if (error) {
return error;
}
const hash = await api.rpc.chain.getBlockHash(number);
return await handler.fetchMetadata(hash);
});
post('/tx/', async (_, body) => {
if (body && typeof body.tx !== 'string') {
return {
error: "Missing field `tx` on request body.",
};
}
return await handler.submitTx(body.tx);
});
app.listen(PORT, HOST, () => console.log(`Running on http://${HOST}:${PORT}/`))