Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: buffer transactions #3365

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-trees-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/explorer": patch
---

The transactions list in the explorer is now updated every 100ms instead of on every incoming transaction, to improve performance when there are many incoming transactions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
parseEventLogs,
} from "viem";
import { UserOperation, entryPoint07Abi, entryPoint07Address } from "viem/account-abstraction";
import { anvil } from "viem/chains";
import { useConfig, useWatchBlocks } from "wagmi";
import { getTransaction, simulateContract, waitForTransactionReceipt } from "wagmi/actions";
import { useStore } from "zustand";
Expand Down Expand Up @@ -225,7 +226,7 @@ export function TransactionsWatcher() {
}
},
chainId,
pollingInterval: 500,
pollingInterval: chainId === anvil.id ? 10 : 500,
});

return null;
Expand Down
76 changes: 48 additions & 28 deletions packages/explorer/src/observer/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,57 @@ export const store = createStore<State>(() => ({
}));

debug("listening for relayed messages", relayChannelName);
const messages: MessageEvent<Message>[] = [];
const channel = new BroadcastChannel(relayChannelName);
channel.addEventListener("message", ({ data }: MessageEvent<Message>) => {
if (data.type === "ping") return;
channel.addEventListener("message", (message: MessageEvent<Message>) => {
if (message.data.type === "ping") return;
messages.push(message);
});

function processMessages(): void {
if (messages.length === 0) return;

store.setState((state) => {
const write = data.type === "write" ? ({ ...data, events: [] } satisfies Write) : state.writes[data.writeId];
if (!write) return state;
let updated = state;

let hash = write.hash;
if (data.type === "waitForTransactionReceipt") {
hash = data.hash;
} else if (
data.type === "waitForUserOperationReceipt:result" &&
isPromiseFulfilled<
Messages["waitForUserOperationReceipt:result"] extends PromiseSettledResult<infer T> ? T : never
>(data)
) {
hash = data.value.receipt.transactionHash;
}
for (const { data } of messages) {
if (data.type === "ping") continue;

const write = data.type === "write" ? ({ ...data, events: [] } satisfies Write) : updated.writes[data.writeId];
if (!write) continue;

return {
...state,
writes: {
...state.writes,
[data.writeId]: {
...write,
type: data.type,
hash,
userOpHash: data.type === "waitForUserOperationReceipt" ? data.userOpHash : write.userOpHash,
events: [...write.events, data],
let hash = write.hash;
if (data.type === "waitForTransactionReceipt") {
hash = data.hash;
} else if (
data.type === "waitForUserOperationReceipt:result" &&
isPromiseFulfilled<
Messages["waitForUserOperationReceipt:result"] extends PromiseSettledResult<infer T> ? T : never
>(data)
) {
hash = data.value.receipt.transactionHash;
}

updated = {
...updated,
writes: {
...updated.writes,
[data.writeId]: {
...write,
type: data.type,
hash,
userOpHash: data.type === "waitForUserOperationReceipt" ? data.userOpHash : write.userOpHash,
events: [...write.events, data],
},
},
},
};
};
}

// Clear messages after processing
messages.length = 0;
return updated;
});
});
}

const processInterval = 100;
setInterval(processMessages, processInterval);
Loading