-
Notifications
You must be signed in to change notification settings - Fork 491
feat: resolve flashloan fee on-chain via ACLManager #2972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21b2292
feat: resolve flashloan fee on-chain via ACLManager
mgrabina 1f1394c
fix: gate flashloan-fee readers on resolved on-chain value
mgrabina 3e7f14b
fix: throw on swap submit when flashloan fee unresolved
mgrabina ed687af
chore: instrument useFlashLoanFeeBps for preview debugging
mgrabina 4358456
chore: log useFlashloan + applied bps in swap order amounts
mgrabina 864ad9a
chore: remove preview debug logs
mgrabina f888f57
fix: clear flashloan fee on pending; map Linea + Plasma ACLManagers
mgrabina 13f32ad
refactor: skip ACLManager check for Paraswap (msg.sender is user)
mgrabina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
src/components/transactions/Swap/hooks/useFlashLoanFeeBps.ts
This file contains hidden or 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,104 @@ | ||
| import { | ||
| AaveV3Arbitrum, | ||
| AaveV3Avalanche, | ||
| AaveV3Base, | ||
| AaveV3BNB, | ||
| AaveV3Ethereum, | ||
| AaveV3EthereumEtherFi, | ||
| AaveV3EthereumLido, | ||
| AaveV3Gnosis, | ||
| AaveV3Optimism, | ||
| AaveV3Polygon, | ||
| AaveV3Sonic, | ||
| } from '@aave-dao/aave-address-book'; | ||
| import { SupportedChainId } from '@cowprotocol/cow-sdk'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { Contract } from 'ethers'; | ||
| import { CustomMarket, MarketDataType } from 'src/ui-config/marketsConfig'; | ||
| import { getProvider } from 'src/utils/marketsAndNetworksConfig'; | ||
|
|
||
| import { ADAPTER_FACTORY, FLASH_LOAN_FEE_BPS } from '../constants/cow.constants'; | ||
| import { PARASWAP_FLASH_LOAN_FEE_BPS } from '../constants/paraswap.constants'; | ||
| import { SwapProvider, SwapType } from '../types'; | ||
|
|
||
| const ACL_MANAGER_ABI = ['function isFlashBorrower(address) view returns (bool)']; | ||
|
|
||
| const ACL_MANAGER_BY_MARKET: Partial<Record<CustomMarket, string>> = { | ||
| [CustomMarket.proto_mainnet_v3]: AaveV3Ethereum.ACL_MANAGER, | ||
| [CustomMarket.proto_lido_v3]: AaveV3EthereumLido.ACL_MANAGER, | ||
| [CustomMarket.proto_etherfi_v3]: AaveV3EthereumEtherFi.ACL_MANAGER, | ||
| [CustomMarket.proto_arbitrum_v3]: AaveV3Arbitrum.ACL_MANAGER, | ||
| [CustomMarket.proto_base_v3]: AaveV3Base.ACL_MANAGER, | ||
| [CustomMarket.proto_polygon_v3]: AaveV3Polygon.ACL_MANAGER, | ||
| [CustomMarket.proto_optimism_v3]: AaveV3Optimism.ACL_MANAGER, | ||
| [CustomMarket.proto_avalanche_v3]: AaveV3Avalanche.ACL_MANAGER, | ||
| [CustomMarket.proto_sonic_v3]: AaveV3Sonic.ACL_MANAGER, | ||
| [CustomMarket.proto_gnosis_v3]: AaveV3Gnosis.ACL_MANAGER, | ||
| [CustomMarket.proto_bnb_v3]: AaveV3BNB.ACL_MANAGER, | ||
| }; | ||
|
|
||
| const fallbackBps = (provider: SwapProvider): number => | ||
| provider === SwapProvider.COW_PROTOCOL ? FLASH_LOAN_FEE_BPS : PARASWAP_FLASH_LOAN_FEE_BPS; | ||
|
|
||
| const resolveTarget = ( | ||
| provider: SwapProvider, | ||
| swapType: SwapType, | ||
| marketData: MarketDataType | ||
| ): string | undefined => { | ||
| if (provider === SwapProvider.COW_PROTOCOL) { | ||
| return ADAPTER_FACTORY[marketData.chainId as unknown as SupportedChainId] || undefined; | ||
| } | ||
| if (provider === SwapProvider.PARASWAP) { | ||
| switch (swapType) { | ||
| case SwapType.CollateralSwap: | ||
| return marketData.addresses.SWAP_COLLATERAL_ADAPTER; | ||
| case SwapType.RepayWithCollateral: | ||
| return marketData.addresses.REPAY_WITH_COLLATERAL_ADAPTER; | ||
| case SwapType.DebtSwap: | ||
| return marketData.addresses.DEBT_SWITCH_ADAPTER; | ||
| case SwapType.WithdrawAndSwap: | ||
| return marketData.addresses.WITHDRAW_SWITCH_ADAPTER; | ||
| default: | ||
| return undefined; | ||
| } | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * Resolve the flashloan fee bps for the active swap, defaulting to the | ||
| * provider's hardcoded bps when the on-chain ACLManager check is unavailable | ||
| * or pending. Returns 0 only when the target adapter (Paraswap per-flow) / | ||
| * factory (CoW) is registered as a FLASH_BORROWER on the market's ACLManager. | ||
| */ | ||
| export const useFlashLoanFeeBps = ({ | ||
| provider, | ||
| swapType, | ||
| marketData, | ||
| }: { | ||
| provider: SwapProvider; | ||
| swapType: SwapType; | ||
| marketData: MarketDataType; | ||
| }): number => { | ||
| const aclManager = ACL_MANAGER_BY_MARKET[marketData.market]; | ||
| const target = resolveTarget(provider, swapType, marketData); | ||
| const fallback = fallbackBps(provider); | ||
|
|
||
| const enabled = Boolean(aclManager && target); | ||
|
|
||
| const { data: isWhitelisted } = useQuery({ | ||
| queryFn: async (): Promise<boolean> => { | ||
| const contract = new Contract( | ||
| aclManager as string, | ||
| ACL_MANAGER_ABI, | ||
| getProvider(marketData.chainId) | ||
| ); | ||
| return contract.isFlashBorrower(target); | ||
| }, | ||
| queryKey: ['flashBorrowerCheck', marketData.chainId, aclManager, target], | ||
| enabled, | ||
| staleTime: 5 * 60 * 1000, | ||
| }); | ||
|
|
||
| return isWhitelisted === true ? 0 : fallback; | ||
| }; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.