Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Oct 15, 2024
1 parent c59708b commit 29b0c2c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { useConfig, useWatchBlocks } from "wagmi";
import { getTransaction, simulateContract, waitForTransactionReceipt } from "wagmi/actions";
import { useStore } from "zustand";
import { useCallback, useEffect, useMemo, useState } from "react";

Check failure on line 18 in packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/useTransactionWatcher.ts

View workflow job for this annotation

GitHub Actions / Run lint

'useMemo' is defined but never used
import { observer } from "../../../../../../observer/decorator";

Check failure on line 19 in packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/useTransactionWatcher.ts

View workflow job for this annotation

GitHub Actions / Run lint

'observer' is defined but never used
import { Message } from "../../../../../../observer/messages";

Check failure on line 20 in packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/useTransactionWatcher.ts

View workflow job for this annotation

GitHub Actions / Run lint

'Message' is defined but never used
import { type Write, store } from "../../../../../../observer/store";
import { useChain } from "../../../../hooks/useChain";
import { usePrevious } from "../../../../hooks/usePrevious";

Check failure on line 23 in packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/observe/useTransactionWatcher.ts

View workflow job for this annotation

GitHub Actions / Run lint

'usePrevious' is defined but never used
import { useWorldAbiQuery } from "../../../../queries/useWorldAbiQuery";

export type WatchedTransaction = {
Expand All @@ -45,6 +47,10 @@ export function useTransactionWatcher() {
const [transactions, setTransactions] = useState<WatchedTransaction[]>([]);
const observerWrites = useStore(store, (state) => state.writes);

// const observerWritesLen = observerWrites.length;
// const prevObserverWritesLen = usePrevious(observerWrites.length);
// const latestUpdatesLen = observerWritesLen - (prevObserverWritesLen || 0);

const handleTransaction = useCallback(
async (hash: Hex, timestamp: bigint) => {
if (!abi) return;
Expand All @@ -65,7 +71,7 @@ export function useTransactionWatcher() {
functionName = transaction.input.length > 10 ? transaction.input.slice(0, 10) : "unknown";
}

const write = Object.values(observerWrites).find((write) => write.hash === hash);
const write = observerWrites.find((write) => write.hash === hash);
setTransactions((prevTransactions) => [
{
hash,
Expand Down Expand Up @@ -133,7 +139,7 @@ export function useTransactionWatcher() {
);

useEffect(() => {
for (const write of Object.values(observerWrites)) {
for (const write of observerWrites.slice(0, 50)) {
const hash = write.hash;
if (write.type === "waitForTransactionReceipt" && hash && write.address === worldAddress) {
const transaction = transactions.find((transaction) => transaction.hash === hash);
Expand All @@ -155,42 +161,42 @@ export function useTransactionWatcher() {
pollingInterval: 500,
});

const mergedTransactions = useMemo((): WatchedTransaction[] => {
const mergedMap = new Map<string | undefined, WatchedTransaction>();

for (const write of Object.values(observerWrites)) {
if (write.address !== worldAddress) continue;

const parsedAbiItem = parseAbiItem(`function ${write.functionSignature}`) as AbiFunction;
const writeResult = write.events.find((event): event is Message<"write:result"> => event.type === "write:result");

mergedMap.set(write.hash || write.writeId, {
hash: write.hash,
writeId: write.writeId,
from: write.from,
status: writeResult?.status === "rejected" ? "rejected" : "pending",
timestamp: BigInt(write.time) / 1000n,
functionData: {
functionName: parsedAbiItem.name,
args: write.args,
},
value: write.value,
error: writeResult && "reason" in writeResult ? (writeResult.reason as BaseError) : undefined,
write,
});
}

for (const transaction of transactions) {
const existing = mergedMap.get(transaction.hash);
if (existing) {
mergedMap.set(transaction.hash, { ...transaction, write: existing.write });
} else {
mergedMap.set(transaction.hash, { ...transaction });
}
}

return Array.from(mergedMap.values()).sort((a, b) => Number(b.timestamp ?? 0n) - Number(a.timestamp ?? 0n));
}, [observerWrites, worldAddress, transactions]);

return mergedTransactions;
// const mergedTransactions = useMemo((): WatchedTransaction[] => {
// const mergedMap = new Map<string | undefined, WatchedTransaction>();

// for (const write of observerWrites) {
// if (write.address !== worldAddress) continue;

// const parsedAbiItem = parseAbiItem(`function ${write.functionSignature}`) as AbiFunction;
// const writeResult = write.events.find((event): event is Message<"write:result"> => event.type === "write:result");

// mergedMap.set(write.hash || write.writeId, {
// hash: write.hash,
// writeId: write.writeId,
// from: write.from,
// status: writeResult?.status === "rejected" ? "rejected" : "pending",
// timestamp: BigInt(write.time) / 1000n,
// functionData: {
// functionName: parsedAbiItem.name,
// args: write.args,
// },
// value: write.value,
// error: writeResult && "reason" in writeResult ? (writeResult.reason as BaseError) : undefined,
// write,
// });
// }

// for (const transaction of transactions) {
// const existing = mergedMap.get(transaction.hash);
// if (existing) {
// mergedMap.set(transaction.hash, { ...transaction, write: existing.write });
// } else {
// mergedMap.set(transaction.hash, { ...transaction });
// }
// }

// return Array.from(mergedMap.values()).sort((a, b) => Number(b.timestamp ?? 0n) - Number(a.timestamp ?? 0n));
// }, [observerWrites, worldAddress, transactions]);

return transactions;
}
42 changes: 28 additions & 14 deletions packages/explorer/src/observer/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,45 @@ export type Write = {
};

export type State = {
writes: {
[id: string]: Write;
};
writes: Write[];
};

export const store = createStore<State>(() => ({
writes: {},
writes: [],
}));

debug("listening for relayed messages", relayChannelName);
const channel = new BroadcastChannel(relayChannelName);
channel.addEventListener("message", ({ data }: MessageEvent<Message>) => {
if (data.type === "ping") return;
store.setState((state) => {
const write = data.type === "write" ? ({ ...data, events: [] } satisfies Write) : state.writes[data.writeId];
const writeIndex = state.writes.findIndex((w) => w.writeId === data.writeId);
const write =
writeIndex !== -1
? state.writes[writeIndex]
: data.type === "write"
? ({ ...data, events: [] } satisfies Write)
: undefined;

if (!write) return state;

const updatedWrite = {
...write,
type: data.type,
hash: data.type === "waitForTransactionReceipt" ? data.hash : write.hash,
events: [...write.events, data],
};

const newWrites = [...state.writes];
if (writeIndex !== -1) {
newWrites.splice(writeIndex, 1);
newWrites.unshift(updatedWrite);
} else {
newWrites.unshift(updatedWrite);
}

return {
writes: {
...state.writes,
[data.writeId]: {
...write,
type: data.type,
hash: data.type === "waitForTransactionReceipt" ? data.hash : write.hash,
events: [...write.events, data],
},
},
writes: newWrites,
};
});
});

0 comments on commit 29b0c2c

Please sign in to comment.