-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-eth-price.ts
More file actions
33 lines (28 loc) · 800 Bytes
/
Copy pathuse-eth-price.ts
File metadata and controls
33 lines (28 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"use client";
import { useReadContract } from "wagmi";
const CHAINLINK_ETH_USD = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419" as const;
const aggregatorAbi = [
{
type: "function",
name: "latestRoundData",
inputs: [],
outputs: [
{ name: "roundId", type: "uint80" },
{ name: "answer", type: "int256" },
{ name: "startedAt", type: "uint256" },
{ name: "updatedAt", type: "uint256" },
{ name: "answeredInRound", type: "uint80" },
],
stateMutability: "view",
},
] as const;
export function useEthPrice(): number | null {
const { data } = useReadContract({
address: CHAINLINK_ETH_USD,
abi: aggregatorAbi,
functionName: "latestRoundData",
});
if (!data) return null;
const answer = data[1];
return Number(answer) / 1e8;
}