generated from scaffold-eth/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove useNativeCurrencyPrice.ts"
This reverts commit 58f36a0.
- Loading branch information
1 parent
58f36a0
commit 3cbf332
Showing
6 changed files
with
122 additions
and
1 deletion.
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
35 changes: 35 additions & 0 deletions
35
packages/nextjs/hooks/scaffold-eth/useNativeCurrencyPrice.ts
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,35 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useInterval } from "usehooks-ts"; | ||
import scaffoldConfig from "~~/scaffold.config"; | ||
import { useGlobalState } from "~~/services/store/store"; | ||
import { fetchPriceFromUniswap } from "~~/utils/scaffold-eth"; | ||
|
||
const enablePolling = false; | ||
|
||
/** | ||
* Get the price of Native Currency based on Native Token/DAI trading pair from Uniswap SDK | ||
* @returns nativeCurrencyPrice: number | ||
*/ | ||
export const useNativeCurrencyPrice = () => { | ||
const targetNetwork = useGlobalState(state => state.targetNetwork); | ||
const [nativeCurrencyPrice, setNativeCurrencyPrice] = useState(0); | ||
|
||
// Get the price of ETH from Uniswap on mount | ||
useEffect(() => { | ||
(async () => { | ||
const price = await fetchPriceFromUniswap(targetNetwork); | ||
setNativeCurrencyPrice(price); | ||
})(); | ||
}, [targetNetwork]); | ||
|
||
// Get the price of ETH from Uniswap at a given interval | ||
useInterval( | ||
async () => { | ||
const price = await fetchPriceFromUniswap(targetNetwork); | ||
setNativeCurrencyPrice(price); | ||
}, | ||
enablePolling ? scaffoldConfig.pollingInterval : null, | ||
); | ||
|
||
return nativeCurrencyPrice; | ||
}; |
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
73 changes: 73 additions & 0 deletions
73
packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts
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,73 @@ | ||
import { ChainWithAttributes, getAlchemyHttpUrl } from "./networks"; | ||
import { CurrencyAmount, Token } from "@uniswap/sdk-core"; | ||
import { Pair, Route } from "@uniswap/v2-sdk"; | ||
import { Address, createPublicClient, http, parseAbi } from "viem"; | ||
import { mainnet } from "viem/chains"; | ||
|
||
const publicClient = createPublicClient({ | ||
chain: mainnet, | ||
transport: http(getAlchemyHttpUrl(mainnet.id)), | ||
}); | ||
|
||
const ABI = parseAbi([ | ||
"function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)", | ||
"function token0() external view returns (address)", | ||
"function token1() external view returns (address)", | ||
]); | ||
|
||
export const fetchPriceFromUniswap = async (targetNetwork: ChainWithAttributes): Promise<number> => { | ||
if (!targetNetwork?.nativeCurrency) { | ||
return 0; | ||
} | ||
if ( | ||
targetNetwork.nativeCurrency.symbol !== "ETH" && | ||
targetNetwork.nativeCurrency.symbol !== "SEP" && | ||
!targetNetwork.nativeCurrencyTokenAddress | ||
) { | ||
return 0; | ||
} | ||
try { | ||
const DAI = new Token(1, "0x6B175474E89094C44Da98b954EedeAC495271d0F", 18); | ||
const TOKEN = new Token( | ||
1, | ||
targetNetwork.nativeCurrencyTokenAddress || "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", | ||
18, | ||
); | ||
const pairAddress = Pair.getAddress(TOKEN, DAI) as Address; | ||
|
||
const wagmiConfig = { | ||
address: pairAddress, | ||
abi: ABI, | ||
}; | ||
|
||
const reserves = await publicClient.readContract({ | ||
...wagmiConfig, | ||
functionName: "getReserves", | ||
}); | ||
|
||
const token0Address = await publicClient.readContract({ | ||
...wagmiConfig, | ||
functionName: "token0", | ||
}); | ||
|
||
const token1Address = await publicClient.readContract({ | ||
...wagmiConfig, | ||
functionName: "token1", | ||
}); | ||
const token0 = [TOKEN, DAI].find(token => token.address === token0Address) as Token; | ||
const token1 = [TOKEN, DAI].find(token => token.address === token1Address) as Token; | ||
const pair = new Pair( | ||
CurrencyAmount.fromRawAmount(token0, reserves[0].toString()), | ||
CurrencyAmount.fromRawAmount(token1, reserves[1].toString()), | ||
); | ||
const route = new Route([pair], TOKEN, DAI); | ||
const price = parseFloat(route.midPrice.toSignificant(6)); | ||
return price; | ||
} catch (error) { | ||
console.error( | ||
`useNativeCurrencyPrice - Error fetching ${targetNetwork.nativeCurrency.symbol} price from Uniswap: `, | ||
error, | ||
); | ||
return 0; | ||
} | ||
}; |
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