-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
166 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { stringify } from "viem"; | ||
import fs from "fs"; | ||
|
||
export async function GET(req: Request) { | ||
const searchParams = new URL(req.url).searchParams; | ||
const ids = searchParams.get("ids"); | ||
const currencies = searchParams.get("currencies"); | ||
|
||
const { isCached, priceCached } = getFromCache(ids, currencies); | ||
if (isCached) { | ||
return Response.json(JSON.parse(priceCached)); | ||
} | ||
|
||
const price = await fetch( | ||
`https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=${currencies}`, | ||
); | ||
|
||
const priceJson = await price.json(); | ||
|
||
saveToCache(ids, currencies, priceJson); | ||
|
||
return Response.json(JSON.parse(stringify(priceJson))); | ||
} | ||
|
||
function saveToCache( | ||
ids: string | null, | ||
currencies: string | null, | ||
priceJson: Response | null, | ||
): void { | ||
const key = `${ids}-${currencies}`; | ||
// create cache folder if not exist | ||
if (!fs.existsSync("./cache")) { | ||
fs.mkdirSync("./cache"); | ||
} | ||
// save to local files | ||
fs.writeFileSync(`./cache/${key}.json`, JSON.stringify({ ...priceJson, timestamp: Date.now() })); | ||
} | ||
|
||
function getFromCache( | ||
ids: string | null, | ||
currencies: string | null, | ||
): { isCached: boolean; priceCached: string } { | ||
const key = `${ids}-${currencies}`; | ||
// retrieve from local files | ||
try { | ||
const priceCached = fs.readFileSync(`./cache/${key}.json`, "utf8"); | ||
const priceCachedJson = JSON.parse(priceCached); | ||
|
||
const timestamp = priceCachedJson.timestamp; | ||
const now = Date.now(); | ||
// cache for 1 minute | ||
if (now - timestamp > 1 * 60 * 1000) { | ||
return { isCached: false, priceCached: "" }; | ||
} | ||
return { isCached: true, priceCached }; | ||
} catch (e) { | ||
return { isCached: false, priceCached: "" }; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,25 +1,21 @@ | ||
import { PUBLIC_CLIENT } from "@/constants/client"; | ||
import { FACTORY_ABI, FACTORY_ADDRESS } from "@/constants/factory"; | ||
import { Hex, createPublicClient, http, stringify, toHex } from "viem"; | ||
import { baseGoerli } from "viem/chains"; | ||
import { Hex, stringify, toHex } from "viem"; | ||
|
||
export async function GET(_req: Request, { params }: { params: { id: Hex } }) { | ||
const { id } = params; | ||
console.log(id); | ||
if (!id) { | ||
return Response.json(JSON.parse(stringify({ error: "id is required" }))); | ||
} | ||
|
||
const publicClient = createPublicClient({ | ||
chain: baseGoerli, | ||
transport: http(), | ||
}); | ||
|
||
const user = await publicClient.readContract({ | ||
const user = await PUBLIC_CLIENT.readContract({ | ||
address: FACTORY_ADDRESS, | ||
abi: FACTORY_ABI, | ||
functionName: "getUser", | ||
args: [BigInt(id)], | ||
}); | ||
|
||
return Response.json(JSON.parse(stringify({ ...user, id: toHex(user.id) }))); | ||
const balance = await PUBLIC_CLIENT.getBalance({ address: user.account }); | ||
|
||
return Response.json(JSON.parse(stringify({ ...user, id: toHex(user.id), balance }))); | ||
} |
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,28 @@ | ||
"use client"; | ||
|
||
import { useBalance } from "@/providers/BalanceProvider"; | ||
import { Flex, Text } from "@radix-ui/themes"; | ||
import { CSSProperties, useEffect } from "react"; | ||
|
||
const css: CSSProperties = { | ||
margin: "1rem", | ||
}; | ||
|
||
export default function Balance() { | ||
const KEY_ID = "0x9e925f1ff5b39500f805ff205534b589c72603c740b3de6975511818095eec36"; | ||
|
||
const { balance, getBalance } = useBalance(); | ||
|
||
useEffect(() => { | ||
getBalance(KEY_ID); | ||
console.log("balance", balance); | ||
}, [balance, getBalance]); | ||
|
||
return ( | ||
<Flex style={css} direction="column"> | ||
<Text highContrast={true} color="green" weight="bold" size="8"> | ||
$ {balance.toFixed(2)} | ||
</Text> | ||
</Flex> | ||
); | ||
} |
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,7 @@ | ||
import { createPublicClient, http } from "viem"; | ||
import { baseGoerli } from "viem/chains"; | ||
|
||
export const PUBLIC_CLIENT = createPublicClient({ | ||
chain: baseGoerli, | ||
transport: http(), | ||
}); |
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,40 @@ | ||
"use client"; | ||
|
||
import { getUser } from "@/libs/factory/getUser"; | ||
import { createContext, useContext, useState } from "react"; | ||
import { Hex } from "viem"; | ||
|
||
function useBalanceHook() { | ||
// balance in usd | ||
const [balance, setBalance] = useState<number>(0); | ||
|
||
async function getBalance(keyId: Hex) { | ||
const user = await getUser(keyId); | ||
const ethBalance = Number(user.balance) / 1e18; | ||
const priceData = await fetch("/api/price?ids=ethereum¤cies=usd"); | ||
const price: number = (await priceData.json()).ethereum.usd; | ||
setBalance(ethBalance * price); | ||
} | ||
|
||
return { | ||
balance, | ||
getBalance, | ||
}; | ||
} | ||
|
||
type UseBalanceHook = ReturnType<typeof useBalanceHook>; | ||
const BalanceContext = createContext<UseBalanceHook | null>(null); | ||
|
||
export const useBalance = (): UseBalanceHook => { | ||
const context = useContext(BalanceContext); | ||
if (!context) { | ||
throw new Error("useBalanceHook must be used within a BalanceProvider"); | ||
} | ||
return context; | ||
}; | ||
|
||
export function BalanceProvider({ children }: { children: React.ReactNode }) { | ||
const hook = useBalanceHook(); | ||
|
||
return <BalanceContext.Provider value={hook}>{children}</BalanceContext.Provider>; | ||
} |