Auto Lend Switch (#660)
* refactor: moved the autolend logic to the switch * tidy: update default value * adjust logic of auto-lend for switch component --------- Co-authored-by: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com>
This commit is contained in:
parent
88a7c27a28
commit
befb34de6c
@ -11,7 +11,6 @@ import WalletBridges from 'components/Wallet/WalletBridges'
|
|||||||
import { DEFAULT_SETTINGS } from 'constants/defaultSettings'
|
import { DEFAULT_SETTINGS } from 'constants/defaultSettings'
|
||||||
import { LocalStorageKeys } from 'constants/localStorageKeys'
|
import { LocalStorageKeys } from 'constants/localStorageKeys'
|
||||||
import { BN_ZERO } from 'constants/math'
|
import { BN_ZERO } from 'constants/math'
|
||||||
import useAutoLend from 'hooks/useAutoLend'
|
|
||||||
import useLocalStorage from 'hooks/useLocalStorage'
|
import useLocalStorage from 'hooks/useLocalStorage'
|
||||||
import useMarketAssets from 'hooks/useMarketAssets'
|
import useMarketAssets from 'hooks/useMarketAssets'
|
||||||
import useToggle from 'hooks/useToggle'
|
import useToggle from 'hooks/useToggle'
|
||||||
@ -34,20 +33,17 @@ interface Props {
|
|||||||
|
|
||||||
export default function AccountFundContent(props: Props) {
|
export default function AccountFundContent(props: Props) {
|
||||||
const deposit = useStore((s) => s.deposit)
|
const deposit = useStore((s) => s.deposit)
|
||||||
const accounts = useStore((s) => s.accounts)
|
|
||||||
const walletAssetModal = useStore((s) => s.walletAssetsModal)
|
const walletAssetModal = useStore((s) => s.walletAssetsModal)
|
||||||
const [isConfirming, setIsConfirming] = useState(false)
|
const [isConfirming, setIsConfirming] = useState(false)
|
||||||
const [lendAssets, setLendAssets] = useLocalStorage<boolean>(
|
const [lendAssets] = useLocalStorage<boolean>(
|
||||||
LocalStorageKeys.LEND_ASSETS,
|
LocalStorageKeys.LEND_ASSETS,
|
||||||
DEFAULT_SETTINGS.lendAssets,
|
DEFAULT_SETTINGS.lendAssets,
|
||||||
)
|
)
|
||||||
const [fundingAssets, setFundingAssets] = useState<BNCoin[]>([])
|
const [fundingAssets, setFundingAssets] = useState<BNCoin[]>([])
|
||||||
const { data: marketAssets } = useMarketAssets()
|
const { data: marketAssets } = useMarketAssets()
|
||||||
const { data: walletBalances } = useWalletBalances(props.address)
|
const { data: walletBalances } = useWalletBalances(props.address)
|
||||||
const { autoLendEnabledAccountIds } = useAutoLend()
|
|
||||||
const [isLending, toggleIsLending] = useToggle(lendAssets)
|
const [isLending, toggleIsLending] = useToggle(lendAssets)
|
||||||
const { simulateDeposits } = useUpdatedAccount(props.account)
|
const { simulateDeposits } = useUpdatedAccount(props.account)
|
||||||
const autoLendEnabled = autoLendEnabledAccountIds.includes(props.accountId)
|
|
||||||
const baseAsset = getBaseAsset()
|
const baseAsset = getBaseAsset()
|
||||||
|
|
||||||
const hasAssetSelected = fundingAssets.length > 0
|
const hasAssetSelected = fundingAssets.length > 0
|
||||||
@ -134,14 +130,6 @@ export default function AccountFundContent(props: Props) {
|
|||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
toggleIsLending(autoLendEnabled)
|
|
||||||
}, [autoLendEnabled, toggleIsLending])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (accounts?.length === 1 && isLending) setLendAssets(true)
|
|
||||||
}, [isLending, accounts, lendAssets, setLendAssets])
|
|
||||||
|
|
||||||
const depositCapReachedCoins = useMemo(() => {
|
const depositCapReachedCoins = useMemo(() => {
|
||||||
const depositCapReachedCoins: BNCoin[] = []
|
const depositCapReachedCoins: BNCoin[] = []
|
||||||
fundingAssets.forEach((asset) => {
|
fundingAssets.forEach((asset) => {
|
||||||
@ -205,8 +193,6 @@ export default function AccountFundContent(props: Props) {
|
|||||||
<SwitchAutoLend
|
<SwitchAutoLend
|
||||||
className='pt-4 mt-4 border border-transparent border-t-white/10'
|
className='pt-4 mt-4 border border-transparent border-t-white/10'
|
||||||
accountId={props.accountId}
|
accountId={props.accountId}
|
||||||
value={!props.isFullPage ? isLending : undefined}
|
|
||||||
onChange={!props.isFullPage ? toggleIsLending : undefined}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
|
@ -1,32 +1,51 @@
|
|||||||
import classNames from 'classnames'
|
import classNames from 'classnames'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
import SwitchWithLabel from 'components/Switch/SwitchWithLabel'
|
import SwitchWithLabel from 'components/Switch/SwitchWithLabel'
|
||||||
|
import { DEFAULT_SETTINGS } from 'constants/defaultSettings'
|
||||||
|
import { LocalStorageKeys } from 'constants/localStorageKeys'
|
||||||
import useAutoLend from 'hooks/useAutoLend'
|
import useAutoLend from 'hooks/useAutoLend'
|
||||||
|
import useLocalStorage from 'hooks/useLocalStorage'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
accountId: string
|
accountId: string
|
||||||
className?: string
|
className?: string
|
||||||
onChange?: () => void
|
|
||||||
value?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function SwitchAutoLend(props: Props) {
|
export default function SwitchAutoLend(props: Props) {
|
||||||
const { accountId, className } = props
|
const { accountId, className } = props
|
||||||
const { autoLendEnabledAccountIds, toggleAutoLend } = useAutoLend()
|
const { autoLendEnabledAccountIds, disableAutoLend, enableAutoLend } = useAutoLend()
|
||||||
const isAutoLendEnabled = autoLendEnabledAccountIds.includes(accountId)
|
const isAutoLendEnabledForAccount = autoLendEnabledAccountIds.includes(accountId)
|
||||||
|
const [isAutoLendEnabled, setIsAutoLendEnabled] = useLocalStorage<boolean>(
|
||||||
|
LocalStorageKeys.LEND_ASSETS,
|
||||||
|
DEFAULT_SETTINGS.lendAssets,
|
||||||
|
)
|
||||||
|
|
||||||
function handleToggle() {
|
const handleToggle = useCallback(() => {
|
||||||
if (props.onChange) return props.onChange()
|
if (!isAutoLendEnabledForAccount) {
|
||||||
|
enableAutoLend(accountId)
|
||||||
toggleAutoLend(accountId)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isAutoLendEnabled) {
|
||||||
|
setIsAutoLendEnabled(false)
|
||||||
|
disableAutoLend(accountId)
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
accountId,
|
||||||
|
disableAutoLend,
|
||||||
|
enableAutoLend,
|
||||||
|
isAutoLendEnabled,
|
||||||
|
isAutoLendEnabledForAccount,
|
||||||
|
setIsAutoLendEnabled,
|
||||||
|
])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames('w-full', className)}>
|
<div className={classNames('w-full', className)}>
|
||||||
<SwitchWithLabel
|
<SwitchWithLabel
|
||||||
name='isLending'
|
name={`isLending-${accountId}`}
|
||||||
label='Lend assets to earn yield'
|
label='Lend assets to earn yield'
|
||||||
value={props.value !== undefined ? props.value : isAutoLendEnabled}
|
value={isAutoLendEnabledForAccount}
|
||||||
onChange={handleToggle}
|
onChange={handleToggle}
|
||||||
tooltip={`Fund your account and lend assets effortlessly! By lending, you'll earn attractive interest (APY) without impacting your loan to value (LTV). It's a win-win situation - don't miss out on this easy opportunity to grow your holdings!`}
|
tooltip={`Fund your account and lend assets effortlessly! By lending, you'll earn attractive interest (APY) without impacting your loan to value (LTV). It's a win-win situation - don't miss out on this easy opportunity to grow your holdings!`}
|
||||||
/>
|
/>
|
||||||
|
@ -5,7 +5,8 @@ import useStore from 'store'
|
|||||||
|
|
||||||
export default function useAutoLend(): {
|
export default function useAutoLend(): {
|
||||||
autoLendEnabledAccountIds: string[]
|
autoLendEnabledAccountIds: string[]
|
||||||
toggleAutoLend: (accountId: string) => void
|
enableAutoLend: (accountId: string) => void
|
||||||
|
disableAutoLend: (accountId: string) => void
|
||||||
isAutoLendEnabledForCurrentAccount: boolean
|
isAutoLendEnabledForCurrentAccount: boolean
|
||||||
setAutoLendOnAllAccounts: (lendAssets: boolean) => void
|
setAutoLendOnAllAccounts: (lendAssets: boolean) => void
|
||||||
enableAutoLendAccountId: (accountId: string) => void
|
enableAutoLendAccountId: (accountId: string) => void
|
||||||
@ -17,13 +18,15 @@ export default function useAutoLend(): {
|
|||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
|
||||||
const toggleAutoLend = (accountId: string) => {
|
const enableAutoLend = (accountId: string) => {
|
||||||
const setOfAccountIds = new Set(autoLendEnabledAccountIds)
|
const setOfAccountIds = new Set(autoLendEnabledAccountIds)
|
||||||
|
setOfAccountIds.add(accountId)
|
||||||
|
setAutoLendEnabledAccountIds(Array.from(setOfAccountIds))
|
||||||
|
}
|
||||||
|
|
||||||
setOfAccountIds.has(accountId)
|
const disableAutoLend = (accountId: string) => {
|
||||||
? setOfAccountIds.delete(accountId)
|
const setOfAccountIds = new Set(autoLendEnabledAccountIds)
|
||||||
: setOfAccountIds.add(accountId)
|
setOfAccountIds.delete(accountId)
|
||||||
|
|
||||||
setAutoLendEnabledAccountIds(Array.from(setOfAccountIds))
|
setAutoLendEnabledAccountIds(Array.from(setOfAccountIds))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +48,8 @@ export default function useAutoLend(): {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
autoLendEnabledAccountIds,
|
autoLendEnabledAccountIds,
|
||||||
toggleAutoLend,
|
enableAutoLend,
|
||||||
|
disableAutoLend,
|
||||||
isAutoLendEnabledForCurrentAccount,
|
isAutoLendEnabledForCurrentAccount,
|
||||||
setAutoLendOnAllAccounts,
|
setAutoLendOnAllAccounts,
|
||||||
enableAutoLendAccountId,
|
enableAutoLendAccountId,
|
||||||
|
@ -46,6 +46,7 @@ export default function Layout({ children }: { children: React.ReactNode }) {
|
|||||||
LocalStorageKeys.REDUCE_MOTION,
|
LocalStorageKeys.REDUCE_MOTION,
|
||||||
DEFAULT_SETTINGS.reduceMotion,
|
DEFAULT_SETTINGS.reduceMotion,
|
||||||
)
|
)
|
||||||
|
|
||||||
const accountDetailsExpanded = useStore((s) => s.accountDetailsExpanded)
|
const accountDetailsExpanded = useStore((s) => s.accountDetailsExpanded)
|
||||||
const isFullWidth =
|
const isFullWidth =
|
||||||
location.pathname.includes('trade') ||
|
location.pathname.includes('trade') ||
|
||||||
@ -64,7 +65,10 @@ export default function Layout({ children }: { children: React.ReactNode }) {
|
|||||||
'lg:mt-[73px]',
|
'lg:mt-[73px]',
|
||||||
'flex',
|
'flex',
|
||||||
'min-h-screen gap-6 px-4 py-6 w-full relative',
|
'min-h-screen gap-6 px-4 py-6 w-full relative',
|
||||||
isFullWidth && accountId && (accountDetailsExpanded ? 'pr-118' : 'pr-24'),
|
!focusComponent &&
|
||||||
|
isFullWidth &&
|
||||||
|
accountId &&
|
||||||
|
(accountDetailsExpanded ? 'pr-118' : 'pr-24'),
|
||||||
!reduceMotion && isFullWidth && 'transition-all duration-300',
|
!reduceMotion && isFullWidth && 'transition-all duration-300',
|
||||||
'justify-center',
|
'justify-center',
|
||||||
focusComponent && 'items-center',
|
focusComponent && 'items-center',
|
||||||
|
Loading…
Reference in New Issue
Block a user