Skip to content

Commit

Permalink
fix pretry
Browse files Browse the repository at this point in the history
  • Loading branch information
dhvanipa committed Oct 31, 2023
1 parent 3548bc0 commit a08d809
Show file tree
Hide file tree
Showing 6 changed files with 1,597 additions and 1,421 deletions.
9 changes: 0 additions & 9 deletions examples/indexer-client/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"chalk": "4.1.2",
"debug": "^4.3.4",
"execa": "^5.1.1",
"p-retry": "4.6.2",
"prettier": "^2.8.4",
"prettier-plugin-solidity": "^1.1.2",
"viem": "1.6.0"
Expand Down
58 changes: 28 additions & 30 deletions packages/common/src/createContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
WriteContractParameters,
getContract,
} from "viem";
// import pRetry from "p-retry";
import pRetry from "p-retry";
import { createNonceManager } from "./createNonceManager";
import { debug as parentDebug } from "./debug";
import { UnionOmit } from "./type-utils/common";
Expand Down Expand Up @@ -113,35 +113,33 @@ export function createContract<
async function write(options: WriteContractParameters): Promise<Hex> {
const preparedWrite = await prepareWrite(options);

if (!contract.nonceManager.hasNonce()) {
await contract.nonceManager.resetNonce();
}

const nonce = contract.nonceManager.nextNonce();
debug("calling write function with nonce", nonce, preparedWrite);
return await walletClient.writeContract({
nonce,
...preparedWrite,
});

// return await pRetry(
// async () => {

// },
// {
// retries: 3,
// onFailedAttempt: async (error) => {
// // On nonce errors, reset the nonce and retry
// if (contract.nonceManager.shouldResetNonce(error)) {
// debug("got nonce error, retrying", error);
// await contract.nonceManager.resetNonce();
// return;
// }
// // TODO: prepareWrite again if there are gas errors?
// throw error;
// },
// }
// );
return await pRetry(
async () => {
if (!contract.nonceManager.hasNonce()) {
await contract.nonceManager.resetNonce();
}

const nonce = contract.nonceManager.nextNonce();
debug("calling write function with nonce", nonce, preparedWrite);
return await walletClient.writeContract({
nonce,
...preparedWrite,
});
},
{
retries: 3,
onFailedAttempt: async (error) => {
// On nonce errors, reset the nonce and retry
if (contract.nonceManager.shouldResetNonce(error)) {
debug("got nonce error, retrying", error);
await contract.nonceManager.resetNonce();
return;
}
// TODO: prepareWrite again if there are gas errors?
throw error;
},
}
);
}

return (...parameters) => {
Expand Down
1 change: 1 addition & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"esbuild": "^0.17.15",
"ethers": "^5.7.2",
"find-up": "5.0.0",
"p-retry": "4.6.2",
"zod": "^3.21.4",
"zod-validation-error": "^1.3.0"
},
Expand Down
1 change: 0 additions & 1 deletion packages/store-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"debug": "^4.3.4",
"drizzle-orm": "^0.27.0",
"kysely": "^0.26.1",
"p-retry": "^5.1.2",
"rxjs": "7.5.5",
"sql.js": "^1.8.0",
"superjson": "^1.12.4",
Expand Down
Loading

0 comments on commit a08d809

Please sign in to comment.