diff --git a/packages/frontend/src/state/crab/hooks.ts b/packages/frontend/src/state/crab/hooks.ts index b6fbd1fa8..87ba50357 100644 --- a/packages/frontend/src/state/crab/hooks.ts +++ b/packages/frontend/src/state/crab/hooks.ts @@ -636,7 +636,7 @@ export const useFlashDepositV2 = (calculateETHtoBorrowFromUniswap: any) => { export const useFlashDepositUSDC = (calculateETHtoBorrowFromUniswap: any) => { const maxCap = useAtomValue(maxCapAtomV2) const address = useAtomValue(addressAtom) - const { usdc, weth } = useAtomValue(addressesAtom) + const { usdc, weth, crabStrategy2 } = useAtomValue(addressesAtom) const network = useAtomValue(networkIdAtom) const vault = useAtomValue(crabStrategyVaultAtomV2) const contract = useAtomValue(crabHelperContractAtom) @@ -669,7 +669,29 @@ export const useFlashDepositUSDC = (calculateETHtoBorrowFromUniswap: any) => { const ethBorrow = fromTokenAmount(_ethBorrow, 18) const ethDeposit = ethAmount track(CRAB_EVENTS.DEPOSIT_CRAB_USDC_CLICK, { amount: amount.toString() }) + let gasEstimate try { + try { + gasEstimate = await contract.methods + .flashDepositERC20( + ethBorrow.plus(ethDeposit).toFixed(0), + usdcAmount.toFixed(0), + ethDeposit.toFixed(0), + usdcFee, + UNI_POOL_FEES, + usdc, + ) + .estimateGas({ + to: crabStrategy2, + from: address, + }) + if (gasEstimate === 0) throw new Error('WRONG_GAS_ESTIMATE') + } catch (e) { + track(CRAB_EVENTS.DEPOSIT_CRAB_WRONG_GAS, { gas: gasEstimate }) + alert('Error occurred, please refresh and try again') + throw e + } + const tx = await handleTransaction( contract.methods .flashDepositERC20( @@ -793,6 +815,7 @@ export const useFlashWithdrawV2 = () => { const contract = useAtomValue(crabStrategyContractAtomV2) const handleTransaction = useHandleTransaction() const address = useAtomValue(addressAtom) + const { crabStrategy2 } = useAtomValue(addressesAtom) const calculateEthWillingToPay = useCalculateEthWillingToPayV2() const { track } = useAmplitude() @@ -806,7 +829,22 @@ export const useFlashWithdrawV2 = () => { const crabAmount = fromTokenAmount(amount, 18) const poolFeePercent = 3000 track(CRAB_EVENTS.WITHDRAW_CRAB_CLICK) + let gasEstimate try { + try { + gasEstimate = await contract.methods + .flashWithdraw(crabAmount.toFixed(0), ethWillingToPay.toFixed(0), poolFeePercent.toFixed(0)) + .estimateGas({ + to: crabStrategy2, + from: address, + }) + if (gasEstimate === 0) throw new Error('WRONG_GAS_ESTIMATE') + } catch (e) { + track(CRAB_EVENTS.WITHDRAW_CRAB_WRONG_GAS, { gas: gasEstimate }) + alert('Error occurred, please refresh and try again') + throw e + } + const tx = await handleTransaction( contract.methods .flashWithdraw(crabAmount.toFixed(0), ethWillingToPay.toFixed(0), poolFeePercent.toFixed(0)) @@ -835,7 +873,8 @@ export const useFlashWithdrawV2USDC = () => { const address = useAtomValue(addressAtom) const calculateEthWillingToPay = useCalculateEthWillingToPayV2() const { getExactIn } = useUniswapQuoter() - const { usdc, weth } = useAtomValue(addressesAtom) + const { track } = useAmplitude() + const { usdc, weth, crabStrategy2 } = useAtomValue(addressesAtom) const network = useAtomValue(networkIdAtom) const usdcFee = getUSDCPoolFee(network) @@ -851,21 +890,53 @@ export const useFlashWithdrawV2USDC = () => { const { minAmountOut } = await getExactIn(weth, usdc, fromTokenAmount(ethToGet, 18), usdcFee, slippage) console.log('Min amount out USDC', minAmountOut.toString()) const poolFeePercent = 3000 - return await handleTransaction( - contract.methods - .flashWithdrawERC20( - crabAmount.toFixed(0), - ethWillingToPay.toFixed(0), - usdc, - minAmountOut, - usdcFee, - poolFeePercent, - ) - .send({ - from: address, - }), - onTxConfirmed, - ) + + track(CRAB_EVENTS.WITHDRAW_CRAB_USDC_CLICK, { amount: amount.toNumber() }) + let gasEstimate + try { + try { + gasEstimate = await contract.methods + .flashWithdrawERC20( + crabAmount.toFixed(0), + ethWillingToPay.toFixed(0), + usdc, + minAmountOut, + usdcFee, + poolFeePercent, + ) + .estimateGas({ + to: crabStrategy2, + from: address, + }) + if (gasEstimate === 0) throw new Error('WRONG_GAS_ESTIMATE') + } catch (e) { + track(CRAB_EVENTS.WITHDRAW_CRAB_WRONG_GAS, { gas: gasEstimate }) + alert('Error occurred, please refresh and try again') + throw e + } + + await handleTransaction( + contract.methods + .flashWithdrawERC20( + crabAmount.toFixed(0), + ethWillingToPay.toFixed(0), + usdc, + minAmountOut, + usdcFee, + poolFeePercent, + ) + .send({ + from: address, + }), + onTxConfirmed, + ) + track(CRAB_EVENTS.WITHDRAW_CRAB_USDC_SUCCESS, { amount: amount.toNumber() }) + } catch (e: any) { + e?.code === REVERTED_TRANSACTION_CODE + ? track(CRAB_EVENTS.WITHDRAW_CRAB_USDC_REVERT, { amount: amount.toNumber() }) + : null + track(CRAB_EVENTS.WITHDRAW_CRAB_USDC_FAILED, { code: e?.code, message: e?.message, amount: amount.toNumber() }) + } }, [contract, address, handleTransaction, calculateEthWillingToPay], ) diff --git a/packages/frontend/src/utils/amplitude.ts b/packages/frontend/src/utils/amplitude.ts index 581e77c47..c4060e60f 100644 --- a/packages/frontend/src/utils/amplitude.ts +++ b/packages/frontend/src/utils/amplitude.ts @@ -73,6 +73,10 @@ export enum CRAB_EVENTS { DEPOSIT_CRAB_USDC_SUCCESS = 'DEPOSIT_CRAB_USDC_SUCCESS', DEPOSIT_CRAB_USDC_REVERT = 'DEPOSIT_CRAB_USDC_REVERT', DEPOSIT_CRAB_USDC_FAILED = 'DEPOSIT_CRAB_USDC_FAILED', + WITHDRAW_CRAB_USDC_CLICK = 'WITHDRAW_CRAB_USDC_CLICK', + WITHDRAW_CRAB_USDC_SUCCESS = 'WITHDRAW_CRAB_USDC_SUCCESS', + WITHDRAW_CRAB_USDC_REVERT = 'WITHDRAW_CRAB_USDC_REVERT', + WITHDRAW_CRAB_USDC_FAILED = 'WITHDRAW_CRAB_USDC_FAILED', WITHDRAW_CRAB_AMOUNT_ENTERED = 'WITHDRAW_CRAB_AMOUNT_ENTERED', WITHDRAW_CRAB_CLICK = 'WITHDRAW_CRAB_CLICK', WITHDRAW_CRAB_SUCCESS = 'WITHDRAW_CRAB_SUCCESS', @@ -100,6 +104,8 @@ export enum CRAB_EVENTS { APPROVE_DEPOSIT_STN_CRAB_USDC = 'APPROVE_DEPOSIT_STN_CRAB_USDC', APPROVE_WITHDRAW_CRAB_USDC = 'APPROVE_WITHDRAW_CRAB_USDC', APPROVE_WITHDRAW_STN_CRAB_USDC = 'APPROVE_WITHDRAW_STN_CRAB_USDC', + DEPOSIT_CRAB_WRONG_GAS = 'DEPOSIT_CRAB_WRONG_GAS', + WITHDRAW_CRAB_WRONG_GAS = 'WITHDRAW_CRAB_WRONG_GAS', } export const isOptedOut = async () => {