Skip to content

Commit

Permalink
fix(remove-liq,approvals): fixes approvals in swap and in remove liqu…
Browse files Browse the repository at this point in the history
…idity
  • Loading branch information
Alexangelj committed Sep 26, 2021
1 parent 18cfae8 commit 21f4dc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 211 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import React, { useState, useEffect, useCallback } from 'react'
import React, { useState, useCallback } from 'react'

import { useWeb3React } from '@web3-react/core'

import { useAddNotif, useClearNotif } from '@/state/notifs/hooks'
import { useAddNotif } from '@/state/notifs/hooks'

import { SignitureData, Venue } from '@primitivefi/sdk'
import { SignitureData } from '@primitivefi/sdk'

const TRADER = '0xc1de48E9577A7CF18594323A73CDcF1EE19962E8'
const ROUTER = '0x9264416B621054e16fAB8d6423b5a1e354D19fEc'
const LIQUIDITY = '0x996Eeff28277FD17738913e573D1c452b4377A16'
const WETH = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'

import { optionAddresses, tokens } from '@/constants/options'

import { abi as RouterAbi } from '@primitivefi/v1-connectors/build/contracts/PrimitiveRouter.sol/PrimitiveRouter.json'
import { abi as LiquidityAbi } from '@primitivefi/v1-connectors/build/contracts/connectors/PrimitiveLiquidity.sol/PrimitiveLiquidity.json'
import { abi as TraderAbi } from '@primitivefi/contracts/artifacts/contracts/option/extensions/Trader.sol/Trader.json'
Expand All @@ -22,26 +20,6 @@ import { ethers } from 'ethers'
import { usePermit } from '@/hooks/transactions/usePermit'
import { useItem } from '@/state/order/hooks'

async function executeTransaction(
provider: any,
target: string,
calldata: string,
value: string
): Promise<any> {
console.log(` executing tx: ${target} ${calldata}`)
let tx: any = {}
try {
tx = await provider
.getSigner()
.sendTransaction({ to: target, data: calldata, value })
console.log(`tx: ${tx}`)
} catch (err) {
throw Error(`Issue when attempting to submit transaction`)
}

return tx
}

