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

Add utils for sumBigInt and makeQueryKey in hyperdrive/core #248

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add makeQueryKey util for consistency in hyperdrive/core
  • Loading branch information
DannyDelott committed Jul 20, 2023
commit d8989098736dcf33873bde8a3936d5adc229dfae
3 changes: 2 additions & 1 deletion packages/hyperdrive/src/amm/getPoolConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "viem";
import { HyperdriveABI } from "src/abis/Hyperdrive";
import { QueryObserverOptions } from "@tanstack/query-core";
import { makeQueryKey } from "src/makeQueryKey";

interface GetPoolConfigOptions {
hyperdriveAddress: Address;
Expand Down Expand Up @@ -36,7 +37,7 @@ export function getPoolConfigQuery({

return {
enabled: queryEnabled,
queryKey: ["@hyperdrive/core", "getPoolConfig", { hyperdriveAddress }],
queryKey: makeQueryKey("getPoolConfig", { hyperdriveAddress }),
queryFn: queryEnabled
? async () =>
getPoolConfig({
Expand Down
3 changes: 2 additions & 1 deletion packages/hyperdrive/src/amm/getPoolInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "viem";
import { HyperdriveABI } from "src/abis/Hyperdrive";
import { QueryObserverOptions } from "@tanstack/query-core";
import { makeQueryKey } from "src/makeQueryKey";

interface GetPoolInfoOptions {
hyperdriveAddress: Address;
Expand Down Expand Up @@ -36,7 +37,7 @@ export function getPoolInfoQuery({

return {
enabled: queryEnabled,
queryKey: ["@hyperdrive/core", "getPoolInfo", { hyperdriveAddress }],
queryKey: makeQueryKey("getPoolInfo", { hyperdriveAddress }),
queryFn: queryEnabled
? async () =>
getPoolInfo({
Expand Down
5 changes: 4 additions & 1 deletion packages/hyperdrive/src/amm/liquidity/getLiquidity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, Chain, PublicClient, Transport, formatUnits } from "viem";
import { makeQueryKey } from "src/makeQueryKey";

import { QueryObserverOptions } from "@tanstack/query-core";
import { getPoolInfo } from "src/amm/getPoolInfo";
Expand Down Expand Up @@ -43,7 +44,9 @@ export function getLiquidityQuery({
publicClient,
}: GetLiquidityQueryOptions): QueryObserverOptions<GetLiquidityReturnType> {
return {
queryKey: ["liquidity", { hyperdriveAddress, publicClient }],
queryKey: makeQueryKey("get-liquidity", {
hyperdriveAddress,
}),
queryFn: () => getLiquidity(hyperdriveAddress as Address, publicClient),
};
}
7 changes: 2 additions & 5 deletions packages/hyperdrive/src/amm/longs/getClosedLongs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { QueryObserverOptions } from "@tanstack/query-core";
import { PublicClient, Address, Transport, Chain } from "viem";
import { ClosedLong } from "./types";
import { getCloseLongEvents } from "src/amm/longs/getCloseLongEvents";
import { makeQueryKey } from "src/makeQueryKey";

export interface GetClosedLongsOptions {
traderAddress: Address;
Expand Down Expand Up @@ -61,11 +62,7 @@ export function getCloseLongsQuery({
const queryEnabled = !!account && !!hyperdriveAddress && !!publicClient;
return {
enabled: queryEnabled,
queryKey: [
"@hyperdrive/core",
"closed-longs",
{ hyperdriveAddress, account },
],
queryKey: makeQueryKey("closed-longs", { hyperdriveAddress, account }),
queryFn: queryEnabled
? () =>
getClosedLongs({
Expand Down
3 changes: 2 additions & 1 deletion packages/hyperdrive/src/amm/lp/getLpShares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PublicClient, Address, Transport, Chain } from "viem";
import { HyperdriveABI } from "src/abis/Hyperdrive";
import { LP_ASSET_ID } from "./constants";
import { QueryObserverOptions } from "@tanstack/query-core";
import { makeQueryKey } from "src/makeQueryKey";

export interface GetLpSharesOptions {
account: Address;
Expand Down Expand Up @@ -40,7 +41,7 @@ export function getLpSharesQuery({
const queryEnabled = !!account && !!hyperdriveAddress && !!publicClient;
return {
enabled: queryEnabled,
queryKey: ["@hyperdrive/core", "lp-shares", { hyperdriveAddress, account }],
queryKey: makeQueryKey("lp-shares", { hyperdriveAddress, account }),
queryFn: queryEnabled
? () => getLpShares({ account, hyperdriveAddress, publicClient })
: undefined,
Expand Down
13 changes: 13 additions & 0 deletions packages/hyperdrive/src/makeQueryKey.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { QueryKey } from "@tanstack/query-core";

/**
* This is a factory function for generating query keys. By using this function,
* we enforce a consistent namespacing across our queries and prevent a
* potential collision with existing queries within the application.

* Recommended use: All queries should leverage this function for query key
* creation to maintain namespace integrity.
*/
export function makeQueryKey<T>(queryName: string, options: T): QueryKey {
return ["@hyperdrive/core", queryName, options];
}
3 changes: 2 additions & 1 deletion packages/hyperdrive/src/token/getDecimals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { QueryObserverOptions } from "@tanstack/query-core";
import { ERC20MintableABI } from "src/abis/ERC20Mintable";
import { makeQueryKey } from "src/makeQueryKey";
import { Address, Chain, PublicClient, Transport } from "viem";

interface GetDecimalsOptions {
Expand Down Expand Up @@ -28,7 +29,7 @@ export function getDecimalsQuery({

return {
enabled: queryEnabled,
queryKey: ["@hyperdrive/core", "getDecimals", { tokenAddress }],
queryKey: makeQueryKey("getDecimals", { tokenAddress }),
queryFn: queryEnabled
? () =>
getDecimals({
Expand Down