Skip to content

Commit

Permalink
revamp fetch usdc price
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Jun 19, 2024
1 parent 168a820 commit 46ba802
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions src/custom/usdPrice/useFetchPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,42 @@ import {
import { formatAmount } from '@massalabs/react-ui-kit';
import { useDebounceValue } from 'usehooks-ts';
import { useBridgeModeStore } from '../../store/modeStore';
import { useAccountStore } from '../../store/store';
import { IToken } from '../../store/tokenStore';
import {
useAccountStore,
useOperationStore,
useTokenStore,
} from '../../store/store';
import { useMassaNetworkValidation } from '../bridge/useNetworkValidation';

export const useUsdValue = (amount?: bigint, token?: IToken) => {
const { isMainnet } = useBridgeModeStore();
const [usdValue, setUsdValue] = useState<string>();
const [debouncedAmount] = useDebounceValue(amount, 300);
export function useUsdValue() {
const { isMainnet, currentMode } = useBridgeModeStore();
const { inputAmount } = useOperationStore();
const [debouncedAmount] = useDebounceValue(inputAmount, 300);
const { massaClient } = useAccountStore();

const { selectedToken } = useTokenStore();
const isValidMassaNetwork = useMassaNetworkValidation();
const [usdValue, setUsdValue] = useState<string>();
const [isFetching, setIsFetching] = useState<boolean>(false);
const getUsdValue = useCallback(async () => {
if (!token || !debouncedAmount || !massaClient) {
if (
!selectedToken ||
!debouncedAmount ||
!massaClient ||
!inputAmount ||
!isValidMassaNetwork
) {
setIsFetching(false);
setUsdValue('0');
return;
}

const symbol = token.symbolEVM.toUpperCase();
setIsFetching(true);
const symbol = selectedToken.symbolEVM.toUpperCase();
let outputAmount: string;
if (symbol.includes('USD') || symbol.includes('DAI')) {
outputAmount = formatAmount(debouncedAmount, token.decimals).preview;
outputAmount = formatAmount(
debouncedAmount,
selectedToken.decimals,
).preview;
} else {
const chainId = isMainnet() ? ChainId.MAINNET : ChainId.BUILDNET;
const USDC = _USDC[chainId];
Expand All @@ -50,6 +68,13 @@ export const useUsdValue = (amount?: bigint, token?: IToken) => {

const allPairs = PairV2.initPairs(allTokenPairs);

if (!allPairs) {
console.warn('No pairs found');
setIsFetching(false);
setUsdValue('0');
return;
}

// generates all possible routes to consider
const allRoutes = RouteV2.createAllRoutes(
allPairs,
Expand All @@ -70,27 +95,44 @@ export const useUsdValue = (amount?: bigint, token?: IToken) => {
);

// chooses the best trade
const bestTrade = TradeV2.chooseBestTrade(trades, true);
let bestTrade;

try {
bestTrade = TradeV2.chooseBestTrade(trades, true);
} catch (error) {
console.warn('No best trade found');
setIsFetching(false);
setUsdValue('0');
return;
}

const quoter = new IQuoter(LB_QUOTER_ADDRESS[chainId], massaClient);

const prices = await quoter.findBestPathFromAmountIn(
bestTrade.route.pathToStrArr(),
debouncedAmount.toString(),
);

if (!prices.virtualAmountsWithoutSlippage[2]) {
console.warn('No slippage amount found');
setIsFetching(false);
return;
}
// get the output amount without slippage
outputAmount = formatAmount(
prices.virtualAmountsWithoutSlippage[2],
outputToken.decimals,
).preview;
}

console.log('outputAmount', outputAmount);
setUsdValue(outputAmount);
}, [debouncedAmount, token, massaClient, isMainnet]);
setIsFetching(false);
}, [debouncedAmount, massaClient, isMainnet, inputAmount]);

useEffect(() => {
getUsdValue();
}, [getUsdValue]);
}, [getUsdValue, currentMode, selectedToken]);

return { usdValue };
};
return { usdValue, isFetching };
}

0 comments on commit 46ba802

Please sign in to comment.