diff --git a/src/features/transactions.js b/src/features/transactions.js index 6cb5d95..a61ab83 100644 --- a/src/features/transactions.js +++ b/src/features/transactions.js @@ -4,11 +4,16 @@ const { chainId, gelatoApiKey } = require('../config'); const { funder } = require('../utils'); const relay = new GelatoRelay(); + const fundLocalTransaction = ( - { body: { target: to, ...rest }, pendingRequests = 0 }, - _, -) => - funder + { body: { target: to, ...rest }, onFinish, pendingRequests = 0 }, + reply, +) => { + reply.raw.on('close', () => { + onFinish(); + }); + + return funder .getTransactionCount() .then((nonce) => funder.sendTransaction({ @@ -19,6 +24,7 @@ const fundLocalTransaction = ( ) .then((tx) => tx.wait()) .then((receipt) => ({ taskId: '0x', receipt })); +}; const fundGelatoTransaction = ({ body }, _) => relay.sponsoredCall({ ...body, chainId }, gelatoApiKey); diff --git a/src/routes/transactions.js b/src/routes/transactions.js index a19e9bf..a02a504 100644 --- a/src/routes/transactions.js +++ b/src/routes/transactions.js @@ -15,13 +15,12 @@ module.exports = async (fastify, _) => { { onRequest: (request, reply, done) => { request.pendingRequests = pendingRequests; + request.onFinish = () => { + pendingRequests -= 1; + }; pendingRequests += 1; done(); }, - onResponse: (request, reply, done) => { - pendingRequests -= 1; - done(); - }, }, ]), isProduction ? fundGelatoTransaction : fundLocalTransaction,