-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add sendTransaction, add mempool queue to nonce manager (…
- Loading branch information
Showing
10 changed files
with
180 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@latticexyz/common": minor | ||
--- | ||
|
||
- Added a `sendTransaction` helper to mirror viem's `sendTransaction`, but with our nonce manager | ||
- Added an internal mempool queue to `sendTransaction` and `writeContract` for better nonce handling | ||
- Defaults block tag to `pending` for transaction simulation and transaction count (when initializing the nonce manager) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
Account, | ||
CallParameters, | ||
Chain, | ||
Client, | ||
SendTransactionParameters, | ||
Transport, | ||
WriteContractReturnType, | ||
} from "viem"; | ||
import { call, sendTransaction as viem_sendTransaction } from "viem/actions"; | ||
import pRetry from "p-retry"; | ||
import { debug as parentDebug } from "./debug"; | ||
import { getNonceManager } from "./getNonceManager"; | ||
import { parseAccount } from "viem/accounts"; | ||
|
||
const debug = parentDebug.extend("sendTransaction"); | ||
|
||
// TODO: migrate away from this approach once we can hook into viem's nonce management: https://github.com/wagmi-dev/viem/discussions/1230 | ||
|
||
export async function sendTransaction< | ||
TChain extends Chain | undefined, | ||
TAccount extends Account | undefined, | ||
TChainOverride extends Chain | undefined | ||
>( | ||
client: Client<Transport, TChain, TAccount>, | ||
request: SendTransactionParameters<TChain, TAccount, TChainOverride> | ||
): Promise<WriteContractReturnType> { | ||
const rawAccount = request.account ?? client.account; | ||
if (!rawAccount) { | ||
// TODO: replace with viem AccountNotFoundError once its exported | ||
throw new Error("No account provided"); | ||
} | ||
const account = parseAccount(rawAccount); | ||
|
||
const nonceManager = await getNonceManager({ | ||
client, | ||
address: account.address, | ||
blockTag: "pending", | ||
}); | ||
|
||
async function prepare(): Promise<SendTransactionParameters<TChain, TAccount, TChainOverride>> { | ||
if (request.gas) { | ||
debug("gas provided, skipping simulate", request.to); | ||
return request; | ||
} | ||
|
||
debug("simulating tx to", request.to); | ||
await call(client, { | ||
...request, | ||
blockTag: "pending", | ||
account, | ||
} as CallParameters<TChain>); | ||
|
||
// TODO: estimate gas | ||
|
||
return request; | ||
} | ||
|
||
const preparedRequest = await prepare(); | ||
|
||
return await nonceManager.mempoolQueue.add( | ||
() => | ||
pRetry( | ||
async () => { | ||
if (!nonceManager.hasNonce()) { | ||
await nonceManager.resetNonce(); | ||
} | ||
|
||
const nonce = nonceManager.nextNonce(); | ||
debug("sending tx with nonce", nonce, "to", preparedRequest.to); | ||
return await viem_sendTransaction(client, { nonce, ...preparedRequest }); | ||
}, | ||
{ | ||
retries: 3, | ||
onFailedAttempt: async (error) => { | ||
// On nonce errors, reset the nonce and retry | ||
if (nonceManager.shouldResetNonce(error)) { | ||
debug("got nonce error, retrying", error.message); | ||
await nonceManager.resetNonce(); | ||
return; | ||
} | ||
// TODO: prepare again if there are gas errors? | ||
throw error; | ||
}, | ||
} | ||
), | ||
{ throwOnTimeout: true } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.