Skip to content

Commit

Permalink
collect fees hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Darya Kaviani authored and Darya Kaviani committed Jul 13, 2022
1 parent 4a5382c commit f28e289
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/frontend/src/state/lp/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useGetDebtAmount, useGetTwapSqueethPrice, useGetVault } from '../contro
const TICK_SPACE = 60
const COLLAT_RATIO = 1.5
const POOL_FEE = 3000
const MAX_INT = new BigNumber(2).pow(128).minus(1).toFixed(0)

/*** ACTIONS ***/

Expand Down Expand Up @@ -124,6 +125,58 @@ export const useClosePosition = () => {
return closePosition
}

// Collect fees
export const useCollectFees = () => {
const address = useAtomValue(addressAtom)
const controllerHelperContract = useAtomValue(controllerHelperHelperContractAtom)
const controllerContract = useAtomValue(controllerContractAtom)
const handleTransaction = useHandleTransaction()
const getDebtAmount = useGetDebtAmount()
const getVault = useGetVault()
const collectFees = useAppCallback(async (vaultId: number, onTxConfirmed?: () => void) => {
const vaultBefore = await getVault(vaultId)
const uniTokenId = vaultBefore?.NFTCollateralId

if (
!controllerContract ||
!controllerHelperContract ||
!address ||
!vaultBefore ||
!vaultBefore.shortAmount
)
return

const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS)
const debtInEth = await getDebtAmount(shortAmount)
const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO)
const amount0Max = MAX_INT
const amount1Max = MAX_INT
const abiCoder = new ethers.utils.AbiCoder()
const rebalanceLpInVaultParams = [
{
rebalanceLpInVaultType: new BigNumber(6).toFixed(0),
// CollectFees
data: abiCoder.encode(['uint256', 'uint128', 'uint128'], [uniTokenId, amount0Max, amount1Max]),
},
{
rebalanceLpInVaultType: new BigNumber(7).toFixed(0),
// DepositExistingNftParams
data: abiCoder.encode(["uint256"], [uniTokenId])
}
]

return handleTransaction(
await controllerHelperContract.methods
.rebalanceLpInVault(vaultId, collateralToFlashloan.toFixed(0), rebalanceLpInVaultParams)
.send({
from: address,
}),
onTxConfirmed,
)
}, [address, controllerHelperContract, controllerContract, handleTransaction, getDebtAmount, getVault])
return collectFees
}

/*** GETTERS ***/

export const useGetPosition = () => {
Expand Down

0 comments on commit f28e289

Please sign in to comment.