-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add link to polymarket * check shape of polymarket response
- Loading branch information
1 parent
1b5863c
commit 2059673
Showing
5 changed files
with
127 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
type PolymarketResponse = { | ||
slug: string; | ||
title: string; | ||
icon: string; | ||
description: string; | ||
startDate: string; | ||
creationDate: string; | ||
endDate: string; | ||
volume: number; | ||
createdAt: string; | ||
updatedAt: string; | ||
}[]; | ||
|
||
export type PolymarketData = PolymarketResponse[number] & { | ||
link: string; | ||
}; | ||
|
||
function isPolymarketResponse(data: unknown): data is PolymarketResponse { | ||
return Array.isArray(data) && | ||
data.length && | ||
typeof data[0] === "object" && | ||
data[0] && | ||
"slug" in data[0] | ||
? true | ||
: false; | ||
} | ||
|
||
/** | ||
* Searches Polymarket's api for a a market using a market's title. | ||
* | ||
* @param {string} title - The title of the market. | ||
* @returns {PolymarketData} data - Relevant market data | ||
*/ | ||
async function getPolymarketData(title: string): Promise<PolymarketData> { | ||
const POLYMARKET_BASE_URL = "https://polymarket.com"; | ||
const params = new URLSearchParams({ | ||
_c: "all", | ||
_p: "1", | ||
_q: title, | ||
}); | ||
|
||
const url = `https://polymarket.com/api/events/search?${params.toString()}`; | ||
const res = await fetch(url); | ||
const search = await res.json(); | ||
if (!isPolymarketResponse(search)) { | ||
throw new Error("Unable to find market."); | ||
} | ||
const data = search[0]; | ||
|
||
return { | ||
...data, | ||
link: `${POLYMARKET_BASE_URL}/event/${data.slug}`, | ||
}; | ||
} | ||
|
||
export function usePolymarketData(title: string, shouldFetch = true) { | ||
return useQuery({ | ||
queryKey: [title], | ||
queryFn: () => getPolymarketData(title), | ||
onError: (err) => console.error(err), | ||
enabled: !!title && shouldFetch, | ||
refetchInterval: Infinity, | ||
refetchOnMount: false, | ||
}); | ||
} |
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