diff --git a/backend/packages/server/src/features/txs/controllers/tx.js b/backend/packages/server/src/features/txs/controllers/tx.js new file mode 100644 index 00000000..248895b4 --- /dev/null +++ b/backend/packages/server/src/features/txs/controllers/tx.js @@ -0,0 +1,32 @@ +const { + block: { getEvmTxCol, getExtrinsicCollection }, +} = require("@statescan/mongo"); +const { HttpError } = require("../../../utils"); + +async function getTx(ctx) { + const { hash } = ctx.params; + const evmCol = await getEvmTxCol(); + const evmTx = await evmCol.findOne({ hash }, { projection: { _id: 0 } }); + if (evmTx) { + ctx.body = { + isEvm: true, + ...evmTx, + }; + return; + } + + const exCol = await getExtrinsicCollection(); + const extrinsic = await exCol.findOne({ hash }, { projection: { _id: 0 } }); + if (!extrinsic) { + throw new HttpError(404, "extrinsic not found"); + } + + ctx.body = { + isEvm: false, + ...extrinsic, + }; +} + +module.exports = { + getTx, +}; diff --git a/backend/packages/server/src/features/txs/routes.js b/backend/packages/server/src/features/txs/routes.js new file mode 100644 index 00000000..d9188b5a --- /dev/null +++ b/backend/packages/server/src/features/txs/routes.js @@ -0,0 +1,8 @@ +const Router = require("koa-router"); +const { getTx } = require("./controllers/tx"); + +const router = new Router(); + +router.get("/txs/:hash", getTx); + +module.exports = router; diff --git a/backend/packages/server/src/routes.js b/backend/packages/server/src/routes.js index f7c4465b..d845ceee 100644 --- a/backend/packages/server/src/routes.js +++ b/backend/packages/server/src/routes.js @@ -15,6 +15,7 @@ const routes = [ require("./features/runtime/routes"), require("./features/status/routes"), require("./features/overview/routes"), + require("./features/txs/routes"), ]; const assetsRoutes = [require("./features/assets/routes")];