forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 118
feat(swap): rpc to find best swap with liquidity routing for ask #2362
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
71 commits
Select commit
Hold shift + click to select a range
84663a4
add LR swap rpc scaffolding
05c15fc
refactor LR rpcs
43d2bd6
fix doc comments
e5c73b5
fix draft lr rpc params
6b4124d
WiP: find_best_lr_buy_for_multiple_orders initial compiled code
e2f8a53
add missed lr_impl.rs
0ea3dd9
fix comment
92bf147
wip: working code for finding best swap with LR to for ask with rel=t…
b86f433
Merge branch 'dev' into lr-swap-wip
08ff400
remove unused test code
6e55e7e
remove debug println
54d13c3
remove unused derive and import
5553bdc
fix fmt
1e3a5d8
refactor: eliminate SomeError in LR code
7ab51fc
wrap LR test with feature
5e96440
fix LR error desc
bbb115e
add another coin to LR quote test
c850505
review (shamardy): eliminate dup fn, reuse get_erc20_ticker_by_contra…
1255141
Merge branch 'dev' into lr-swap-wip
f4fba47
fix doc comment
18e4668
fix wasm build
c5f5734
fix (laruh): propagate errors
9aee864
review (mariocynicys): add type for Vec<CrossPricesData>
6bc98f3
unused import
d097ad3
review (mariocynicys): fix using decimals in result (take from api)
0ecb31d
review (laruh): refactor calc_total_price
465fe23
review (laruh): more refactor select best swap code
f3bf400
review: add errors in update_with_contracts, refactor get_coin_for_on…
32ddfa2
review (laruh): refactor decimals error, remove extra to_string
204763d
Merge branch 'dev' into lr-swap-wip
ba1a032
refactor: avoid clone in update lr_data fn's
4326068
review (mariocynicys): avoid clone
1907beb
review: fix StatusCode for internal error
61641c9
review: improve doc comments
e930b14
review (mariocynicys): use most recent prices by CROSS_PRICES_LIMIT as 1
23e0d81
fix 1inch test feature placement
f18e57f
fix 1inch result conversion test
177c8de
refactor 1inch url builder
0238033
fix find best lr swap behaviour: skip lr provider error results (to u…
6505e80
add TODO
9ed36d2
review (laruh): add fn to get contracts from LrData
559b1c5
review (laruh): rename fn
568f364
improve bad api TokenInfo error messages
f11acaa
fix src_decimals var name
66e59d2
eliminate from_api_error fn
f468fcc
Merge branch 'dev' into lr-swap-wip
9d5c76b
fix tx value eth conversion
0276312
error msg improved
e98c1ff
added doc comments for LrData struct
2a668ac
review (mariocynicys): fix iterators zipping, refactor 1inch url buil…
d05697c
Merge branch 'dev' into lr-swap-wip
2a226ab
refactor (review):
e4ef307
Merge branch 'dev' into lr-swap-wip
8f13278
fix getting chain_id from protocol_data
37249ce
fix custom token error name
20364ed
refactor (review): fix namespace for 1inch and lr rpcs
0ccd318
improve logging in LR find quote test
855bf74
fix mod tron placement
f454d6c
review (mariocynicys): remove extra into_iter
c1a430c
refactor (review): use liquidity_routing rpc prefix
161afb8
refactor (review): rename LR rpcs to use verbs
be33db7
refactor: improve and extend LR RPC structures to prepare for support…
3c6f16b
fix doc comment for lr_find_best_quote_rpc
6d6fe5c
refactor: use RwLock for lr swap candidates
8cdddff
fix shared swap candidate lock use (avoid excessive read lock)
4d8bbe7
Rename find best swap path fn (to make it more general)
2e384a4
fix doc comment for find_best_swap_path_with_lr improved with gpt
161164d
add eth_utils.rs and move mm_number_to_u256 and like fn's there
d705741
improve doc comment for lr_find_best_quote_rpc fn
fafd785
Merge branch 'dev' into lr-swap-wip
shamardy 5b24a8e
post merge fix
shamardy 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,107 @@ | ||
| use super::{ETH_DECIMALS, ETH_GWEI_DECIMALS}; | ||
| use crate::{NumConversError, NumConversResult}; | ||
| use ethabi::{Function, Token}; | ||
| use ethereum_types::{Address, FromDecStrErr, U256}; | ||
| use ethkey::{public_to_address, Public}; | ||
| use mm2_err_handle::prelude::MapToMmResult; | ||
| use mm2_number::{BigDecimal, MmNumber}; | ||
| use secp256k1::PublicKey; | ||
|
|
||
| pub(crate) fn get_function_input_data(decoded: &[Token], func: &Function, index: usize) -> Result<Token, String> { | ||
| decoded.get(index).cloned().ok_or(format!( | ||
| "Missing input in function {}: No input found at index {}", | ||
| func.name.clone(), | ||
| index | ||
| )) | ||
| } | ||
|
|
||
| pub(crate) fn get_function_name(name: &str, watcher_reward: bool) -> String { | ||
| if watcher_reward { | ||
| format!("{}{}", name, "Reward") | ||
| } else { | ||
| name.to_owned() | ||
| } | ||
| } | ||
|
|
||
| pub fn addr_from_raw_pubkey(pubkey: &[u8]) -> Result<Address, String> { | ||
| let pubkey = try_s!(PublicKey::from_slice(pubkey).map_err(|e| ERRL!("{:?}", e))); | ||
| let eth_public = Public::from_slice(&pubkey.serialize_uncompressed()[1..65]); | ||
| Ok(public_to_address(ð_public)) | ||
| } | ||
|
|
||
| pub fn addr_from_pubkey_str(pubkey: &str) -> Result<String, String> { | ||
| let pubkey_bytes = try_s!(hex::decode(pubkey)); | ||
| let addr = try_s!(addr_from_raw_pubkey(&pubkey_bytes)); | ||
| Ok(format!("{:#02x}", addr)) | ||
| } | ||
|
|
||
| pub(crate) fn display_u256_with_decimal_point(number: U256, decimals: u8) -> String { | ||
| let mut string = number.to_string(); | ||
| let decimals = decimals as usize; | ||
| if string.len() <= decimals { | ||
| string.insert_str(0, &"0".repeat(decimals - string.len() + 1)); | ||
| } | ||
|
|
||
| string.insert(string.len() - decimals, '.'); | ||
| string.trim_end_matches('0').into() | ||
| } | ||
|
|
||
| /// Converts 'number' to value with decimal point and shifts it left by 'decimals' places | ||
| pub fn u256_to_big_decimal(number: U256, decimals: u8) -> NumConversResult<BigDecimal> { | ||
| let string = display_u256_with_decimal_point(number, decimals); | ||
| Ok(string.parse::<BigDecimal>()?) | ||
| } | ||
|
|
||
| /// Shifts 'number' with decimal point right by 'decimals' places and converts it to U256 value | ||
| pub fn wei_from_big_decimal(amount: &BigDecimal, decimals: u8) -> NumConversResult<U256> { | ||
| let mut amount = amount.to_string(); | ||
| let dot = amount.find(|c| c == '.'); | ||
| let decimals = decimals as usize; | ||
| if let Some(index) = dot { | ||
| let mut fractional = amount.split_off(index); | ||
| // remove the dot from fractional part | ||
| fractional.remove(0); | ||
| if fractional.len() < decimals { | ||
| fractional.insert_str(fractional.len(), &"0".repeat(decimals - fractional.len())); | ||
| } | ||
| fractional.truncate(decimals); | ||
| amount.push_str(&fractional); | ||
| } else { | ||
| amount.insert_str(amount.len(), &"0".repeat(decimals)); | ||
| } | ||
| U256::from_dec_str(&amount).map_to_mm(|e| NumConversError::new(format!("{:?}", e))) | ||
| } | ||
|
|
||
| /// Converts BigDecimal gwei value to wei value as U256 | ||
| #[inline(always)] | ||
| pub fn wei_from_gwei_decimal(bigdec: &BigDecimal) -> NumConversResult<U256> { | ||
| wei_from_big_decimal(bigdec, ETH_GWEI_DECIMALS) | ||
| } | ||
|
|
||
| /// Converts a U256 wei value to an gwei value as a BigDecimal | ||
| #[inline(always)] | ||
| pub fn wei_to_gwei_decimal(wei: U256) -> NumConversResult<BigDecimal> { u256_to_big_decimal(wei, ETH_GWEI_DECIMALS) } | ||
|
|
||
| /// Converts a U256 wei value to an ETH value as a BigDecimal | ||
| /// TODO: use wei_to_eth_decimal instead of u256_to_big_decimal(gas_cost_wei, ETH_DECIMALS) | ||
| #[inline(always)] | ||
| pub fn wei_to_eth_decimal(wei: U256) -> NumConversResult<BigDecimal> { u256_to_big_decimal(wei, ETH_DECIMALS) } | ||
|
|
||
| #[inline] | ||
| pub fn mm_number_to_u256(mm_number: &MmNumber) -> Result<U256, FromDecStrErr> { | ||
| U256::from_dec_str(mm_number.to_ratio().to_integer().to_string().as_str()) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn mm_number_from_u256(u256: U256) -> MmNumber { MmNumber::from(u256.to_string().as_str()) } | ||
|
|
||
| #[inline] | ||
| pub fn wei_from_coins_mm_number(mm_number: &MmNumber, decimals: u8) -> NumConversResult<U256> { | ||
| wei_from_big_decimal(&mm_number.to_decimal(), decimals) | ||
| } | ||
|
|
||
| #[inline] | ||
| #[allow(unused)] | ||
| pub fn wei_to_coins_mm_number(u256: U256, decimals: u8) -> NumConversResult<MmNumber> { | ||
| Ok(MmNumber::from(u256_to_big_decimal(u256, decimals)?)) | ||
| } |
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
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.
This is just makes things more confusing, an example, it's not used in this module.
I would create a proper type instead of creating an alias, which would also resolve this issue.
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.
I indeed thought about creating such a new type but it would require to change a lot of code, if deploy it properly everywhere. So I decided to make a small step to that.
I am dealing with multiple various coins/tokens in this liquidity routing PR and it is easy to get lost when String is used everywhere for tickers. High level types with the 'Ticker' alias like below allow better code understanding :
inner: HashMap<(Ticker, Ticker), LrData>orfn new_with_src_token(src_token: Ticker, orders: Vec<RpcOrderbookEntryV2>)(Ofc I agree the new type is much better)