diff --git a/__tests__/transaction.spec.ts b/__tests__/transaction.spec.ts index f4cb0f7..01f0699 100644 --- a/__tests__/transaction.spec.ts +++ b/__tests__/transaction.spec.ts @@ -28,6 +28,15 @@ describe('', () => { expect(res.text).toEqual('Pending'); }); + it('returns the list of pending tx ids', async () => { + const txId1 = await createTransaction(blockweave, 'test1'); + const txId2 = await createTransaction(blockweave, 'test2'); + + const res = await request(server).get(`/tx/pending`); + + expect(res.body.sort()).toEqual([ txId1, txId2 ].sort()); + }); + it('returns the height of the block', async () => { const txid = await createTransaction(blockweave, 'test'); await mine(blockweave); diff --git a/src/app.ts b/src/app.ts index 214b8b3..39d6976 100644 --- a/src/app.ts +++ b/src/app.ts @@ -26,6 +26,7 @@ import { txRawDataRoute, deleteTxRoute, txDataRoute, + txPendingRoute, } from './routes/transaction'; import { txAccessMiddleware, txValidateMiddleware } from './middlewares/transaction'; import { Utils } from './utils/utils'; @@ -134,6 +135,8 @@ export default class ArLocal { async (ctx) => (ctx.body = Math.round((+ctx.params.bytes / 1000) * 65595508).toString()), ); + this.router.get('/tx/pending', txPendingRoute); + // tx filter endpoint to restrict ans-104 txs this.router.get(/^\/tx(?:\/|$)/, txAccessMiddleware); diff --git a/src/routes/transaction.ts b/src/routes/transaction.ts index 6fc0032..6953893 100644 --- a/src/routes/transaction.ts +++ b/src/routes/transaction.ts @@ -506,6 +506,29 @@ export async function txDataRoute(ctx: Router.RouterContext, next: Next) { } } +export async function txPendingRoute(ctx: Router.RouterContext) { + try { + if ( + oldDbPath !== ctx.dbPath || + !transactionDB || + connectionSettings !== ctx.connection.client.connectionSettings.filename + ) { + transactionDB = new TransactionDB(ctx.connection); + oldDbPath = ctx.dbPath; + connectionSettings = ctx.connection.client.connectionSettings.filename; + } + + const txIds = await transactionDB.getUnminedTxs(); + + ctx.status = 200 + ctx.body = txIds + } catch (error) { + console.error({ error }) + ctx.status = 500; + ctx.body = { error: error.message }; + } +} + export async function deleteTxRoute(ctx: Router.RouterContext) { try { if (