-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add ows swap quote command via LI.FI #192
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
Open
Sertug17
wants to merge
1
commit into
open-wallet-standard:main
Choose a base branch
from
Sertug17:feat/ows-swap
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| use crate::CliError; | ||
| use ows_lib::vault; | ||
|
|
||
| pub struct QuoteArgs<'a> { | ||
| pub wallet_name: &'a str, | ||
| pub from_token: &'a str, | ||
| pub to_token: &'a str, | ||
| pub amount: &'a str, | ||
| pub from_chain: &'a str, | ||
| pub to_chain: Option<&'a str>, | ||
| pub slippage: f64, | ||
| pub order: &'a str, | ||
| } | ||
|
|
||
| pub fn quote(args: QuoteArgs) -> Result<(), CliError> { | ||
| let QuoteArgs { wallet_name, from_token, to_token, amount, from_chain, to_chain, slippage, order } = args; | ||
| let to_chain = to_chain.unwrap_or(from_chain); | ||
|
|
||
| // Load wallet to get address | ||
| let wallet = vault::load_wallet_by_name_or_id(wallet_name, None) | ||
| .map_err(|e| CliError::InvalidArgs(format!("wallet not found: {e}")))?; | ||
|
|
||
| // Find EVM address for the from_chain | ||
| let from_address = wallet | ||
| .accounts | ||
| .iter() | ||
| .find(|a| a.chain_id.starts_with("eip155:")) | ||
| .map(|a| a.address.clone()) | ||
| .ok_or_else(|| CliError::InvalidArgs("no EVM account found in wallet".into()))?; | ||
|
|
||
| // Convert human-readable amount to raw (assume 18 decimals for ETH, 6 for USDC) | ||
| let decimals = if from_token.to_uppercase() == "USDC" || from_token.to_uppercase() == "USDT" { | ||
| 6u32 | ||
| } else { | ||
| 18u32 | ||
| }; | ||
| let raw_amount = amount_to_raw(amount, decimals) | ||
| .map_err(|e| CliError::InvalidArgs(format!("invalid amount: {e}")))?; | ||
|
|
||
| let params = ows_pay::SwapParams { | ||
| from_chain: from_chain.to_string(), | ||
| to_chain: to_chain.to_string(), | ||
| from_token: from_token.to_string(), | ||
| to_token: to_token.to_string(), | ||
| from_amount: raw_amount, | ||
| from_address, | ||
| slippage, | ||
| order: order.to_string(), | ||
| }; | ||
|
|
||
| let rt = | ||
| tokio::runtime::Runtime::new().map_err(|e| CliError::InvalidArgs(format!("tokio: {e}")))?; | ||
|
|
||
| let result = rt | ||
| .block_on(async { | ||
| // Use a dummy wallet for dry-run (no signing needed) | ||
| struct DummyWallet; | ||
| impl ows_pay::WalletAccess for DummyWallet { | ||
| fn supported_chains(&self) -> Vec<ows_core::ChainType> { | ||
| vec![] | ||
| } | ||
| fn account(&self, _: &str) -> Result<ows_pay::Account, ows_pay::PayError> { | ||
| Err(ows_pay::PayError::new( | ||
| ows_pay::PayErrorCode::WalletNotFound, | ||
| "dry-run", | ||
| )) | ||
| } | ||
| fn sign_payload( | ||
| &self, | ||
| _: &str, | ||
| _: &str, | ||
| _: &str, | ||
| ) -> Result<String, ows_pay::PayError> { | ||
| Err(ows_pay::PayError::new( | ||
| ows_pay::PayErrorCode::SigningFailed, | ||
| "dry-run", | ||
| )) | ||
| } | ||
| } | ||
| ows_pay::swap_dry_run(&DummyWallet, params).await | ||
| }) | ||
| .map_err(|e| CliError::InvalidArgs(format!("swap quote failed: {e}")))?; | ||
|
|
||
| // Display result | ||
| eprintln!(); | ||
| eprintln!(" Swap Route"); | ||
| eprintln!(" ----------"); | ||
| eprintln!( | ||
| " {} {} -> {} {}", | ||
| result.from_amount, result.from_symbol, result.to_amount, result.to_symbol | ||
| ); | ||
| eprintln!( | ||
| " Min received: {} {}", | ||
| result.to_amount_min, result.to_symbol | ||
| ); | ||
| eprintln!(" Via: {}", result.tool); | ||
| if let Some(gas) = &result.gas_cost_usd { | ||
| eprintln!(" Gas cost: ~${gas}"); | ||
| } | ||
| eprintln!( | ||
| " Est. time: {}s", | ||
| result.execution_duration_secs as u64 | ||
| ); | ||
| eprintln!(); | ||
| eprintln!(" [dry-run — no transaction signed]"); | ||
| eprintln!(); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn amount_to_raw(amount: &str, decimals: u32) -> Result<String, String> { | ||
| let amount = amount.trim(); | ||
| let (int_part, frac_part) = if let Some(dot) = amount.find('.') { | ||
| (&amount[..dot], &amount[dot + 1..]) | ||
| } else { | ||
| (amount, "") | ||
| }; | ||
|
|
||
| if int_part.is_empty() && frac_part.is_empty() { | ||
| return Err("empty amount".into()); | ||
| } | ||
|
|
||
| let frac_trimmed = if frac_part.len() > decimals as usize { | ||
| &frac_part[..decimals as usize] | ||
| } else { | ||
| frac_part | ||
| }; | ||
|
|
||
| let frac_padded = format!("{:0<width$}", frac_trimmed, width = decimals as usize); | ||
| let combined = format!("{}{}", int_part.trim_start_matches('0'), frac_padded); | ||
| let trimmed = combined.trim_start_matches('0'); | ||
| if trimmed.is_empty() { | ||
| Ok("0".into()) | ||
| } else { | ||
| Ok(trimmed.to_string()) | ||
| } | ||
| } | ||
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.
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.
Hardcoded decimals wrong for most token types
High Severity
The decimal heuristic only recognizes USDC and USDT as 6-decimal tokens and defaults everything else to 18. Common tokens like WBTC (8 decimals), GUSD (2 decimals), or any other non-18-decimal token will have
amount_to_rawproduce a wildly incorrect raw amount. For example, swapping 0.1 WBTC computes afromAmountof 10^17 instead of 10^7 — off by a factor of 10 billion — resulting in a completely wrong quote from the LI.FI API.Reviewed by Cursor Bugbot for commit 5b1e6aa. Configure here.