Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update add liquidity button redirect #552

Merged
merged 5 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/components/NewPosition/DepositSelector/DepositSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
WSOL_MIN_DEPOSIT_SWAP_FROM_AMOUNT,
WSOL_POOL_INIT_LAMPORTS
} from '@consts/static'
import { parsePathFeeToFeeString, tickerToAddress } from '@consts/uiUtils'
import { getScaleFromString, printBN, printBNtoBN } from '@consts/utils'
import { Grid, Typography } from '@material-ui/core'
import { BN } from '@project-serum/anchor'
Expand All @@ -30,7 +31,6 @@ export interface IDepositSelector {
initialTokenFrom: string
initialTokenTo: string
initialFee: string
getAddress: (address: string) => string
tokens: SwapToken[]
setPositionTokens: (
tokenAIndex: number | null,
Expand Down Expand Up @@ -65,7 +65,6 @@ export const DepositSelector: React.FC<IDepositSelector> = ({
initialTokenFrom,
initialTokenTo,
initialFee,
getAddress,
tokens,
setPositionTokens,
onAddLiquidity,
Expand Down Expand Up @@ -108,18 +107,18 @@ export const DepositSelector: React.FC<IDepositSelector> = ({
let feeTierIndexFromPath = 0

tokens.forEach((token, index) => {
if (token.assetAddress.toString() === getAddress(initialTokenFrom)) {
if (token.assetAddress.toString() === tickerToAddress(initialTokenFrom)) {
tokenAIndexFromPath = index
}

if (token.assetAddress.toString() === getAddress(initialTokenTo)) {
if (token.assetAddress.toString() === tickerToAddress(initialTokenTo)) {
tokenBIndexFromPath = index
}
})

ALL_FEE_TIERS_DATA.forEach((feeTierData, index) => {
const parsedFee = (+initialFee.replace('_', '') * Math.pow(10, 8)).toString()
const parsedFee = parsePathFeeToFeeString(initialFee)

ALL_FEE_TIERS_DATA.forEach((feeTierData, index) => {
if (feeTierData.tier.fee.toString() === parsedFee) {
feeTierIndexFromPath = index
}
Expand Down
44 changes: 7 additions & 37 deletions src/components/NewPosition/NewPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Slippage from '@components/Modals/Slippage/Slippage'
import { INoConnected, NoConnected } from '@components/NoConnected/NoConnected'
import { TickPlotPositionData } from '@components/PriceRangePlot/PriceRangePlot'
import { ALL_FEE_TIERS_DATA, BestTier } from '@consts/static'
import { blurContent, unblurContent } from '@consts/uiUtils'
import { addressToTicker, blurContent, parseFeeToPathFee, unblurContent } from '@consts/uiUtils'
import {
CoingeckoPriceData,
PositionTokenBlock,
Expand All @@ -25,7 +25,7 @@ import { PublicKey } from '@solana/web3.js'
import backIcon from '@static/svg/back-arrow.svg'
import settingIcon from '@static/svg/settings.svg'
import { History } from 'history'
import React, { useEffect, useMemo, useState } from 'react'
import React, { useEffect, useState } from 'react'
import { Link } from 'react-router-dom'
import ConcentrationTypeSwitch from './ConcentrationTypeSwitch/ConcentrationTypeSwitch'
import DepositSelector from './DepositSelector/DepositSelector'
Expand Down Expand Up @@ -314,47 +314,18 @@ export const NewPosition: React.FC<INewPosition> = ({
onSlippageChange(slippage)
}

const tokenAliasMap: { [key: string]: string } = {
SOL: 'So11111111111111111111111111111111111111112',
USDC: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
USDT: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
USDH: 'USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX',
mSOL: 'mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So',
bSOL: 'bSo13r4TkiE4KumL71LsHTPpL2euBYLFx6h9HP3piy1',
stSOL: '7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj',
SNY: '4dmKkXNHdgYsXqBHCuMikNQWwVomZURhYvkkX5c4pQ7y',
ETH: '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs',
LFNTY: 'LFNTYraetVioAPnGJht4yNg2aUZFXR776cMeN9VMjXp'
}

const reversedTokenAliasMap = useMemo(() => {
return Object.fromEntries(Object.entries(tokenAliasMap).map(([key, value]) => [value, key]))
}, [tokenAliasMap])

const getAlias = (address: string): string => {
return reversedTokenAliasMap[address] ?? address
}

const getAddress = (alias: string): string => {
return tokenAliasMap[alias] ?? alias
}

const updatePath = (index1: number | null, index2: number | null, fee: number) => {
let parsedFee = (+ALL_FEE_TIERS_DATA[fee].tier.fee / Math.pow(10, 8))
.toString()
.padStart(3, '0')
parsedFee =
parsedFee.slice(0, parsedFee.length - 2) + '_' + parsedFee.slice(parsedFee.length - 2)
const parsedFee = parseFeeToPathFee(+ALL_FEE_TIERS_DATA[fee].tier.fee)

if (index1 != null && index2 != null) {
const address1 = getAlias(tokens[index1].assetAddress.toString())
const address2 = getAlias(tokens[index2].assetAddress.toString())
const address1 = addressToTicker(tokens[index1].assetAddress.toString())
const address2 = addressToTicker(tokens[index2].assetAddress.toString())
history.replace(`/newPosition/${address1}/${address2}/${parsedFee}`)
} else if (index1 != null) {
const address = getAlias(tokens[index1].assetAddress.toString())
const address = addressToTicker(tokens[index1].assetAddress.toString())
history.replace(`/newPosition/${address}/${parsedFee}`)
} else if (index2 != null) {
const address = getAlias(tokens[index2].assetAddress.toString())
const address = addressToTicker(tokens[index2].assetAddress.toString())
history.replace(`/newPosition/${address}/${parsedFee}`)
} else if (fee != null) {
history.replace(`/newPosition/${parsedFee}`)
Expand Down Expand Up @@ -408,7 +379,6 @@ export const NewPosition: React.FC<INewPosition> = ({
initialTokenFrom={initialTokenFrom}
initialTokenTo={initialTokenTo}
initialFee={initialFee}
getAddress={getAddress}
className={classes.deposit}
tokens={tokens}
setPositionTokens={(index1, index2, fee) => {
Expand Down
31 changes: 22 additions & 9 deletions src/components/PositionDetails/PositionDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import React, { useState } from 'react'
import SinglePositionInfo from '@components/PositionDetails/SinglePositionInfo/SinglePositionInfo'
import SinglePositionPlot from '@components/PositionDetails/SinglePositionPlot/SinglePositionPlot'
import { TickPlotPositionData } from '@components/PriceRangePlot/PriceRangePlot'
import { addressToTicker, parseFeeToPathFee } from '@consts/uiUtils'
import { printBN } from '@consts/utils'
import { Decimal } from '@invariant-labs/sdk/lib/market'
import { DECIMAL } from '@invariant-labs/sdk/lib/utils'
import { Button, Grid, Hidden, Typography } from '@material-ui/core'
import { Link, useHistory } from 'react-router-dom'
import backIcon from '@static/svg/back-arrow.svg'
import useStyles from './style'
import { PlotTickData } from '@reducers/positions'
import { TickPlotPositionData } from '@components/PriceRangePlot/PriceRangePlot'
import { PublicKey } from '@solana/web3.js'
import backIcon from '@static/svg/back-arrow.svg'
import React, { useState } from 'react'
import { Link, useHistory } from 'react-router-dom'
import { ILiquidityToken } from './SinglePositionInfo/consts'
import SinglePositionPlot from './SinglePositionPlot/SinglePositionPlot'
import useStyles from './style'

interface IProps {
tokenXAddress: PublicKey
tokenYAddress: PublicKey
detailsData: PlotTickData[]
leftRange: TickPlotPositionData
rightRange: TickPlotPositionData
Expand All @@ -21,7 +28,7 @@ interface IProps {
closePosition: (claimFarmRewards?: boolean) => void
ticksLoading: boolean
tickSpacing: number
fee: number
fee: Decimal
min: number
max: number
initialIsDiscreteValue: boolean
Expand All @@ -37,6 +44,8 @@ interface IProps {
}

const PositionDetails: React.FC<IProps> = ({
tokenXAddress,
tokenYAddress,
detailsData,
leftRange,
rightRange,
Expand Down Expand Up @@ -76,7 +85,7 @@ const PositionDetails: React.FC<IProps> = ({
</Link>

<SinglePositionInfo
fee={fee}
fee={+printBN(fee.v, DECIMAL - 2)}
onClickClaimFee={onClickClaimFee}
closePosition={closePosition}
tokenX={tokenX}
Expand All @@ -99,7 +108,11 @@ const PositionDetails: React.FC<IProps> = ({
className={classes.button}
variant='contained'
onClick={() => {
history.push('/newPosition')
const parsedFee = parseFeeToPathFee(fee.v)
const address1 = addressToTicker(tokenXAddress.toString())
const address2 = addressToTicker(tokenYAddress.toString())

history.push(`/newPosition/${address1}/${address2}/${parsedFee}`)
}}>
<span className={classes.buttonText}>+ Add Liquidity</span>
</Button>
Expand Down
40 changes: 21 additions & 19 deletions src/containers/SinglePositionWrapper/SinglePositionWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import React, { useMemo, useEffect, useState } from 'react'
import { useHistory } from 'react-router'
import { useDispatch, useSelector } from 'react-redux'
import { actions } from '@reducers/positions'
import {
currentPositionRangeTicks,
isLoadingPositionsList,
plotTicks,
singlePositionData
} from '@selectors/positions'
import PositionDetails from '@components/PositionDetails/PositionDetails'
import { Grid, Typography } from '@material-ui/core'
import {
CoingeckoPriceData,
calcPrice,
calcYPerXPrice,
CoingeckoPriceData,
createPlaceholderLiquidityPlot,
getCoingeckoTokenPrice,
printBN
} from '@consts/utils'
import { calculatePriceSqrt } from '@invariant-labs/sdk'
import { calculateClaimAmount, DECIMAL } from '@invariant-labs/sdk/src/utils'
import { getX, getY, MAX_TICK } from '@invariant-labs/sdk/lib/math'
import loader from '@static/gif/loader.gif'
import useStyles from './style'
import { hasTokens, volumeRanges } from '@selectors/pools'
import { hasFarms, hasUserStakes, stakesForPosition } from '@selectors/farms'
import { MAX_TICK, getX, getY } from '@invariant-labs/sdk/lib/math'
import { calculateClaimAmount } from '@invariant-labs/sdk/src/utils'
import { Grid, Typography } from '@material-ui/core'
import { actions as farmsActions } from '@reducers/farms'
import { actions } from '@reducers/positions'
import { Status } from '@reducers/solanaWallet'
import { hasFarms, hasUserStakes, stakesForPosition } from '@selectors/farms'
import { hasTokens, volumeRanges } from '@selectors/pools'
import {
currentPositionRangeTicks,
isLoadingPositionsList,
plotTicks,
singlePositionData
} from '@selectors/positions'
import { status } from '@selectors/solanaWallet'
import loader from '@static/gif/loader.gif'
import React, { useEffect, useMemo, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { useHistory } from 'react-router'
import useStyles from './style'

export interface IProps {
id: string
Expand Down Expand Up @@ -332,6 +332,8 @@ export const SinglePositionWrapper: React.FC<IProps> = ({ id }) => {

return !isLoadingList && position ? (
<PositionDetails
tokenXAddress={position.tokenX.assetAddress}
tokenYAddress={position.tokenY.assetAddress}
detailsData={data}
midPrice={midPrice}
leftRange={leftRange}
Expand Down Expand Up @@ -377,7 +379,7 @@ export const SinglePositionWrapper: React.FC<IProps> = ({ id }) => {
? undefined
: tokenYPriceData.price * +printBN(position.tokenY.balance, position.tokenY.decimals)
}}
fee={+printBN(position.poolData.fee.v, DECIMAL - 2)}
fee={position.poolData.fee}
min={min}
max={max}
initialIsDiscreteValue={initialIsDiscreteValue}
Expand Down
36 changes: 36 additions & 0 deletions src/store/consts/uiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { BN } from '@project-serum/anchor'

export const toBlur = 'global-blur'

export const addressTickerMap: { [key: string]: string } = {
SOL: 'So11111111111111111111111111111111111111112',
USDC: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
USDT: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
USDH: 'USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX',
mSOL: 'mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So',
bSOL: 'bSo13r4TkiE4KumL71LsHTPpL2euBYLFx6h9HP3piy1',
stSOL: '7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj',
SNY: '4dmKkXNHdgYsXqBHCuMikNQWwVomZURhYvkkX5c4pQ7y',
ETH: '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs',
LFNTY: 'LFNTYraetVioAPnGJht4yNg2aUZFXR776cMeN9VMjXp'
}

export const reversedAddressTickerMap = Object.fromEntries(
Object.entries(addressTickerMap).map(([key, value]) => [value, key])
)

// could use rewriting to backdrop-filter when browser support is better
export const blurContent = () => {
const el = document.getElementById(toBlur)
Expand All @@ -22,3 +41,20 @@ export const importantStyles = (styleObject: { [key: string]: string | number })
}),
styleObject
)

export const parseFeeToPathFee = (fee: BN): string => {
const parsedFee = (fee / Math.pow(10, 8)).toString().padStart(3, '0')
return parsedFee.slice(0, parsedFee.length - 2) + '_' + parsedFee.slice(parsedFee.length - 2)
}

export const parsePathFeeToFeeString = (pathFee: string): string => {
return (+pathFee.replace('_', '') * Math.pow(10, 8)).toString()
}

export const tickerToAddress = (ticker: string): string => {
return addressTickerMap[ticker] || ticker
}

export const addressToTicker = (address: string): string => {
return reversedAddressTickerMap[address] || address
}
Loading