Skip to content

Commit

Permalink
Updated to more robust fee price calculation (#3964)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrwitek authored Jun 24, 2024
1 parent 199c59e commit 8d7ed9f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
7 changes: 4 additions & 3 deletions features/refinance/components/steps/RefinanceSwapSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import React from 'react'
export const RefinanceSwapSection = () => {
const { t } = useTranslation()
const {
simulation: { refinanceSimulation, debtPrice, collateralPrice },
environment: { getTokenUsdPrice },
simulation: { refinanceSimulation },
} = useRefinanceContext()

if (!refinanceSimulation || !collateralPrice || !debtPrice) {
if (!refinanceSimulation) {
return null
}

Expand All @@ -36,7 +37,7 @@ export const RefinanceSwapSection = () => {

const priceImpact = new BigNumber(swap.priceImpact.value).div(100)
const slippage = new BigNumber(swap.slippage.value)
const feePrice = new BigNumber(isCollateral ? collateralPrice : debtPrice)
const feePrice = getTokenUsdPrice(swap.summerFee.token.symbol.toUpperCase())
const fee = new BigNumber(swap.summerFee.amount).times(feePrice)
const rawPrice = new BigNumber(1).div(swap.offerPrice.value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const RefinanceWhatsChangingStep = () => {
const { t } = useTranslation()

const {
environment: { gasEstimation },
environment: { gasEstimation, getTokenUsdPrice },
metadata: {
validations: { errors, warnings, notices, successes },
},
Expand All @@ -31,7 +31,7 @@ export const RefinanceWhatsChangingStep = () => {
token: { symbol: oldDebtToken },
},
},
simulation: { refinanceSimulation, collateralPrice, debtPrice },
simulation: { refinanceSimulation },
tx: { isTxSuccess, isTxInProgress },
form: {
state: { dpm },
Expand Down Expand Up @@ -76,12 +76,10 @@ export const RefinanceWhatsChangingStep = () => {
}

let summerFee = new BigNumber(0)
const { swaps, sourcePosition } = refinanceSimulation || {}
const { swaps } = refinanceSimulation || {}

swaps?.forEach((swap) => {
const isCollateral =
swap.fromTokenAmount.token.symbol === sourcePosition?.collateralAmount.token.symbol
const feePrice = new BigNumber(isCollateral ? collateralPrice || 0 : debtPrice || 0)
const feePrice = getTokenUsdPrice(swap.summerFee.token.symbol.toUpperCase())
const fee = new BigNumber(swap.summerFee.amount).times(feePrice)
summerFee = summerFee.plus(fee)
})
Expand Down
1 change: 1 addition & 0 deletions features/refinance/contexts/RefinanceGeneralContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export type RefinanceGeneralContextBase = {
slippage: number
gasEstimation: GasEstimationContext | undefined
isOwner: boolean
getTokenUsdPrice: (symbol: string) => string
}
position: {
positionId: PositionId
Expand Down
10 changes: 10 additions & 0 deletions features/refinance/hooks/useInitializeRefinanceContextBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { type AddressValue, TokenAmount } from '@summer_fi/summerfi-sdk-common'
import { getTokenPrice } from 'blockchain/prices'
import { tokenPriceStore } from 'blockchain/prices.constants'
import type { GasEstimationContext } from 'components/context/GasEstimationContextProvider'
import {
getOmniTxStatuses,
Expand Down Expand Up @@ -75,6 +77,13 @@ export const useInitializeRefinanceContextBase = ({
[LendingProtocol.Ajna]: [],
}[contextInput.position.lendingProtocol]

const getTokenUsdPrice = (symbol: string) =>
getTokenPrice(
symbol.toUpperCase(),
tokenPriceStore.prices,
'getTokenUsdPrice - init refinance context base',
).toString()

const {
environment: { slippage, address, isOwner },
poolData: { collateralTokenSymbol, debtTokenSymbol, poolId, maxLtv, pairId },
Expand Down Expand Up @@ -154,6 +163,7 @@ export const useInitializeRefinanceContextBase = ({
slippage,
gasEstimation,
isOwner,
getTokenUsdPrice,
},
position: {
collateralTokenData,
Expand Down

0 comments on commit 8d7ed9f

Please sign in to comment.