-
Notifications
You must be signed in to change notification settings - Fork 113
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(spaceward): reduce calls to evm endpoint #1068
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,12 +14,14 @@ import { ActionModel } from "@/hooks/query/types"; | |||||||||||||||||||||||||||||||||||||
import { fromBytes, isAddressEqual, toBytes } from "viem"; | ||||||||||||||||||||||||||||||||||||||
import { ActionStatus } from "@/hooks/query/act"; | ||||||||||||||||||||||||||||||||||||||
import { fromBech32 } from "@cosmjs/encoding"; | ||||||||||||||||||||||||||||||||||||||
import { QueryKey, useQueryClient } from "@tanstack/react-query"; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export function Action({ action }: { action: ActionModel }) { | ||||||||||||||||||||||||||||||||||||||
export function Action({ action, queryKey }: { action: ActionModel, queryKey: QueryKey }) { | ||||||||||||||||||||||||||||||||||||||
const [{ wallet }] = useConnectWallet(); | ||||||||||||||||||||||||||||||||||||||
const address = wallet?.accounts[0].address; | ||||||||||||||||||||||||||||||||||||||
const { writeContractAsync } = useWriteContract(); | ||||||||||||||||||||||||||||||||||||||
const client = usePublicClient(); | ||||||||||||||||||||||||||||||||||||||
const queryClient = useQueryClient(); | ||||||||||||||||||||||||||||||||||||||
const [{ chains, connectedChain }, setChain] = useSetChain(); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
async function voteFor(voteType: ActionVoteType) { | ||||||||||||||||||||||||||||||||||||||
|
@@ -36,6 +38,8 @@ export function Action({ action }: { action: ActionModel }) { | |||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||
client, | ||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
queryClient.invalidateQueries({ queryKey }); | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider error handling for cache invalidation. The cache invalidation is placed after the contract write operation but before confirming the transaction success. This could lead to premature cache invalidation if the transaction fails. Consider wrapping the cache invalidation in the success callback: await handleContractWrite(
() => writeContractAsync({
address: PRECOMPILE_ACT_ADDRESS,
abi: actPrecompileAbi,
functionName: "voteForAction",
args: [action.id, voteType],
account: address,
connector: wallet?.wagmiConnector,
}),
client,
+ {
+ onSuccess: () => {
+ queryClient.invalidateQueries({ queryKey });
+ }
+ }
);
- queryClient.invalidateQueries({ queryKey }); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if (!action.msg) { | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import "./animate.css"; | ||
import clsx from "clsx"; | ||
import { useContext, useEffect, useRef, useState } from "react"; | ||
import { sendTransaction as _sendTransaction } from "viem/actions"; | ||
import { getAction, parseEventLogs } from "viem/utils"; | ||
import { useConfig, usePublicClient, useWalletClient } from "wagmi"; | ||
import { readContractQueryOptions } from "wagmi/query" | ||
import { walletContext } from "@cosmos-kit/react-lite"; | ||
import { Action, ActionStatus } from "@wardenprotocol/wardenjs/codegen/warden/act/v1beta1/action"; | ||
import { ActionStatus } from "@wardenprotocol/wardenjs/codegen/warden/act/v1beta1/action"; | ||
import { useToast } from "@/components/ui/use-toast"; | ||
|
||
import { | ||
|
@@ -17,13 +21,12 @@ import { QueuedAction, QueuedActionStatus, useActionsState } from "./hooks"; | |
import { getActionHandler, GetStatus, handleCosmos, handleEth, handleEthRaw } from "./util"; | ||
import { TEMP_KEY, useKeySettingsState } from "../keys/state"; | ||
import Assets from "../keys/assets"; | ||
import { useQuery, useQueryClient } from "@tanstack/react-query"; | ||
import { QueryKey, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
import { capitalize } from "../modals/util"; | ||
import { queryCosmosClients } from "../assets/queries"; | ||
import { sendTransaction as _sendTransaction } from "viem/actions"; | ||
import { getAction, parseEventLogs } from "viem/utils"; | ||
import { usePublicClient, useWalletClient } from "wagmi"; | ||
import actPrecompileAbi from "@/contracts/actPrecompileAbi"; | ||
import { env } from "@/env"; | ||
import { PRECOMPILE_ACT_ADDRESS } from "@/contracts/constants"; | ||
|
||
interface ItemProps extends QueuedAction { | ||
single?: boolean; | ||
|
@@ -54,15 +57,15 @@ function ActionItem({ single, ...item }: ItemProps) { | |
const cosmosClients = useQuery(queryCosmosClients(walletManager)).data; | ||
const clientsRef = useRef(cosmosClients); | ||
clientsRef.current = cosmosClients; | ||
|
||
const { data: ks, setData: setKeySettings } = useKeySettingsState(); | ||
const { toast } = useToast() | ||
const config = useConfig(); | ||
const { w } = useWeb3Wallet("wss://relay.walletconnect.org"); | ||
const { setData } = useActionsState(); | ||
|
||
const type = typeof item.keyThemeIndex !== "undefined" ? | ||
"key" : (["walletConnectRequestId", "walletConnectTopic"] as const).every(key => typeof item[key] !== "undefined") ? | ||
"wc" : typeof item.snapRequestId ? | ||
"key" : item.wc ? | ||
"wc" : item.snap ? | ||
"snap" : undefined | ||
|
||
const ready = Boolean(walletClient && publicClient); | ||
|
@@ -208,12 +211,23 @@ function ActionItem({ single, ...item }: ItemProps) { | |
} | ||
|
||
const client = await getClient(); | ||
let action: Action | undefined; | ||
|
||
const queryOptions = readContractQueryOptions(config, { | ||
chainId: env.evmChainId, | ||
address: PRECOMPILE_ACT_ADDRESS, | ||
abi: actPrecompileAbi, | ||
functionName: "actionById", | ||
args: [item.actionId], | ||
}); | ||
|
||
let action: Awaited<ReturnType<typeof queryOptions.queryFn>>["action"] | undefined; | ||
|
||
try { | ||
const res = await client.warden.act.v1beta1.actionById({ | ||
id: item.actionId, | ||
}); | ||
|
||
const res = await queryClient.fetchQuery( | ||
// @ts-expect-error fixme update @tanstack/react-query version to > 5 | ||
queryOptions | ||
); | ||
Comment on lines
+227
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid suppressing TypeScript errors with Using Would you like assistance in updating the dependency or refactoring the code to handle the TypeScript error properly? |
||
|
||
action = res.action; | ||
} catch (e) { | ||
|
@@ -277,9 +291,12 @@ function ActionItem({ single, ...item }: ItemProps) { | |
|
||
case QueuedActionStatus.ActionReady: { | ||
let getStatus: GetStatus | undefined; | ||
let queryKeys: QueryKey[] = []; | ||
|
||
try { | ||
getStatus = getActionHandler(item).getStatus; | ||
const res = getActionHandler(item); | ||
getStatus = res.getStatus; | ||
queryKeys = res.queryKeys; | ||
} catch (e) { | ||
console.error(e); | ||
|
||
|
@@ -322,6 +339,10 @@ function ActionItem({ single, ...item }: ItemProps) { | |
} else if (status.pending) { | ||
setTimeout(checkResult, 1000); | ||
} else if (status.done) { | ||
for (const queryKey of queryKeys) { | ||
queryClient.invalidateQueries({ queryKey }); | ||
} | ||
|
||
if (!status.next) { | ||
toast({ | ||
title: "Success", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider optimizing batch parameters
The batch configuration implements JSON-RPC batching to reduce EVM endpoint calls, but the current parameters might need adjustment:
batchSize: 1000
is quite large and might:wait: 16ms
is reasonable for collecting requestsConsider adjusting the batch parameters:
📝 Committable suggestion