Skip to content

Commit

Permalink
Merge pull request #577 from invariant-labs/add-market-id-non-existin…
Browse files Browse the repository at this point in the history
…g-pools

Added marketId to non existing pools
  • Loading branch information
wojciech-cichocki authored Jan 31, 2024
2 parents 5c27528 + 57d3d23 commit b08fbdc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/components/NewPosition/NewPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface INewPosition {
initialFee: string
history: History<unknown>
poolAddress: string
calculatePoolAddress: () => Promise<string>
copyPoolAddressHandler: (message: string, variant: Color) => void
tokens: SwapToken[]
data: PlotTickData[]
Expand Down Expand Up @@ -112,6 +113,7 @@ export const NewPosition: React.FC<INewPosition> = ({
initialFee,
history,
poolAddress,
calculatePoolAddress,
copyPoolAddressHandler,
tokens,
data,
Expand Down Expand Up @@ -169,6 +171,7 @@ export const NewPosition: React.FC<INewPosition> = ({
const [tokenADeposit, setTokenADeposit] = useState<string>('')
const [tokenBDeposit, setTokenBDeposit] = useState<string>('')

const [address, setAddress] = useState<string>(poolAddress)
const [settings, setSettings] = React.useState<boolean>(false)
const [slippTolerance, setSlippTolerance] = React.useState<string>(initialSlippage)
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null)
Expand Down Expand Up @@ -304,6 +307,14 @@ export const NewPosition: React.FC<INewPosition> = ({
}
}, [midPrice.index])

useEffect(() => {
const configurePoolAddress = async () => {
const configuredAddress = poolAddress === '' ? await calculatePoolAddress() : poolAddress
setAddress(configuredAddress)
}
void configurePoolAddress()
}, [initialTokenFrom, initialTokenTo, initialFee])

const handleClickSettings = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget)
blurContent()
Expand Down Expand Up @@ -350,10 +361,10 @@ export const NewPosition: React.FC<INewPosition> = ({
<Grid container justifyContent='space-between'>
<Typography className={classes.title}>Add new liquidity position</Typography>
<Grid container item alignItems='center' className={classes.options}>
{poolIndex !== null ? (
{address !== '' ? (
<MarketIdLabel
displayLength={9}
marketId={poolAddress}
marketId={address}
copyPoolAddressHandler={copyPoolAddressHandler}
/>
) : null}
Expand Down
27 changes: 25 additions & 2 deletions src/containers/NewPositionWrapper/NewPositionWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getNewTokenOrThrow,
printBN
} from '@consts/utils'
import { MAX_TICK, Pair, calculatePriceSqrt } from '@invariant-labs/sdk'
import { MAX_TICK, Pair, calculatePriceSqrt, getMarketAddress } from '@invariant-labs/sdk'
import { Decimal } from '@invariant-labs/sdk/lib/market'
import { DECIMAL } from '@invariant-labs/sdk/lib/utils'
import { getLiquidityByX, getLiquidityByY } from '@invariant-labs/sdk/src/math'
Expand All @@ -31,7 +31,8 @@ import {
import { initPosition, plotTicks } from '@selectors/positions'
import { network } from '@selectors/solanaConnection'
import { canCreateNewPool, canCreateNewPosition, status, swapTokens } from '@selectors/solanaWallet'
import { getCurrentSolanaConnection } from '@web3/connection'
import { PublicKey } from '@solana/web3.js'
import { getCurrentSolanaConnection, networkTypetoProgramNetwork } from '@web3/connection'
import { openWalletSelectorModal } from '@web3/selector'
import { History } from 'history'
import React, { useEffect, useMemo, useState } from 'react'
Expand All @@ -52,6 +53,8 @@ export const NewPositionWrapper: React.FC<IProps> = ({
}) => {
const dispatch = useDispatch()

const networkType = useSelector(network)

const connection = getCurrentSolanaConnection()

const tokens = useSelector(swapTokens)
Expand Down Expand Up @@ -384,6 +387,25 @@ export const NewPositionWrapper: React.FC<IProps> = ({
localStorage.setItem('INVARIANT_NEW_POSITION_SLIPPAGE', slippage)
}

const calculatePoolAddress = async () => {
if (tokenAIndex === null || tokenBIndex === null) {
return ''
}

const pair = new Pair(
tokens[tokenAIndex].assetAddress,
tokens[tokenBIndex].assetAddress,
ALL_FEE_TIERS_DATA[feeIndex].tier
)

const marketProgramId = new PublicKey(
getMarketAddress(networkTypetoProgramNetwork(networkType))
)
const poolAddress: string = (await pair.getAddress(marketProgramId)).toString()

return poolAddress
}

return (
<NewPosition
initialTokenFrom={initialTokenFrom}
Expand All @@ -392,6 +414,7 @@ export const NewPositionWrapper: React.FC<IProps> = ({
history={history}
copyPoolAddressHandler={copyPoolAddressHandler}
poolAddress={poolIndex !== null ? allPools[poolIndex].address.toString() : ''}
calculatePoolAddress={calculatePoolAddress}
tokens={tokens}
onChangePositionTokens={(tokenA, tokenB, feeTierIndex) => {
if (
Expand Down

0 comments on commit b08fbdc

Please sign in to comment.