From 2826aba65e315e2e45eca2ef5d056215266e2c3d Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 2 Aug 2022 11:44:13 -0700 Subject: [PATCH 01/29] merge --- packages/frontend/src/state/positions/atoms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/state/positions/atoms.ts b/packages/frontend/src/state/positions/atoms.ts index 9ce484913..c429d5fa0 100644 --- a/packages/frontend/src/state/positions/atoms.ts +++ b/packages/frontend/src/state/positions/atoms.ts @@ -95,4 +95,4 @@ export const vaultHistoryUpdatingAtom = atom(false) export const isToHidePnLAtom = atom(false) export const swapsAtom = atom({ swaps: [] }) export const longPositionValueAtom = atom(BIG_ZERO) -export const shortPositionValueAtom = atom(BIG_ZERO) +export const shortPositionValueAtom = atom(BIG_ZERO) \ No newline at end of file From bd642dc8cec367aec7fca51802929e9ad3cc1c5e Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:37:12 -0700 Subject: [PATCH 02/29] squeeth price hook --- packages/frontend/src/state/controller/hooks.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/frontend/src/state/controller/hooks.ts b/packages/frontend/src/state/controller/hooks.ts index 830e0ff39..5217b2751 100644 --- a/packages/frontend/src/state/controller/hooks.ts +++ b/packages/frontend/src/state/controller/hooks.ts @@ -210,6 +210,17 @@ export const useGetTwapEthPrice = () => { return getTwapEthPrice } +export const useGetTwapSqueethPrice = () => { + const { squeethPool, oSqueeth, weth } = useAtomValue(addressesAtom) + const { getTwapSafe } = useOracle() + const getTwapSqueethPrice = useCallback( + async () => await getTwapSafe(squeethPool, oSqueeth, weth, TWAP_PERIOD), + [oSqueeth, weth, getTwapSafe, squeethPool], + ) + + return getTwapSqueethPrice +} + export const useGetShortAmountFromDebt = () => { const { ethUsdcPool, weth, usdc } = useAtomValue(addressesAtom) const normFactor = useAtomValue(normFactorAtom) From 39b66108ed5d763ee4acfaef8624f781d1e6cec6 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 2 Aug 2022 12:03:11 -0700 Subject: [PATCH 03/29] merge atoms merge --- packages/frontend/src/state/contracts/atoms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/state/contracts/atoms.ts b/packages/frontend/src/state/contracts/atoms.ts index f4e89597d..f1758fda9 100644 --- a/packages/frontend/src/state/contracts/atoms.ts +++ b/packages/frontend/src/state/contracts/atoms.ts @@ -90,4 +90,4 @@ export const controllerHelperHelperContractAtom = atom((get) => const { controllerHelper } = get(addressesAtom) if (!web3) return null return getContract(web3, controllerHelper, controllerHelperAbi) -}) \ No newline at end of file +}) From 47b639f290a10d939d179ed58e3b13a57bb36202 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Mon, 8 Aug 2022 23:00:55 -0700 Subject: [PATCH 04/29] twap -> spot --- packages/frontend/src/state/controller/hooks.ts | 11 ----------- packages/frontend/src/state/positions/atoms.ts | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/frontend/src/state/controller/hooks.ts b/packages/frontend/src/state/controller/hooks.ts index 5217b2751..830e0ff39 100644 --- a/packages/frontend/src/state/controller/hooks.ts +++ b/packages/frontend/src/state/controller/hooks.ts @@ -210,17 +210,6 @@ export const useGetTwapEthPrice = () => { return getTwapEthPrice } -export const useGetTwapSqueethPrice = () => { - const { squeethPool, oSqueeth, weth } = useAtomValue(addressesAtom) - const { getTwapSafe } = useOracle() - const getTwapSqueethPrice = useCallback( - async () => await getTwapSafe(squeethPool, oSqueeth, weth, TWAP_PERIOD), - [oSqueeth, weth, getTwapSafe, squeethPool], - ) - - return getTwapSqueethPrice -} - export const useGetShortAmountFromDebt = () => { const { ethUsdcPool, weth, usdc } = useAtomValue(addressesAtom) const normFactor = useAtomValue(normFactorAtom) diff --git a/packages/frontend/src/state/positions/atoms.ts b/packages/frontend/src/state/positions/atoms.ts index c429d5fa0..9ce484913 100644 --- a/packages/frontend/src/state/positions/atoms.ts +++ b/packages/frontend/src/state/positions/atoms.ts @@ -95,4 +95,4 @@ export const vaultHistoryUpdatingAtom = atom(false) export const isToHidePnLAtom = atom(false) export const swapsAtom = atom({ swaps: [] }) export const longPositionValueAtom = atom(BIG_ZERO) -export const shortPositionValueAtom = atom(BIG_ZERO) \ No newline at end of file +export const shortPositionValueAtom = atom(BIG_ZERO) From c00c897aa973a54a80a930fef3f23809e19d3686 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:43:10 -0700 Subject: [PATCH 05/29] imports --- packages/frontend/src/state/lp/lp.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 packages/frontend/src/state/lp/lp.tsx diff --git a/packages/frontend/src/state/lp/lp.tsx b/packages/frontend/src/state/lp/lp.tsx new file mode 100644 index 000000000..78ed6f8f0 --- /dev/null +++ b/packages/frontend/src/state/lp/lp.tsx @@ -0,0 +1,14 @@ +import { nearestUsableTick, TickMath } from '@uniswap/v3-sdk' +import { fromTokenAmount } from '@utils/calculations' +import { useAtomValue } from 'jotai' +import { addressesAtom, isWethToken0Atom } from '../positions/atoms' +import BigNumber from 'bignumber.js' +import { OSQUEETH_DECIMALS } from '@constants/index' +import { controllerContractAtom, controllerHelperHelperContractAtom, nftManagerContractAtom, quoterContractAtom, squeethPoolContractAtom } from '../contracts/atoms' +import useAppCallback from '@hooks/useAppCallback' +import { addressAtom } from '../wallet/atoms' +import { Contract } from 'web3-eth-contract' +import { useHandleTransaction } from '../wallet/hooks' +import { ethers } from 'ethers' +import { useCallback } from 'react' +import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../controller/hooks' \ No newline at end of file From 907923537154bcccc10afcc492d5f25a96956ce2 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:43:41 -0700 Subject: [PATCH 06/29] get position hook --- packages/frontend/src/state/lp/lp.tsx | 33 ++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/frontend/src/state/lp/lp.tsx b/packages/frontend/src/state/lp/lp.tsx index 78ed6f8f0..2ee968c0a 100644 --- a/packages/frontend/src/state/lp/lp.tsx +++ b/packages/frontend/src/state/lp/lp.tsx @@ -11,4 +11,35 @@ import { Contract } from 'web3-eth-contract' import { useHandleTransaction } from '../wallet/hooks' import { ethers } from 'ethers' import { useCallback } from 'react' -import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../controller/hooks' \ No newline at end of file +import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../controller/hooks' + +/*** GETTERS ***/ + +export const useGetPosition = () => { + const contract = useAtomValue(nftManagerContractAtom) + + const getPosition = useCallback( + async (uniTokenId: number) => { + if (!contract) return null + const position = await contract.methods.positions(uniTokenId).call() + const { nonce, operator, token0, token1, fee, tickLower, tickUpper, liquidity, feeGrowthInside0LastX128, feeGrowthInside1LastX128, tokensOwed0, tokensOwed1 } = position + return { + nonce, + operator, + token0, + token1, + fee, + tickLower, + tickUpper, + liquidity, + feeGrowthInside0LastX128, + feeGrowthInside1LastX128, + tokensOwed0, + tokensOwed1 + } + }, + [contract], + ) + + return getPosition + } \ No newline at end of file From f2a7ff65a0b688a6beb3fbc1edc9b5b086c0ca7f Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:44:06 -0700 Subject: [PATCH 07/29] decrease liquidity hook --- packages/frontend/src/state/lp/lp.tsx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/frontend/src/state/lp/lp.tsx b/packages/frontend/src/state/lp/lp.tsx index 2ee968c0a..3660f87cd 100644 --- a/packages/frontend/src/state/lp/lp.tsx +++ b/packages/frontend/src/state/lp/lp.tsx @@ -42,4 +42,29 @@ export const useGetPosition = () => { ) return getPosition + } + + export const useGetDecreaseLiquidity = () => { + const contract = useAtomValue(nftManagerContractAtom) + + const getDecreaseLiquiduity = useCallback( + async (tokenId: number, liquidity: number, amount0Min: number, amount1Min: number, deadline: number) => { + if (!contract) return null + const DecreaseLiquidityParams = { + tokenId, + liquidity, + amount0Min, + amount1Min, + deadline, + } + console.log("DecreaseLiquidityParams", DecreaseLiquidityParams) + + const decreaseLiquidity = await contract.methods.decreaseLiquidity(DecreaseLiquidityParams).call() + + return decreaseLiquidity + }, + [contract], + ) + + return getDecreaseLiquiduity } \ No newline at end of file From 281ab341c7d6d18db3f141943b047ae84fa61f88 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:44:34 -0700 Subject: [PATCH 08/29] get quote hook --- packages/frontend/src/state/lp/lp.tsx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/frontend/src/state/lp/lp.tsx b/packages/frontend/src/state/lp/lp.tsx index 3660f87cd..a1f3ca2e5 100644 --- a/packages/frontend/src/state/lp/lp.tsx +++ b/packages/frontend/src/state/lp/lp.tsx @@ -67,4 +67,29 @@ export const useGetPosition = () => { ) return getDecreaseLiquiduity + } + + export const useGetQuote = () => { + const contract = useAtomValue(quoterContractAtom) + const {weth, oSqueeth} = useAtomValue(addressesAtom) + + const getQuote = useCallback( + async (amount: BigNumber, squeethIn: boolean) => { + if (!contract) return null + + const QuoteExactInputSingleParams = { + tokenIn: squeethIn ? oSqueeth : weth, + tokenOut: squeethIn ? weth : oSqueeth, + amountIn: amount.toFixed(0), + fee: 3000, + sqrtPriceLimitX96: 0 + } + + const quote = await contract.methods.quoteExactInputSingle(QuoteExactInputSingleParams).call() + return quote.amountOut + }, + [contract, weth, oSqueeth], + ) + + return getQuote } \ No newline at end of file From f514d6be496391739aee293ec58a3e1fe61347d7 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:45:01 -0700 Subject: [PATCH 09/29] get pool state hook --- packages/frontend/src/state/lp/lp.tsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/frontend/src/state/lp/lp.tsx b/packages/frontend/src/state/lp/lp.tsx index a1f3ca2e5..43730bbda 100644 --- a/packages/frontend/src/state/lp/lp.tsx +++ b/packages/frontend/src/state/lp/lp.tsx @@ -92,4 +92,24 @@ export const useGetPosition = () => { ) return getQuote - } \ No newline at end of file + } + + async function getPoolState(squeethContract: Contract) { + const [slot, liquidity] = await Promise.all([ + squeethContract?.methods.slot0().call(), + squeethContract?.methods.liquidity().call(), + ]) + + const PoolState = { + liquidity, + sqrtPriceX96: slot[0], + tick: slot[1], + observationIndex: slot[2], + observationCardinality: slot[3], + observationCardinalityNext: slot[4], + feeProtocol: slot[5], + unlocked: slot[6], + } + + return PoolState + } \ No newline at end of file From d08aaab5337876de3f2322e42c140cf3fa2296b8 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:06:16 -0700 Subject: [PATCH 10/29] rename hooks rebase --- packages/frontend/src/state/lp/hooks.ts | 47 +++++----- packages/frontend/src/state/lp/lp.tsx | 115 ------------------------ 2 files changed, 26 insertions(+), 136 deletions(-) delete mode 100644 packages/frontend/src/state/lp/lp.tsx diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index ceaf48c9e..5e59e2e57 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -19,6 +19,7 @@ import { ethers } from 'ethers' import { useCallback } from 'react' import { useGetDebtAmount, useGetVault } from '../controller/hooks' import { indexAtom, normFactorAtom } from '../controller/atoms' +import { getPoolState } from '../squeethPool/hooks' /*** CONSTANTS ***/ const COLLAT_RATIO_FLASHLOAN = 2 @@ -326,24 +327,28 @@ export const useGetExactOut = () => { return getExactOut } -async function getPoolState(poolContract: Contract) { - const [slot, liquidity, tickSpacing] = await Promise.all([ - poolContract?.methods.slot0().call(), - poolContract?.methods.liquidity().call(), - poolContract.methods.tickSpacing().call(), - ]) - - const PoolState = { - liquidity, - sqrtPriceX96: slot[0], - tick: slot[1], - observationIndex: slot[2], - observationCardinality: slot[3], - observationCardinalityNext: slot[4], - feeProtocol: slot[5], - unlocked: slot[6], - tickSpacing, - } - - return PoolState -} \ No newline at end of file + +export const useGetQuote = () => { + const contract = useAtomValue(quoterContractAtom) + const {weth, oSqueeth} = useAtomValue(addressesAtom) + + const getQuote = useCallback( + async (amount: BigNumber, squeethIn: boolean) => { + if (!contract) return null + + const QuoteExactInputSingleParams = { + tokenIn: squeethIn ? oSqueeth : weth, + tokenOut: squeethIn ? weth : oSqueeth, + amountIn: amount.toFixed(0), + fee: 3000, + sqrtPriceLimitX96: 0 + } + + const quote = await contract.methods.quoteExactInputSingle(QuoteExactInputSingleParams).call() + return quote.amountOut + }, + [contract, weth, oSqueeth], + ) + + return getQuote +} diff --git a/packages/frontend/src/state/lp/lp.tsx b/packages/frontend/src/state/lp/lp.tsx deleted file mode 100644 index 43730bbda..000000000 --- a/packages/frontend/src/state/lp/lp.tsx +++ /dev/null @@ -1,115 +0,0 @@ -import { nearestUsableTick, TickMath } from '@uniswap/v3-sdk' -import { fromTokenAmount } from '@utils/calculations' -import { useAtomValue } from 'jotai' -import { addressesAtom, isWethToken0Atom } from '../positions/atoms' -import BigNumber from 'bignumber.js' -import { OSQUEETH_DECIMALS } from '@constants/index' -import { controllerContractAtom, controllerHelperHelperContractAtom, nftManagerContractAtom, quoterContractAtom, squeethPoolContractAtom } from '../contracts/atoms' -import useAppCallback from '@hooks/useAppCallback' -import { addressAtom } from '../wallet/atoms' -import { Contract } from 'web3-eth-contract' -import { useHandleTransaction } from '../wallet/hooks' -import { ethers } from 'ethers' -import { useCallback } from 'react' -import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../controller/hooks' - -/*** GETTERS ***/ - -export const useGetPosition = () => { - const contract = useAtomValue(nftManagerContractAtom) - - const getPosition = useCallback( - async (uniTokenId: number) => { - if (!contract) return null - const position = await contract.methods.positions(uniTokenId).call() - const { nonce, operator, token0, token1, fee, tickLower, tickUpper, liquidity, feeGrowthInside0LastX128, feeGrowthInside1LastX128, tokensOwed0, tokensOwed1 } = position - return { - nonce, - operator, - token0, - token1, - fee, - tickLower, - tickUpper, - liquidity, - feeGrowthInside0LastX128, - feeGrowthInside1LastX128, - tokensOwed0, - tokensOwed1 - } - }, - [contract], - ) - - return getPosition - } - - export const useGetDecreaseLiquidity = () => { - const contract = useAtomValue(nftManagerContractAtom) - - const getDecreaseLiquiduity = useCallback( - async (tokenId: number, liquidity: number, amount0Min: number, amount1Min: number, deadline: number) => { - if (!contract) return null - const DecreaseLiquidityParams = { - tokenId, - liquidity, - amount0Min, - amount1Min, - deadline, - } - console.log("DecreaseLiquidityParams", DecreaseLiquidityParams) - - const decreaseLiquidity = await contract.methods.decreaseLiquidity(DecreaseLiquidityParams).call() - - return decreaseLiquidity - }, - [contract], - ) - - return getDecreaseLiquiduity - } - - export const useGetQuote = () => { - const contract = useAtomValue(quoterContractAtom) - const {weth, oSqueeth} = useAtomValue(addressesAtom) - - const getQuote = useCallback( - async (amount: BigNumber, squeethIn: boolean) => { - if (!contract) return null - - const QuoteExactInputSingleParams = { - tokenIn: squeethIn ? oSqueeth : weth, - tokenOut: squeethIn ? weth : oSqueeth, - amountIn: amount.toFixed(0), - fee: 3000, - sqrtPriceLimitX96: 0 - } - - const quote = await contract.methods.quoteExactInputSingle(QuoteExactInputSingleParams).call() - return quote.amountOut - }, - [contract, weth, oSqueeth], - ) - - return getQuote - } - - async function getPoolState(squeethContract: Contract) { - const [slot, liquidity] = await Promise.all([ - squeethContract?.methods.slot0().call(), - squeethContract?.methods.liquidity().call(), - ]) - - const PoolState = { - liquidity, - sqrtPriceX96: slot[0], - tick: slot[1], - observationIndex: slot[2], - observationCardinality: slot[3], - observationCardinalityNext: slot[4], - feeProtocol: slot[5], - unlocked: slot[6], - } - - return PoolState - } \ No newline at end of file From abbe0d7c7b172e897e7003059e9b14381289ec19 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:32:41 -0700 Subject: [PATCH 11/29] add controllerhelper address --- packages/frontend/src/constants/address.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/constants/address.ts b/packages/frontend/src/constants/address.ts index 7e91cfa46..6908b79d7 100644 --- a/packages/frontend/src/constants/address.ts +++ b/packages/frontend/src/constants/address.ts @@ -55,7 +55,7 @@ export const SWAP_ROUTER: Address = { export const SWAP_ROUTER_02: Address = { 1: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', - 3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', + 3: '', 421611: '', 31337: '', } From c35573fb60c1b54220a3c4bc1dd1b801d77d8b8e Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 2 Aug 2022 12:54:39 -0700 Subject: [PATCH 12/29] merge --- packages/frontend/src/state/contracts/atoms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/state/contracts/atoms.ts b/packages/frontend/src/state/contracts/atoms.ts index f1758fda9..f4e89597d 100644 --- a/packages/frontend/src/state/contracts/atoms.ts +++ b/packages/frontend/src/state/contracts/atoms.ts @@ -90,4 +90,4 @@ export const controllerHelperHelperContractAtom = atom((get) => const { controllerHelper } = get(addressesAtom) if (!web3) return null return getContract(web3, controllerHelper, controllerHelperAbi) -}) +}) \ No newline at end of file From 57179c39cd519ddc1121625a853f842b2ba52f4f Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:12:11 -0700 Subject: [PATCH 13/29] tick space --- packages/frontend/src/state/lp/hooks.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 5e59e2e57..a3e07af19 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -136,6 +136,9 @@ export const useOpenPositionDeposit = () => { return openPositionDeposit } +/*** CONSTANTS ***/ +const TICK_SPACE = 60 + /*** GETTERS ***/ export const useGetPosition = () => { From 7bb6e917c8cda7d86ae007092d6d939aca3dcd10 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:15:06 -0700 Subject: [PATCH 14/29] open hook --- packages/frontend/src/state/lp/hooks.ts | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index a3e07af19..b94539bfb 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -139,6 +139,57 @@ export const useOpenPositionDeposit = () => { /*** CONSTANTS ***/ const TICK_SPACE = 60 +/*** ACTIONS ***/ + +// Opening a mint and LP position and depositing +export const useOpenPositionDeposit = () => { + const { squeethPool } = useAtomValue(addressesAtom) + const address = useAtomValue(addressAtom) + const contract = useAtomValue(controllerHelperHelperContractAtom) + const handleTransaction = useHandleTransaction() + const getTwapSqueethPrice = useGetTwapSqueethPrice() + const getDebtAmount = useGetDebtAmount() + const openPositionDeposit = useAppCallback( + async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, onTxConfirmed?: () => void) => { + if (!contract || !address) return null + + const squeethPrice = await getTwapSqueethPrice() + const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS) + const ethDebt = await getDebtAmount(mintWSqueethAmount) + + const collateralToMint = ethDebt.multipliedBy(1.5) + const collateralToLp = mintWSqueethAmount.multipliedBy(squeethPrice) + + const lowerTick = nearestUsableTick(lowerTickInput, TICK_SPACE) + const upperTick = nearestUsableTick(upperTickInput, TICK_SPACE) + + const flashloanWMintDepositNftParams = { + wPowerPerpPool: squeethPool, + vaultId: 0, + wPowerPerpAmount: mintWSqueethAmount.toFixed(0), + collateralToDeposit: collateralToMint.toFixed(0), + collateralToFlashloan: collateralToMint.toFixed(0), + collateralToLp: collateralToLp.toFixed(0), + collateralToWithdraw: 0, + amount0Min: 0, + amount1Min: 0, + lowerTick: lowerTick, + upperTick: upperTick, + } + + return handleTransaction( + contract.methods.flashloanWMintLpDepositNft(flashloanWMintDepositNftParams).send({ + from: address, + value: collateralToLp.toFixed(0), + }), + onTxConfirmed, + ) + }, + [address, squeethPool, contract, handleTransaction, getTwapSqueethPrice, getDebtAmount], + ) + return openPositionDeposit +} + /*** GETTERS ***/ export const useGetPosition = () => { From cf6df2f8cd3fa99bf03fdf989f4beaac0f5ef2f9 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:15:50 -0700 Subject: [PATCH 15/29] collat ratio const --- packages/frontend/src/state/lp/hooks.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index b94539bfb..b4f3a10ea 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -138,6 +138,7 @@ export const useOpenPositionDeposit = () => { /*** CONSTANTS ***/ const TICK_SPACE = 60 +const COLLAT_RATIO = 1.5 /*** ACTIONS ***/ @@ -157,7 +158,7 @@ export const useOpenPositionDeposit = () => { const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS) const ethDebt = await getDebtAmount(mintWSqueethAmount) - const collateralToMint = ethDebt.multipliedBy(1.5) + const collateralToMint = ethDebt.multipliedBy(COLLAT_RATIO) const collateralToLp = mintWSqueethAmount.multipliedBy(squeethPrice) const lowerTick = nearestUsableTick(lowerTickInput, TICK_SPACE) From 42e9dff43a6b75eab6181546b7708f11dfbd4cb8 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Fri, 15 Jul 2022 22:19:57 -0700 Subject: [PATCH 16/29] variable vault id --- packages/frontend/src/state/lp/hooks.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index b4f3a10ea..c4b8b88a7 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -151,7 +151,7 @@ export const useOpenPositionDeposit = () => { const getTwapSqueethPrice = useGetTwapSqueethPrice() const getDebtAmount = useGetDebtAmount() const openPositionDeposit = useAppCallback( - async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, onTxConfirmed?: () => void) => { + async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, onTxConfirmed?: () => void) => { if (!contract || !address) return null const squeethPrice = await getTwapSqueethPrice() @@ -166,7 +166,7 @@ export const useOpenPositionDeposit = () => { const flashloanWMintDepositNftParams = { wPowerPerpPool: squeethPool, - vaultId: 0, + vaultId: vaultId, wPowerPerpAmount: mintWSqueethAmount.toFixed(0), collateralToDeposit: collateralToMint.toFixed(0), collateralToFlashloan: collateralToMint.toFixed(0), From 7a336907104e7720197dd32232796072d0f1ba0b Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Fri, 15 Jul 2022 22:30:44 -0700 Subject: [PATCH 17/29] update to promise.all --- packages/frontend/src/state/lp/hooks.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index c4b8b88a7..8ddac9692 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -154,9 +154,10 @@ export const useOpenPositionDeposit = () => { async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, onTxConfirmed?: () => void) => { if (!contract || !address) return null - const squeethPrice = await getTwapSqueethPrice() const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS) - const ethDebt = await getDebtAmount(mintWSqueethAmount) + const squeethPricePromise = getTwapSqueethPrice() + const ethDebtPromise = getDebtAmount(mintWSqueethAmount) + const [squeethPrice, ethDebt] = await Promise.all([squeethPricePromise, ethDebtPromise]) const collateralToMint = ethDebt.multipliedBy(COLLAT_RATIO) const collateralToLp = mintWSqueethAmount.multipliedBy(squeethPrice) From 089b4a37a91b13c0b9fb92c4d54a2386838f51ba Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 2 Aug 2022 13:00:40 -0700 Subject: [PATCH 18/29] router 02 address --- packages/frontend/src/constants/address.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/constants/address.ts b/packages/frontend/src/constants/address.ts index 6908b79d7..7e91cfa46 100644 --- a/packages/frontend/src/constants/address.ts +++ b/packages/frontend/src/constants/address.ts @@ -55,7 +55,7 @@ export const SWAP_ROUTER: Address = { export const SWAP_ROUTER_02: Address = { 1: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', - 3: '', + 3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', 421611: '', 31337: '', } From a0883ee731badfc4763fa262cc01356790e4480d Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 2 Aug 2022 13:09:33 -0700 Subject: [PATCH 19/29] update collat ratio to 200 --- packages/frontend/src/state/lp/hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 8ddac9692..74b291ad8 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -138,7 +138,7 @@ export const useOpenPositionDeposit = () => { /*** CONSTANTS ***/ const TICK_SPACE = 60 -const COLLAT_RATIO = 1.5 +const COLLAT_RATIO = 2 /*** ACTIONS ***/ From 1740581fbd9128873fe75ae77ef1c7b794cf5336 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Mon, 8 Aug 2022 23:15:56 -0700 Subject: [PATCH 20/29] review updates --- packages/frontend/src/state/lp/hooks.ts | 50 ++++++++++++++++--------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 74b291ad8..11f2566d7 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -136,10 +136,6 @@ export const useOpenPositionDeposit = () => { return openPositionDeposit } -/*** CONSTANTS ***/ -const TICK_SPACE = 60 -const COLLAT_RATIO = 2 - /*** ACTIONS ***/ // Opening a mint and LP position and depositing @@ -148,22 +144,39 @@ export const useOpenPositionDeposit = () => { const address = useAtomValue(addressAtom) const contract = useAtomValue(controllerHelperHelperContractAtom) const handleTransaction = useHandleTransaction() - const getTwapSqueethPrice = useGetTwapSqueethPrice() const getDebtAmount = useGetDebtAmount() + const squeethPoolContract = useAtomValue(squeethPoolContractAtom) + const isWethToken0 = useAtomValue(isWethToken0Atom) const openPositionDeposit = useAppCallback( - async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, onTxConfirmed?: () => void) => { - if (!contract || !address) return null + async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, collatRatio: number, slippage: number, onTxConfirmed?: () => void) => { + if (!contract || !address || !squeethPoolContract) return null const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS) - const squeethPricePromise = getTwapSqueethPrice() const ethDebtPromise = getDebtAmount(mintWSqueethAmount) - const [squeethPrice, ethDebt] = await Promise.all([squeethPricePromise, ethDebtPromise]) - - const collateralToMint = ethDebt.multipliedBy(COLLAT_RATIO) - const collateralToLp = mintWSqueethAmount.multipliedBy(squeethPrice) + const poolStatePromise = getPoolState(squeethPoolContract) - const lowerTick = nearestUsableTick(lowerTickInput, TICK_SPACE) - const upperTick = nearestUsableTick(upperTickInput, TICK_SPACE) + // Calculate prices from ticks + const [ethDebt, { tick, tickSpacing }] = await Promise.all([ethDebtPromise, poolStatePromise]) + const lowerTick = nearestUsableTick(lowerTickInput, Number(tickSpacing)) + const upperTick = nearestUsableTick(upperTickInput, Number(tickSpacing)) + const sqrtLowerPrice = new BigNumber(TickMath.getSqrtRatioAtTick(lowerTick).toString()).div(x96) + const sqrtUpperPrice = new BigNumber(TickMath.getSqrtRatioAtTick(upperTick).toString()).div(x96) + const squeethPrice = isWethToken0 ? new BigNumber(1).div(new BigNumber(TickMath.getSqrtRatioAtTick(Number(tick)).toString()).div(x96).pow(2)) + : new BigNumber(TickMath.getSqrtRatioAtTick(Number(tick)).toString()).div(x96).pow(2) + const sqrtSqueethPrice = squeethPrice.sqrt() + const collateralToMint = ethDebt.multipliedBy(collatRatio) + + // Lx = x * (sqrtSqueethPrice * sqrtUpperPrice) / (sqrtUpperPrice - sqrtSqueethPrice) + // y = Lx * (sqrtSqueethPrice - sqrtLowerPrice) + const liquidity = mintWSqueethAmount.times(sqrtSqueethPrice.times(sqrtUpperPrice)).div(sqrtUpperPrice.minus(sqrtSqueethPrice)) + const collateralToLp = sqrtUpperPrice.lt(sqrtSqueethPrice) ? liquidity.times(sqrtUpperPrice.minus(sqrtLowerPrice)) + : sqrtSqueethPrice.lt(sqrtLowerPrice) ? new BigNumber(0) + : liquidity.times(sqrtSqueethPrice.minus(sqrtLowerPrice)) + + const amount0 = isWethToken0 ? collateralToLp : mintWSqueethAmount + const amount1 = isWethToken0 ? mintWSqueethAmount : collateralToLp + const amount0Min = amount0.times(new BigNumber(1).minus(slippage)).toFixed(0) + const amount1Min = amount1.times(new BigNumber(1).minus(slippage)).toFixed(0) const flashloanWMintDepositNftParams = { wPowerPerpPool: squeethPool, @@ -173,8 +186,8 @@ export const useOpenPositionDeposit = () => { collateralToFlashloan: collateralToMint.toFixed(0), collateralToLp: collateralToLp.toFixed(0), collateralToWithdraw: 0, - amount0Min: 0, - amount1Min: 0, + amount0Min, + amount1Min, lowerTick: lowerTick, upperTick: upperTick, } @@ -182,13 +195,14 @@ export const useOpenPositionDeposit = () => { return handleTransaction( contract.methods.flashloanWMintLpDepositNft(flashloanWMintDepositNftParams).send({ from: address, - value: collateralToLp.toFixed(0), + value: collateralToLp.plus(collateralToMint).toFixed(0), }), onTxConfirmed, ) }, - [address, squeethPool, contract, handleTransaction, getTwapSqueethPrice, getDebtAmount], + [address, squeethPool, contract, handleTransaction, getDebtAmount], ) + return openPositionDeposit } From 0d4ce8f554ce49478f282b13e3374f83794f88a4 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Mon, 8 Aug 2022 23:29:09 -0700 Subject: [PATCH 21/29] withdraw & dependencies --- packages/frontend/src/state/lp/hooks.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 11f2566d7..8743f1d97 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -148,7 +148,7 @@ export const useOpenPositionDeposit = () => { const squeethPoolContract = useAtomValue(squeethPoolContractAtom) const isWethToken0 = useAtomValue(isWethToken0Atom) const openPositionDeposit = useAppCallback( - async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, collatRatio: number, slippage: number, onTxConfirmed?: () => void) => { + async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, collatRatio: number, slippage: number, withdrawAmount: number, onTxConfirmed?: () => void) => { if (!contract || !address || !squeethPoolContract) return null const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS) @@ -178,6 +178,8 @@ export const useOpenPositionDeposit = () => { const amount0Min = amount0.times(new BigNumber(1).minus(slippage)).toFixed(0) const amount1Min = amount1.times(new BigNumber(1).minus(slippage)).toFixed(0) + const collateralToWithdraw = new BigNumber(withdrawAmount) + const flashloanWMintDepositNftParams = { wPowerPerpPool: squeethPool, vaultId: vaultId, @@ -185,7 +187,7 @@ export const useOpenPositionDeposit = () => { collateralToDeposit: collateralToMint.toFixed(0), collateralToFlashloan: collateralToMint.toFixed(0), collateralToLp: collateralToLp.toFixed(0), - collateralToWithdraw: 0, + collateralToWithdraw: collateralToWithdraw.toFixed(0), amount0Min, amount1Min, lowerTick: lowerTick, @@ -195,12 +197,12 @@ export const useOpenPositionDeposit = () => { return handleTransaction( contract.methods.flashloanWMintLpDepositNft(flashloanWMintDepositNftParams).send({ from: address, - value: collateralToLp.plus(collateralToMint).toFixed(0), + value: collateralToLp.plus(collateralToMint).minus(collateralToWithdraw).toFixed(0), }), onTxConfirmed, ) }, - [address, squeethPool, contract, handleTransaction, getDebtAmount], + [address, squeethPool, contract, handleTransaction, getDebtAmount, squeethPoolContract, isWethToken0], ) return openPositionDeposit From df902563a5c2b7dbfdbaa21b3215d5df527b0026 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:32:41 -0700 Subject: [PATCH 22/29] add controllerhelper address --- packages/frontend/src/constants/address.ts | 2 +- packages/frontend/src/state/positions/atoms.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/frontend/src/constants/address.ts b/packages/frontend/src/constants/address.ts index 7e91cfa46..6908b79d7 100644 --- a/packages/frontend/src/constants/address.ts +++ b/packages/frontend/src/constants/address.ts @@ -55,7 +55,7 @@ export const SWAP_ROUTER: Address = { export const SWAP_ROUTER_02: Address = { 1: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', - 3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', + 3: '', 421611: '', 31337: '', } diff --git a/packages/frontend/src/state/positions/atoms.ts b/packages/frontend/src/state/positions/atoms.ts index 9ce484913..c429d5fa0 100644 --- a/packages/frontend/src/state/positions/atoms.ts +++ b/packages/frontend/src/state/positions/atoms.ts @@ -95,4 +95,4 @@ export const vaultHistoryUpdatingAtom = atom(false) export const isToHidePnLAtom = atom(false) export const swapsAtom = atom({ swaps: [] }) export const longPositionValueAtom = atom(BIG_ZERO) -export const shortPositionValueAtom = atom(BIG_ZERO) +export const shortPositionValueAtom = atom(BIG_ZERO) \ No newline at end of file From 27b0146b6f0c3609672c7185061bdb675169da13 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:12:11 -0700 Subject: [PATCH 23/29] tick space --- packages/frontend/src/state/lp/hooks.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 8743f1d97..da8b69785 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -208,6 +208,9 @@ export const useOpenPositionDeposit = () => { return openPositionDeposit } +/*** CONSTANTS ***/ +const TICK_SPACE = 60 + /*** GETTERS ***/ export const useGetPosition = () => { From 8938fe792b7bc4c1e935f59c50d608af1329b04e Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:22:21 -0700 Subject: [PATCH 24/29] close position hook --- packages/frontend/src/state/lp/hooks.ts | 56 ++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index da8b69785..fdcc33f34 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -209,7 +209,61 @@ export const useOpenPositionDeposit = () => { } /*** CONSTANTS ***/ -const TICK_SPACE = 60 + +// Close position with flashloan +export const useClosePosition = () => { + const address = useAtomValue(addressAtom) + const controllerHelperContract = useAtomValue(controllerHelperHelperContractAtom) + const controllerContract = useAtomValue(controllerContractAtom) + const handleTransaction = useHandleTransaction() + const getDebtAmount = useGetDebtAmount() + const getVault = useGetVault() + const getPosition = useGetPosition() + const getQuote = useGetQuote() + const closePosition = useAppCallback(async (vaultId: number, onTxConfirmed?: () => void) => { + const vaultBefore = await getVault(vaultId) + const uniTokenId = vaultBefore?.NFTCollateralId + const position = await getPosition(uniTokenId) + + if ( + !controllerContract || + !controllerHelperContract || + !address || + !position || + !vaultBefore || + !vaultBefore.shortAmount + ) + return + + const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) + const debtInEth = await getDebtAmount(shortAmount) + const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO) + const limitEth = await getQuote(shortAmount, true) + + const flashloanCloseVaultLpNftParam = { + vaultId: vaultId, + tokenId: uniTokenId, + liquidity: position.liquidity, + liquidityPercentage: fromTokenAmount(1, 18).toFixed(0), + wPowerPerpAmountToBurn: shortAmount.toFixed(0), + collateralToFlashloan: collateralToFlashloan.toFixed(0), + collateralToWithdraw: 0, + limitPriceEthPerPowerPerp: limitEth, + amount0Min: 0, + amount1Min: 0, + poolFee: POOL_FEE, + burnExactRemoved: true, + } + + return handleTransaction( + await controllerHelperContract.methods.flashloanCloseVaultLpNft(flashloanCloseVaultLpNftParam).send({ + from: address, + }), + onTxConfirmed, + ) + }, [address, controllerHelperContract, controllerContract, handleTransaction, getDebtAmount, getVault, getPosition, getQuote]) + return closePosition +} /*** GETTERS ***/ From 8d439dabdeeee49a3681a44775d6a94339507cf1 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Fri, 15 Jul 2022 22:42:22 -0700 Subject: [PATCH 25/29] use promise.all --- packages/frontend/src/state/lp/hooks.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index fdcc33f34..8284b6663 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -235,10 +235,11 @@ export const useClosePosition = () => { ) return - const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) - const debtInEth = await getDebtAmount(shortAmount) - const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO) - const limitEth = await getQuote(shortAmount, true) + const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) + const debtInEthPromise = getDebtAmount(shortAmount) + const limitEthPromise = getQuote(shortAmount, true) + const [debtInEth, limitEth] = await Promise.all([debtInEthPromise, limitEthPromise]) + const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO) const flashloanCloseVaultLpNftParam = { vaultId: vaultId, From a47064b00faab18b5ab8bcc2279fcaa3bf39eae0 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Mon, 8 Aug 2022 23:41:36 -0700 Subject: [PATCH 26/29] review changes --- packages/frontend/src/state/lp/hooks.ts | 49 ++++++++++++++++--------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 8284b6663..7ecf1aaa2 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -208,8 +208,6 @@ export const useOpenPositionDeposit = () => { return openPositionDeposit } -/*** CONSTANTS ***/ - // Close position with flashloan export const useClosePosition = () => { const address = useAtomValue(addressAtom) @@ -219,8 +217,11 @@ export const useClosePosition = () => { const getDebtAmount = useGetDebtAmount() const getVault = useGetVault() const getPosition = useGetPosition() - const getQuote = useGetQuote() - const closePosition = useAppCallback(async (vaultId: number, onTxConfirmed?: () => void) => { + const getExactIn = useGetExactIn() + const getExactOut = useGetExactOut() + const getDecreaseLiquidity = useGetDecreaseLiquidity() + const isWethToken0 = useAtomValue(isWethToken0Atom) + const closePosition = useAppCallback(async (vaultId: number, liquidityPercentage: number, burnPercentage: number, collateralToWithdraw: number, burnExactRemoved: boolean, slippage: number, onTxConfirmed?: () => void) => { const vaultBefore = await getVault(vaultId) const uniTokenId = vaultBefore?.NFTCollateralId const position = await getPosition(uniTokenId) @@ -235,25 +236,39 @@ export const useClosePosition = () => { ) return - const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) - const debtInEthPromise = getDebtAmount(shortAmount) - const limitEthPromise = getQuote(shortAmount, true) - const [debtInEth, limitEth] = await Promise.all([debtInEthPromise, limitEthPromise]) - const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO) + const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) + const debtInEthPromise = getDebtAmount(shortAmount) + + // Get current LP positions + const { amount0, amount1 } = await getDecreaseLiquidity(uniTokenId, position.liquidity, 0, 0, Math.floor(Date.now() / 1000 + 86400)) + const wPowerPerpAmountInLP = isWethToken0 ? amount1 : amount0 + + const amountToLiquidate = new BigNumber(wPowerPerpAmountInLP).times(liquidityPercentage) + const amountToBurn = shortAmount.times(burnPercentage) + const limitEthPromise = amountToLiquidate.gt(amountToBurn) ? getExactIn(amountToLiquidate.minus(amountToBurn), true) + : getExactOut(amountToBurn.minus(amountToLiquidate), true) + + const [debtInEth, limitEth] = await Promise.all([debtInEthPromise, limitEthPromise]) + const limitPrice = new BigNumber(limitEth).div(amountToLiquidate.minus(amountToBurn).abs()).times(new BigNumber(1).minus(slippage)) + + const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO_FLASHLOAN) + + const amount0Min = new BigNumber(amount0).times(liquidityPercentage).times(new BigNumber(1).minus(slippage)).toFixed(0) + const amount1Min = new BigNumber(amount1).times(liquidityPercentage).times(new BigNumber(1).minus(slippage)).toFixed(0) const flashloanCloseVaultLpNftParam = { vaultId: vaultId, tokenId: uniTokenId, liquidity: position.liquidity, - liquidityPercentage: fromTokenAmount(1, 18).toFixed(0), - wPowerPerpAmountToBurn: shortAmount.toFixed(0), + liquidityPercentage: fromTokenAmount(liquidityPercentage, 18).toFixed(0), + wPowerPerpAmountToBurn: amountToBurn.toFixed(0), collateralToFlashloan: collateralToFlashloan.toFixed(0), - collateralToWithdraw: 0, - limitPriceEthPerPowerPerp: limitEth, - amount0Min: 0, - amount1Min: 0, + collateralToWithdraw: collateralToWithdraw.toFixed(0), + limitPriceEthPerPowerPerp: fromTokenAmount(limitPrice, 18).toFixed(0), + amount0Min, + amount1Min, poolFee: POOL_FEE, - burnExactRemoved: true, + burnExactRemoved, } return handleTransaction( @@ -262,7 +277,7 @@ export const useClosePosition = () => { }), onTxConfirmed, ) - }, [address, controllerHelperContract, controllerContract, handleTransaction, getDebtAmount, getVault, getPosition, getQuote]) + }, [address, controllerHelperContract, controllerContract, handleTransaction, getDebtAmount, getVault, getPosition, getExactIn, getExactOut, getDecreaseLiquidity, isWethToken0]) return closePosition } From 7d9e72f1bc23382d83945ea53d91da36fc2aea6a Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Mon, 8 Aug 2022 23:46:30 -0700 Subject: [PATCH 27/29] address --- packages/frontend/src/constants/address.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/constants/address.ts b/packages/frontend/src/constants/address.ts index 6908b79d7..7e91cfa46 100644 --- a/packages/frontend/src/constants/address.ts +++ b/packages/frontend/src/constants/address.ts @@ -55,7 +55,7 @@ export const SWAP_ROUTER: Address = { export const SWAP_ROUTER_02: Address = { 1: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', - 3: '', + 3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', 421611: '', 31337: '', } From 77b8d9c01e5c6cf55fae581c1b68a696d15aeef4 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Thu, 25 Aug 2022 13:20:59 -0700 Subject: [PATCH 28/29] title --- packages/frontend/src/state/lp/hooks.ts | 74 +------------------------ 1 file changed, 1 insertion(+), 73 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 7ecf1aaa2..c90231a0d 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -136,80 +136,8 @@ export const useOpenPositionDeposit = () => { return openPositionDeposit } -/*** ACTIONS ***/ - -// Opening a mint and LP position and depositing -export const useOpenPositionDeposit = () => { - const { squeethPool } = useAtomValue(addressesAtom) - const address = useAtomValue(addressAtom) - const contract = useAtomValue(controllerHelperHelperContractAtom) - const handleTransaction = useHandleTransaction() - const getDebtAmount = useGetDebtAmount() - const squeethPoolContract = useAtomValue(squeethPoolContractAtom) - const isWethToken0 = useAtomValue(isWethToken0Atom) - const openPositionDeposit = useAppCallback( - async (squeethToMint: BigNumber, lowerTickInput: number, upperTickInput: number, vaultId: number, collatRatio: number, slippage: number, withdrawAmount: number, onTxConfirmed?: () => void) => { - if (!contract || !address || !squeethPoolContract) return null - - const mintWSqueethAmount = fromTokenAmount(squeethToMint, OSQUEETH_DECIMALS) - const ethDebtPromise = getDebtAmount(mintWSqueethAmount) - const poolStatePromise = getPoolState(squeethPoolContract) - - // Calculate prices from ticks - const [ethDebt, { tick, tickSpacing }] = await Promise.all([ethDebtPromise, poolStatePromise]) - const lowerTick = nearestUsableTick(lowerTickInput, Number(tickSpacing)) - const upperTick = nearestUsableTick(upperTickInput, Number(tickSpacing)) - const sqrtLowerPrice = new BigNumber(TickMath.getSqrtRatioAtTick(lowerTick).toString()).div(x96) - const sqrtUpperPrice = new BigNumber(TickMath.getSqrtRatioAtTick(upperTick).toString()).div(x96) - const squeethPrice = isWethToken0 ? new BigNumber(1).div(new BigNumber(TickMath.getSqrtRatioAtTick(Number(tick)).toString()).div(x96).pow(2)) - : new BigNumber(TickMath.getSqrtRatioAtTick(Number(tick)).toString()).div(x96).pow(2) - const sqrtSqueethPrice = squeethPrice.sqrt() - const collateralToMint = ethDebt.multipliedBy(collatRatio) - - // Lx = x * (sqrtSqueethPrice * sqrtUpperPrice) / (sqrtUpperPrice - sqrtSqueethPrice) - // y = Lx * (sqrtSqueethPrice - sqrtLowerPrice) - const liquidity = mintWSqueethAmount.times(sqrtSqueethPrice.times(sqrtUpperPrice)).div(sqrtUpperPrice.minus(sqrtSqueethPrice)) - const collateralToLp = sqrtUpperPrice.lt(sqrtSqueethPrice) ? liquidity.times(sqrtUpperPrice.minus(sqrtLowerPrice)) - : sqrtSqueethPrice.lt(sqrtLowerPrice) ? new BigNumber(0) - : liquidity.times(sqrtSqueethPrice.minus(sqrtLowerPrice)) - - const amount0 = isWethToken0 ? collateralToLp : mintWSqueethAmount - const amount1 = isWethToken0 ? mintWSqueethAmount : collateralToLp - const amount0Min = amount0.times(new BigNumber(1).minus(slippage)).toFixed(0) - const amount1Min = amount1.times(new BigNumber(1).minus(slippage)).toFixed(0) - - const collateralToWithdraw = new BigNumber(withdrawAmount) - - const flashloanWMintDepositNftParams = { - wPowerPerpPool: squeethPool, - vaultId: vaultId, - wPowerPerpAmount: mintWSqueethAmount.toFixed(0), - collateralToDeposit: collateralToMint.toFixed(0), - collateralToFlashloan: collateralToMint.toFixed(0), - collateralToLp: collateralToLp.toFixed(0), - collateralToWithdraw: collateralToWithdraw.toFixed(0), - amount0Min, - amount1Min, - lowerTick: lowerTick, - upperTick: upperTick, - } - - return handleTransaction( - contract.methods.flashloanWMintLpDepositNft(flashloanWMintDepositNftParams).send({ - from: address, - value: collateralToLp.plus(collateralToMint).minus(collateralToWithdraw).toFixed(0), - }), - onTxConfirmed, - ) - }, - [address, squeethPool, contract, handleTransaction, getDebtAmount, squeethPoolContract, isWethToken0], - ) - - return openPositionDeposit -} - // Close position with flashloan -export const useClosePosition = () => { +export const useFlashClosePosition = () => { const address = useAtomValue(addressAtom) const controllerHelperContract = useAtomValue(controllerHelperHelperContractAtom) const controllerContract = useAtomValue(controllerContractAtom) From 4da583c39cb455be7e7317fa9a09a9a0ba119fc1 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Mon, 29 Aug 2022 00:32:35 -0700 Subject: [PATCH 29/29] flashclose --- packages/frontend/src/state/lp/hooks.ts | 176 +++++++++++++++--------- 1 file changed, 113 insertions(+), 63 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index c90231a0d..e82e2efce 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -78,8 +78,8 @@ export const useOpenPositionDeposit = () => { const amount0Min = amount0New.times(new BigNumber(1).minus(slippage)).toFixed(0) const amount1Min = amount1New.times(new BigNumber(1).minus(slippage)).toFixed(0) - const collateralToWithdraw = fromTokenAmount(withdrawAmount, OSQUEETH_DECIMALS) - const ethIndexPrice = toTokenAmount(index, 18).sqrt() + const collateralToWithdraw = fromTokenAmount(withdrawAmount, WETH_DECIMALS) + const ethIndexPrice = toTokenAmount(index, WETH_DECIMALS).sqrt() const vaultShortAmt = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) const vaultCollateralAmt = fromTokenAmount(vaultBefore.collateralAmount, WETH_DECIMALS) @@ -149,64 +149,115 @@ export const useFlashClosePosition = () => { const getExactOut = useGetExactOut() const getDecreaseLiquidity = useGetDecreaseLiquidity() const isWethToken0 = useAtomValue(isWethToken0Atom) - const closePosition = useAppCallback(async (vaultId: number, liquidityPercentage: number, burnPercentage: number, collateralToWithdraw: number, burnExactRemoved: boolean, slippage: number, onTxConfirmed?: () => void) => { - const vaultBefore = await getVault(vaultId) - const uniTokenId = vaultBefore?.NFTCollateralId - const position = await getPosition(uniTokenId) - - if ( - !controllerContract || - !controllerHelperContract || - !address || - !position || - !vaultBefore || - !vaultBefore.shortAmount - ) - return - - const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) - const debtInEthPromise = getDebtAmount(shortAmount) - - // Get current LP positions - const { amount0, amount1 } = await getDecreaseLiquidity(uniTokenId, position.liquidity, 0, 0, Math.floor(Date.now() / 1000 + 86400)) - const wPowerPerpAmountInLP = isWethToken0 ? amount1 : amount0 - - const amountToLiquidate = new BigNumber(wPowerPerpAmountInLP).times(liquidityPercentage) - const amountToBurn = shortAmount.times(burnPercentage) - const limitEthPromise = amountToLiquidate.gt(amountToBurn) ? getExactIn(amountToLiquidate.minus(amountToBurn), true) - : getExactOut(amountToBurn.minus(amountToLiquidate), true) - - const [debtInEth, limitEth] = await Promise.all([debtInEthPromise, limitEthPromise]) - const limitPrice = new BigNumber(limitEth).div(amountToLiquidate.minus(amountToBurn).abs()).times(new BigNumber(1).minus(slippage)) - - const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO_FLASHLOAN) - - const amount0Min = new BigNumber(amount0).times(liquidityPercentage).times(new BigNumber(1).minus(slippage)).toFixed(0) - const amount1Min = new BigNumber(amount1).times(liquidityPercentage).times(new BigNumber(1).minus(slippage)).toFixed(0) - - const flashloanCloseVaultLpNftParam = { - vaultId: vaultId, - tokenId: uniTokenId, - liquidity: position.liquidity, - liquidityPercentage: fromTokenAmount(liquidityPercentage, 18).toFixed(0), - wPowerPerpAmountToBurn: amountToBurn.toFixed(0), - collateralToFlashloan: collateralToFlashloan.toFixed(0), - collateralToWithdraw: collateralToWithdraw.toFixed(0), - limitPriceEthPerPowerPerp: fromTokenAmount(limitPrice, 18).toFixed(0), - amount0Min, - amount1Min, - poolFee: POOL_FEE, - burnExactRemoved, - } - - return handleTransaction( - await controllerHelperContract.methods.flashloanCloseVaultLpNft(flashloanCloseVaultLpNftParam).send({ - from: address, - }), - onTxConfirmed, - ) - }, [address, controllerHelperContract, controllerContract, handleTransaction, getDebtAmount, getVault, getPosition, getExactIn, getExactOut, getDecreaseLiquidity, isWethToken0]) - return closePosition + const index = useAtomValue(indexAtom) + const normFactor = useAtomValue(normFactorAtom) + const flashClosePosition = useAppCallback( + async ( + vaultId: number, + liquidityPercentage: number, + burnPercentage: number, + withdrawAmount: number, + burnExactRemoved: boolean, + slippage: number, + onTxConfirmed?: () => void, + ) => { + const vaultBefore = await getVault(vaultId) + const uniTokenId = vaultBefore?.NFTCollateralId + const position = await getPosition(uniTokenId) + + if ( + !controllerContract || + !controllerHelperContract || + !address || + !position || + !vaultBefore || + !vaultBefore.shortAmount + ) + return + + const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) + + // Get current LP positions + const { amount0, amount1 } = await getDecreaseLiquidity( + uniTokenId, + position.liquidity, + 0, + 0, + Math.floor(Date.now() / 1000 + 86400), + ) + const wPowerPerpAmountInLP = isWethToken0 ? amount1 : amount0 + + const amountToLiquidate = new BigNumber(wPowerPerpAmountInLP).times(liquidityPercentage) + const amountToBurn = shortAmount.times(burnPercentage) + const limitEth = await (amountToLiquidate.gt(amountToBurn) + ? getExactIn(amountToLiquidate.minus(amountToBurn), true) + : getExactOut(amountToBurn.minus(amountToLiquidate), true)) + + const collateralToWithdraw = fromTokenAmount(withdrawAmount, WETH_DECIMALS) + const ethIndexPrice = toTokenAmount(index, WETH_DECIMALS).sqrt() + const vaultShortAmt = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) + const vaultCollateralAmt = fromTokenAmount(vaultBefore.collateralAmount, WETH_DECIMALS) + + const flashLoanAmount = new BigNumber(COLLAT_RATIO_FLASHLOAN + FLASHLOAN_BUFFER) + .times(vaultShortAmt) + .times(normFactor) + .times(ethIndexPrice) + .div(INDEX_SCALE) + .minus(vaultCollateralAmt) + const flashLoanAmountPos = BigNumber.max(flashLoanAmount, 0) + + const limitPrice = new BigNumber(limitEth) + .div(amountToLiquidate.minus(amountToBurn).abs()) + .times(new BigNumber(1).minus(slippage)) + + const amount0Min = new BigNumber(amount0) + .times(liquidityPercentage) + .times(new BigNumber(1).minus(slippage)) + .toFixed(0) + const amount1Min = new BigNumber(amount1) + .times(liquidityPercentage) + .times(new BigNumber(1).minus(slippage)) + .toFixed(0) + + const flashloanCloseVaultLpNftParam = { + vaultId: vaultId, + tokenId: uniTokenId, + liquidity: position.liquidity, + liquidityPercentage: fromTokenAmount(liquidityPercentage, 18).toFixed(0), + wPowerPerpAmountToBurn: amountToBurn.toFixed(0), + collateralToFlashloan: flashLoanAmountPos.toFixed(0), + collateralToWithdraw: collateralToWithdraw.toFixed(0), + limitPriceEthPerPowerPerp: fromTokenAmount(limitPrice, 18).toFixed(0), + amount0Min, + amount1Min, + poolFee: POOL_FEE, + burnExactRemoved, + } + + return handleTransaction( + await controllerHelperContract.methods.flashloanCloseVaultLpNft(flashloanCloseVaultLpNftParam).send({ + from: address, + }), + onTxConfirmed, + ) + }, + [ + address, + controllerHelperContract, + controllerContract, + handleTransaction, + getDebtAmount, + getVault, + getPosition, + getExactIn, + getExactOut, + getDecreaseLiquidity, + isWethToken0, + index, + normFactor, + ], + ) + return flashClosePosition } /*** GETTERS ***/ @@ -400,10 +451,9 @@ export const useGetExactOut = () => { return getExactOut } - export const useGetQuote = () => { const contract = useAtomValue(quoterContractAtom) - const {weth, oSqueeth} = useAtomValue(addressesAtom) + const { weth, oSqueeth } = useAtomValue(addressesAtom) const getQuote = useCallback( async (amount: BigNumber, squeethIn: boolean) => { @@ -414,7 +464,7 @@ export const useGetQuote = () => { tokenOut: squeethIn ? weth : oSqueeth, amountIn: amount.toFixed(0), fee: 3000, - sqrtPriceLimitX96: 0 + sqrtPriceLimitX96: 0, } const quote = await contract.methods.quoteExactInputSingle(QuoteExactInputSingleParams).call()