Skip to content

Commit

Permalink
feat: track gas estimate in crab (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushikeshakhare authored Jan 18, 2023
1 parent 027b8ac commit 4306054
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
105 changes: 88 additions & 17 deletions packages/frontend/src/state/crab/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()

Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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],
)
Expand Down
6 changes: 6 additions & 0 deletions packages/frontend/src/utils/amplitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 () => {
Expand Down

1 comment on commit 4306054

@vercel
Copy link

@vercel vercel bot commented on 4306054 Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.