Skip to content

Commit

Permalink
fix(storage): increment tx_nonce manually
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Aug 28, 2024
1 parent b1fcc70 commit 5540f30
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,12 @@ export async function configDeps(
) {
const storageProvider = new JsonRpcProvider(config.storageConfig.rpcURL);
const stakingProvider = new JsonRpcProvider(config.stakingConfig.rpcURL);
const storageSigner = new NonceManager(
new Wallet(secrets.evmWallet.privateKey, storageProvider),
);
const storage = BridgeStorage__factory.connect(
config.storageConfig.contractAddress,
new NonceManager(new Wallet(secrets.evmWallet.privateKey, storageProvider)),
storageSigner,
);
const staking = ERC20Staking__factory.connect(
config.stakingConfig.contractAddress,
Expand Down Expand Up @@ -472,6 +475,7 @@ export async function configDeps(
return {
storage,
em,
storageSigner,
serverLinkHandler,
staking: await configStakingHandler(
em.fork(),
Expand Down
34 changes: 25 additions & 9 deletions src/handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { setTimeout } from "node:timers/promises";
import type { EntityManager } from "@mikro-orm/sqlite";
import type { AxiosInstance } from "axios";
import type { NonceManager } from "ethers";
import type { TSupportedChainTypes, TSupportedChains } from "../config";
import type { BridgeStorage } from "../contractsTypes/evm";
import { LockedEvent } from "../persistence/entities/locked";
Expand All @@ -17,6 +18,7 @@ import { retry } from "./utils";
export async function listenEvents(
chains: Array<THandler>,
storage: BridgeStorage,
storageSigner: NonceManager,
em: EntityManager,
serverLinkHandler: AxiosInstance | undefined,
log: LogInstance,
Expand Down Expand Up @@ -102,19 +104,26 @@ export async function listenEvents(
);
return;
}
let tx_nonce: number | undefined = undefined;

const approvalFn = async () => {
try {
const tx = await Promise.race([
(
await deps.storage.approveLockNft(
inft.transactionHash,
chain.chainIdent,
signature.signature,
signature.signer,
)
).wait(),
setTimeout(10 * 1000),
(async () => {
log.info(`Using nonce: ${tx_nonce}`);
return await (
await deps.storage.approveLockNft(
inft.transactionHash,
chain.chainIdent,
signature.signature,
signature.signer,
{
nonce: tx_nonce,
},
)
).wait();
})(),
setTimeout(20 * 1000),
]);
//@ts-ignore
if (!tx?.status) throw new Error("Approve failed");
Expand All @@ -124,6 +133,13 @@ export async function listenEvents(
if (err_.shortMessage?.includes("Signature already used")) {
return null;
}
if (err_.shortMessage?.includes("Invalid transaction nonce")) {
tx_nonce = (await storageSigner.getNonce()) + 1;
log.warn(
`Invalid nonce for ${inft.transactionHash}. Incrementing nonce and retrying`,
tx_nonce,
);
}
log.error(err_, "Error while approving lock");
throw err;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async function main() {
listenEvents(
deps.chains,
deps.storage,
deps.storageSigner,
deps.em.fork(),
deps.serverLinkHandler,
logger,
Expand Down

0 comments on commit 5540f30

Please sign in to comment.