Skip to content
main.ts 1.39 KiB
Newer Older
Maciej Hirsz's avatar
Maciej Hirsz committed
import * as express from 'express';
import { Request, Response } from 'express';
import { ApiPromise } from '@polkadot/api';
import { TypeRegistry } from '@polkadot/types';
import { BlockHash } from '@polkadot/types/interfaces/rpc';

const PORT = 8080;

async function main() {
	const api = await ApiPromise.create();
	const app = express();

	async function getBlock(hash: BlockHash, req: Request, res: Response) {
		const { block } = await api.rpc.chain.getBlock(hash);
		const { parentHash, number, stateRoot, extrinsicsRoot } = block.header;

		// console.log(block.toJSON());

		const logs = block.header.digest.logs.map(log => log.type);
		const extrinsics = block.extrinsics.map(extrinsic => ({
			method: `${extrinsic.method.sectionName}.${extrinsic.method.methodName}`,
			args: extrinsic.args.map(arg => arg.toJSON()),
		}));

		res.send({
			number,
			hash,
			parentHash,
			stateRoot,
			extrinsicsRoot,
			logs,
			extrinsics,
		});
	}

	app.get('/', (req, res) => res.send('Hello World!'))
	app.get('/block/:number', async (req, res) => {
		const number = Number(req.params.number) || 0;
		const hash = await api.rpc.chain.getBlockHash(number);

		await getBlock(hash, req, res);
	})
	app.get('/block/', async (req, res) => {
		const hash = await api.rpc.chain.getFinalizedHead();

		await getBlock(hash, req, res);
	})

	app.listen(PORT, () => console.log(`Example app listening on port ${PORT}!`))
}

main();