From 941c0f01ee8de7adfc8bbb33aee7cf2eb3883d90 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 12:08:27 +0100 Subject: [PATCH 1/7] Added marketId to non existing pools --- src/components/NewPosition/NewPosition.tsx | 24 +++++++++++++++-- .../NewPositionWrapper/NewPositionWrapper.tsx | 27 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index 7c58f1fcd..ff293ac7a 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, @@ -156,6 +158,10 @@ export const NewPosition: React.FC = ({ onSlippageChange, initialSlippage }) => { + console.log(poolAddress) + console.log(poolAddress === '') + console.log(tokens) + console.log(commonTokens) const classes = useStyles() const [isConcentrated, setIsConcentrated] = useState(initialIsConcentratedValue) @@ -169,6 +175,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 +311,19 @@ export const NewPosition: React.FC = ({ } }, [midPrice.index]) + useEffect(() => { + const configurePoolAddress = async () => { + console.log(poolAddress) + if (poolAddress === '') { + const configuredAddress = await calculatePoolAddress() + setAddress(configuredAddress) + } else { + setAddress(poolAddress) + } + } + configurePoolAddress() + }, [poolAddress, initialTokenFrom, initialTokenTo]) + const handleClickSettings = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget) blurContent() @@ -350,10 +370,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..a3311584b 100644 --- a/src/containers/NewPositionWrapper/NewPositionWrapper.tsx +++ b/src/containers/NewPositionWrapper/NewPositionWrapper.tsx @@ -19,6 +19,7 @@ import { getLiquidityByX, getLiquidityByY } from '@invariant-labs/sdk/src/math' import { feeToTickSpacing } from '@invariant-labs/sdk/src/utils' import { Color } from '@material-ui/lab' import { BN } from '@project-serum/anchor' +import { token } from '@project-serum/anchor/dist/cjs/utils' import { actions as poolsActions } from '@reducers/pools' import { actions } from '@reducers/positions' import { actions as snackbarsActions } from '@reducers/snackbars' @@ -29,9 +30,10 @@ import { volumeRanges } from '@selectors/pools' import { initPosition, plotTicks } from '@selectors/positions' -import { network } from '@selectors/solanaConnection' +import { network, rpcAddress } from '@selectors/solanaConnection' import { canCreateNewPool, canCreateNewPosition, status, swapTokens } from '@selectors/solanaWallet' import { getCurrentSolanaConnection } from '@web3/connection' +import { getMarketProgram, getMarketProgramSync } from '@web3/programs/amm' import { openWalletSelectorModal } from '@web3/selector' import { History } from 'history' import React, { useEffect, useMemo, useState } from 'react' @@ -52,6 +54,10 @@ export const NewPositionWrapper: React.FC = ({ }) => { const dispatch = useDispatch() + const networkType = useSelector(network) + const rpc = useSelector(rpcAddress) + const marketProgram = getMarketProgramSync(networkType, rpc) + const connection = getCurrentSolanaConnection() const tokens = useSelector(swapTokens) @@ -384,6 +390,24 @@ 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 poolAddress: string = await pair + .getAddress(marketProgram.program.programId) + .then(address => address.toString()) + + return poolAddress + } + return ( = ({ history={history} copyPoolAddressHandler={copyPoolAddressHandler} poolAddress={poolIndex !== null ? allPools[poolIndex].address.toString() : ''} + calculatePoolAddress={calculatePoolAddress} tokens={tokens} onChangePositionTokens={(tokenA, tokenB, feeTierIndex) => { if ( From ddcdc6c1ec394280d83d3747acb2f441f5b702da Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 12:10:45 +0100 Subject: [PATCH 2/7] Cleanup --- src/components/NewPosition/NewPosition.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index ff293ac7a..a448ca43e 100644 --- a/src/components/NewPosition/NewPosition.tsx +++ b/src/components/NewPosition/NewPosition.tsx @@ -158,10 +158,6 @@ export const NewPosition: React.FC = ({ onSlippageChange, initialSlippage }) => { - console.log(poolAddress) - console.log(poolAddress === '') - console.log(tokens) - console.log(commonTokens) const classes = useStyles() const [isConcentrated, setIsConcentrated] = useState(initialIsConcentratedValue) From ea3aad989952078e0159abab6272f1d264ce8bef Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 12:13:40 +0100 Subject: [PATCH 3/7] Updated useEffect --- src/components/NewPosition/NewPosition.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index a448ca43e..fb1f23548 100644 --- a/src/components/NewPosition/NewPosition.tsx +++ b/src/components/NewPosition/NewPosition.tsx @@ -309,7 +309,6 @@ export const NewPosition: React.FC = ({ useEffect(() => { const configurePoolAddress = async () => { - console.log(poolAddress) if (poolAddress === '') { const configuredAddress = await calculatePoolAddress() setAddress(configuredAddress) From 8d97bf6000348fdb56c6ca3d87934dd3d8868304 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 12:20:24 +0100 Subject: [PATCH 4/7] Fixed trunk --- src/components/NewPosition/NewPosition.tsx | 10 +++------- .../NewPositionWrapper/NewPositionWrapper.tsx | 3 +-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index fb1f23548..211571228 100644 --- a/src/components/NewPosition/NewPosition.tsx +++ b/src/components/NewPosition/NewPosition.tsx @@ -309,14 +309,10 @@ export const NewPosition: React.FC = ({ useEffect(() => { const configurePoolAddress = async () => { - if (poolAddress === '') { - const configuredAddress = await calculatePoolAddress() - setAddress(configuredAddress) - } else { - setAddress(poolAddress) - } + const configuredAddress = poolAddress === '' ? await calculatePoolAddress() : poolAddress + setAddress(configuredAddress) } - configurePoolAddress() + void configurePoolAddress() }, [poolAddress, initialTokenFrom, initialTokenTo]) const handleClickSettings = (event: React.MouseEvent) => { diff --git a/src/containers/NewPositionWrapper/NewPositionWrapper.tsx b/src/containers/NewPositionWrapper/NewPositionWrapper.tsx index a3311584b..65ce88a60 100644 --- a/src/containers/NewPositionWrapper/NewPositionWrapper.tsx +++ b/src/containers/NewPositionWrapper/NewPositionWrapper.tsx @@ -19,7 +19,6 @@ import { getLiquidityByX, getLiquidityByY } from '@invariant-labs/sdk/src/math' import { feeToTickSpacing } from '@invariant-labs/sdk/src/utils' import { Color } from '@material-ui/lab' import { BN } from '@project-serum/anchor' -import { token } from '@project-serum/anchor/dist/cjs/utils' import { actions as poolsActions } from '@reducers/pools' import { actions } from '@reducers/positions' import { actions as snackbarsActions } from '@reducers/snackbars' @@ -33,7 +32,7 @@ import { initPosition, plotTicks } from '@selectors/positions' import { network, rpcAddress } from '@selectors/solanaConnection' import { canCreateNewPool, canCreateNewPosition, status, swapTokens } from '@selectors/solanaWallet' import { getCurrentSolanaConnection } from '@web3/connection' -import { getMarketProgram, getMarketProgramSync } from '@web3/programs/amm' +import { getMarketProgramSync } from '@web3/programs/amm' import { openWalletSelectorModal } from '@web3/selector' import { History } from 'history' import React, { useEffect, useMemo, useState } from 'react' From 83645571b51356cc07d75be75caacf8bac69c740 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 12:27:21 +0100 Subject: [PATCH 5/7] Reduced useEffect invokes --- src/components/NewPosition/NewPosition.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index 211571228..9b0a2301b 100644 --- a/src/components/NewPosition/NewPosition.tsx +++ b/src/components/NewPosition/NewPosition.tsx @@ -313,7 +313,7 @@ export const NewPosition: React.FC = ({ setAddress(configuredAddress) } void configurePoolAddress() - }, [poolAddress, initialTokenFrom, initialTokenTo]) + }, [initialTokenFrom, initialTokenTo]) const handleClickSettings = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget) From 3ab205e4cf6923bc57681dfcb7fe28286bdc9f4b Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 13:15:38 +0100 Subject: [PATCH 6/7] Added changing address on fee tier change --- src/components/NewPosition/NewPosition.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/NewPosition/NewPosition.tsx b/src/components/NewPosition/NewPosition.tsx index 9b0a2301b..9b067c54f 100644 --- a/src/components/NewPosition/NewPosition.tsx +++ b/src/components/NewPosition/NewPosition.tsx @@ -313,7 +313,7 @@ export const NewPosition: React.FC = ({ setAddress(configuredAddress) } void configurePoolAddress() - }, [initialTokenFrom, initialTokenTo]) + }, [initialTokenFrom, initialTokenTo, initialFee]) const handleClickSettings = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget) From 57d3d23e8b0e48a97f2626032960ffa1b3d7c516 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Tue, 30 Jan 2024 16:17:38 +0100 Subject: [PATCH 7/7] Updated getting programId --- .../NewPositionWrapper/NewPositionWrapper.tsx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/containers/NewPositionWrapper/NewPositionWrapper.tsx b/src/containers/NewPositionWrapper/NewPositionWrapper.tsx index 65ce88a60..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' @@ -29,10 +29,10 @@ import { volumeRanges } from '@selectors/pools' import { initPosition, plotTicks } from '@selectors/positions' -import { network, rpcAddress } from '@selectors/solanaConnection' +import { network } from '@selectors/solanaConnection' import { canCreateNewPool, canCreateNewPosition, status, swapTokens } from '@selectors/solanaWallet' -import { getCurrentSolanaConnection } from '@web3/connection' -import { getMarketProgramSync } from '@web3/programs/amm' +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' @@ -54,8 +54,6 @@ export const NewPositionWrapper: React.FC = ({ const dispatch = useDispatch() const networkType = useSelector(network) - const rpc = useSelector(rpcAddress) - const marketProgram = getMarketProgramSync(networkType, rpc) const connection = getCurrentSolanaConnection() @@ -400,9 +398,10 @@ export const NewPositionWrapper: React.FC = ({ ALL_FEE_TIERS_DATA[feeIndex].tier ) - const poolAddress: string = await pair - .getAddress(marketProgram.program.programId) - .then(address => address.toString()) + const marketProgramId = new PublicKey( + getMarketAddress(networkTypetoProgramNetwork(networkType)) + ) + const poolAddress: string = (await pair.getAddress(marketProgramId)).toString() return poolAddress }