Skip to content

Commit

Permalink
univ3 - func - get user liquidity pools
Browse files Browse the repository at this point in the history
  • Loading branch information
shendel committed Jul 26, 2024
1 parent 5e15821 commit 3ffc1e7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/front/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ const config = {
factory: '0x1F98431c8aD98523631AE4a59f267346ea31F984',
quoter: '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6',
router: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
position_manager: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88',
},
11155111: { // Eth Sepolia testnet
factory: '0x0227628f3F023bb0B980b67D528571c95c6DaC1c',
quoter: '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6',
router: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
position_manager: '0x1238536071E1c677A632429e3655c799b22cDA52',
},
56: { // Binance
factory: '0xdB1d10011AD0Ff90774D0C6Bb92e5C5c8b4461F7',
quoter: '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6',
router: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
position_manager: '0x7b8A01B39D58278b5DE7e48c8449c9f4F5170613',
},
137: { // Polygon
factory: '0x1F98431c8aD98523631AE4a59f267346ea31F984',
quoter: '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6',
router: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
position_manager: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88',
},
}
}
Expand Down
78 changes: 77 additions & 1 deletion src/front/shared/redux/actions/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import config from 'helpers/externalConfig'
import { abi as FactoryV3ABI } from '@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Factory.sol/IUniswapV3Factory.json'
import { abi as QuoterV3ABI } from '@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json'
import { abi as SwapRouterV3ABI } from '@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json'
import { abi as PositionManagerV3ABI } from '@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json'
import { Interface as AbiInterface } from '@ethersproject/abi'


Expand All @@ -24,6 +25,7 @@ const ABIS = {
factory_v3: FactoryV3ABI,
quoter_v3: QuoterV3ABI,
router_v3: SwapRouterV3ABI,
position_manager_v3: PositionManagerV3ABI,
}

enum SwapMethods {
Expand All @@ -44,7 +46,7 @@ enum LiquidityMethods {
}

type GetContractParams = {
name: 'factory' | 'router' | 'pair' | 'factory_v3' | 'quoter_v3' | 'router_v3'
name: 'factory' | 'router' | 'pair' | 'factory_v3' | 'quoter_v3' | 'router_v3' | 'position_manager_v3'
address: string
baseCurrency: string
}
Expand Down Expand Up @@ -345,6 +347,80 @@ const returnSwapMethod = (params) => {
}
}

const createPoolV3 = async (params) => {
}

const addLiquidityPositionsV3 = async (params) => {
}
// fromToken 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270
// toToken 0xD10b8A62764852C754f66ebA75556F63938E9026
const getUserLiquidityPositionsV3 = async (params) => {
const {
owner,
baseCurrency,
chainId,
fromToken,
toToken,
} = params

console.log('>> getUserLiquidityPositionsV3', params)
const positionsContractAddress = config?.UNISWAP_V3_CONTRACTS[chainId]?.position_manager
const positionsContract = getContract({
name: 'position_manager_v3',
address: positionsContractAddress,
baseCurrency,
})

const positionsInterface = new AbiInterface(PositionManagerV3ABI)

// get count of user positions
const positionsCount = await positionsContract.methods.balanceOf(owner).call()
// get user positions ids
const tokenIdsCallArgs = (Array.apply(null, Array(Number(positionsCount)))).map((_, index) => {
return positionsInterface.encodeFunctionData('tokenOfOwnerByIndex', [owner, index])
})

const tokenIdsAnswer = await positionsContract.methods.multicall(tokenIdsCallArgs).call()

const userPositionsIds = tokenIdsAnswer.map((answer) => {
return positionsInterface.decodeFunctionResult('tokenOfOwnerByIndex', answer).toString()
})
// fetch positions detail information
const positionsInfoCallArgs = userPositionsIds.map((positionId) => {
return positionsInterface.encodeFunctionData('positions', [positionId])
})
const positionsInfoAnswer = await positionsContract.methods.multicall(positionsInfoCallArgs).call()

const positionsInfo = positionsInfoAnswer.map((answer, i) => {
const result = positionsInterface.decodeFunctionResult('positions', answer)
return {
tokenId: userPositionsIds[i],
fee: result.fee,
feeGrowthInside0LastX128: result.feeGrowthInside0LastX128.toString(),
feeGrowthInside1LastX128: result.feeGrowthInside1LastX128.toString(),
liquidity: result.liquidity.toString(),
nonce: result.nonce.toString(),
operator: result.operator,
tickLower: result.tickLower,
tickUpper: result.tickUpper,
token0: result.token0,
token1: result.token1,
tokensOwed0: result.tokensOwed0.toString(),
tokensOwed1: result.tokensOwed1.toString(),
}
})
if (fromToken && toToken) {
return positionsInfo.filter((positionInfo) => {
return (
((fromToken.toLowerCase() == positionInfo.token0.toLowerCase()) && (toToken.toLowerCase() == positionInfo.token1.toLowerCase()))
||
((fromToken.toLowerCase() == positionInfo.token1.toLowerCase()) && (toToken.toLowerCase() == positionInfo.token0.toLowerCase()))
)
})
} else return positionsInfo
}

window.getUserLiquidityPositionsV3 = getUserLiquidityPositionsV3

const swapCallbackV3 = async (params) => {

Expand Down

0 comments on commit 3ffc1e7

Please sign in to comment.