-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve objectives creation at savings deposit dialog
- Loading branch information
Oskar
committed
Jun 18, 2024
1 parent
5d73726
commit ab1cb84
Showing
8 changed files
with
112 additions
and
54 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
packages/app/src/features/actions/flavours/native-sexy-dai-deposit/types.ts
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
packages/app/src/features/actions/flavours/native-xdai-deposit/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 85 additions & 26 deletions
111
packages/app/src/features/dialogs/savings/deposit/logic/objectives.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
), | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters