-
Notifications
You must be signed in to change notification settings - Fork 54
[codex] Improve Uniswap quote routing #1132
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
Draft
gemcoder21
wants to merge
1
commit into
main
Choose a base branch
from
codex/uniswap-quote-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,11 +23,10 @@ pub struct TokenPairs(pub Vec<TokenPair>); | |
| impl Display for TokenPairs { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "[")?; | ||
| let mut iter = self.0.iter(); | ||
| if let Some(first) = iter.next() { | ||
| write!(f, "{first}")?; // Write first element without a leading comma | ||
| for item in iter { | ||
| write!(f, ", {item}")?; // Write subsequent elements with a leading comma | ||
| if let Some((first, rest)) = self.0.split_first() { | ||
| write!(f, "{first}")?; | ||
| for item in rest { | ||
| write!(f, ", {item}")?; | ||
| } | ||
| } | ||
| write!(f, "]") | ||
|
|
@@ -36,16 +35,20 @@ impl Display for TokenPairs { | |
|
|
||
| impl TokenPair { | ||
| pub fn new_two_hop(token_in: &Address, intermediary: &Address, token_out: &Address, fee_tier: FeeTier) -> Vec<TokenPair> { | ||
| Self::new_two_hop_with_fees(token_in, intermediary, token_out, fee_tier, fee_tier) | ||
| } | ||
|
|
||
| pub fn new_two_hop_with_fees(token_in: &Address, intermediary: &Address, token_out: &Address, first_fee_tier: FeeTier, second_fee_tier: FeeTier) -> Vec<TokenPair> { | ||
| vec![ | ||
| TokenPair { | ||
| token_in: *token_in, | ||
| token_out: *intermediary, | ||
| fee_tier, | ||
| fee_tier: first_fee_tier, | ||
| }, | ||
| TokenPair { | ||
| token_in: *intermediary, | ||
| token_out: *token_out, | ||
| fee_tier, | ||
| fee_tier: second_fee_tier, | ||
| }, | ||
| ] | ||
| } | ||
|
|
@@ -60,17 +63,15 @@ pub struct BasePair { | |
|
|
||
| impl BasePair { | ||
| pub fn path_building_array(&self) -> Vec<Address> { | ||
| let mut array = vec![self.native]; | ||
| array.extend(self.stables.iter().cloned()); | ||
| // alternatives is not used for path building to reduce requests | ||
| array | ||
| std::iter::once(self.native).chain(self.stables.iter().copied()).collect() | ||
| } | ||
|
|
||
| pub fn fee_token_array(&self) -> Vec<Address> { | ||
| let mut array = vec![self.native]; | ||
| array.extend(self.stables.iter().cloned()); | ||
| array.extend(self.alternatives.iter().cloned()); | ||
| array | ||
| std::iter::once(self.native) | ||
| .chain(self.stables.iter().copied()) | ||
| .chain(self.alternatives.iter().copied()) | ||
| .collect() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -150,57 +151,43 @@ pub fn get_base_pair(chain: &EVMChain, weth_as_native: bool) -> Option<BasePair> | |
| _ => panic!("USDT is not configured for this chain"), | ||
| }; | ||
|
|
||
| let mut stables = vec![]; | ||
| if !usdc.is_empty() { | ||
| stables.push(usdc.parse().ok()?); | ||
| } | ||
| if !usdt.is_empty() { | ||
| stables.push(usdt.parse().ok()?); | ||
| } | ||
| let alternatives = { if btc.is_empty() { vec![] } else { vec![btc.parse().ok()?] } }; | ||
| let stables = [usdc, usdt] | ||
| .into_iter() | ||
| .filter(|token| !token.is_empty()) | ||
| .map(|token| token.parse().ok()) | ||
| .collect::<Option<Vec<_>>>()?; | ||
| let alternatives = if btc.is_empty() { vec![] } else { vec![btc.parse().ok()?] }; | ||
|
|
||
| Some(BasePair { native, stables, alternatives }) | ||
| } | ||
|
|
||
| pub fn build_direct_pair(token_in: &Address, token_out: &Address, fee_tier: FeeTier) -> Bytes { | ||
| let mut bytes: Vec<u8> = vec![]; | ||
| let fee = U24::from(fee_tier.as_u24()); | ||
| bytes.extend(token_in.as_slice()); | ||
| bytes.extend(&fee.to_be_bytes_vec()); | ||
| bytes.extend(token_out.as_slice()); | ||
| Bytes::from(bytes) | ||
| let fee = U24::from(fee_tier.as_u24()).to_be_bytes_vec(); | ||
| Bytes::from([token_in.as_slice(), fee.as_slice(), token_out.as_slice()].concat()) | ||
| } | ||
|
|
||
| pub fn validate_pairs(token_pairs: &[TokenPair]) -> bool { | ||
| // verify token in and out are chained | ||
| let mut iter = token_pairs.iter().peekable(); | ||
| let mut valid = true; | ||
| while let Some(current_pair) = iter.next() { | ||
| if let Some(next_pair) = iter.peek() | ||
| && current_pair.token_out != next_pair.token_in | ||
| { | ||
| valid = false; | ||
| break; | ||
| } | ||
| } | ||
| valid | ||
| token_pairs.windows(2).all(|pairs| pairs[0].token_out == pairs[1].token_in) | ||
| } | ||
|
|
||
| pub fn build_pairs(token_pairs: &[TokenPair]) -> Bytes { | ||
| let valid = validate_pairs(token_pairs); | ||
| if !valid { | ||
| if !validate_pairs(token_pairs) { | ||
| panic!("invalid token pairs"); | ||
| } | ||
|
|
||
| let mut bytes: Vec<u8> = vec![]; | ||
| for (idx, token_pair) in token_pairs.iter().enumerate() { | ||
| let fee = U24::from(token_pair.fee_tier.as_u24()); | ||
| if idx == 0 { | ||
| bytes.extend(token_pair.token_in.as_slice()); | ||
| } | ||
| bytes.extend(&fee.to_be_bytes_vec()); | ||
| bytes.extend(token_pair.token_out.as_slice()); | ||
| } | ||
| let bytes = token_pairs | ||
| .iter() | ||
| .enumerate() | ||
| .flat_map(|(idx, token_pair)| { | ||
| let fee = U24::from(token_pair.fee_tier.as_u24()).to_be_bytes_vec(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if idx == 0 { | ||
| [token_pair.token_in.as_slice(), fee.as_slice(), token_pair.token_out.as_slice()].concat() | ||
| } else { | ||
| [fee.as_slice(), token_pair.token_out.as_slice()].concat() | ||
| } | ||
| }) | ||
| .collect::<Vec<u8>>(); | ||
| Bytes::from(bytes) | ||
| } | ||
|
|
||
|
|
||
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.
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.
fee_tier.as_u24()already returns aU24type. Wrapping it inU24::from()is redundant.