-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): latency improvements (#2641)
Co-authored-by: Kevin Ingersoll <[email protected]>
- Loading branch information
Showing
5 changed files
with
118 additions
and
23 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,5 @@ | ||
--- | ||
"@latticexyz/common": patch | ||
--- | ||
|
||
Reduced the number of RPC requests before sending a transaction in the `transactionQueue` viem decorator. |
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,29 @@ | ||
import { EstimateFeesPerGasParameters, Client, EstimateFeesPerGasReturnType } from "viem"; | ||
import { estimateFeesPerGas } from "viem/actions"; | ||
|
||
export type CreateFeeRefOptions = { | ||
client: Client; | ||
refreshInterval: number; | ||
args?: EstimateFeesPerGasParameters; | ||
}; | ||
|
||
export type FeeRef = { | ||
fees: EstimateFeesPerGasReturnType | {}; | ||
lastUpdatedTimestamp: number; | ||
}; | ||
|
||
/** Update fee values once every `refreshInterval` instead of right before every request */ | ||
export async function createFeeRef({ client, args, refreshInterval }: CreateFeeRefOptions): Promise<FeeRef> { | ||
const feeRef: FeeRef = { fees: {}, lastUpdatedTimestamp: 0 }; | ||
|
||
async function updateFees(): Promise<void> { | ||
const fees = await estimateFeesPerGas(client, args); | ||
feeRef.fees = fees; | ||
feeRef.lastUpdatedTimestamp = Date.now(); | ||
} | ||
|
||
setInterval(updateFees, refreshInterval); | ||
await updateFees(); | ||
|
||
return feeRef; | ||
} |
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,17 @@ | ||
import { getChainId } from "viem/actions"; | ||
import { CreateFeeRefOptions, FeeRef, createFeeRef } from "./createFeeRef"; | ||
|
||
const feeRefs = new Map<number, FeeRef>(); | ||
|
||
export async function getFeeRef(opts: CreateFeeRefOptions): Promise<FeeRef> { | ||
const chainId = opts.args?.chain?.id ?? opts.client.chain?.id ?? (await getChainId(opts.client)); | ||
|
||
const existingFeeRef = feeRefs.get(chainId); | ||
if (existingFeeRef) { | ||
return existingFeeRef; | ||
} | ||
|
||
const feeRef = await createFeeRef(opts); | ||
feeRefs.set(chainId, feeRef); | ||
return feeRef; | ||
} |
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