Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/abis/ChainlinkPriceFeed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "description",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "latestAnswer",
"outputs": [
{
"internalType": "int256",
"name": "",
"type": "int256"
}
],
"stateMutability": "view",
"type": "function"
}
]
12 changes: 12 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ export const ACTIONS_TABS: { value: ActionType; label: string }[] = [
{ value: 'mint', label: 'Mint' },
{ value: 'withdraw', label: 'Withdraw' },
];

export interface ChainlinkPriceFeeds {
[key: string]: string;
}

export const CHAINLINK_PRICE_FEEDS: Record<string, ChainlinkPriceFeeds> = {
[MAINNET_CHAIN_ID_STRING]: {
'ETH': '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419',
'WETH': '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419',
'WSTETH': '0x8B6851156023f4f5A66F68BEA80851c3D905Ac93'
}
};
19 changes: 14 additions & 5 deletions src/contexts/VaultContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { ltvToLeverage, getLendingProtocolAddress, isVaultExists, isUserRejected
import { ApyData, isAddressWhitelistedToMint, refreshTokenHolders } from '@/utils/api';
import { isWETHAddress, GAS_RESERVE_WEI, SEPOLIA_CHAIN_ID_STRING, SEPOLIA_MORPHO_MARKET_ID, CONNECTOR_ADDRESSES } from '@/constants';
import { useAdaptiveInterval, useVaultApy, useVaultPointsRate } from '@/hooks';
import { loadGhostLtv, loadAaveLtv, loadMorphoLtv, fetchTokenPrice } from '@/utils';
import { loadGhostLtv, loadAaveLtv, loadMorphoLtv } from '@/utils';
import { getAssetPrice } from '@/utils/getAssetPrice';
import vaultsConfig from '../../vaults.config.json';
import signaturesConfig from '../../signatures.config.json';

Expand Down Expand Up @@ -661,26 +662,34 @@ export const VaultContextProvider = ({ children, vaultAddress, params }: { child
}, [publicProvider, vaultLens, lendingAddress, vaultAddress, borrowTokenDecimals, currentNetwork, vaultConfig]);

const loadPrices = useCallback(async () => {
if (!isMainnet) {
if (!isMainnet || !publicProvider || !currentNetwork) {
setBorrowTokenPrice(null);
setCollateralTokenPrice(null);
return;
}

try {
if (borrowTokenSymbol) {
const price = await fetchTokenPrice(borrowTokenSymbol);
const price = await getAssetPrice(
borrowTokenSymbol.toUpperCase(),
currentNetwork,
publicProvider
);
setBorrowTokenPrice(price);
}

if (collateralTokenSymbol) {
const price = await fetchTokenPrice(collateralTokenSymbol);
const price = await getAssetPrice(
collateralTokenSymbol.toUpperCase(),
currentNetwork,
publicProvider
);
setCollateralTokenPrice(price);
}
} catch (err) {
console.error('Error loading token prices:', err);
}
}, [isMainnet, borrowTokenSymbol, collateralTokenSymbol]);
}, [isMainnet, borrowTokenSymbol, collateralTokenSymbol, publicProvider, currentNetwork]);

useAdaptiveInterval(loadPrices, {
initialDelay: 60000,
Expand Down
29 changes: 29 additions & 0 deletions src/utils/getAssetPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { JsonRpcProvider, formatUnits } from "ethers";
import { CHAINLINK_PRICE_FEEDS } from "@/constants";
import { ChainlinkPriceFeed__factory } from "@/typechain-types";

export const getAssetPrice = async (
symbol: string,
networkId: string,
provider: JsonRpcProvider
): Promise<number | null> => {
try {
const feedsForNetwork = CHAINLINK_PRICE_FEEDS[networkId];
if (!feedsForNetwork) return null;

const feedAddress = feedsForNetwork[symbol.toUpperCase()];
if (!feedAddress) return null;

const feedContract = ChainlinkPriceFeed__factory.connect(feedAddress, provider);

const [latestAnswer, decimals] = await Promise.all([
feedContract.latestAnswer(),
feedContract.decimals()
]);

return parseFloat(formatUnits(latestAnswer, decimals));
} catch (err) {
console.error(`Error retrieving price for ${symbol} from Chainlink:`, err);
return null;
}
};