Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0f8b883
update types
kaladinlight Apr 20, 2023
1b3d516
update types and add top level asset check
kaladinlight Apr 20, 2023
8e0d76f
add fee and build/broadcast helpers and improve approve helper
kaladinlight Apr 20, 2023
4a8ecd4
update zrx to use new helpers (eip1559 fees)
kaladinlight Apr 20, 2023
4a150ff
updated thorchain swapper to use new helpers (eip1559)
kaladinlight Apr 20, 2023
3333f67
use updated approve helper
kaladinlight Apr 20, 2023
1d72155
remove requirement to pass in hex number to buildCustomTx
kaladinlight Apr 20, 2023
e128062
Merge branch 'develop' into eip1559-fees
kaladinlight Apr 20, 2023
6ecdf28
cleanup
kaladinlight Apr 20, 2023
d606eee
pass back eip1559 fees for thorchain
kaladinlight Apr 20, 2023
fc835f7
add fee and build/broadcast helpers
kaladinlight Apr 21, 2023
f57a6de
update fox-farming to use new helpers
kaladinlight Apr 21, 2023
17f8805
pass back eip1559 fees for cowswap quote and use bn for const
kaladinlight Apr 21, 2023
1d7e4fa
cleanup naming and types
kaladinlight Apr 21, 2023
1145778
update univ2 to use new helpers
kaladinlight Apr 21, 2023
a48710f
update naming changes
kaladinlight Apr 21, 2023
ab22c75
jesus fucking tits tests
kaladinlight Apr 21, 2023
6a70be9
format
kaladinlight Apr 21, 2023
3840e65
additional cowswap cleanup
kaladinlight Apr 24, 2023
38f6f65
additional zrx swapper cleanup
kaladinlight Apr 24, 2023
89455ef
additional thorchain swapper cleanup
kaladinlight Apr 24, 2023
33859cb
review feedback
kaladinlight Apr 24, 2023
eea80a8
explicit data
kaladinlight Apr 24, 2023
52e0151
fix tests again
kaladinlight Apr 24, 2023
0f0d47d
fee data helper to pull average eip1559 and fast legacy fees
kaladinlight Apr 24, 2023
0c42599
Merge branch 'develop' into eip1559-fees
kaladinlight Apr 24, 2023
967fcf2
average eip1559 vs fast legacy with updated comment
kaladinlight Apr 25, 2023
cb00807
small cleanup approval fee
kaladinlight Apr 25, 2023
4c9bfde
fix tests
kaladinlight Apr 25, 2023
63140b5
remove parser log
kaladinlight Apr 25, 2023
f369882
Merge branch 'develop' into eip1559-fees
kaladinlight Apr 25, 2023
e1af646
fix: pass value for deposit
kaladinlight Apr 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/features/defi/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { Asset } from '@shapeshiftoss/asset-service'
import type { AccountId, ChainId } from '@shapeshiftoss/caip'
import { cosmosChainId, osmosisChainId } from '@shapeshiftoss/caip'
import type { evm, EvmChainAdapter } from '@shapeshiftoss/chain-adapters'
import type {
evm,
EvmChainAdapter,
EvmChainId,
FeeData,
FeeDataEstimate,
} from '@shapeshiftoss/chain-adapters'
import type { HDWallet } from '@shapeshiftoss/hdwallet-core'
import { supportsETH } from '@shapeshiftoss/hdwallet-core'
import { bnOrZero } from 'lib/bignumber/bignumber'
Expand Down Expand Up @@ -38,6 +44,19 @@ export const canCoverTxFees = ({
return bnOrZero(feeAssetBalanceCryptoHuman).minus(bnOrZero(estimatedGasCryptoPrecision)).gte(0)
}

export const getFeeDataFromEstimate = ({
average,
fast,
}: FeeDataEstimate<EvmChainId>): FeeData<EvmChainId> => ({
txFee: fast.txFee, // use worst case tx fee for display purposes
chainSpecific: {
gasLimit: average.chainSpecific.gasLimit, // average and fast gasLimit values are the same
gasPrice: fast.chainSpecific.gasPrice, // fast gas price since it is underestimated currently
maxFeePerGas: average.chainSpecific.maxFeePerGas,
maxPriorityFeePerGas: average.chainSpecific.maxPriorityFeePerGas,
},
})

type GetFeesFromFeeDataArgs = {
wallet: HDWallet
feeData: evm.FeeData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ export const ClaimConfirm = ({ accountId, assetId, amount, onBack }: ClaimConfir
!(walletState.wallet && feeAsset && feeMarketData && foxFarmingContract && accountAddress)
)
return
const gasEstimate = await getClaimFeeData(accountAddress)
if (!gasEstimate) throw new Error('Gas estimation failed')
const estimatedGasCrypto = bnOrZero(gasEstimate.average.txFee)
const feeData = await getClaimFeeData(accountAddress)
if (!feeData) throw new Error('Gas estimation failed')
const estimatedGasCrypto = bnOrZero(feeData.txFee)
.div(`1e${feeAsset.precision}`)
.toPrecision()
setCanClaim(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ export const Approve: React.FC<FoxFarmingApproveProps> = ({ accountId, onNext })
maxAttempts: 30,
})
// Get deposit gas estimate
const gasData = await getStakeFeeData(state.deposit.cryptoAmount)
if (!gasData) return
const estimatedGasCryptoPrecision = bnOrZero(gasData.average.txFee)
const feeData = await getStakeFeeData(state.deposit.cryptoAmount)
if (!feeData) return
const estimatedGasCryptoPrecision = bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision()
dispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ export const Deposit: React.FC<DepositProps> = ({
): Promise<string | undefined> => {
if (!assetReference) return
try {
const gasData = await getStakeFeeData(deposit.cryptoAmount)
if (!gasData) return
return bnOrZero(gasData.average.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
const feeData = await getStakeFeeData(deposit.cryptoAmount)
if (!feeData) return
return bnOrZero(feeData.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
} catch (error) {
moduleLogger.error(
{ fn: 'getDepositGasEstimateCryptoPrecision', error },
Expand Down Expand Up @@ -180,12 +180,12 @@ export const Deposit: React.FC<DepositProps> = ({
assets,
)
} else {
const estimatedGasCryptoBaseUnit = await getApproveFeeData()
if (!estimatedGasCryptoBaseUnit) return
const feeData = await getApproveFeeData()
if (!feeData) return
dispatch({
type: FoxFarmingDepositActionType.SET_APPROVE,
payload: {
estimatedGasCryptoPrecision: bnOrZero(estimatedGasCryptoBaseUnit.average.txFee)
estimatedGasCryptoPrecision: bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
maxAttempts: 30,
})
// Get withdraw gas estimate
const gasData = await getUnstakeFeeData(state.withdraw.lpAmount, state.withdraw.isExiting)
if (!gasData) return
const estimatedGasCrypto = bnOrZero(gasData.average.txFee)
const feeData = await getUnstakeFeeData(state.withdraw.lpAmount, state.withdraw.isExiting)
if (!feeData) return
const estimatedGasCrypto = bnOrZero(feeData.txFee)
.div(bn(10).pow(underlyingAsset?.precision ?? 0))
.toPrecision()
dispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export const ExpiredWithdraw: React.FC<ExpiredWithdrawProps> = ({

const getWithdrawGasEstimate = async () => {
try {
const fee = await getUnstakeFeeData(amountAvailableCryptoPrecision.toFixed(), true)
if (!fee) return
return bnOrZero(fee.average.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
const feeData = await getUnstakeFeeData(amountAvailableCryptoPrecision.toFixed(), true)
if (!feeData) return
return bnOrZero(feeData.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
} catch (error) {
// TODO: handle client side errors maybe add a toast?
moduleLogger.error(
Expand Down Expand Up @@ -162,12 +162,12 @@ export const ExpiredWithdraw: React.FC<ExpiredWithdrawProps> = ({
assets,
)
} else {
const estimatedGasCrypto = await getApproveFeeData()
if (!estimatedGasCrypto) return
const feeData = await getApproveFeeData()
if (!feeData) return
dispatch({
type: FoxFarmingWithdrawActionType.SET_APPROVE,
payload: {
estimatedGasCryptoPrecision: bnOrZero(estimatedGasCrypto.average.txFee)
estimatedGasCryptoPrecision: bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export const Withdraw: React.FC<WithdrawProps> = ({
const getWithdrawGasEstimateCryptoPrecision = useCallback(
async (withdraw: WithdrawValues) => {
try {
const fee = await getUnstakeFeeData(withdraw.cryptoAmount, isExiting)
if (!fee) return
return bnOrZero(fee.average.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
const feeData = await getUnstakeFeeData(withdraw.cryptoAmount, isExiting)
if (!feeData) return
return bnOrZero(feeData.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
} catch (error) {
// TODO: handle client side errors maybe add a toast?
moduleLogger.error(error, 'FoxFarmingWithdraw:getWithdrawGasEstimate error:')
Expand Down Expand Up @@ -147,12 +147,12 @@ export const Withdraw: React.FC<WithdrawProps> = ({
assets,
)
} else {
const estimatedGasCrypto = await getApproveFeeData()
if (!estimatedGasCrypto) return
const feeData = await getApproveFeeData()
if (!feeData) return
dispatch({
type: FoxFarmingWithdrawActionType.SET_APPROVE,
payload: {
estimatedGasCryptoPrecision: bnOrZero(estimatedGasCrypto.average.txFee)
estimatedGasCryptoPrecision: bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision(),
},
Expand Down
24 changes: 12 additions & 12 deletions src/features/defi/providers/fox-farming/hooks/useFoxFarming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
UNISWAP_V2_ROUTER_02_CONTRACT_ADDRESS,
} from 'contracts/constants'
import { getOrCreateContractByAddress } from 'contracts/contractManager'
import { buildAndBroadcast } from 'features/defi/helpers/utils'
import { buildAndBroadcast, getFeeDataFromEstimate } from 'features/defi/helpers/utils'
import { useCallback, useMemo } from 'react'
import { useFoxEth } from 'context/FoxEthProvider/FoxEthProvider'
import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton'
Expand Down Expand Up @@ -81,7 +81,7 @@ export const useFoxFarming = (
bnOrZero(lpAmount).times(bnOrZero(10).exponentiatedBy(lpAsset.precision)).toFixed(0),
])

const { average } = await adapter.getFeeData({
const feeData = await adapter.getFeeData({
to: contractAddress,
value: '0',
chainSpecific: {
Expand All @@ -93,7 +93,7 @@ export const useFoxFarming = (
const txid = await buildAndBroadcast({
accountNumber,
adapter,
feeData: average.chainSpecific,
feeData: getFeeDataFromEstimate(feeData).chainSpecific,
to: contractAddress,
value: '0',
wallet,
Expand Down Expand Up @@ -135,7 +135,7 @@ export const useFoxFarming = (
bnOrZero(lpAmount).times(bnOrZero(10).exponentiatedBy(lpAsset.precision)).toFixed(0),
])

const { average } = await adapter.getFeeData({
const feeData = await adapter.getFeeData({
to: contractAddress,
value: '0',
chainSpecific: {
Expand All @@ -147,7 +147,7 @@ export const useFoxFarming = (
const txid = await buildAndBroadcast({
accountNumber,
adapter,
feeData: average.chainSpecific,
feeData: getFeeDataFromEstimate(feeData).chainSpecific,
to: contractAddress,
value: '0',
wallet,
Expand Down Expand Up @@ -201,7 +201,7 @@ export const useFoxFarming = (
},
})

return feeData
return getFeeDataFromEstimate(feeData)
}, [adapter, farmingAccountId, contractAddress, uniV2LPContract])

const getStakeFeeData = useCallback(
Expand All @@ -223,7 +223,7 @@ export const useFoxFarming = (
},
})

return feeData
return getFeeDataFromEstimate(feeData)
},
[
adapter,
Expand Down Expand Up @@ -257,7 +257,7 @@ export const useFoxFarming = (
},
})

return feeData
return getFeeDataFromEstimate(feeData)
},
[
adapter,
Expand Down Expand Up @@ -285,7 +285,7 @@ export const useFoxFarming = (
},
})

return feeData
return getFeeDataFromEstimate(feeData)
},
[adapter, contractAddress, foxFarmingContract],
)
Expand All @@ -306,7 +306,7 @@ export const useFoxFarming = (
const txid = await buildAndBroadcast({
accountNumber,
adapter,
feeData: feeData.average.chainSpecific,
feeData: feeData.chainSpecific,
to: uniV2LPContract.address,
value: '0',
wallet,
Expand Down Expand Up @@ -336,7 +336,7 @@ export const useFoxFarming = (
const data = foxFarmingContract.interface.encodeFunctionData('getReward')
const farmingAccountAddress = fromAccountId(farmingAccountId).account

const { average } = await adapter.getFeeData({
const feeData = await adapter.getFeeData({
to: contractAddress,
value: '0',
chainSpecific: {
Expand All @@ -348,7 +348,7 @@ export const useFoxFarming = (
const txid = await buildAndBroadcast({
accountNumber,
adapter,
feeData: average.chainSpecific,
feeData: getFeeDataFromEstimate(feeData).chainSpecific,
to: contractAddress,
value: '0',
wallet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,12 @@ export const Approve: React.FC<UniV2ApproveProps> = ({ accountId, onNext }) => {
if (!(state && dispatch && lpOpportunity)) return
if (!(isApprove0Needed || isApprove1Needed)) return
if (isAsset0AllowanceGranted && isAsset1AllowanceGranted) {
// Get deposit gas estimate
const gasData = await getDepositFeeData({
const feeData = await getDepositFeeData({
token0Amount: state.deposit.asset0CryptoAmount,
token1Amount: state.deposit.asset1CryptoAmount,
})
if (!gasData) return
const estimatedGasCryptoPrecision = bnOrZero(gasData.average.txFee)
if (!feeData) return
const estimatedGasCryptoPrecision = bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision()
dispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ export const Deposit: React.FC<DepositProps> = ({
if (!feeAsset) return
const { cryptoAmount0: token0Amount, cryptoAmount1: token1Amount } = deposit
try {
const gasData = await getDepositFeeData({
const feeData = await getDepositFeeData({
token0Amount,
token1Amount,
})
if (!gasData) return
return bnOrZero(gasData.average.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
if (!feeData) return
return bnOrZero(feeData.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
} catch (error) {
moduleLogger.error(
{ fn: 'getDepositGasEstimateCryptoPrecision', error },
Expand Down Expand Up @@ -201,28 +201,28 @@ export const Deposit: React.FC<DepositProps> = ({
// While the naive approach would be to think both assets approve() calls are going to result in the same gas estimation,
// this is not necesssarly true. Some ERC-20s approve() might have a bit more logic, and thus require more gas.
// e.g https://github.com/Uniswap/governance/blob/eabd8c71ad01f61fb54ed6945162021ee419998e/contracts/Uni.sol#L119
const asset0EstimatedGasCrypto =
const asset0FeeData =
assetId0 !== ethAssetId && (await getApproveFeeData(asset0ContractAddress!))
const asset1EstimatedGasCrypto =
const asset1FeeData =
assetId1 !== ethAssetId && (await getApproveFeeData(asset1ContractAddress!))
if (!(asset0EstimatedGasCrypto || asset1EstimatedGasCrypto)) return
if (!(asset0FeeData || asset1FeeData)) return

if (!isAsset0AllowanceGranted && asset0EstimatedGasCrypto) {
if (!isAsset0AllowanceGranted && asset0FeeData) {
dispatch({
type: UniV2DepositActionType.SET_APPROVE_0,
payload: {
estimatedGasCryptoPrecision: bnOrZero(asset0EstimatedGasCrypto.average.txFee)
estimatedGasCryptoPrecision: bnOrZero(asset0FeeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision(),
},
})
}

if (!isAsset1AllowanceGranted && asset1EstimatedGasCrypto) {
if (!isAsset1AllowanceGranted && asset1FeeData) {
dispatch({
type: UniV2DepositActionType.SET_APPROVE_1,
payload: {
estimatedGasCryptoPrecision: bnOrZero(asset1EstimatedGasCrypto.average.txFee)
estimatedGasCryptoPrecision: bnOrZero(asset1FeeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@ export const Approve: React.FC<UniV2ApproveProps> = ({ accountId, onNext }) => {
interval: 15000,
maxAttempts: 30,
})
// Get withdraw gas estimate
const gasData = await getWithdrawFeeData(
const feeData = await getWithdrawFeeData(
state.withdraw.lpAmount,
state.withdraw.asset0Amount,
state.withdraw.asset1Amount,
)
if (!gasData) return
const estimatedGasCryptoPrecision = bnOrZero(gasData.average.txFee)
if (!feeData) return
const estimatedGasCryptoPrecision = bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision()
dispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ export const Withdraw: React.FC<WithdrawProps> = ({

const getWithdrawGasEstimateCryptoPrecision = async (withdraw: WithdrawValues) => {
try {
const fee = await getWithdrawFeeData(
const feeData = await getWithdrawFeeData(
withdraw.cryptoAmount,
asset0AmountCryptoPrecision,
asset1AmountCryptoPrecision,
)
if (!fee) return
return bnOrZero(fee.average.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
if (!feeData) return
return bnOrZero(feeData.txFee).div(bn(10).pow(feeAsset.precision)).toPrecision()
} catch (error) {
// TODO: handle client side errors maybe add a toast?
moduleLogger.error(error, 'UniV2Withdraw:getWithdrawGasEstimate error:')
Expand Down Expand Up @@ -196,12 +196,12 @@ export const Withdraw: React.FC<WithdrawProps> = ({
dispatch({ type: UniV2WithdrawActionType.SET_LOADING, payload: false })
} else {
const lpAssetContractAddress = ethers.utils.getAddress(fromAssetId(lpAssetId).assetReference)
const estimatedGasCryptoPrecision = await getApproveFeeData(lpAssetContractAddress)
if (!estimatedGasCryptoPrecision) return
const feeData = await getApproveFeeData(lpAssetContractAddress)
if (!feeData) return
dispatch({
type: UniV2WithdrawActionType.SET_APPROVE,
payload: {
estimatedGasCryptoPrecision: bnOrZero(estimatedGasCryptoPrecision.average.txFee)
estimatedGasCryptoPrecision: bnOrZero(feeData.txFee)
.div(bn(10).pow(feeAsset.precision))
.toPrecision(),
},
Expand Down
Loading