From 1980bae1e1fa65c4defeecb25958a5f4f5036895 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 8 Oct 2024 14:49:10 +0200 Subject: [PATCH] fix: update usage after owm refactor --- apps/lend/src/components/DetailInfoHealth.tsx | 14 ++--- .../DetailsMarket/components/DetailsLoan.tsx | 11 ++-- .../components/DetailsSupply.tsx | 8 +-- .../components/DetailsUserLoan.tsx | 19 +++---- .../DetailsUserLoanAlertSoftLiquidation.tsx | 3 +- .../components/DetailsUserSupply.tsx | 6 +- .../LoanFormCreate/components/DetailInfo.tsx | 12 ++-- .../components/DetailInfoLeverage.tsx | 6 +- .../components/DetailInfoNonLeverage.tsx | 11 ++-- .../PageLoanCreate/LoanFormCreate/index.tsx | 29 +++++----- .../src/components/PageLoanCreate/types.ts | 2 +- .../src/components/PageLoanManage/index.tsx | 6 +- .../components/MarketListNoResult.tsx | 6 +- .../TableRowViewContentTable/TableRow.tsx | 4 +- .../TableRowContainer.tsx | 13 ++--- .../TableRowMobile.tsx | 6 +- .../src/components/PageMarketList/index.tsx | 3 +- .../src/components/PageMarketList/utils.ts | 6 +- .../components/SharedCellData/CellToken.tsx | 2 +- .../CellTotalCollateralValue.tsx | 7 ++- .../src/store/createLoanBorrowMoreSlice.ts | 30 +++++----- .../src/store/createLoanCollateralAddSlice.ts | 14 ++--- .../store/createLoanCollateralRemoveSlice.ts | 12 ++-- apps/lend/src/store/createLoanCreateSlice.ts | 34 +++++------ apps/lend/src/store/createLoanRepaySlice.ts | 16 +++--- .../store/createLoanSelfLiquidationSlice.ts | 10 ++-- apps/lend/src/store/createMarketListSlice.ts | 6 +- apps/lend/src/store/createMarketsSlice.ts | 10 ++-- apps/lend/src/store/createUserSlice.ts | 56 +++++++++---------- 29 files changed, 174 insertions(+), 188 deletions(-) diff --git a/apps/lend/src/components/DetailInfoHealth.tsx b/apps/lend/src/components/DetailInfoHealth.tsx index 288c0f629..ce3904228 100644 --- a/apps/lend/src/components/DetailInfoHealth.tsx +++ b/apps/lend/src/components/DetailInfoHealth.tsx @@ -44,7 +44,7 @@ const DetailInfoHealth = ({ loading: boolean setHealthMode: React.Dispatch> }) => { - const owmData = useOneWayMarket(rChainId, rOwmId)?.data + const market = useOneWayMarket(rChainId, rOwmId)?.data const oraclePriceBand = useStore((state) => state.markets.pricesMapper[rChainId]?.[rOwmId]?.prices?.oraclePriceBand) const userLoanDetails = useStore((state) => state.user.loansDetailsMapper[userActiveKey]?.details) @@ -58,7 +58,7 @@ const DetailInfoHealth = ({ if (typeof oraclePriceBand === 'number' && healthFull) { setHealthMode( getHealthMode( - owmData, + market, oraclePriceBand, amount, bands, @@ -79,7 +79,7 @@ const DetailInfoHealth = ({ formType, healthFull, newHealthModeColorKey, - owmData, + market, setHealthMode, ]) @@ -88,10 +88,10 @@ const DetailInfoHealth = ({ if (typeof oraclePriceBand === 'number' && userLoanDetails) { const { healthFull, bands } = userLoanDetails setCurrentHealthMode( - getHealthMode(owmData, oraclePriceBand, amount, bands, formType, healthFull, '', newHealthModeColorKey) + getHealthMode(market, oraclePriceBand, amount, bands, formType, healthFull, '', newHealthModeColorKey) ) } - }, [oraclePriceBand, amount, formType, newHealthModeColorKey, owmData, userLoanDetails]) + }, [oraclePriceBand, amount, formType, newHealthModeColorKey, market, userLoanDetails]) const healthPercent = useMemo(() => { if (healthMode.percent) { @@ -153,7 +153,7 @@ export default DetailInfoHealth // 1. If health(full=true) < loan_discount, user is at risk to go from healthy mode to soft liquidation mode (green —> orange). // 2. If health(full=false) < liquidation_discount , user is at risk to go from soft liquidation mode to hard liquidation mode (orange —> red). export function getHealthMode( - owmData: OneWayMarketTemplate | undefined, + market: OneWayMarketTemplate | undefined, oraclePriceBand: number | null, amount: string, bands: [number, number] | number[], @@ -179,7 +179,7 @@ export function getHealthMode( message = t`You are still close to soft liquidation.` } else if (newColorKey === 'close_to_liquidation') { const formattedAmount = formatNumber(amount) - const borrowedToken = owmData?.borrowed_token?.symbol + const borrowedToken = market?.borrowed_token?.symbol if (formType === 'collateral-decrease') { message = t`Removing ${formattedAmount} collateral, will put you close to soft liquidation.` } else if (formType === 'create-loan') { diff --git a/apps/lend/src/components/DetailsMarket/components/DetailsLoan.tsx b/apps/lend/src/components/DetailsMarket/components/DetailsLoan.tsx index fe09b12b8..48bf605b1 100644 --- a/apps/lend/src/components/DetailsMarket/components/DetailsLoan.tsx +++ b/apps/lend/src/components/DetailsMarket/components/DetailsLoan.tsx @@ -20,14 +20,14 @@ import ChartOhlcWrapper from '@/components/ChartOhlcWrapper' import ListInfoItem, { ListInfoItems, ListInfoItemsWrapper } from '@/ui/ListInfo' const DetailsLoan = ({ type, ...pageProps }: PageContentProps & { type: MarketListType }) => { - const { rChainId, rOwmId, owmDataCachedOrApi, borrowed_token, collateral_token, titleMapper, userActiveKey } = + const { rChainId, rOwmId, market, titleMapper, userActiveKey } = pageProps const chartExpanded = useStore((state) => state.ohlcCharts.chartExpanded) const cellProps = { rChainId, rOwmId, - owmDataCachedOrApi, + market, size: 'md' as const, } @@ -73,8 +73,7 @@ const DetailsLoan = ({ type, ...pageProps }: PageContentProps & { type: MarketLi @@ -82,9 +81,7 @@ const DetailsLoan = ({ type, ...pageProps }: PageContentProps & { type: MarketLi diff --git a/apps/lend/src/components/DetailsMarket/components/DetailsSupply.tsx b/apps/lend/src/components/DetailsMarket/components/DetailsSupply.tsx index 9b1c4c5be..df2753ebe 100644 --- a/apps/lend/src/components/DetailsMarket/components/DetailsSupply.tsx +++ b/apps/lend/src/components/DetailsMarket/components/DetailsSupply.tsx @@ -16,12 +16,12 @@ import ListInfoItem, { ListInfoItems, ListInfoItemsWrapper } from '@/ui/ListInfo import MarketParameters from '@/components/DetailsMarket/components/MarketParameters' const DetailsSupply = ({ type, ...pageProps }: PageContentProps & { type: MarketListType }) => { - const { rChainId, rOwmId, owmDataCachedOrApi, borrowed_token, collateral_token, titleMapper } = pageProps + const { rChainId, rOwmId, market, titleMapper } = pageProps const cellProps = { rChainId, rOwmId, - owmDataCachedOrApi, + market, size: 'md' as const, } @@ -63,9 +63,7 @@ const DetailsSupply = ({ type, ...pageProps }: PageContentProps & { type: Market
diff --git a/apps/lend/src/components/DetailsUser/components/DetailsUserLoan.tsx b/apps/lend/src/components/DetailsUser/components/DetailsUserLoan.tsx index 406ab66a9..cf82f6721 100644 --- a/apps/lend/src/components/DetailsUser/components/DetailsUserLoan.tsx +++ b/apps/lend/src/components/DetailsUser/components/DetailsUserLoan.tsx @@ -24,7 +24,7 @@ import ChartOhlcWrapper from '@/components/ChartOhlcWrapper' import ListInfoItem, { ListInfoItems, ListInfoItemsWrapper } from '@/ui/ListInfo' const DetailsUserLoan = (pageProps: PageContentProps) => { - const { rChainId, rOwmId, api, owmDataCachedOrApi, titleMapper, userActiveKey } = pageProps + const { rChainId, rOwmId, api, market, titleMapper, userActiveKey } = pageProps const isAdvanceMode = useStore((state) => state.isAdvanceMode) const loanExistsResp = useStore((state) => state.user.loansExistsMapper[userActiveKey]) @@ -43,7 +43,7 @@ const DetailsUserLoan = (pageProps: PageContentProps) => { rChainId, rOwmId, isBold: true, - owmDataCachedOrApi, + market: market!, userActiveKey, size: 'md' as const, } @@ -83,20 +83,17 @@ const DetailsUserLoan = (pageProps: PageContentProps) => { - + {/* stats */} {contents.map((groupedContents, idx) => ( - {groupedContents.map(({ titleKey, content, show, ...props }, idx) => { - if (!_showContent(show)) return null - return ( - - {content} - - ) - })} + {groupedContents.filter(({show}) => _showContent(show)).map(({ titleKey, content }, idx) => ( + + {content} + + ))} ))} diff --git a/apps/lend/src/components/DetailsUser/components/DetailsUserLoanAlertSoftLiquidation.tsx b/apps/lend/src/components/DetailsUser/components/DetailsUserLoanAlertSoftLiquidation.tsx index 0baf16655..150b0d2be 100644 --- a/apps/lend/src/components/DetailsUser/components/DetailsUserLoanAlertSoftLiquidation.tsx +++ b/apps/lend/src/components/DetailsUser/components/DetailsUserLoanAlertSoftLiquidation.tsx @@ -8,7 +8,8 @@ import AlertBox from '@/ui/AlertBox' import Box from '@/ui/Box' import ExternalLink from '@/ui/Link/ExternalLink' -const DetailsUserLoanAlertSoftLiquidation = ({ borrowed_token, collateral_token, userActiveKey }: PageContentProps) => { +const DetailsUserLoanAlertSoftLiquidation = ({ market, userActiveKey }: PageContentProps) => { + const {borrowed_token, collateral_token} = market ?? {} const userLoanDetails = useStore((state) => state.user.loansDetailsMapper[userActiveKey]) // TODO handle error diff --git a/apps/lend/src/components/DetailsUser/components/DetailsUserSupply.tsx b/apps/lend/src/components/DetailsUser/components/DetailsUserSupply.tsx index b1fc86fd8..6bb58de89 100644 --- a/apps/lend/src/components/DetailsUser/components/DetailsUserSupply.tsx +++ b/apps/lend/src/components/DetailsUser/components/DetailsUserSupply.tsx @@ -14,7 +14,7 @@ import DetailsUserSupplyStakedUnstaked from '@/components/DetailsUser/components import ListInfoItem, { ListInfoItems, ListInfoItemsWrapper } from '@/ui/ListInfo' const DetailsUserSupply = (pageProps: PageContentProps) => { - const { rChainId, rOwmId, api, userActiveKey, owmDataCachedOrApi, titleMapper } = pageProps + const { rChainId, rOwmId, api, userActiveKey, market, titleMapper } = pageProps const userBalancesResp = useStore((state) => state.user.marketsBalancesMapper[userActiveKey]) @@ -28,7 +28,7 @@ const DetailsUserSupply = (pageProps: PageContentProps) => { rChainId, rOwmId, isBold: true, - owmDataCachedOrApi, + market, userActiveKey, size: 'md' as const, } @@ -50,7 +50,7 @@ const DetailsUserSupply = (pageProps: PageContentProps) => { ) : foundVaultShares ? ( - + {/* stats */} diff --git a/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfo.tsx b/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfo.tsx index e6dc133c6..8ed575120 100644 --- a/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfo.tsx +++ b/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfo.tsx @@ -1,5 +1,5 @@ import type { LiqRangeSliderIdx } from '@/store/types' -import type { DetailInfoCompProps, DetailInfoCompAdditionalProps } from '@/components/PageLoanCreate/types' +import type { DetailInfoCompAdditionalProps, DetailInfoCompProps } from '@/components/PageLoanCreate/types' import React, { useMemo } from 'react' @@ -10,11 +10,9 @@ import DetailInfoNonLeverage from '@/components/PageLoanCreate/LoanFormCreate/co import DetailInfoLeverage from '@/components/PageLoanCreate/LoanFormCreate/components/DetailInfoLeverage' const DetailInfoComp = ({ isLeverage, ...props }: PageContentProps & DetailInfoCompProps & { isLeverage: boolean }) => { - const { api, owmData, steps, updateFormValues } = props + const { api, market, steps, updateFormValues } = props const { signerAddress } = api ?? {} - const { owm } = owmData ?? {} - const activeKeyLiqRange = useStore((state) => state.loanCreate.activeKeyLiqRange) const formValues = useStore((state) => state.loanCreate.formValues) const isEditLiqRange = useStore((state) => state.loanCreate.isEditLiqRange) @@ -26,12 +24,12 @@ const DetailInfoComp = ({ isLeverage, ...props }: PageContentProps & DetailInfoC const selectedLiqRange = useMemo(() => { if (formValues.n && liqRanges) { return liqRanges[formValues.n] - } else if (owm) { - return { n: owm.defaultBands } as LiqRangeSliderIdx + } else if (market) { + return { n: market.defaultBands } as LiqRangeSliderIdx } else { return undefined } - }, [formValues.n, liqRanges, owm]) + }, [formValues.n, liqRanges, market]) const handleLiqRangesEdit = () => { const showEditLiqRange = !isEditLiqRange diff --git a/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoLeverage.tsx b/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoLeverage.tsx index 80b54c3c6..be0d3c43f 100644 --- a/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoLeverage.tsx +++ b/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoLeverage.tsx @@ -27,10 +27,8 @@ const DetailInfoLeverage = ({ api, healthMode, isLoaded, - owm, + market, steps, - borrowed_token, - collateral_token, userActiveKey, handleSelLiqRange, selectedLiqRange, @@ -49,7 +47,7 @@ const DetailInfoLeverage = ({ const maxSlippage = useStore((state) => state.maxSlippage) const { signerAddress } = api ?? {} - const { minBands, maxBands } = owm ?? {} + const { minBands, maxBands, borrowed_token, collateral_token } = market ?? {} const { expectedCollateral, routes } = detailInfo ?? {} const { userBorrowed, debt } = formValues const { symbol: collateralSymbol = '' } = collateral_token ?? {} diff --git a/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoNonLeverage.tsx b/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoNonLeverage.tsx index 35f46b715..c3d5747b6 100644 --- a/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoNonLeverage.tsx +++ b/apps/lend/src/components/PageLoanCreate/LoanFormCreate/components/DetailInfoNonLeverage.tsx @@ -17,11 +17,9 @@ const DetailInfoNonLeverage = ({ rChainId, rOwmId, api, - borrowed_token, - collateral_token, healthMode, isLoaded, - owm, + market, steps, userActiveKey, handleSelLiqRange, @@ -39,6 +37,7 @@ const DetailInfoNonLeverage = ({ const liqRanges = useStore((state) => state.loanCreate.liqRanges[activeKeyLiqRange]) const { signerAddress } = api ?? {} + const { borrowed_token, collateral_token } = market ?? {} const loading = !isLoaded || typeof detailInfo === 'undefined' @@ -60,8 +59,8 @@ const DetailInfoNonLeverage = ({ - + {isAdvanceMode && ( { - const { rChainId, rOwmId, isLoaded, api, owmData, userActiveKey, borrowed_token, collateral_token } = pageProps + const { rChainId, rOwmId, isLoaded, api, market, userActiveKey } = pageProps const isSubscribed = useRef(false) const params = useParams() const navigate = useNavigate() @@ -70,15 +70,15 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i const [txInfoBar, setTxInfoBar] = useState(null) const { signerAddress } = api ?? {} - const { owm } = owmData ?? {} const { expectedCollateral } = detailInfoLeverage ?? {} + const { borrowed_token, collateral_token } = market ?? {} const updateFormValues = useCallback( (updatedFormValues: Partial, isFullReset?: boolean, shouldRefetch?: boolean) => { setConfirmWarning(DEFAULT_CONFIRM_WARNING) setFormValues( isLoaded ? api : null, - owmData, + market, isFullReset ? DEFAULT_FORM_VALUES : updatedFormValues, maxSlippage, isLeverage, @@ -87,7 +87,7 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i if (isFullReset) setHealthMode(DEFAULT_HEALTH_MODE) }, - [setFormValues, isLoaded, api, owmData, maxSlippage, isLeverage] + [setFormValues, isLoaded, api, market, maxSlippage, isLeverage] ) const handleClickCreate = useCallback( @@ -144,8 +144,7 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i { - const tokensMessage = getStepTokensStr(formValues, market.owm).symbolList + const tokensMessage = getStepTokensStr(formValues, market).symbolList const notifyMessage = t`Please approve spending your ${tokensMessage}.` const notify = notifyNotification(notifyMessage, 'pending') @@ -271,11 +270,11 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i // steps useEffect(() => { - if (isLoaded && api && owmData) { + if (isLoaded && api && market) { let updatedSteps = getSteps( activeKey, api, - owmData, + market, healthMode, confirmedWarning, formEstGas, @@ -301,7 +300,7 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i isLeverage, maxRecv, maxSlippage, - owmData?.owm?.id, + market?.id, signerAddress, userBalances, userDetails?.state, @@ -369,7 +368,7 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i maxRecv={maxRecv} handleInpChange={(debt) => updateFormValues({ debt })} handleMaxClick={async () => { - const debt = await refetchMaxRecv(owmData, isLeverage) + const debt = await refetchMaxRecv(market, isLeverage) updateFormValues({ debt }) }} /> @@ -379,7 +378,7 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i {...pageProps} healthMode={healthMode} isLeverage={isLeverage} - owm={owmData?.owm ?? null} + market={market!} steps={steps} setHealthMode={setHealthMode} updateFormValues={updateFormValues} @@ -430,8 +429,8 @@ const LoanCreate = ({ isLeverage = false, ...pageProps }: PageContentProps & { i /> )} {steps && } - {formStatus.isComplete && owm && ( - + {formStatus.isComplete && market && ( + Manage loan )} diff --git a/apps/lend/src/components/PageLoanCreate/types.ts b/apps/lend/src/components/PageLoanCreate/types.ts index fa1aee630..a56bc872a 100644 --- a/apps/lend/src/components/PageLoanCreate/types.ts +++ b/apps/lend/src/components/PageLoanCreate/types.ts @@ -31,7 +31,7 @@ export type FormEstGas = { export type DetailInfoCompProps = { healthMode: HealthMode - owm: OneWayMarketTemplate | null + market: OneWayMarketTemplate | null steps: Step[] setHealthMode: React.Dispatch> updateFormValues: (updatedFormValues: FormValues) => void diff --git a/apps/lend/src/components/PageLoanManage/index.tsx b/apps/lend/src/components/PageLoanManage/index.tsx index 39ad5bb63..94b861e13 100644 --- a/apps/lend/src/components/PageLoanManage/index.tsx +++ b/apps/lend/src/components/PageLoanManage/index.tsx @@ -17,7 +17,7 @@ import LoanCollateralAdd from '@/components/PageLoanManage/LoanCollateralAdd' import LoanCollateralRemove from '@/components/PageLoanManage/LoanCollateralRemove' const ManageLoan = (pageProps: PageContentProps) => { - const { rOwmId, rFormType, userActiveKey, owmDataCachedOrApi, rChainId } = pageProps + const { rOwmId, rFormType, userActiveKey, market, rChainId } = pageProps const params = useParams() const navigate = useNavigate() const tabsRef = useRef(null) @@ -32,12 +32,12 @@ const ManageLoan = (pageProps: PageContentProps) => { { label: t`Collateral`, key: 'collateral' }, ] - if (owmDataCachedOrApi?.hasLeverage) { + if (market?.leverage?.hasLeverage()) { forms.push({ label: t`Leverage`, key: 'leverage' }) } return forms - }, [owmDataCachedOrApi?.hasLeverage]) + }, [market?.leverage]) const TABS_LOAN: { label: string; formType: LoanFormType }[] = [ { label: t`Borrow more`, formType: 'loan-increase' }, diff --git a/apps/lend/src/components/PageMarketList/components/MarketListNoResult.tsx b/apps/lend/src/components/PageMarketList/components/MarketListNoResult.tsx index a3f2051e6..a914fffcd 100644 --- a/apps/lend/src/components/PageMarketList/components/MarketListNoResult.tsx +++ b/apps/lend/src/components/PageMarketList/components/MarketListNoResult.tsx @@ -12,17 +12,19 @@ import AlertBox from '@/ui/AlertBox' import Box from '@/ui/Box' import Button from '@/ui/Button' import ExternalLink from 'ui/src/Link/ExternalLink' +import { useChainId, useOneWayMarketMapping } from '@/entities/chain' const MarketListNoResult = ({ searchParams, signerAddress, updatePath, }: Pick & { signerAddress: string | undefined }) => { - const owmDatasError = useStore((state) => state.markets.error) + const chainId = useChainId()?.data! + const marketMappingError = useOneWayMarketMapping(chainId)?.error return ( - {owmDatasError ? ( + {marketMappingError ? ( {t`Unable to retrieve markets`} diff --git a/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRow.tsx b/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRow.tsx index 812ac36a3..fd8514150 100644 --- a/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRow.tsx +++ b/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRow.tsx @@ -32,7 +32,7 @@ const TableRowContent = ({ rChainId, api, owmId, - owmDataCachedOrApi, + market, filterTypeKey, loanExists, showBorrowSignerCell, @@ -52,7 +52,7 @@ const TableRowContent = ({ rChainId, rOwmId: owmId, owmId, - owmDataCachedOrApi, + market, userActiveKey, filterTypeKey, isBold: false, diff --git a/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowContainer.tsx b/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowContainer.tsx index 4b2336a36..badf57e23 100644 --- a/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowContainer.tsx +++ b/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowContainer.tsx @@ -9,9 +9,10 @@ import useStore from '@/store/useStore' import TableRow from '@/components/PageMarketList/components/TableRowViewContentTable/TableRow' import TableRowMobile from '@/components/PageMarketList/components/TableRowViewContentTable/TableRowMobile' +import { useOneWayMarket } from '@/entities/chain' const TableRowContainer = ( - props: Omit + props: Omit ) => { const { rChainId, api, owmId, filterTypeKey } = props @@ -21,14 +22,10 @@ const TableRowContainer = ( const isMdUp = useStore((state) => state.layout.isMdUp) const loansExistsMapper = useStore((state) => state.user.loansExistsMapper) const marketsBalancesMapper = useStore((state) => state.user.marketsBalancesMapper) - const owmDatasCachedMapper = useStore((state) => state.storeCache.owmDatasMapper[rChainId]) - const owmDatasMapper = useStore((state) => state.markets.owmDatasMapper[rChainId]) const setMarketsStateByKey = useStore((state) => state.markets.setStateByKey) - const owmDataCached = owmDatasCachedMapper?.[owmId] - const owmData = owmDatasMapper?.[owmId] - const owmDataCachedOrApi = owmData ?? owmDataCached - const userActiveKey = helpers.getUserActiveKey(api, owmDataCachedOrApi) + const market = useOneWayMarket(rChainId, owmId)?.data! + const userActiveKey = helpers.getUserActiveKey(api, market) const loanExists = loansExistsMapper[userActiveKey]?.loanExists ?? false const handleCellClick = (target?: EventTarget) => { @@ -54,7 +51,7 @@ const TableRowContainer = ( const tableRowProps: TableRowProps = { ...props, - owmDataCachedOrApi, + market, loanExists, userActiveKey, handleCellClick, diff --git a/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowMobile.tsx b/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowMobile.tsx index 3fe05045d..4c51681a7 100644 --- a/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowMobile.tsx +++ b/apps/lend/src/components/PageMarketList/components/TableRowViewContentTable/TableRowMobile.tsx @@ -39,7 +39,7 @@ const TableRowContent = ({ rChainId, api, owmId, - owmDataCachedOrApi, + market, filterTypeKey, loanExists, userActiveKey, @@ -51,7 +51,7 @@ const TableRowContent = ({ const [showDetail, setShowDetail] = useState('') const { signerAddress } = api ?? {} - const { borrowed_token } = owmDataCachedOrApi?.owm ?? {} + const { borrowed_token } = market ?? {} const isHideDetail = showDetail === owmId const showMyVaultCell = !!signerAddress && typeof userVaultShares !== 'undefined' && +userVaultShares > 0 @@ -60,7 +60,7 @@ const TableRowContent = ({ rChainId, rOwmId: owmId, owmId, - owmDataCachedOrApi, + market, userActiveKey, filterTypeKey, isBold: false, diff --git a/apps/lend/src/components/PageMarketList/index.tsx b/apps/lend/src/components/PageMarketList/index.tsx index 826ee8a00..3208932da 100644 --- a/apps/lend/src/components/PageMarketList/index.tsx +++ b/apps/lend/src/components/PageMarketList/index.tsx @@ -25,7 +25,6 @@ const MarketList = (pageProps: PageMarketList) => { const loansExistsMapper = useStore((state) => state.user.loansExistsMapper) const userMarketsBalances = useStore((state) => state.user.marketsBalancesMapper) const results = useStore((state) => state.marketList.result) - const resultCached = useStore((state) => state.storeCache.marketListResult[activeKey]) const setFormValues = useStore((state) => state.marketList.setFormValues) const { initCampaignRewards, initiated } = useStore((state) => state.campaigns) @@ -64,7 +63,7 @@ const MarketList = (pageProps: PageMarketList) => { } const parsedResult = - results[activeKey] ?? resultCached ?? (activeKey.charAt(0) === prevActiveKey.charAt(0) && results[prevActiveKey]) + results[activeKey] ?? (activeKey.charAt(0) === prevActiveKey.charAt(0) && results[prevActiveKey]) const updateFormValues = useCallback( (shouldRefetch?: boolean) => { diff --git a/apps/lend/src/components/PageMarketList/utils.ts b/apps/lend/src/components/PageMarketList/utils.ts index c2071c520..18447a9ad 100644 --- a/apps/lend/src/components/PageMarketList/utils.ts +++ b/apps/lend/src/components/PageMarketList/utils.ts @@ -53,11 +53,11 @@ export function _searchByTokensAddresses(parsedSearchText: string, searchText: s )) } -export function _getMarketList(owmDatas: OneWayMarketTemplate[]) { +export function _getMarketList(markets: OneWayMarketTemplate[]) { let marketListMapper: MarketListMapper = {} let marketListMapperCache: { [tokenAddress: string]: { symbol: string; address: string } } = {} - owmDatas.forEach(({ id, collateral_token, borrowed_token }) => { + markets.forEach(({ id, collateral_token, borrowed_token }) => { const { address: cAddress, symbol: cSymbol } = collateral_token const { address: bAddress, symbol: bSymbol } = borrowed_token @@ -72,7 +72,7 @@ export function _getMarketList(owmDatas: OneWayMarketTemplate[]) { }) // filter crvusd - const crvUsdAddress = owmDatas.map((m) => m.borrowed_token).find(({ symbol }) => symbol.toLowerCase() === 'crvusd')?.address + const crvUsdAddress = markets.map((m) => m.borrowed_token).find(({ symbol }) => symbol.toLowerCase() === 'crvusd')?.address if (crvUsdAddress) { delete marketListMapper[crvUsdAddress] delete marketListMapperCache[crvUsdAddress] diff --git a/apps/lend/src/components/SharedCellData/CellToken.tsx b/apps/lend/src/components/SharedCellData/CellToken.tsx index bb296fd1d..d75d8a78c 100644 --- a/apps/lend/src/components/SharedCellData/CellToken.tsx +++ b/apps/lend/src/components/SharedCellData/CellToken.tsx @@ -20,7 +20,7 @@ const CellToken = ({ hideIcon?: boolean rChainId: ChainId isVisible?: boolean - market: OneWayMarketTemplate + market?: OneWayMarketTemplate showLeverageIcon?: boolean type: 'collateral' | 'borrowed' module: 'borrow' | 'supply' diff --git a/apps/lend/src/components/SharedCellData/CellTotalCollateralValue.tsx b/apps/lend/src/components/SharedCellData/CellTotalCollateralValue.tsx index 43cc88d45..c5f54f8cc 100644 --- a/apps/lend/src/components/SharedCellData/CellTotalCollateralValue.tsx +++ b/apps/lend/src/components/SharedCellData/CellTotalCollateralValue.tsx @@ -5,19 +5,20 @@ import { formatNumber } from '@/ui/utils' import useStore from '@/store/useStore' import TextCaption from '@/ui/TextCaption' +import { useOneWayMarket } from '@/entities/chain' const CellTotalCollateralValue = ({ rChainId, rOwmId }: { rChainId: ChainId; rOwmId: string }) => { + const market = useOneWayMarket(rChainId, rOwmId)?.data const isAdvanceMode = useStore((state) => state.isAdvanceMode) - const owmData = useStore((state) => state.markets.owmDatasMapper[rChainId]?.[rOwmId]) const totalCollateralValue = useStore((state) => state.markets.totalCollateralValuesMapper[rChainId]?.[rOwmId]) const fetchTotalCollateralValue = useStore((state) => state.markets.fetchTotalCollateralValue) const { total = null, tooltipContent = [], error } = totalCollateralValue ?? {} useEffect(() => { - if (owmData) fetchTotalCollateralValue(rChainId, owmData) + if (market) fetchTotalCollateralValue(rChainId, market) // eslint-disable-next-line react-hooks/exhaustive-deps - }, [rChainId, owmData]) + }, [rChainId, market]) return ( <> diff --git a/apps/lend/src/store/createLoanBorrowMoreSlice.ts b/apps/lend/src/store/createLoanBorrowMoreSlice.ts index 8f7a5a9c0..eaac64ce4 100644 --- a/apps/lend/src/store/createLoanBorrowMoreSlice.ts +++ b/apps/lend/src/store/createLoanBorrowMoreSlice.ts @@ -69,7 +69,7 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor [sliceKey]: { ...DEFAULT_STATE, - fetchMaxRecv: async (activeKeyMax, api, owmData, isLeverage) => { + fetchMaxRecv: async (activeKeyMax, api, market, isLeverage) => { const { maxRecv, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api const { userCollateral, userBorrowed } = formValues @@ -80,11 +80,11 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor if (typeof updatedMaxRecv === 'undefined') { if (isLeverage) { - const resp = await loanBorrowMore.maxRecvLeverage(owmData, activeKeyMax, userCollateral, userBorrowed) + const resp = await loanBorrowMore.maxRecvLeverage(market, activeKeyMax, userCollateral, userBorrowed) updatedMaxRecv = resp.maxRecv?.maxDebt ?? '' sliceState.setStateByActiveKey('maxRecv', resp.activeKey, updatedMaxRecv) } else { - const resp = await loanBorrowMore.maxRecv(owmData, activeKeyMax, userCollateral) + const resp = await loanBorrowMore.maxRecv(market, activeKeyMax, userCollateral) updatedMaxRecv = resp.maxRecv sliceState.setStateByActiveKey('maxRecv', resp.activeKey, updatedMaxRecv) } @@ -94,12 +94,12 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor const debtError = isTooMuch(formValues.debt, updatedMaxRecv) ? 'too-much' : formValues.debtError sliceState.setStateByKey('formValues', { ...formValues, debtError }) }, - refetchMaxRecv: async (owmData, isLeverage) => { + refetchMaxRecv: async (market, isLeverage) => { const { api } = get() const { activeKeyMax, formValues, ...sliceState } = get()[sliceKey] const { userCollateral, userBorrowed } = formValues - if (!owmData || !api) return '' + if (!market || !api) return '' const { signerAddress } = api @@ -109,17 +109,17 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor sliceState.setStateByActiveKey('maxRecv', activeKeyMax, '') if (isLeverage) { - const resp = await loanBorrowMore.maxRecvLeverage(owmData, activeKeyMax, userCollateral, userBorrowed) + const resp = await loanBorrowMore.maxRecvLeverage(market, activeKeyMax, userCollateral, userBorrowed) const maxDebt = resp.maxRecv?.maxDebt ?? '' sliceState.setStateByActiveKey('maxRecv', resp.activeKey, maxDebt) return maxDebt } else { - const resp = await loanBorrowMore.maxRecv(owmData, activeKeyMax, userCollateral) + const resp = await loanBorrowMore.maxRecv(market, activeKeyMax, userCollateral) sliceState.setStateByActiveKey('maxRecv', resp.activeKey, resp.maxRecv) return resp.maxRecv } }, - fetchDetailInfo: async (activeKey, api, owmData, maxSlippage, isLeverage) => { + fetchDetailInfo: async (activeKey, api, market, maxSlippage, isLeverage) => { const { detailInfo, detailInfoLeverage, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api const { userCollateral, userBorrowed, debt } = formValues @@ -138,7 +138,7 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor const resp = await loanBorrowMore.detailInfoLeverage( activeKey, api, - owmData, + market, userCollateral, userBorrowed, debt, @@ -146,11 +146,11 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor ) sliceState.setStateByActiveKey('detailInfoLeverage', resp.activeKey, { ...resp.resp, error: resp.error }) } else { - const resp = await loanBorrowMore.detailInfo(activeKey, api, owmData, userCollateral, debt) + const resp = await loanBorrowMore.detailInfo(activeKey, api, market, userCollateral, debt) sliceState.setStateByActiveKey('detailInfo', resp.activeKey, { ...resp.resp, error: resp.error }) } }, - fetchEstGasApproval: async (activeKey, api, owmData, maxSlippage, isLeverage) => { + fetchEstGasApproval: async (activeKey, api, market, maxSlippage, isLeverage) => { const { gas } = get() const { formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api @@ -163,7 +163,7 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor await gas.fetchGasInfo(api) const resp = await loanBorrowMore.estGasApproval( activeKey, - owmData, + market, userCollateral, userBorrowed, debt, @@ -232,7 +232,7 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor }, // steps - fetchStepApprove: async (activeKey, api, owmData, formValues, maxSlippage, isLeverage) => { + fetchStepApprove: async (activeKey, api, market, formValues, maxSlippage, isLeverage) => { const { gas, wallet } = get() const sliceState = get()[sliceKey] const provider = wallet.getProvider(sliceKey) @@ -248,7 +248,7 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor const { error, ...resp } = await loanBorrowMore.approve( activeKey, provider, - owmData, + market, userCollateral, userBorrowed, isLeverage @@ -262,7 +262,7 @@ const createLoanBorrowMore = (_: SetState, get: GetState): LoanBor isApprovedCompleted: !error, stepError: error, }) - if (!error) sliceState.fetchEstGasApproval(activeKey, api, owmData, maxSlippage, isLeverage) + if (!error) sliceState.fetchEstGasApproval(activeKey, api, market, maxSlippage, isLeverage) return { ...resp, error } } }, diff --git a/apps/lend/src/store/createLoanCollateralAddSlice.ts b/apps/lend/src/store/createLoanCollateralAddSlice.ts index 363a299d3..a3db0beda 100644 --- a/apps/lend/src/store/createLoanCollateralAddSlice.ts +++ b/apps/lend/src/store/createLoanCollateralAddSlice.ts @@ -66,17 +66,17 @@ const createLoanCollateralAdd = (_: SetState, get: GetState): Loan [sliceKey]: { ...DEFAULT_STATE, - fetchDetailInfo: async (activeKey, api, owmData) => { + fetchDetailInfo: async (activeKey, api, market) => { const { formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api const { collateral } = formValues if (!signerAddress || +collateral <= 0) return - const resp = await loanCollateralAdd.detailInfo(activeKey, api, owmData, collateral) + const resp = await loanCollateralAdd.detailInfo(activeKey, api, market, collateral) sliceState.setStateByActiveKey('detailInfo', resp.activeKey, resp.resp) }, - fetchEstGasApproval: async (activeKey, api, owmData) => { + fetchEstGasApproval: async (activeKey, api, market) => { const { gas } = get() const { formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api @@ -86,7 +86,7 @@ const createLoanCollateralAdd = (_: SetState, get: GetState): Loan sliceState.setStateByKey('formEstGas', { [activeKey]: { ...DEFAULT_FORM_EST_GAS, loading: true } }) await gas.fetchGasInfo(api) - const resp = await loanCollateralAdd.estGasApproval(activeKey, owmData, collateral) + const resp = await loanCollateralAdd.estGasApproval(activeKey, market, collateral) sliceState.setStateByKey('formEstGas', { [resp.activeKey]: { estimatedGas: resp.estimatedGas, loading: false } }) // update formStatus @@ -123,7 +123,7 @@ const createLoanCollateralAdd = (_: SetState, get: GetState): Loan }, // step - fetchStepApprove: async (activeKey, api, owmData, formValues) => { + fetchStepApprove: async (activeKey, api, market, formValues) => { const { gas, wallet } = get() const sliceState = get()[sliceKey] const provider = wallet.getProvider(sliceKey) @@ -135,7 +135,7 @@ const createLoanCollateralAdd = (_: SetState, get: GetState): Loan // api calls await gas.fetchGasInfo(api) - const { error, ...resp } = await loanCollateralAdd.approve(activeKey, provider, owmData, formValues.collateral) + const { error, ...resp } = await loanCollateralAdd.approve(activeKey, provider, market, formValues.collateral) if (resp.activeKey === get()[sliceKey].activeKey) { // update formStatus @@ -145,7 +145,7 @@ const createLoanCollateralAdd = (_: SetState, get: GetState): Loan isApproved: !error, isInProgress: !error, }) - if (!error) sliceState.fetchEstGasApproval(activeKey, api, owmData) + if (!error) sliceState.fetchEstGasApproval(activeKey, api, market) return { ...resp, error } } }, diff --git a/apps/lend/src/store/createLoanCollateralRemoveSlice.ts b/apps/lend/src/store/createLoanCollateralRemoveSlice.ts index a29125ff0..3bd27d007 100644 --- a/apps/lend/src/store/createLoanCollateralRemoveSlice.ts +++ b/apps/lend/src/store/createLoanCollateralRemoveSlice.ts @@ -68,13 +68,13 @@ const createLoanCollateralRemove = (_: SetState, get: GetState): L [sliceKey]: { ...DEFAULT_STATE, - fetchMaxRemovable: async (api, owmData) => { + fetchMaxRemovable: async (api, market) => { const { formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api if (!signerAddress) return - const resp = await loanCollateralRemove.maxRemovable(owmData) + const resp = await loanCollateralRemove.maxRemovable(market) if (resp.error) sliceState.setStateByKey('formStatus', { ...formStatus, error: resp.error }) sliceState.setStateByKey('maxRemovable', resp.maxRemovable) @@ -82,17 +82,17 @@ const createLoanCollateralRemove = (_: SetState, get: GetState): L const collateralError = isTooMuch(formValues.collateral, resp.maxRemovable) ? 'too-much-max' : '' sliceState.setStateByKey('formValues', { ...formValues, collateralError }) }, - fetchDetailInfo: async (activeKey, api, owmData) => { + fetchDetailInfo: async (activeKey, api, market) => { const { formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api const { collateral, collateralError } = formValues if (!signerAddress || +collateral <= 0 || collateralError) return - const resp = await loanCollateralRemove.detailInfo(activeKey, api, owmData, collateral) + const resp = await loanCollateralRemove.detailInfo(activeKey, api, market, collateral) sliceState.setStateByActiveKey('detailInfo', resp.activeKey, resp.resp) }, - fetchEstGas: async (activeKey, api, owmData) => { + fetchEstGas: async (activeKey, api, market) => { const { gas } = get() const { formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api @@ -102,7 +102,7 @@ const createLoanCollateralRemove = (_: SetState, get: GetState): L sliceState.setStateByKey('formEstGas', { [activeKey]: { ...DEFAULT_FORM_EST_GAS, loading: true } }) await gas.fetchGasInfo(api) - const resp = await loanCollateralRemove.estGas(activeKey, owmData, collateral) + const resp = await loanCollateralRemove.estGas(activeKey, market, collateral) sliceState.setStateByKey('formEstGas', { [resp.activeKey]: { estimatedGas: resp.estimatedGas, loading: false } }) // update formStatus diff --git a/apps/lend/src/store/createLoanCreateSlice.ts b/apps/lend/src/store/createLoanCreateSlice.ts index 510660139..53103dec2 100644 --- a/apps/lend/src/store/createLoanCreateSlice.ts +++ b/apps/lend/src/store/createLoanCreateSlice.ts @@ -83,16 +83,16 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat [sliceKey]: { ...DEFAULT_STATE, - fetchMaxLeverage: async (owmData) => { + fetchMaxLeverage: async (market) => { const { formValues, ...sliceState } = get()[sliceKey] const { n } = formValues if (n === null) return - const resp = await loanCreate.maxLeverage(owmData, n) + const resp = await loanCreate.maxLeverage(market, n) sliceState.setStateByActiveKey('maxLeverage', n.toString(), resp.maxLeverage) }, - fetchMaxRecv: async (activeKey, api, owmData, isLeverage) => { + fetchMaxRecv: async (activeKey, api, market, isLeverage) => { const { maxRecv, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api const { userCollateral, userBorrowed, n } = formValues @@ -103,7 +103,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat if (n === null || !haveValues) return if (typeof updatedMaxRecv === 'undefined') { - const resp = await loanCreate.maxRecv(activeKey, owmData, userCollateral, userBorrowed, n, isLeverage) + const resp = await loanCreate.maxRecv(activeKey, market, userCollateral, userBorrowed, n, isLeverage) updatedMaxRecv = resp.maxRecv sliceState.setStateByActiveKey('maxRecv', resp.activeKey, resp.maxRecv) } @@ -114,18 +114,18 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat sliceState.setStateByKey('formValues', { ...formValues, debtError }) } }, - refetchMaxRecv: async (owmData, isLeverage) => { + refetchMaxRecv: async (market, isLeverage) => { const { activeKeyMax, formValues, ...sliceState } = get()[sliceKey] const { userCollateral, userBorrowed, n } = formValues - if (n === null || typeof owmData === 'undefined') return '' + if (n === null || typeof market === 'undefined') return '' sliceState.setStateByActiveKey('maxRecv', activeKeyMax, '') - const { maxRecv } = await loanCreate.maxRecv(activeKeyMax, owmData, userCollateral, userBorrowed, n, isLeverage) + const { maxRecv } = await loanCreate.maxRecv(activeKeyMax, market, userCollateral, userBorrowed, n, isLeverage) sliceState.setStateByActiveKey('maxRecv', activeKeyMax, maxRecv) return maxRecv }, - fetchDetailInfo: async (activeKey, api, owmData, maxSlippage, isLeverage) => { + fetchDetailInfo: async (activeKey, api, market, maxSlippage, isLeverage) => { const { detailInfo, detailInfoLeverage, formStatus, formValues, ...sliceState } = get()[sliceKey] const { userCollateral, userBorrowed, debt, n } = formValues const { haveValues, haveDebt } = _parseValue(formValues) @@ -142,7 +142,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat if (isLeverage) { const resp = await loanCreate.detailInfoLeverage( activeKey, - owmData, + market, userCollateral, userBorrowed, debt, @@ -152,12 +152,12 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat sliceState.setStateByActiveKey('detailInfoLeverage', resp.activeKey, { ...resp.resp, error: resp.error }) if (resp.error) sliceState.setStateByKey('formStatus', { ...formStatus, error: resp.error }) } else { - const resp = await loanCreate.detailInfo(activeKey, owmData, userCollateral, debt, n) + const resp = await loanCreate.detailInfo(activeKey, market, userCollateral, debt, n) sliceState.setStateByActiveKey('detailInfo', resp.activeKey, { ...resp.resp, error: resp.error }) if (resp.error) sliceState.setStateByKey('formStatus', { ...formStatus, error: resp.error }) } }, - fetchLiqRanges: async (activeKeyLiqRange, api, owmData, isLeverage) => { + fetchLiqRanges: async (activeKeyLiqRange, api, market, isLeverage) => { const { detailInfoLeverage, formValues, ...sliceState } = get()[sliceKey] const { userCollateral, userBorrowed, debt } = formValues const { totalCollateral } = detailInfoLeverage[activeKeyLiqRange]?.expectedCollateral ?? {} @@ -167,7 +167,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat const resp = await loanCreate.liqRanges( activeKeyLiqRange, - owmData, + market, totalCollateral, userCollateral, userBorrowed, @@ -177,7 +177,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat sliceState.setStateByKey('liqRanges', { [activeKeyLiqRange]: resp.liqRanges }) sliceState.setStateByKey('liqRangesMapper', { [activeKeyLiqRange]: resp.liqRangesMapper }) }, - fetchEstGasApproval: async (activeKey, api, owmData, maxSlippage, isLeverage) => { + fetchEstGasApproval: async (activeKey, api, market, maxSlippage, isLeverage) => { const { gas } = get() const { formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api @@ -190,7 +190,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat await gas.fetchGasInfo(api) const resp = await loanCreate.estGasApproval( activeKey, - owmData, + market, userCollateral, userBorrowed, debt, @@ -256,7 +256,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat }, // steps - fetchStepApprove: async (activeKey, api, owmData, maxSlippage, formValues, isLeverage) => { + fetchStepApprove: async (activeKey, api, market, maxSlippage, formValues, isLeverage) => { const { gas, wallet } = get() const { formStatus, ...sliceState } = get()[sliceKey] const provider = wallet.getProvider(sliceKey) @@ -272,7 +272,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat const { error, ...resp } = await loanCreate.approve( activeKey, provider, - owmData, + market, userCollateral, userBorrowed, isLeverage @@ -286,7 +286,7 @@ const createLoanCreate = (set: SetState, get: GetState): LoanCreat isApprovedCompleted: !error, stepError: error, }) - if (!error) sliceState.fetchEstGasApproval(activeKey, api, owmData, maxSlippage, isLeverage) + if (!error) sliceState.fetchEstGasApproval(activeKey, api, market, maxSlippage, isLeverage) return { ...resp, error } } }, diff --git a/apps/lend/src/store/createLoanRepaySlice.ts b/apps/lend/src/store/createLoanRepaySlice.ts index 465684475..7c81a0ee7 100644 --- a/apps/lend/src/store/createLoanRepaySlice.ts +++ b/apps/lend/src/store/createLoanRepaySlice.ts @@ -62,7 +62,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR [sliceKey]: { ...DEFAULT_STATE, - fetchDetailInfo: async (activeKey, api, owmData, maxSlippage, userState) => { + fetchDetailInfo: async (activeKey, api, market, maxSlippage, userState) => { const { detailInfo, detailInfoLeverage, formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api const { isFullRepay, userBorrowed, userCollateral, stateCollateral } = formValues @@ -83,7 +83,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR const resp = await loanRepay.detailInfoLeverage( activeKey, api, - owmData, + market, stateCollateral, userCollateral, userBorrowed, @@ -98,7 +98,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR respError = resp.error } } else { - const resp = await loanRepay.detailInfo(activeKey, api, owmData, userBorrowed, isFullRepay, userState.debt) + const resp = await loanRepay.detailInfo(activeKey, api, market, userBorrowed, isFullRepay, userState.debt) sliceState.setStateByActiveKey('detailInfo', resp.activeKey, { ...resp.resp, error: resp.error }) respError = resp.error } @@ -108,7 +108,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR sliceState.setStateByKey('formStatus', { ...formStatus, error: respError }) } }, - fetchEstGasApproval: async (activeKey, api, owmData, maxSlippage) => { + fetchEstGasApproval: async (activeKey, api, market, maxSlippage) => { const { gas } = get() const { formStatus, formValues, ...sliceState } = get()[sliceKey] const { signerAddress } = api @@ -122,7 +122,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR await gas.fetchGasInfo(api) const resp = await loanRepay.estGasApproval( activeKey, - owmData, + market, stateCollateral, userCollateral, userBorrowed, @@ -190,7 +190,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR }, // steps - fetchStepApprove: async (activeKey, api, owmData, formValues, maxSlippage) => { + fetchStepApprove: async (activeKey, api, market, formValues, maxSlippage) => { const { gas, wallet } = get() const sliceState = get()[sliceKey] const provider = wallet.getProvider(sliceKey) @@ -207,7 +207,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR const { error, ...resp } = await loanRepay.approve( activeKey, provider, - owmData, + market, stateCollateral, userCollateral, userBorrowed, @@ -223,7 +223,7 @@ const createLoanRepaySlice = (set: SetState, get: GetState): LoanR isApprovedCompleted: !error, stepError: error, }) - if (!error) sliceState.fetchEstGasApproval(activeKey, api, owmData, maxSlippage) + if (!error) sliceState.fetchEstGasApproval(activeKey, api, market, maxSlippage) return { ...resp, error } } }, diff --git a/apps/lend/src/store/createLoanSelfLiquidationSlice.ts b/apps/lend/src/store/createLoanSelfLiquidationSlice.ts index 0c34abddf..5a4f2f805 100644 --- a/apps/lend/src/store/createLoanSelfLiquidationSlice.ts +++ b/apps/lend/src/store/createLoanSelfLiquidationSlice.ts @@ -105,7 +105,7 @@ const createLoanSelfLiquidationSlice = (set: SetState, get: GetState { + fetchEstGasApproval: async (api, market, maxSlippage) => { const { gas } = get() const { formStatus, ...sliceState } = get()[sliceKey] const { signerAddress } = api @@ -115,7 +115,7 @@ const createLoanSelfLiquidationSlice = (set: SetState, get: GetState, get: GetState { + fetchStepApprove: async (api, market, maxSlippage) => { const { gas, wallet } = get() const { formStatus, ...sliceState } = get()[sliceKey] const provider = wallet.getProvider(sliceKey) @@ -139,7 +139,7 @@ const createLoanSelfLiquidationSlice = (set: SetState, get: GetState, get: GetState, get: GetState): Mark } return results.results }, - sortByUserData: (api, sortKey, owmData) => { + sortByUserData: (api, sortKey, market) => { const { user } = get() - const userActiveKey = helpers.getUserActiveKey(api, owmData) + const userActiveKey = helpers.getUserActiveKey(api, market) if (sortKey === 'myHealth') { return Number(user.loansHealthsMapper[userActiveKey]?.healthNotFull ?? 0) @@ -168,7 +168,7 @@ const createMarketListSlice = (set: SetState, get: GetState): Mark } else if (sortKey === 'totalCollateralValue') { return orderBy(markets, (market) => +(totalCollateralValuesMapper[market.id]?.total ?? '0'), [order]) } else if (sortKey.startsWith('my')) { - return orderBy(markets, (owmData) => sliceState.sortByUserData(api, sortKey, owmData), [order]) + return orderBy(markets, (market) => sliceState.sortByUserData(api, sortKey, market), [order]) } return markets diff --git a/apps/lend/src/store/createMarketsSlice.ts b/apps/lend/src/store/createMarketsSlice.ts index 67edd2277..e9b674241 100644 --- a/apps/lend/src/store/createMarketsSlice.ts +++ b/apps/lend/src/store/createMarketsSlice.ts @@ -29,7 +29,7 @@ const sliceKey = 'markets' export type MarketsSlice = { [sliceKey]: SliceState & { // grouped - fetchDatas(key: string, api: Api, owmDatas: OneWayMarketTemplate[], shouldRefetch?: boolean): Promise + fetchDatas(key: string, api: Api, markets: OneWayMarketTemplate[], shouldRefetch?: boolean): Promise // individual fetchAll(api: Api, OneWayMarketTemplate: OneWayMarketTemplate, shouldRefetch?: boolean): Promise @@ -63,7 +63,7 @@ const DEFAULT_STATE: SliceState = { const createMarketsSlice = (set: SetState, get: GetState): MarketsSlice => ({ [sliceKey]: { ...DEFAULT_STATE, - fetchDatas: async (key, api, owmDatas, shouldRefetch) => { + fetchDatas: async (key, api, markets, shouldRefetch) => { const { ...sliceState } = get()[sliceKey] const { chainId } = api @@ -85,14 +85,14 @@ const createMarketsSlice = (set: SetState, get: GetState): Markets // stored const k = key as keyof typeof fnMapper const storedMapper = get()[sliceKey][k][chainId] ?? {} - const missing = owmDatas.filter(({ id }) => typeof storedMapper[id] === 'undefined') + const missing = markets.filter(({ id }) => typeof storedMapper[id] === 'undefined') if (missing.length === 0 && !shouldRefetch) return const resp = k === 'totalCollateralValuesMapper' - ? await fnMapper[k](api, shouldRefetch ? owmDatas : missing) - : await fnMapper[k](shouldRefetch ? owmDatas : missing) + ? await fnMapper[k](api, shouldRefetch ? markets : missing) + : await fnMapper[k](shouldRefetch ? markets : missing) const cMapper = { ...storedMapper } Object.keys(resp).forEach((owmId) => { cMapper[owmId] = resp[owmId] diff --git a/apps/lend/src/store/createUserSlice.ts b/apps/lend/src/store/createUserSlice.ts index 80aed2cc5..6adbceda0 100644 --- a/apps/lend/src/store/createUserSlice.ts +++ b/apps/lend/src/store/createUserSlice.ts @@ -30,11 +30,11 @@ export type UserSlice = { fetchUsersLoansExists(api: Api, markets: OneWayMarketTemplate[], shouldRefetch?: boolean): Promise // individual - fetchUserLoanExists(api: Api, owmData: OneWayMarketTemplate, shouldRefetch?: boolean): Promise - fetchUserLoanDetails(api: Api, owmData: OneWayMarketTemplate, shouldRefetch?: boolean): Promise - fetchUserMarketBalances(api: Api, owmData: OneWayMarketTemplate, shouldRefetch?: boolean): Promise - fetchUserLoanState(api: Api, owmData: OneWayMarketTemplate, shouldRefetch?: boolean): Promise - fetchAll(api: Api, owmData: OneWayMarketTemplate, shouldRefetch?: boolean): Promise<{ userLoanDetailsResp: UserLoanDetails | null; userLoanBalancesResp: UserMarketBalances; }> + fetchUserLoanExists(api: Api, market: OneWayMarketTemplate, shouldRefetch?: boolean): Promise + fetchUserLoanDetails(api: Api, market: OneWayMarketTemplate, shouldRefetch?: boolean): Promise + fetchUserMarketBalances(api: Api, market: OneWayMarketTemplate, shouldRefetch?: boolean): Promise + fetchUserLoanState(api: Api, market: OneWayMarketTemplate, shouldRefetch?: boolean): Promise + fetchAll(api: Api, market: OneWayMarketTemplate, shouldRefetch?: boolean): Promise<{ userLoanDetailsResp: UserLoanDetails | null; userLoanBalancesResp: UserMarketBalances; }> // helpers setStateByActiveKey(key: StateKey, activeKey: string, value: T): void @@ -69,8 +69,8 @@ const createUserSlice = (set: SetState, get: GetState): UserSlice // stored const k = key as keyof typeof fnMapper const storedMapper = get()[sliceKey][k] ?? {} - const missing = markets.filter((owmData) => { - const userActiveKey = helpers.getUserActiveKey(api, owmData) + const missing = markets.filter((market) => { + const userActiveKey = helpers.getUserActiveKey(api, market) return typeof storedMapper[userActiveKey] === 'undefined' }) @@ -99,8 +99,8 @@ const createUserSlice = (set: SetState, get: GetState): UserSlice const loansExistsMapper = get()[sliceKey].loansExistsMapper ?? {} const storedMapper = get()[sliceKey][k] ?? {} - const missing = markets.filter((owmData) => { - const userActiveKey = helpers.getUserActiveKey(api, owmData) + const missing = markets.filter((market) => { + const userActiveKey = helpers.getUserActiveKey(api, market) const { loanExists } = loansExistsMapper[userActiveKey] ?? {} return loanExists && typeof storedMapper[userActiveKey] === 'undefined' }) @@ -111,8 +111,8 @@ const createUserSlice = (set: SetState, get: GetState): UserSlice // get only markets with loans if (shouldRefetch) { - parsedOwmDatas = markets.filter((owmData) => { - const userActiveKey = helpers.getUserActiveKey(api, owmData) + parsedOwmDatas = markets.filter((market) => { + const userActiveKey = helpers.getUserActiveKey(api, market) return loansExistsMapper[userActiveKey]?.loanExists }) } @@ -130,8 +130,8 @@ const createUserSlice = (set: SetState, get: GetState): UserSlice fetchUsersLoansExists: async (api, markets, shouldRefetch) => { const storedLoanExistsMapper = get()[sliceKey].loansExistsMapper - const missing = markets.filter((owmData) => { - const userActiveKey = helpers.getUserActiveKey(api, owmData) + const missing = markets.filter((market) => { + const userActiveKey = helpers.getUserActiveKey(api, market) return typeof storedLoanExistsMapper[userActiveKey] === 'undefined' }) @@ -147,34 +147,34 @@ const createUserSlice = (set: SetState, get: GetState): UserSlice }, // individual - fetchUserLoanExists: async (api, owmData, shouldRefetch) => { - await get()[sliceKey].fetchUsersLoansExists(api, [owmData], shouldRefetch) - const userActiveKey = helpers.getUserActiveKey(api, owmData) + fetchUserLoanExists: async (api, market, shouldRefetch) => { + await get()[sliceKey].fetchUsersLoansExists(api, [market], shouldRefetch) + const userActiveKey = helpers.getUserActiveKey(api, market) return get()[sliceKey].loansExistsMapper[userActiveKey] }, - fetchUserLoanDetails: async (api, owmData, shouldRefetch) => { + fetchUserLoanDetails: async (api, market, shouldRefetch) => { const key = 'loansDetailsMapper' - await get()[sliceKey].fetchDatas(key, api, [owmData], shouldRefetch) - const userActiveKey = helpers.getUserActiveKey(api, owmData) + await get()[sliceKey].fetchDatas(key, api, [market], shouldRefetch) + const userActiveKey = helpers.getUserActiveKey(api, market) return get()[sliceKey][key][userActiveKey] }, - fetchUserLoanState: async (api, owmData, shouldRefetch) => { + fetchUserLoanState: async (api, market, shouldRefetch) => { const key = 'loansStatesMapper' - await get()[sliceKey].fetchLoanDatas(key, api, [owmData], shouldRefetch) - const userActiveKey = helpers.getUserActiveKey(api, owmData) + await get()[sliceKey].fetchLoanDatas(key, api, [market], shouldRefetch) + const userActiveKey = helpers.getUserActiveKey(api, market) return get()[sliceKey][key][userActiveKey] }, - fetchUserMarketBalances: async (api, owmData, shouldRefetch) => { + fetchUserMarketBalances: async (api, market, shouldRefetch) => { const key = 'marketsBalancesMapper' - await get()[sliceKey].fetchDatas(key, api, [owmData], shouldRefetch) - const userActiveKey = helpers.getUserActiveKey(api, owmData) + await get()[sliceKey].fetchDatas(key, api, [market], shouldRefetch) + const userActiveKey = helpers.getUserActiveKey(api, market) return get()[sliceKey][key][userActiveKey] }, - fetchAll: async (api, owmData, shouldRefetch) => { - const userActiveKey = helpers.getUserActiveKey(api, owmData) + fetchAll: async (api, market, shouldRefetch) => { + const userActiveKey = helpers.getUserActiveKey(api, market) const keys = ['loansDetailsMapper', 'marketsBalancesMapper'] as const - await Promise.all(keys.map((key) => get()[sliceKey].fetchLoanDatas(key, api, [owmData], shouldRefetch))) + await Promise.all(keys.map((key) => get()[sliceKey].fetchLoanDatas(key, api, [market], shouldRefetch))) return { userLoanDetailsResp: get()[sliceKey].loansDetailsMapper[userActiveKey], userLoanBalancesResp: get()[sliceKey].marketsBalancesMapper[userActiveKey],