-
-
Notifications
You must be signed in to change notification settings - Fork 246
feat: dynamic swap stablecoins and default slippage selector #6616
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e5a4150
chore: read stablecoins list from LD
micaelae 49b60e0
feat: getSlippagePercentage util
micaelae 2af91cc
feat: selectDefaultSlippagePercentage selector
micaelae 05c6845
chore: deprecate isCustomSlippage util
micaelae c7750b9
chore: update changelog
micaelae 4d8fdbb
test: add unit tests
micaelae 8029191
fix: fallback to src stablecoins if destChainId is not defined (swap)
micaelae d195b01
Merge branch 'main' into swaps2873-dynamic-stablecoins
micaelae 5c44d04
Merge branch 'main' into swaps2873-dynamic-stablecoins
micaelae a5fa587
Merge branch 'main' into swaps2873-dynamic-stablecoins
micaelae 69594e1
Merge branch 'main' into swaps2873-dynamic-stablecoins
micaelae 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { isCrossChain, isSolanaChainId } from './bridge'; | ||
import type { GenericQuoteRequest } from '../types'; | ||
|
||
export const BRIDGE_DEFAULT_SLIPPAGE = 0.5; | ||
const SWAP_SOLANA_SLIPPAGE = undefined; | ||
const SWAP_EVM_STABLECOIN_SLIPPAGE = 0.5; | ||
const SWAP_EVM_DEFAULT_SLIPPAGE = 2; | ||
|
||
/** | ||
* Calculates the appropriate slippage based on the transaction context | ||
* | ||
* Rules: | ||
* - Bridge (cross-chain): Always 0.5% | ||
* - Swap on Solana: Always undefined (AUTO mode) | ||
* - Swap on EVM stablecoin pairs (same chain only): 0.5% | ||
* - Swap on EVM other pairs: 2% | ||
* | ||
* @param options - the options for the destination chain | ||
* @param options.srcTokenAddress - the source token address | ||
* @param options.destTokenAddress - the destination token address | ||
* @param options.srcChainId - the source chain id | ||
* @param options.destChainId - the destination chain id | ||
* @param srcStablecoins - the list of stablecoins on the source chain | ||
* @param destStablecoins - the list of stablecoins on the destination chain | ||
|
||
* @returns the default slippage percentage for the chain and token pair | ||
*/ | ||
export const getDefaultSlippagePercentage = ( | ||
{ | ||
srcTokenAddress, | ||
destTokenAddress, | ||
srcChainId, | ||
destChainId, | ||
}: Partial< | ||
Pick< | ||
GenericQuoteRequest, | ||
'srcTokenAddress' | 'destTokenAddress' | 'srcChainId' | 'destChainId' | ||
> | ||
>, | ||
srcStablecoins?: string[], | ||
destStablecoins?: string[], | ||
) => { | ||
if (!srcChainId || isCrossChain(srcChainId, destChainId)) { | ||
return BRIDGE_DEFAULT_SLIPPAGE; | ||
} | ||
|
||
if (isSolanaChainId(srcChainId)) { | ||
return SWAP_SOLANA_SLIPPAGE; | ||
} | ||
|
||
if ( | ||
srcTokenAddress && | ||
destTokenAddress && | ||
srcStablecoins | ||
?.map((stablecoin) => stablecoin.toLowerCase()) | ||
.includes(srcTokenAddress.toLowerCase()) && | ||
// If destChainId is undefined, treat req as a swap and fallback to srcStablecoins | ||
(destStablecoins ?? srcStablecoins) | ||
?.map((stablecoin) => stablecoin.toLowerCase()) | ||
.includes(destTokenAddress.toLowerCase()) | ||
) { | ||
return SWAP_EVM_STABLECOIN_SLIPPAGE; | ||
} | ||
|
||
return SWAP_EVM_DEFAULT_SLIPPAGE; | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Chain ID Mismatch Causes Incorrect Slippage
The
selectDefaultSlippagePercentage
selector passes CAIP-formatted chain IDs togetDefaultSlippagePercentage
. This function expects original numeric chain IDs for its internal logic (likeisCrossChain
andisSolanaChainId
), which can lead to incorrect slippage calculations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inaccurate