soso-widget
Version:
LI.FI Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.
27 lines (21 loc) • 729 B
text/typescript
import type { Token } from '@lifi/sdk'
import { formatTokenAmount, formatTokenPrice } from './format.js'
interface GetPriceImpractProps {
fromToken: Token
toToken: Token
fromAmount?: bigint
toAmount?: bigint
}
export const getPriceImpact = ({
fromToken,
toToken,
fromAmount,
toAmount,
}: GetPriceImpractProps) => {
const fromTokenAmount = formatTokenAmount(fromAmount, fromToken.decimals)
const fromTokenPrice = formatTokenPrice(fromTokenAmount, fromToken.priceUSD)
const toTokenAmount = formatTokenAmount(toAmount, toToken.decimals)
const toTokenPrice = formatTokenPrice(toTokenAmount, toToken.priceUSD) || 0.01
const priceImpact = toTokenPrice / fromTokenPrice - 1
return Number(priceImpact)
}