diff --git a/src/App.tsx b/src/App.tsx index 1e81948..8847702 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,12 +5,13 @@ import { useNetwork, useAccount, useDisconnect, + useContractRead, useContractReads, useQuery, } from "wagmi"; import { Address } from "viem"; -import { getMainnetUsdHex, getPulseXDaiHex } from "./util"; +import { getPulseXDaiHex } from "./util"; import { HexContext } from "./Context"; import { UriAccount } from "./lib/App"; @@ -229,16 +230,82 @@ function App() { onSuccess: (data) => setUSDHEX(data), }); - useQuery([networkName, "UsdHex"], getMainnetUsdHex, { + // Uniswap V2 Pair contract ABI + const UNISWAP_V2_PAIR_ABI = [ + { + constant: true, + inputs: [], + name: "getReserves", + outputs: [ + { internalType: "uint112", name: "reserve0", type: "uint112" }, + { internalType: "uint112", name: "reserve1", type: "uint112" }, + { internalType: "uint32", name: "blockTimestampLast", type: "uint32" }, + ], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: true, + inputs: [], + name: "token0", + outputs: [{ internalType: "address", name: "", type: "address" }], + payable: false, + stateMutability: "view", + type: "function", + }, + { + constant: true, + inputs: [], + name: "token1", + outputs: [{ internalType: "address", name: "", type: "address" }], + payable: false, + stateMutability: "view", + type: "function", + }, + ]; + // useQuery([networkName, "UsdHex"], getMainnetUsdHex, { + // enabled: chainId == 1, + // refetchInterval: 10000, + // retry: 5, + // retryDelay: 5000, + // onSuccess: (data) => setUSDHEX(data), + // }); + + const { data: price } = useContractRead({ + address: "0xF6DCdce0ac3001B2f67F750bc64ea5beB37B5824", // Uniswap v2 HEX / USDC + abi: UNISWAP_V2_PAIR_ABI, + functionName: "getReserves", enabled: chainId == 1, - refetchInterval: 10000, - retry: 5, - retryDelay: 5000, - onSuccess: (data) => setUSDHEX(data), - }); + watch: true, + cacheTime: 10000, // 10 seconds + staleTime: 10000, // 10 seconds + select: (reserves) => { + const [reserve0, reserve1, _blockTimestampLast] = reserves as [ bigint, bigint, number ] + const bnPrice = reserve0 > 0 ? (reserve1 * 100000000n) / reserve0 : 0.0 + const price = Number(bnPrice) / 1000000 + debug("HEX USDC: ", price) + return price + }, + }) + + useEffect(() => { + setUSDHEX(price || 0.00); + }, [price]); + + // Refetch Mainnet Price every 10 seconds + // useEffect(() => { + // let interval: NodeJS.Timer | undefined = undefined; + // if (chainId == 1) { + // interval = setInterval(() => { + // refetchMainnetPrice(); + // }, 10000); + // return () => clearInterval(interval); + // } else if (interval) clearInterval(interval); + // }, [refetchMainnetPrice, chainId]); // start the show when walletAddress appears - // Lo and behold! Hardhat (Viem) appears to come with Multicall3 built in. + // Lo and behold! Hardhat (Viem) appears to come with Multicall3 built in. useContractReads({ enabled: !!walletAddress, scopeKey: `HexData:${chainId}`, diff --git a/src/util.ts b/src/util.ts index fcc38c8..7bfbb78 100644 --- a/src/util.ts +++ b/src/util.ts @@ -9,18 +9,35 @@ import _debug from 'debug' const debug = _debug("util") debug("loaded") +// export const getMainnetUsdHex = async () => { +// const response = await axios.get("https://uniswapdataapi.azurewebsites.net/api/hexPrice") +// debug("HEX-USD: ", response.data?.hexUsd) +// return parseFloat(response.data?.hexUsd || "?.??") +// } + export const getMainnetUsdHex = async () => { - const response = await axios.get("https://uniswapdataapi.azurewebsites.net/api/hexPrice") - debug("HEX-USD: ", response.data?.hexUsd) - return parseFloat(response.data?.hexUsd || "0.0") + const response = await axios.post( + "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2", + { + query: '{pair(id:"0xF6DCdce0ac3001B2f67F750bc64ea5beB37B5824"){token1Price}}' // HEX-USDC, where token1Price => USDC per HEX + }, + { + headers: { + "Content-Type": "application/json", + }, + responseType: "json", + } + ) + debug("HEX-USDC: ", response.data.data.pair.token1Price) + return parseFloat(response.data.data.pair?.token1Price || "?.??") } export const getPulseXDaiHex = async () => { const response = await axios.post("https://graph.pulsechain.com/subgraphs/name/pulsechain/pulsex", { - query: '{pair(id:"0x6f1747370b1cacb911ad6d4477b718633db328c8"){token1Price}}' // HEX-DAI, where token1Price => DAI per HEX + query: '{pair(id:"0x6f1747370b1cacb911ad6d4477b718633db328c8"){token1Price}}' // HEX/DAI, where token1Price => DAI per HEX }) - debug("HEX-DAI: ", response.data.data.pair.token1Price) - return parseFloat(response.data.data.pair?.token1Price || "0.0") + debug("HEX DAI: ", response.data.data.pair.token1Price) + return parseFloat(response.data.data.pair?.token1Price || "?.??") } /*