async function callTrader(
trader: ethers.Contract,
method: string,
Expand Down Expand Up @@ -80,7 +58,7 @@ import useTokenBalance from '@/hooks/useTokenBalance'
import { abi as PairAbi } from '@uniswap/v2-core/build/UniswapV2Pair.json'
import useTokenTotalSupply from '@/hooks/useTokenTotalSupply'
import Button from '@/components/Button'
import { parseEther } from '@ethersproject/units'
import { formatEther, parseEther } from '@ethersproject/units'
import useApprove from '@/hooks/transactions/useApprove'

const allOptions = {
Expand Down Expand Up @@ -169,19 +147,6 @@ const Downgrade = () => {

const args = [option, optionQuantity.toString(), account]

const calldata = trader.interface.encodeFunctionData('safeUnwind', args)

console.log([
option.toString(),
optionQuantity.toString(),
account.toString(),
])

/* await trader.safeUnwind(
option.toString(),
optionQuantity.toString(),
account.toString()
) */
await callTrader(trader, 'safeUnwind', args)
}, [library, account, redeemTokenBalance])

Expand Down Expand Up @@ -219,52 +184,35 @@ const Downgrade = () => {
.mul(underlyingReserve)
.div(totalSupply)

/* amountAMin = trade.calcMinimumOutSlippage(
amountAMin.toString(),
tradeSettings.slippage
)
amountBMin = trade.calcMinimumOutSlippage(
amountBMin.toString(),
tradeSettings.slippage
) */

const value = '0'
const deadline = signData ? signData.deadline : 1000000000000000
console.log({ signData }, 'in fn caller')
const slippage = ethers.BigNumber.from(0.05 * 1000)
const one = ethers.BigNumber.from(1000)

const amountAMinAdjusted = ethers.BigNumber.from(amountAMin)
.mul(one.sub(slippage))
.div(1000)
const amountBMinAdjusted = ethers.BigNumber.from(amountBMin)
.mul(one.sub(slippage))
.div(1000)

const params = liquidity.interface.encodeFunctionData(
'removeShortLiquidityThenCloseOptionsWithPermit',
[
option,
inputAmount,
amountAMin.toString(),
amountBMin.toString(),
item.entity.isPut ? amountBMinAdjusted : amountAMinAdjusted.toString(),
item.entity.isPut ? amountAMinAdjusted : amountBMinAdjusted.toString(),
signData.deadline,
signData.v,
signData.r,
signData.s,
]
)

/* const params = liquidity.interface.encodeFunctionData(
'removeShortLiquidityThenCloseOptions',
[
option,
inputAmount,
amountAMin.toString(),
amountBMin.toString(),
deadline,
]
) */

const calldata = router.interface.encodeFunctionData('executeCall', [
LIQUIDITY,
params,
])

console.log(LIQUIDITY, params)

await router.executeCall(LIQUIDITY, params)
}, [
item.entity.isPut,
account,
library,
lpTotalSupply,
Expand Down
157 changes: 15 additions & 142 deletions src/components/Market/OrderCard/components/Swap/Downgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useWeb3React } from '@web3-react/core'

import { useAddNotif, useClearNotif } from '@/state/notifs/hooks'

import { SignitureData, Venue } from '@primitivefi/sdk'
import { getAllowance, SignitureData, Venue } from '@primitivefi/sdk'

const TRADER = '0xc1de48E9577A7CF18594323A73CDcF1EE19962E8'
const ROUTER = '0x9264416B621054e16fAB8d6423b5a1e354D19fEc'
Expand Down Expand Up @@ -83,6 +83,7 @@ import useTokenTotalSupply from '@/hooks/useTokenTotalSupply'
import Button from '@/components/Button'
import { parseEther } from '@ethersproject/units'
import useApprove from '@/hooks/transactions/useApprove'
import useTokenAllowance from '@/hooks/useTokenAllowance'

const allOptions = {
option0: {
Expand Down Expand Up @@ -130,24 +131,10 @@ const Downgrade = () => {

const lpTokenBalance = useTokenBalance(lpToken)
const lpTotalSupply = useTokenTotalSupply(lpToken)
const redeemAllowance = useTokenAllowance(redeem, TRADER)

const redeemTokenBalance = useTokenBalance(redeem)

const handleApprovalPermit = useCallback(
(spender: string) => {
const amount = parseEther(lpTokenBalance)
handlePermit(lpToken, spender, amount.toString())
.then((data) => {
console.log({ data })
setSignData(data)
})
.catch((error) => {
addNotif(0, `Approving ${lpToken}`, error.message, '')
})
},
[underlying, lpToken, handlePermit, setSignData, lpTokenBalance]
)

const getSigner = useCallback(async () => {
if (library) {
return await library.getSigner()
Expand All @@ -170,130 +157,24 @@ const Downgrade = () => {

const args = [option, optionQuantity.toString(), account]

const calldata = trader.interface.encodeFunctionData('safeUnwind', args)

console.log([
option.toString(),
optionQuantity.toString(),
account.toString(),
])

/* await trader.safeUnwind(
option.toString(),
optionQuantity.toString(),
account.toString()
) */
await callTrader(trader, 'safeUnwind', args)
}, [library, account, redeemTokenBalance])

const removeLiquidity = useCallback(async () => {
const signer = await getSigner()
const router = new ethers.Contract(ROUTER, RouterAbi, signer)
const liquidity = new ethers.Contract(LIQUIDITY, LiquidityAbi, signer)

const pool = lpToken
const reserves = await getReserves(library, pool)
console.log(reserves)

const [token0, token1] =
underlying.toLowerCase() < redeem.toLowerCase()
? [underlying, redeem]
: [redeem, underlying]

const [reserve0, reserve1] = [reserves._reserve0, reserves._reserve1]

const isUnderlying = token0 == underlying ? true : false

const [underlyingReserve, redeemReserve] = isUnderlying
? [reserve0, reserve1]
: [reserve1, reserve0]
console.log(underlyingReserve, redeemReserve)

const inputAmount = ethers.utils.parseEther(lpTokenBalance.toString()) // amount of liquidity in users wallet
const totalSupply = ethers.utils.parseEther(lpTotalSupply)
// should always be redeem
const amountAMin = BigNumber.from(inputAmount)
.mul(redeemReserve)
.div(totalSupply)
// should always be underlying
const amountBMin = BigNumber.from(inputAmount)
.mul(underlyingReserve)
.div(totalSupply)

/* amountAMin = trade.calcMinimumOutSlippage(
amountAMin.toString(),
tradeSettings.slippage
)
amountBMin = trade.calcMinimumOutSlippage(
amountBMin.toString(),
tradeSettings.slippage
) */

const value = '0'
const deadline = signData ? signData.deadline : 1000000000000000
console.log({ signData }, 'in fn caller')
const params = liquidity.interface.encodeFunctionData(
'removeShortLiquidityThenCloseOptionsWithPermit',
[
option,
inputAmount,
amountAMin.toString(),
amountBMin.toString(),
signData.deadline,
signData.v,
signData.r,
signData.s,
]
)

/* const params = liquidity.interface.encodeFunctionData(
'removeShortLiquidityThenCloseOptions',
[
option,
inputAmount,
amountAMin.toString(),
amountBMin.toString(),
deadline,
]
) */

const calldata = router.interface.encodeFunctionData('executeCall', [
LIQUIDITY,
params,
])

console.log(LIQUIDITY, params)

await router.executeCall(LIQUIDITY, params)
}, [
account,
library,
lpTotalSupply,
lpTokenBalance,
lpToken,
signData,
setSignData,
])

const isLPApproved = useCallback(() => {
return approved || signData
}, [approved, signData])

const isRDMApproved = useCallback(() => {
return approved || signData
}, [approved, signData])
return redeemAllowance > redeemTokenBalance
}, [library, redeemAllowance, redeemTokenBalance])

const handleApproval = useCallback(
(token: string, spender: string, amount: string) => {
onApprove(token, spender, amount)
(amount: string) => {
onApprove(redeem, TRADER, amount)
.then(() => {
setApproved(true)
})
.catch((error) => {
addNotif(0, `Approving ${token} for ${spender}`, error.message, '')
addNotif(0, `Approving ${redeem} for ${TRADER}`, error.message, '')
})
},
[underlying, onApprove]
[redeem, onApprove]
)
return (
<div>
Expand All @@ -303,21 +184,17 @@ const Downgrade = () => {
{isRDMApproved() ? (
<>
<Button
disabled={submitting}
disabled={true}
full
size="sm"
onClick={() =>
handleApproval(
redeem,
TRADER,
parseEther('1000000000000').toString()
)
handleApproval(parseEther('1000000000000').toString())
}
isLoading={submitting}
text="Approve Redeem"
/>
<Button
disabled={true}
disabled={false}
full
size="sm"
onClick={safeUnwind}
Expand All @@ -328,21 +205,17 @@ const Downgrade = () => {
) : (
<>
<Button
disabled={true}
disabled={false}
full
size="sm"
onClick={() =>
handleApproval(
redeem,
TRADER,
parseEther('1000000000000').toString()
)
handleApproval(parseEther('1000000000000').toString())
}
isLoading={submitting}
text="Approve Redeem"
/>
<Button
disabled={submitting}
disabled={true}
full
size="sm"
onClick={safeUnwind}
Expand Down

0 comments on commit 21f4dc2

Please sign in to comment.