Skip to content

Commit

Permalink
Merge pull request #514 from invariant-labs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
awojciak authored Nov 7, 2023
2 parents 1bfc7af + 964be69 commit a93d33d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 63 deletions.
48 changes: 10 additions & 38 deletions src/containers/NewPositionWrapper/NewPositionWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ import { getLiquidityByX, getLiquidityByY } from '@invariant-labs/sdk/src/math'
import { Decimal } from '@invariant-labs/sdk/lib/market'
import { initPosition, plotTicks } from '@selectors/positions'
import { BN } from '@project-serum/anchor'
import {
ALL_FEE_TIERS_DATA,
bestTiers,
commonTokensForNetworks,
getNewPositionUIFeeTiers
} from '@consts/static'
import { ALL_FEE_TIERS_DATA, bestTiers, commonTokensForNetworks } from '@consts/static'
import { Status } from '@reducers/solanaWallet'
import { ProgressState } from '@components/AnimatedButton/AnimatedButton'
import { TickPlotPositionData } from '@components/PriceRangePlot/PriceRangePlot'
Expand Down Expand Up @@ -128,21 +123,8 @@ export const NewPositionWrapper = () => {
return 0
}, [tokenAIndex, tokenBIndex])

const currentUiTiers = useMemo(
() =>
getNewPositionUIFeeTiers(
tokenAIndex === null ? null : tokens[tokenAIndex].assetAddress,
tokenBIndex === null ? null : tokens[tokenBIndex].assetAddress
),
[tokenAIndex, tokenBIndex]
)

