-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hooks: historical state client order & task history hooks
- Loading branch information
Showing
11 changed files
with
202 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { HistoricalStateClient } from "@renegade-fi/internal-sdk" | ||
import { | ||
Config, | ||
OrderMetadata, | ||
useConfig, | ||
UseOrderHistoryParameters, | ||
UseOrderHistoryReturnType, | ||
useOrderHistoryWebSocket, | ||
useStatus, | ||
useQuery, | ||
} from "@renegade-fi/react" | ||
import { | ||
GetOrderHistoryData, | ||
GetOrderHistoryQueryKey, | ||
getOrderHistoryQueryOptions, | ||
} from "@renegade-fi/react/query" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
|
||
export function useOrderHistory<selectData = GetOrderHistoryData>( | ||
parameters: UseOrderHistoryParameters<selectData> = {}, | ||
): UseOrderHistoryReturnType<selectData> { | ||
const { query = {} } = parameters | ||
|
||
const config = useConfig(parameters) | ||
const status = useStatus(parameters) | ||
const queryClient = useQueryClient() | ||
|
||
const baseOptions = getOrderHistoryQueryOptions(config, { | ||
...parameters, | ||
}) | ||
const options = { | ||
...baseOptions, | ||
queryFn: getHistoricalStateOrderHistoryQueryFn(config), | ||
} | ||
|
||
const enabled = Boolean(status === "in relayer" && (query.enabled ?? true)) | ||
|
||
useOrderHistoryWebSocket({ | ||
enabled, | ||
onUpdate: (incoming: OrderMetadata) => { | ||
if (queryClient && options.queryKey) { | ||
const existingMap = | ||
queryClient.getQueryData<GetOrderHistoryData>(options.queryKey) || | ||
new Map() | ||
const existingOrder = existingMap.get(incoming.id) | ||
|
||
if (!existingOrder || incoming.state !== existingOrder.state) { | ||
const newMap = new Map(existingMap) | ||
newMap.set(incoming.id, incoming) | ||
queryClient.setQueryData(options.queryKey, newMap) | ||
} | ||
} | ||
}, | ||
}) | ||
|
||
return useQuery({ ...query, ...options, enabled }) | ||
} | ||
|
||
function getHistoricalStateOrderHistoryQueryFn(config: Config) { | ||
return async function queryFn({ | ||
queryKey, | ||
}: { | ||
queryKey: GetOrderHistoryQueryKey | ||
}) { | ||
const { scopeKey: _ } = queryKey[1] | ||
|
||
const hseClient = new HistoricalStateClient( | ||
process.env.NEXT_PUBLIC_HISTORICAL_STATE_URL, | ||
config, | ||
) | ||
const history = await hseClient.getOrderHistory() | ||
|
||
return history ?? null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { HistoricalStateClient } from "@renegade-fi/internal-sdk" | ||
import { | ||
Config, | ||
Task, | ||
useConfig, | ||
useQuery, | ||
useStatus, | ||
UseTaskHistoryParameters, | ||
UseTaskHistoryReturnType, | ||
useTaskHistoryWebSocket, | ||
} from "@renegade-fi/react" | ||
import { | ||
GetTaskHistoryData, | ||
GetTaskHistoryQueryKey, | ||
getTaskHistoryQueryOptions, | ||
} from "@renegade-fi/react/query" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
|
||
export function useTaskHistory<selectData = GetTaskHistoryData>( | ||
parameters: UseTaskHistoryParameters<selectData> = {}, | ||
): UseTaskHistoryReturnType<selectData> { | ||
const { query = {} } = parameters | ||
|
||
const config = useConfig(parameters) | ||
const status = useStatus(parameters) | ||
const queryClient = useQueryClient() | ||
|
||
const baseOptions = getTaskHistoryQueryOptions(config, { | ||
...parameters, | ||
}) | ||
const options = { | ||
...baseOptions, | ||
queryFn: getHistoricalStateTaskHistoryQueryFn(config), | ||
} | ||
|
||
const enabled = Boolean(status === "in relayer" && (query.enabled ?? true)) | ||
|
||
useTaskHistoryWebSocket({ | ||
enabled, | ||
onUpdate: (incoming: Task) => { | ||
if (queryClient && options.queryKey) { | ||
const existingMap = | ||
queryClient.getQueryData<GetTaskHistoryData>(options.queryKey) || | ||
new Map() | ||
const existingTask = existingMap.get(incoming.id) | ||
|
||
if (!existingTask || incoming.state !== existingTask.state) { | ||
const newMap = new Map(existingMap) | ||
newMap.set(incoming.id, incoming) | ||
queryClient.setQueryData(options.queryKey, newMap) | ||
} | ||
} | ||
}, | ||
}) | ||
|
||
return useQuery({ ...query, ...options, enabled }) | ||
} | ||
|
||
function getHistoricalStateTaskHistoryQueryFn(config: Config) { | ||
return async function queryFn({ | ||
queryKey, | ||
}: { | ||
queryKey: GetTaskHistoryQueryKey | ||
}) { | ||
const { scopeKey: _ } = queryKey[1] | ||
|
||
const hseClient = new HistoricalStateClient( | ||
process.env.NEXT_PUBLIC_HISTORICAL_STATE_URL, | ||
config, | ||
) | ||
const history = await hseClient.getTaskHistory() | ||
|
||
return history ?? null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.