Skip to content

Commit

Permalink
Fix nonce when losing connetion unexpectedly
Browse files Browse the repository at this point in the history
  • Loading branch information
juanenrisley committed Oct 3, 2023
1 parent adde217 commit beae9b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/features/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -19,6 +24,7 @@ const fundLocalTransaction = (
)
.then((tx) => tx.wait())
.then((receipt) => ({ taskId: '0x', receipt }));
};
const fundGelatoTransaction = ({ body }, _) =>
relay.sponsoredCall({ ...body, chainId }, gelatoApiKey);

Expand Down
7 changes: 3 additions & 4 deletions src/routes/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit beae9b8

Please sign in to comment.