const [feeIndex, setFeeIndex] = useState(0)
const currentUiFeeIndex = useMemo(() => {
const index = currentUiTiers.findIndex(t => t.primaryIndex === feeIndex)

return index === -1 ? 0 : index
}, [currentUiTiers, feeIndex])
const fee = useMemo(() => ALL_FEE_TIERS_DATA[feeIndex].tier.fee, [feeIndex])
const tickSpacing = useMemo(
() =>
Expand Down Expand Up @@ -382,29 +364,19 @@ export const NewPositionWrapper = () => {
<NewPosition
tokens={tokens}
onChangePositionTokens={(tokenA, tokenB, feeTierIndex) => {
let newTierIndex: number

if (typeof currentUiTiers?.[feeTierIndex] === 'undefined') {
const indexForFee = ALL_FEE_TIERS_DATA.findIndex(tier => fee.eq(tier.tier.fee))

newTierIndex = indexForFee === -1 ? 0 : indexForFee
} else {
newTierIndex = currentUiTiers[feeTierIndex].primaryIndex
}

if (
tokenA !== null &&
tokenB !== null &&
tokenA !== tokenB &&
!(
tokenAIndex === tokenA &&
tokenBIndex === tokenB &&
fee.eq(ALL_FEE_TIERS_DATA[newTierIndex].tier.fee)
fee.eq(ALL_FEE_TIERS_DATA[feeTierIndex].tier.fee)
)
) {
const index = allPools.findIndex(
pool =>
pool.fee.v.eq(ALL_FEE_TIERS_DATA[newTierIndex].tier.fee) &&
pool.fee.v.eq(ALL_FEE_TIERS_DATA[feeTierIndex].tier.fee) &&
((pool.tokenX.equals(tokens[tokenA].assetAddress) &&
pool.tokenY.equals(tokens[tokenB].assetAddress)) ||
(pool.tokenX.equals(tokens[tokenB].assetAddress) &&
Expand All @@ -416,15 +388,15 @@ export const NewPositionWrapper = () => {
!(
tokenAIndex === tokenB &&
tokenBIndex === tokenA &&
fee.eq(ALL_FEE_TIERS_DATA[newTierIndex].tier.fee)
fee.eq(ALL_FEE_TIERS_DATA[feeTierIndex].tier.fee)
)
) {
setPoolIndex(index !== -1 ? index : null)
setCurrentPairReversed(null)
} else if (
tokenAIndex === tokenB &&
tokenBIndex === tokenA &&
fee.eq(ALL_FEE_TIERS_DATA[newTierIndex].tier.fee)
fee.eq(ALL_FEE_TIERS_DATA[feeTierIndex].tier.fee)
) {
setCurrentPairReversed(currentPairReversed === null ? true : !currentPairReversed)
}
Expand All @@ -440,13 +412,13 @@ export const NewPositionWrapper = () => {
!(
tokenAIndex === tokenB &&
tokenBIndex === tokenA &&
fee.eq(ALL_FEE_TIERS_DATA[newTierIndex].tier.fee)
fee.eq(ALL_FEE_TIERS_DATA[feeTierIndex].tier.fee)
)
) {
dispatch(
poolsActions.getPoolData(
new Pair(tokens[tokenA].assetAddress, tokens[tokenB].assetAddress, {
fee: ALL_FEE_TIERS_DATA[newTierIndex].tier.fee
fee: ALL_FEE_TIERS_DATA[feeTierIndex].tier.fee
})
)
)
Expand All @@ -455,9 +427,9 @@ export const NewPositionWrapper = () => {

setTokenAIndex(tokenA)
setTokenBIndex(tokenB)
setFeeIndex(newTierIndex)
setFeeIndex(feeTierIndex)
}}
feeTiers={currentUiTiers.map(tier => ({
feeTiers={ALL_FEE_TIERS_DATA.map(tier => ({
feeValue: +printBN(tier.tier.fee, DECIMAL - 2),
maxSafeConcentration: tier.maxSafeConcentration,
minimumRange: tier.minimumRange
Expand Down Expand Up @@ -595,7 +567,7 @@ export const NewPositionWrapper = () => {
}
}}
plotVolumeRange={currentVolumeRange}
currentFeeIndex={currentUiFeeIndex}
currentFeeIndex={feeIndex}
onSlippageChange={onSlippageChange}
initialSlippage={initialSlippage}
/>
Expand Down
31 changes: 6 additions & 25 deletions src/store/consts/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const mainnetBestTiersCreator = () => {
bestTiers.push({
tokenX,
tokenY,
bestTierIndex: 2
bestTierIndex: 4
})
}
}
Expand All @@ -244,7 +244,7 @@ const mainnetBestTiersCreator = () => {
bestTiers.push({
tokenX,
tokenY,
bestTierIndex: 3
bestTierIndex: 4
})
}
}
Expand All @@ -262,22 +262,22 @@ export const bestTiers: Record<NetworkType, BestTier[]> = {
{
tokenX: USDC_DEV.address,
tokenY: WSOL_DEV.address,
bestTierIndex: 3
bestTierIndex: 4
},
{
tokenX: USDC_DEV.address,
tokenY: BTC_DEV.address,
bestTierIndex: 3
bestTierIndex: 4
},
{
tokenX: RENDOGE_DEV.address,
tokenY: BTC_DEV.address,
bestTierIndex: 5
bestTierIndex: 6
},
{
tokenX: USDC_DEV.address,
tokenY: RENDOGE_DEV.address,
bestTierIndex: 5
bestTierIndex: 6
}
],
Testnet: [],
Expand Down Expand Up @@ -365,23 +365,4 @@ export const ALL_FEE_TIERS_DATA = FEE_TIERS.map((tier, index) => ({
primaryIndex: index
}))

export const getNewPositionUIFeeTiers = (
tokenAAddress: PublicKey | null,
tokenBAddress: PublicKey | null
) => {
const USDC = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v')
const USDT = new PublicKey('Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB')

if (
tokenAAddress !== null &&
tokenBAddress !== null &&
((tokenAAddress.equals(USDC) && tokenBAddress.equals(USDT)) ||
(tokenAAddress.equals(USDT) && tokenBAddress.equals(USDC)))
) {
return ALL_FEE_TIERS_DATA
}

return ALL_FEE_TIERS_DATA.slice(1)
}

export { SolanaNetworks, DEFAULT_PUBLICKEY, MAX_U64, NetworkType }

0 comments on commit a93d33d

Please sign in to comment.