diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index 7c58f1fcd..9b067c54f 100644 --- a/src/components/NewPosition/NewPosition.tsx +++ b/src/components/NewPosition/NewPosition.tsx @@ -41,6 +41,7 @@ export interface INewPosition { initialFee: string history: History poolAddress: string + calculatePoolAddress: () => Promise copyPoolAddressHandler: (message: string, variant: Color) => void tokens: SwapToken[] data: PlotTickData[] @@ -112,6 +113,7 @@ export const NewPosition: React.FC = ({ initialFee, history, poolAddress, + calculatePoolAddress, copyPoolAddressHandler, tokens, data, @@ -169,6 +171,7 @@ export const NewPosition: React.FC = ({ const [tokenADeposit, setTokenADeposit] = useState('') const [tokenBDeposit, setTokenBDeposit] = useState('') + const [address, setAddress] = useState(poolAddress) const [settings, setSettings] = React.useState(false) const [slippTolerance, setSlippTolerance] = React.useState(initialSlippage) const [anchorEl, setAnchorEl] = React.useState(null) @@ -304,6 +307,14 @@ export const NewPosition: React.FC = ({ } }, [midPrice.index]) + useEffect(() => { + const configurePoolAddress = async () => { + const configuredAddress = poolAddress === '' ? await calculatePoolAddress() : poolAddress + setAddress(configuredAddress) + } + void configurePoolAddress() + }, [initialTokenFrom, initialTokenTo, initialFee]) + const handleClickSettings = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget) blurContent() @@ -350,10 +361,10 @@ export const NewPosition: React.FC = ({ Add new liquidity position - {poolIndex !== null ? ( + {address !== '' ? ( ) : null} diff --git a/src/containers/NewPositionWrapper/NewPositionWrapper.tsx b/src/containers/NewPositionWrapper/NewPositionWrapper.tsx index 154d19b29..ab3502e27 100644 --- a/src/containers/NewPositionWrapper/NewPositionWrapper.tsx +++ b/src/containers/NewPositionWrapper/NewPositionWrapper.tsx @@ -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' @@ -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' @@ -52,6 +53,8 @@ export const NewPositionWrapper: React.FC = ({ }) => { const dispatch = useDispatch() + const networkType = useSelector(network) + const connection = getCurrentSolanaConnection() const tokens = useSelector(swapTokens) @@ -384,6 +387,25 @@ export const NewPositionWrapper: React.FC = ({ 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 ( = ({ history={history} copyPoolAddressHandler={copyPoolAddressHandler} poolAddress={poolIndex !== null ? allPools[poolIndex].address.toString() : ''} + calculatePoolAddress={calculatePoolAddress} tokens={tokens} onChangePositionTokens={(tokenA, tokenB, feeTierIndex) => { if (