-
Notifications
You must be signed in to change notification settings - Fork 107
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): technical debt #868
Changes from all commits
5303082
eecc329
a67a633
9805d52
15b522a
5c14d6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,26 +1,85 @@ | ||||||||
import { useQueryHooks } from "@/hooks/useClient"; | ||||||||
import { walletContext } from "@cosmos-kit/react-lite"; | ||||||||
import type { ExtendedHttpEndpoint, WalletManager } from "@cosmos-kit/core"; | ||||||||
import { AddressType } from "@wardenprotocol/wardenjs/codegen/warden/warden/v1beta3/key"; | ||||||||
import { cosmos } from "@wardenprotocol/wardenjs"; | ||||||||
import { useQueries } from "@tanstack/react-query"; | ||||||||
import { useQueries, useQuery } from "@tanstack/react-query"; | ||||||||
import { useContext, useEffect, useMemo, useState } from "react"; | ||||||||
import { COSMOS_CHAINS } from "@/config/tokens"; | ||||||||
import { useQueryHooks } from "@/hooks/useClient"; | ||||||||
import { | ||||||||
balancesQueryCosmos, | ||||||||
balancesQueryEth, | ||||||||
fiatPricesQuery, | ||||||||
} from "./queries"; | ||||||||
import type { CosmosQueryClient, PriceMapSlinky } from "./types"; | ||||||||
import { COSMOS_CHAINS } from "@/config/tokens"; | ||||||||
import { walletContext } from "@cosmos-kit/react-lite"; | ||||||||
import { ExtendedHttpEndpoint } from "@cosmos-kit/core"; | ||||||||
|
||||||||
const DERIVE_ADDRESSES = [ | ||||||||
AddressType.ADDRESS_TYPE_ETHEREUM, | ||||||||
AddressType.ADDRESS_TYPE_OSMOSIS, | ||||||||
]; | ||||||||
|
||||||||
const queryCosmosClients = (walletManager: WalletManager) => { | ||||||||
const rpcClients: Record<string, CosmosQueryClient | undefined> = {}; | ||||||||
const rpcRetry: Record<string, number> = {}; | ||||||||
|
||||||||
return { | ||||||||
queryKey: ["cosmos", "rpcClients"], | ||||||||
queryFn: async () => { | ||||||||
const clients: [CosmosQueryClient, string][] = []; | ||||||||
|
||||||||
for (let i = 0; i < COSMOS_CHAINS.length; i++) { | ||||||||
const { chainName, rpc } = COSMOS_CHAINS[i]; | ||||||||
let client = rpcClients[chainName]; | ||||||||
|
||||||||
if (client) { | ||||||||
// todo implement client health check | ||||||||
clients.push([client, chainName]); | ||||||||
continue; | ||||||||
} | ||||||||
|
||||||||
let endpoint: ExtendedHttpEndpoint | string; | ||||||||
|
||||||||
if (rpc) { | ||||||||
const retry = rpcRetry[chainName] ?? 0; | ||||||||
endpoint = rpc[retry % rpc.length]; | ||||||||
rpcRetry[chainName] = retry + 1; | ||||||||
} else { | ||||||||
const repo = walletManager.getWalletRepo(chainName); | ||||||||
repo.activate(); | ||||||||
|
||||||||
try { | ||||||||
endpoint = await repo.getRpcEndpoint(); | ||||||||
} catch (e) { | ||||||||
console.error(e); | ||||||||
endpoint = `https://rpc.cosmos.directory/${chainName}`; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
try { | ||||||||
const client = | ||||||||
await cosmos.ClientFactory.createRPCQueryClient({ | ||||||||
rpcEndpoint: | ||||||||
typeof endpoint === "string" | ||||||||
? endpoint | ||||||||
: endpoint.url, | ||||||||
}); | ||||||||
|
||||||||
clients.push([client, chainName]); | ||||||||
} catch (e) { | ||||||||
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. Store created clients in Currently, the newly created clients are not stored in Apply this diff to store the clients in clients.push([client, chainName]);
+ rpcClients[chainName] = client; Committable suggestion
Suggested change
|
||||||||
console.error(e); | ||||||||
continue; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return clients; | ||||||||
}, | ||||||||
} as const; | ||||||||
}; | ||||||||
|
||||||||
export const useAssetQueries = (spaceId?: string | null) => { | ||||||||
const { walletManager } = useContext(walletContext); | ||||||||
const { isReady, useKeysBySpaceId, slinky } = useQueryHooks(); | ||||||||
const [clients, setClients] = useState<[CosmosQueryClient, string][]>([]); | ||||||||
const clients = useQuery(queryCosmosClients(walletManager)).data; | ||||||||
|
||||||||
const pairs = slinky.oracle.v1.useGetAllCurrencyPairs({ | ||||||||
options: { enabled: isReady, refetchInterval: Infinity }, | ||||||||
|
@@ -83,39 +142,6 @@ export const useAssetQueries = (spaceId?: string | null) => { | |||||||
return priceMap; | ||||||||
}, [prices.data, currencyPairs]); | ||||||||
|
||||||||
useEffect(() => { | ||||||||
Promise.all( | ||||||||
COSMOS_CHAINS.map(({ chainName, rpc }) => { | ||||||||
let promise: Promise<ExtendedHttpEndpoint | string>; | ||||||||
|
||||||||
if (!rpc) { | ||||||||
const repo = walletManager.getWalletRepo(chainName); | ||||||||
repo.activate(); | ||||||||
promise = repo.getRpcEndpoint(); | ||||||||
} else { | ||||||||
promise = Promise.resolve(rpc[0]); | ||||||||
} | ||||||||
|
||||||||
return promise | ||||||||
.then((endpoint) => | ||||||||
cosmos.ClientFactory.createRPCQueryClient({ | ||||||||
rpcEndpoint: endpoint | ||||||||
? typeof endpoint === "string" | ||||||||
? endpoint | ||||||||
: endpoint.url | ||||||||
: `https://rpc.cosmos.directory/${chainName}`, | ||||||||
}), | ||||||||
) | ||||||||
.then( | ||||||||
(client) => | ||||||||
[client, chainName] as [CosmosQueryClient, string], | ||||||||
); | ||||||||
}), | ||||||||
).then((clients) => { | ||||||||
setClients(clients); | ||||||||
}); | ||||||||
}, []); | ||||||||
|
||||||||
const queryKeys = useKeysBySpaceId({ | ||||||||
request: { | ||||||||
spaceId: spaceId ? BigInt(spaceId) : BigInt(0), | ||||||||
|
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.
Add error handling for transaction confirmation and query invalidation.
Currently, if
provider.waitForTransaction(res.hash)
orqueryClient.invalidateQueries()
throws an error, it might cause unhandled promise rejections. Consider adding error handling to manage potential issues during transaction confirmation or query invalidation.You can modify the code as follows to include error handling:
Committable suggestion