Skip to content

Commit

Permalink
Merge pull request #836 from invariant-labs/fix-price-ratio-by-set-de…
Browse files Browse the repository at this point in the history
…nominator

fix token ratio when navigate to new position
  • Loading branch information
wojciech-cichocki authored Dec 31, 2024
2 parents 2d3433a + 285a7fa commit 17ad85a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
14 changes: 12 additions & 2 deletions src/components/PopularPools/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import RevertIcon from '@static/svg/revert.svg'
import { shortenAddress } from '@utils/uiUtils'
import StatsLabel from './StatsLabel/StatsLabel'
import backIcon from '@static/svg/back-arrow-2.svg'
import { addressToTicker, formatNumber, parseFeeToPathFee } from '@utils/utils'
import { addressToTicker, formatNumber, initialXtoY, parseFeeToPathFee } from '@utils/utils'
import { useNavigate } from 'react-router-dom'
import { NetworkType } from '@store/consts/static'
import { DECIMAL } from '@invariant-labs/sdk/lib/utils'
Expand Down Expand Up @@ -43,8 +43,18 @@ const Card: React.FC<ICard> = ({

const handleOpenPosition = () => {
if (fee === undefined) return

const revertRatio = initialXtoY(addressFrom ?? '', addressTo ?? '')

const tokenA = revertRatio
? addressToTicker(network, addressTo ?? '')
: addressToTicker(network, addressFrom ?? '')
const tokenB = revertRatio
? addressToTicker(network, addressFrom ?? '')
: addressToTicker(network, addressTo ?? '')

navigate(
`/newPosition/${addressToTicker(network, addressFrom ?? '')}/${addressToTicker(network, addressTo ?? '')}/${parseFeeToPathFee(Math.round(fee * 10 ** (DECIMAL - 2)))}`,
`/newPosition/${tokenA}/${tokenB}/${parseFeeToPathFee(Math.round(fee * 10 ** (DECIMAL - 2)))}`,
{ state: { referer: 'liquidity' } }
)
}
Expand Down
14 changes: 11 additions & 3 deletions src/components/PositionDetails/PositionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Link, useNavigate } from 'react-router-dom'
import { ILiquidityToken } from './SinglePositionInfo/consts'
import { useStyles } from './style'
import { TokenPriceData } from '@store/consts/types'
import { addressToTicker, parseFeeToPathFee, printBN } from '@utils/utils'
import { addressToTicker, initialXtoY, parseFeeToPathFee, printBN } from '@utils/utils'
import { PublicKey } from '@solana/web3.js'
import { Decimal } from '@invariant-labs/sdk/lib/market'
import { DECIMAL } from '@invariant-labs/sdk/lib/utils'
Expand Down Expand Up @@ -221,11 +221,19 @@ const PositionDetails: React.FC<IProps> = ({
className={classes.button}
variant='contained'
onClick={() => {
const parsedFee = parseFeeToPathFee(fee.v)
const parsedFee = parseFeeToPathFee(fee)
const address1 = addressToTicker(network, tokenXAddress.toString())
const address2 = addressToTicker(network, tokenYAddress.toString())

navigate(`/newPosition/${address1}/${address2}/${parsedFee}`)
const revertRatio = !initialXtoY(
tokenXAddress.toString() ?? '',
tokenYAddress.toString() ?? ''
)

const tokenA = revertRatio ? address1 : address2
const tokenB = revertRatio ? address2 : address1

navigate(`/newPosition/${tokenA}/${tokenB}/${parsedFee}`)
}}>
<span className={classes.buttonText}>+ Add Position</span>
</Button>
Expand Down
13 changes: 11 additions & 2 deletions src/components/Stats/PoolListItem/PoolListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useNavigate } from 'react-router-dom'
import icons from '@static/icons'
import { NetworkType, SortTypePoolList } from '@store/consts/static'

import { addressToTicker, parseFeeToPathFee, shortenAddress } from '@utils/utils'
import { addressToTicker, initialXtoY, parseFeeToPathFee, shortenAddress } from '@utils/utils'
import { formatNumber } from '@utils/utils'
import { DECIMAL } from '@invariant-labs/sdk/lib/utils'
import { TooltipHover } from '@components/TooltipHover/TooltipHover'
Expand Down Expand Up @@ -77,8 +77,17 @@ const PoolListItem: React.FC<IProps> = ({
const isMd = useMediaQuery(theme.breakpoints.down('md'))

const handleOpenPosition = () => {
const revertRatio = initialXtoY(addressFrom ?? '', addressTo ?? '')

const tokenA = revertRatio
? addressToTicker(network, addressTo ?? '')
: addressToTicker(network, addressFrom ?? '')
const tokenB = revertRatio
? addressToTicker(network, addressFrom ?? '')
: addressToTicker(network, addressTo ?? '')

navigate(
`/newPosition/${addressToTicker(network, addressFrom ?? '')}/${addressToTicker(network, addressTo ?? '')}/${parseFeeToPathFee(Math.round(fee * 10 ** (DECIMAL - 2)))}`,
`/newPosition/${tokenA}/${tokenB}/${parseFeeToPathFee(Math.round(fee * 10 ** (DECIMAL - 2)))}`,
{ state: { referer: 'stats' } }
)
}
Expand Down
10 changes: 6 additions & 4 deletions src/store/consts/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,12 @@ export const SIGNING_SNACKBAR_CONFIG: Omit<ISnackbar, 'open'> = {
persist: true
}

export const STABLECOIN_ADDRESSES: string[] = [
'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'
] // USDC, USDT
export const ADDRESSES_TO_REVERT_TOKEN_PAIRS: string[] = [
'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB', // USDT
'So11111111111111111111111111111111111111112', //SOL
'7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs' //ETH
]

export const REFRESHER_INTERVAL = 120

Expand Down
12 changes: 6 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
NetworkType,
PRICE_DECIMAL,
RENDOGE_DEV,
STABLECOIN_ADDRESSES,
USDC_DEV,
USDH_DEV,
USDT_DEV,
Expand All @@ -51,7 +50,8 @@ import {
WEN_MAIN,
SUI_MAIN,
WRAPPED_SOL_ADDRESS,
NATIVE_TICK_CROSSES_PER_IX
NATIVE_TICK_CROSSES_PER_IX,
ADDRESSES_TO_REVERT_TOKEN_PAIRS
} from '@store/consts/static'
import mainnetList from '@store/consts/tokenLists/mainnet.json'
import { FormatConfig, subNumbers } from '@store/consts/static'
Expand Down Expand Up @@ -1633,15 +1633,15 @@ export const addressToTicker = (network: NetworkType, address: string): string =
return getReversedAddressTickerMap(network)[address] || address
}

export const initialXtoY = (tokenXAddress?: string, tokenYAddress?: string) => {
export const initialXtoY = (tokenXAddress?: string | null, tokenYAddress?: string | null) => {
if (!tokenXAddress || !tokenYAddress) {
return true
}

const isTokeXStablecoin = STABLECOIN_ADDRESSES.includes(tokenXAddress)
const isTokenYStablecoin = STABLECOIN_ADDRESSES.includes(tokenYAddress)
const tokenXIndex = ADDRESSES_TO_REVERT_TOKEN_PAIRS.findIndex(token => token === tokenXAddress)
const tokenYIndex = ADDRESSES_TO_REVERT_TOKEN_PAIRS.findIndex(token => token === tokenYAddress)

return isTokeXStablecoin === isTokenYStablecoin || (!isTokeXStablecoin && !isTokenYStablecoin)
return tokenXIndex < tokenYIndex
}

export const parseFeeToPathFee = (fee: BN): string => {
Expand Down

0 comments on commit 17ad85a

Please sign in to comment.