-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: undelegate with repay in single tx (#240)
- Loading branch information
Showing
9 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useUndelegateBaseAndromeda'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@snx-v3/useUndelegateBaseAndromeda", | ||
"private": true, | ||
"main": "index.ts", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@snx-v3/fetchPythPrices": "workspace:*", | ||
"@snx-v3/format": "workspace:*", | ||
"@snx-v3/isBaseAndromeda": "workspace:*", | ||
"@snx-v3/tsHelpers": "workspace:*", | ||
"@snx-v3/txnReducer": "workspace:*", | ||
"@snx-v3/useAllCollateralPriceIds": "workspace:*", | ||
"@snx-v3/useApprove": "workspace:*", | ||
"@snx-v3/useBlockchain": "workspace:*", | ||
"@snx-v3/useCoreProxy": "workspace:*", | ||
"@snx-v3/useGasOptions": "workspace:*", | ||
"@snx-v3/useGasPrice": "workspace:*", | ||
"@snx-v3/useGasSpeed": "workspace:*", | ||
"@snx-v3/useLiquidityPosition": "workspace:*", | ||
"@snx-v3/withERC7412": "workspace:*", | ||
"@synthetixio/wei": "^2.74.4", | ||
"@tanstack/react-query": "^5.8.3", | ||
"ethers": "^5.7.2", | ||
"react": "^18.2.0" | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
liquidity/lib/useUndelegateBaseAndromeda/useUndelegateBaseAndromeda.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { useReducer } from 'react'; | ||
import { useCoreProxy } from '@snx-v3/useCoreProxy'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useNetwork, useProvider, useSigner } from '@snx-v3/useBlockchain'; | ||
import { initialState, reducer } from '@snx-v3/txnReducer'; | ||
import Wei, { wei } from '@synthetixio/wei'; | ||
import { BigNumber, Contract, PopulatedTransaction } from 'ethers'; | ||
import { formatGasPriceForTransaction } from '@snx-v3/useGasOptions'; | ||
import { getGasPrice } from '@snx-v3/useGasPrice'; | ||
import { useGasSpeed } from '@snx-v3/useGasSpeed'; | ||
import { withERC7412 } from '@snx-v3/withERC7412'; | ||
import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; | ||
import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; | ||
import { LiquidityPosition } from '@snx-v3/useLiquidityPosition'; | ||
import { useApprove } from '@snx-v3/useApprove'; | ||
import { USDC_BASE_MARKET, getRepayerContract, getUSDCAddress } from '@snx-v3/isBaseAndromeda'; | ||
import { parseUnits } from '@snx-v3/format'; | ||
import { DEBT_REPAYER_ABI } from '../useClearDebt'; | ||
import { useSpotMarketProxy } from '../useSpotMarketProxy'; | ||
import { notNil } from '@snx-v3/tsHelpers'; | ||
|
||
export const useUndelegateBaseAndromeda = ({ | ||
accountId, | ||
poolId, | ||
collateralTypeAddress, | ||
collateralChange, | ||
currentCollateral, | ||
liquidityPosition, | ||
}: { | ||
accountId?: string; | ||
poolId?: string; | ||
collateralTypeAddress?: string; | ||
currentCollateral: Wei; | ||
collateralChange: Wei; | ||
liquidityPosition?: LiquidityPosition; | ||
}) => { | ||
const [txnState, dispatch] = useReducer(reducer, initialState); | ||
const { data: CoreProxy } = useCoreProxy(); | ||
const { data: SpotMarketProxy } = useSpotMarketProxy(); | ||
|
||
const signer = useSigner(); | ||
const { gasSpeed } = useGasSpeed(); | ||
const provider = useProvider(); | ||
const { data: collateralPriceUpdates } = useAllCollateralPriceIds(); | ||
const { network } = useNetwork(); | ||
|
||
const debtExists = liquidityPosition?.debt.gt(0.01); | ||
const currentDebt = debtExists && liquidityPosition ? liquidityPosition.debt : wei(0); | ||
|
||
const { approve, requireApproval } = useApprove({ | ||
contractAddress: getUSDCAddress(network?.id), | ||
//slippage for approval | ||
amount: parseUnits(currentDebt.toString(), 6).mul(110).div(100), | ||
spender: getRepayerContract(network?.id), | ||
}); | ||
|
||
const mutation = useMutation({ | ||
mutationFn: async () => { | ||
if (!signer || !network || !provider) throw new Error('No signer or network'); | ||
if ( | ||
!(CoreProxy && poolId && collateralTypeAddress && collateralPriceUpdates && SpotMarketProxy) | ||
) | ||
return; | ||
if (collateralChange.eq(0)) return; | ||
if (currentCollateral.eq(0)) return; | ||
try { | ||
dispatch({ type: 'prompting' }); | ||
|
||
if (debtExists && requireApproval) { | ||
await approve(false); | ||
} | ||
|
||
const transactions: Promise<PopulatedTransaction>[] = []; | ||
|
||
if (debtExists) { | ||
const repayer = new Contract(getRepayerContract(network.id), DEBT_REPAYER_ABI, signer); | ||
|
||
const depositDebtToRepay = repayer.populateTransaction.depositDebtToRepay( | ||
CoreProxy.address, | ||
SpotMarketProxy.address, | ||
accountId, | ||
poolId, | ||
collateralTypeAddress, | ||
USDC_BASE_MARKET | ||
); | ||
transactions.push(depositDebtToRepay); | ||
|
||
const burn = CoreProxy.populateTransaction.burnUsd( | ||
BigNumber.from(accountId), | ||
BigNumber.from(poolId), | ||
collateralTypeAddress, | ||
currentDebt?.mul(110).div(100).toBN().toString() || '0' | ||
); | ||
transactions.push(burn); | ||
} | ||
|
||
const populatedTxnPromised = CoreProxy.populateTransaction.delegateCollateral( | ||
BigNumber.from(accountId), | ||
BigNumber.from(poolId), | ||
collateralTypeAddress, | ||
currentCollateral.add(collateralChange).toBN(), | ||
wei(1).toBN() | ||
); | ||
|
||
const walletAddress = await signer.getAddress(); | ||
|
||
const callsPromise = Promise.all([...transactions, populatedTxnPromised].filter(notNil)); | ||
|
||
const collateralPriceCallsPromise = fetchPriceUpdates( | ||
collateralPriceUpdates, | ||
network.isTestnet | ||
).then((signedData) => | ||
priceUpdatesToPopulatedTx(walletAddress, collateralPriceUpdates, signedData) | ||
); | ||
const [calls, gasPrices, collateralPriceCalls] = await Promise.all([ | ||
callsPromise, | ||
getGasPrice({ provider }), | ||
collateralPriceCallsPromise, | ||
]); | ||
const allCalls = collateralPriceCalls.concat(...calls); | ||
|
||
const erc7412Tx = await withERC7412( | ||
network, | ||
allCalls, | ||
'useUndelegate', | ||
CoreProxy.interface | ||
); | ||
|
||
const gasOptionsForTransaction = formatGasPriceForTransaction({ | ||
gasLimit: erc7412Tx.gasLimit, | ||
gasPrices, | ||
gasSpeed, | ||
}); | ||
|
||
const txn = await signer.sendTransaction({ ...erc7412Tx, ...gasOptionsForTransaction }); | ||
dispatch({ type: 'pending', payload: { txnHash: txn.hash } }); | ||
|
||
await txn.wait(); | ||
dispatch({ type: 'success' }); | ||
} catch (error: any) { | ||
dispatch({ type: 'error', payload: { error } }); | ||
throw error; | ||
} | ||
}, | ||
}); | ||
return { | ||
mutation, | ||
txnState, | ||
settle: () => dispatch({ type: 'settled' }), | ||
isLoading: mutation.isPending, | ||
exec: mutation.mutateAsync, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters