Skip to content

Commit

Permalink
Improve objectives creation at savings deposit dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskar committed Jun 18, 2024
1 parent 5d73726 commit ab1cb84
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 54 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NormalizedUnitNumber } from '@/domain/types/NumericValues'
import { Token } from '@/domain/types/Token'

export interface NativeXDaiDepositObjective {
type: 'nativeXDaiDeposit'
xDai: Token
value: NormalizedUnitNumber
}

export interface NativeXDaiDepositAction {
type: 'nativeXDaiDeposit'
xDai: Token
value: NormalizedUnitNumber
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { ActionHandler } from '../../logic/types'
import { mapWriteResultToActionState } from '../../logic/utils'
import { NativeXDaiDepositAction } from './types'

export interface UseCreateNativeSexyDaiDepositHandlerOptions {
export interface UseCreateNativeXDaiDepositHandlerOptions {
enabled: boolean
onFinish?: () => void
}

export function useCreateNativeSexyDaiDepositHandler(
export function useCreateNativeXDaiDepositHandler(
action: NativeXDaiDepositAction,
options: UseCreateNativeSexyDaiDepositHandlerOptions,
options: UseCreateNativeXDaiDepositHandlerOptions,
): ActionHandler {
const { enabled, onFinish } = options

const deposit = useSexyDaiDeposit({
value: action.value,
value: action.xDai.toBaseUnit(action.value),
enabled,
onTransactionSettled: onFinish,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/features/actions/logic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ExchangeAction, ExchangeObjective } from '../flavours/exchange/types'
import { NativeDaiDepositAction, NativeDaiDepositObjective } from '../flavours/native-dai-deposit/types'
import { NativeSDaiDepositAction, NativeSDaiDepositObjective } from '../flavours/native-sdai-deposit/types'
import { NativeSDaiWithdrawAction, NativeSDaiWithdrawObjective } from '../flavours/native-sdai-withdraw/types'
import { NativeXDaiDepositAction, NativeXDaiDepositObjective } from '../flavours/native-sexy-dai-deposit/types'
import { NativeUSDCDepositAction, NativeUSDCDepositObjective } from '../flavours/native-usdc-deposit/types'
import { NativeXDaiDepositAction, NativeXDaiDepositObjective } from '../flavours/native-xdai-deposit/types'
import { RepayAction, RepayObjective } from '../flavours/repay/types'
import { SetUseAsCollateralAction, SetUseAsCollateralObjective } from '../flavours/set-use-as-collateral/types'
import { SetUserEModeAction, SetUserEModeObjective } from '../flavours/set-user-e-mode/types'
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/features/actions/logic/useActionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { useCreateExchangeHandler } from '../flavours/exchange/useCreateExchange
import { useCreateNativeDaiDepositHandler } from '../flavours/native-dai-deposit/useCreateNativeDaiDepositHandler'
import { useCreateNativeSDaiDepositHandler } from '../flavours/native-sdai-deposit/useCreateNativeSDaiDepositHandler'
import { useCreateNativeSDaiWithdrawHandler } from '../flavours/native-sdai-withdraw/useCreateNativeSDaiWithdrawHandler'
import { useCreateNativeSexyDaiDepositHandler } from '../flavours/native-sexy-dai-deposit/useCreateNativeSexyDaiDepositHandler'
import { useCreateNativeUSDCDepositHandler } from '../flavours/native-usdc-deposit/useCreateNativeUSDCDepositHandler'
import { useCreateNativeXDaiDepositHandler } from '../flavours/native-xdai-deposit/useCreateNativeXDaiDepositHandler'
import { useCreateRepayHandler } from '../flavours/repay/useCreateRepayHandler'
import { useCreateSetUseAsCollateralHandler } from '../flavours/set-use-as-collateral/useCreateSetUseAsCollateralHandler'
import { useCreateSetUserEModeHandler } from '../flavours/set-user-e-mode/useCreateSetUserEModeHandler'
Expand Down Expand Up @@ -128,6 +128,6 @@ function useCreateActionHandler(
return useCreateNativeUSDCDepositHandler(action, { enabled, onFinish })
case 'nativeXDaiDeposit':
// biome-ignore lint/correctness/useHookAtTopLevel:
return useCreateNativeSexyDaiDepositHandler(action, { enabled, onFinish })
return useCreateNativeXDaiDepositHandler(action, { enabled, onFinish })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { ExchangeAction } from '../flavours/exchange/types'
import { NativeDaiDepositAction } from '../flavours/native-dai-deposit/types'
import { NativeSDaiDepositAction } from '../flavours/native-sdai-deposit/types'
import { NativeSDaiWithdrawAction } from '../flavours/native-sdai-withdraw/types'
import { NativeXDaiDepositAction } from '../flavours/native-sexy-dai-deposit/types'
import { NativeUSDCDepositAction } from '../flavours/native-usdc-deposit/types'
import { NativeXDaiDepositAction } from '../flavours/native-xdai-deposit/types'
import { RepayAction } from '../flavours/repay/types'
import { SetUseAsCollateralAction } from '../flavours/set-use-as-collateral/types'
import { SetUserEModeAction } from '../flavours/set-user-e-mode/types'
Expand Down
111 changes: 85 additions & 26 deletions packages/app/src/features/dialogs/savings/deposit/logic/objectives.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,111 @@
import { getChainConfigEntry } from '@/config/chain'
import { SwapInfo, SwapParams } from '@/domain/exchanges/types'
import { MarketInfo } from '@/domain/market-info/marketInfo'
import { SavingsInfo } from '@/domain/savings-info/types'
import { NormalizedUnitNumber } from '@/domain/types/NumericValues'
import { TokenSymbol } from '@/domain/types/TokenSymbol'
import { ExchangeObjective } from '@/features/actions/flavours/exchange/types'
import { NativeSDaiDepositObjective } from '@/features/actions/flavours/native-sdai-deposit/types'
import { NativeDaiDepositObjective } from '@/features/actions/flavours/native-dai-deposit/types'
import { NativeXDaiDepositObjective } from '@/features/actions/flavours/native-sexy-dai-deposit/types'
import { NativeUSDCDepositObjective } from '@/features/actions/flavours/native-usdc-deposit/types'
import { simplifyQueryResult } from '@/features/actions/logic/simplifyQueryResult'
import { DialogFormNormalizedData } from '@/features/dialogs/common/logic/form'
import { gnosis, mainnet } from 'viem/chains'

export interface CreateObjectivesParams {
swapInfo: SwapInfo
swapParams: SwapParams
formValues: DialogFormNormalizedData
marketInfo: MarketInfo
savingsInfo: SavingsInfo
useNativeRoutes: boolean
chainId: number
}
export function createObjectives({
swapInfo,
swapParams,
formValues,
marketInfo,
savingsInfo,
useNativeRoutes,
}: CreateObjectivesParams): (ExchangeObjective | NativeSDaiDepositObjective)[] {
if (useNativeRoutes) {
return [
chainId,
}: CreateObjectivesParams): (
| ExchangeObjective
| NativeDaiDepositObjective
| NativeUSDCDepositObjective
| NativeXDaiDepositObjective
)[] {
const nativeObjectives = getNativeObjectivesByChainAndToken({ formValues, marketInfo, chainId })

return (
nativeObjectives ?? [
{
type: 'nativeSDaiDeposit',
token: formValues.token,
value: formValues.value,
sDai: marketInfo.sDAI,
type: 'exchange',
swapInfo: simplifyQueryResult(swapInfo),
swapParams,
formatAsDAIValue: (amount: NormalizedUnitNumber) =>
marketInfo.DAI.format(
savingsInfo.convertSharesToDai({
shares: amount,
}),
{ style: 'auto' },
),
},
]
)
}

interface GetNativeObjectivesByChainAndTokenParams {
marketInfo: MarketInfo
formValues: DialogFormNormalizedData
chainId: number
}

function getNativeObjectivesByChainAndToken({
marketInfo,
formValues,
chainId,
}: GetNativeObjectivesByChainAndTokenParams):
| (NativeDaiDepositObjective | NativeUSDCDepositObjective | NativeXDaiDepositObjective)[]
| undefined {
const tokenSymbol = formValues.token.symbol
const isNativeRouteSupported = getChainConfigEntry(chainId).savingsNativeRouteTokens.includes(tokenSymbol)

if (!isNativeRouteSupported) {
return
}

if (chainId === mainnet.id) {
if (tokenSymbol === marketInfo.DAI.symbol) {
return [
{
type: 'nativeDaiDeposit',
value: formValues.value,
dai: formValues.token,
sDai: marketInfo.sDAI,
},
]
}

if (tokenSymbol === TokenSymbol('USDC')) {
return [
{
type: 'nativeUSDCDeposit',
value: formValues.value,
usdc: formValues.token,
dai: marketInfo.DAI,
},
]
}
}

if (chainId === gnosis.id) {
if (tokenSymbol === marketInfo.DAI.symbol) {
return [
{
type: 'nativeXDaiDeposit',
value: formValues.value,
xDai: formValues.token,
},
]
}
}
const DAI = marketInfo.DAI

return [
{
type: 'exchange',
swapInfo: simplifyQueryResult(swapInfo),
swapParams,
formatAsDAIValue: (amount: NormalizedUnitNumber) =>
DAI.format(
savingsInfo.convertSharesToDai({
shares: amount,
}),
{ style: 'auto' },
),
},
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import { RiskWarning } from '@/features/dialogs/common/components/risk-acknowled
import { AssetInputSchema, useDebouncedDialogFormValues } from '@/features/dialogs/common/logic/form'
import { FormFieldsForDialog, PageState, PageStatus } from '@/features/dialogs/common/types'

import { useOriginChainId } from '@/domain/hooks/useOriginChainId'
import { TokenSymbol } from '@/domain/types/TokenSymbol'
import { mainnet } from 'viem/chains'
import { getChainConfigEntry } from '@/config/chain'
import { useChainId } from 'wagmi'
import { SavingsDialogTxOverview } from '../../common/types'
import { createMakerTxOverview, createTxOverview } from './createTxOverview'
import { getFormFieldsForDepositDialog } from './form'
Expand Down Expand Up @@ -52,7 +51,7 @@ export function useSavingsDepositDialog({
const { savingsInfo } = useSavingsInfo()
assert(savingsInfo, 'Savings info is not available')
const walletInfo = useWalletInfo()
const originChainId = useOriginChainId()
const chainId = useChainId()

const { assets: depositOptions } = makeAssetsInWalletList({ walletInfo })

Expand All @@ -76,10 +75,7 @@ export function useSavingsDepositDialog({
marketInfo,
})

const useNativeRoutes =
originChainId === mainnet.id &&
((import.meta.env.VITE_DEV_DAI_NATIVE_ROUTES === '1' && formValues.token.address === marketInfo.DAI.address) ||
(import.meta.env.VITE_DEV_USDC_NATIVE_ROUTES === '1' && formValues.token.symbol === TokenSymbol('USDC')))
const useNativeRoutes = getChainConfigEntry(chainId).savingsNativeRouteTokens.includes(formValues.token.symbol)

const { swapInfo, swapParams } = useDepositIntoSavings({
formValues,
Expand All @@ -101,7 +97,7 @@ export function useSavingsDepositDialog({
formValues,
marketInfo,
savingsInfo,
useNativeRoutes,
chainId,
})
const txOverview = useNativeRoutes
? createMakerTxOverview({
Expand Down

0 comments on commit ab1cb84

Please sign in to comment.