feat(LRAPI): add 1inch classic swap rpc#2222
Conversation
| } | ||
|
|
||
| /// "1inch_classic_swap_create" rpc impl | ||
| pub async fn one_inch_v6_0_classic_swap_create_rpc( |
There was a problem hiding this comment.
Note, that this rpc actually creates a transaction to call the swap aggregation contract. It is supposed that the GUI should sign it and send. We don't verify the tx in any way and trust the 1inch api.
There was a problem hiding this comment.
Then lest add this clarification to doc comment
laruh
left a comment
There was a problem hiding this comment.
Great start! Here is the 1st review iteration
| pub struct ClassicSwapQuoteRequest { | ||
| pub base: String, | ||
| pub rel: String, | ||
| pub amount: BigDecimal, |
There was a problem hiding this comment.
What do you think about using the U256 type for the "amount" from the beginning? We don’t have a 1inch coin, in wallet or our own trading protocol features, so we are free to use coin-specific types for external third-party trading
There was a problem hiding this comment.
I used BigDecimal to make it like most our other rpcs do (for e.g. withdraw).
But now I see I probably should use MmNumber as those classic swap rpcs are close to our "buy" and "sell" methods
There was a problem hiding this comment.
I made the swap amount as MmNumber (like in our own swap rpcs). I hope this is consistent
There was a problem hiding this comment.
I made the swap amount as MmNumber (like in our own swap rpcs). I hope this is consistent
Hmm, I think yes. We use MmNumber type for atomic swaps and, as 1inch is a trading aggregator, then we can do the same for it.
I will leave comment as unresolved, so other people can find this.
mariocynicys
left a comment
There was a problem hiding this comment.
Thanks :)
First review iteration.
| pub fn with_fee(mut self, fee: Option<f32>) -> Self { | ||
| self.fee = fee; | ||
| self | ||
| } | ||
| pub fn with_protocols(mut self, protocols: Option<String>) -> Self { | ||
| self.protocols = protocols; | ||
| self | ||
| } | ||
| pub fn with_gas_price(mut self, gas_price: Option<String>) -> Self { | ||
| self.gas_price = gas_price; | ||
| self | ||
| } | ||
| pub fn with_complexity_level(mut self, complexity_level: Option<u32>) -> Self { | ||
| self.complexity_level = complexity_level; | ||
| self | ||
| } | ||
| pub fn with_parts(mut self, parts: Option<u32>) -> Self { | ||
| self.parts = parts; | ||
| self | ||
| } | ||
| pub fn with_main_route_parts(mut self, main_route_parts: Option<u32>) -> Self { | ||
| self.main_route_parts = main_route_parts; | ||
| self | ||
| } | ||
| pub fn with_gas_limit(mut self, gas_limit: Option<u128>) -> Self { | ||
| self.gas_limit = gas_limit; | ||
| self | ||
| } | ||
| pub fn with_include_tokens_info(mut self, include_tokens_info: Option<bool>) -> Self { | ||
| self.include_tokens_info = include_tokens_info; | ||
| self | ||
| } | ||
| pub fn with_include_protocols(mut self, include_protocols: Option<bool>) -> Self { | ||
| self.include_protocols = include_protocols; | ||
| self | ||
| } | ||
| pub fn with_include_gas(mut self, include_gas: Option<bool>) -> Self { | ||
| self.include_gas = include_gas; | ||
| self | ||
| } | ||
| pub fn with_connector_tokens(mut self, connector_tokens: Option<String>) -> Self { | ||
| self.connector_tokens = connector_tokens; | ||
| self | ||
| } |
There was a problem hiding this comment.
I am not a fan of this pattern personally, but if we really want to do this I guess the best way would be creating macro which implements this pattern automatically without taking too much space in the module.
There was a problem hiding this comment.
I am not a fan of this pattern personally, but if we really want to do this I guess the best way would be creating macro which implements this pattern automatically without taking too much space in the module.
really like this idea
There was a problem hiding this comment.
made a macro, great idea, ty
borngraced
left a comment
There was a problem hiding this comment.
great work! couple notes for first review
| (*guard).clone() | ||
| } | ||
|
|
||
| pub fn chain_id(&self) -> u64 { self.chain_id } |
There was a problem hiding this comment.
| pub fn chain_id(&self) -> u64 { self.chain_id } | |
| #[inline(always)] | |
| pub fn chain_id(&self) -> u64 { self.chain_id } |
|
@dimxy please fix branch conflicts |
shamardy
left a comment
There was a problem hiding this comment.
Thanks for the PR! First review iteration from my side where I gave the types.rs a first look.
* dev: fix(cosmos): fix tx broadcasting error (#2238) chore(solana): remove solana implementation (#2239) chore(cli): remove leftover subcommands from help message (#2235) fix(orders): fix cancel order race condition using time-based cache (#2232) fix(legacy-swap): taker failed spend maker payment marked as failed (#2199) chore(adex-cli): deprecate adex-cli (#2234) feat(new-RPC): connection healthcheck implementation for peers (#2194) fix(proxy-signature): add message lifetime overflows (#2233) feat(CI): handle remote files in a safer way (#2217) chore(doc): update issue address in README (#2227) fix(merge): remove duplicated db_root function (#2229) feat(wallets): add `get_wallet_names` rpc (#2202) chore(tests): don't use `.wait()` and use `block_on` instead (#2220) fix(native-rpc): remove escaped response body (#2219) fix(clippy): fix coins mod clippy warnings in wasm (#2224) feat(core): handling CTRL-C signal with graceful shutdown (#2213) docs(README): fix typos (#2212) remove the non-sense arguments (#2216) fix(db): stop creating the all-zeroes dir on KDF start (#2218)
|
In the recent 5 commits I fixed most review notes above, |
| impl TxFields { | ||
| pub(crate) fn from_api_value( | ||
| tx_fields: one_inch_api::types::TxFields, | ||
| decimals: u8, | ||
| ) -> MmResult<Self, FromApiValueError> { | ||
| Ok(Self { | ||
| from: tx_fields.from, | ||
| to: tx_fields.to, | ||
| data: BytesJson::from(hex::decode(str_strip_0x!(tx_fields.data.as_str()))?), | ||
| value: u256_to_big_decimal(U256::from_dec_str(&tx_fields.value)?, decimals)?, | ||
| gas_price: wei_to_gwei_decimal(U256::from_dec_str(&tx_fields.gas_price)?)?, | ||
| gas: tx_fields.gas, | ||
| }) | ||
| } | ||
| } |
| fn validate_slippage(slippage: f32) -> MmResult<(), ApiClientError> { | ||
| if !(0.0..=ONE_INCH_MAX_SLIPPAGE).contains(&slippage) { | ||
| return Err(ApiClientError::OutOfBounds { | ||
| param: "slippage".to_owned(), | ||
| value: slippage.to_string(), | ||
| min: 0.0.to_string(), | ||
| max: ONE_INCH_MAX_SLIPPAGE.to_string(), | ||
| } | ||
| .into()); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_fee(fee: &Option<f32>) -> MmResult<(), ApiClientError> { | ||
| if let Some(fee) = fee { | ||
| if !(0.0..=ONE_INCH_MAX_FEE_SHARE).contains(fee) { | ||
| return Err(ApiClientError::OutOfBounds { | ||
| param: "fee".to_owned(), | ||
| value: fee.to_string(), | ||
| min: 0.0.to_string(), | ||
| max: ONE_INCH_MAX_FEE_SHARE.to_string(), | ||
| } | ||
| .into()); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_gas_limit(gas_limit: &Option<u128>) -> MmResult<(), ApiClientError> { | ||
| if let Some(gas_limit) = gas_limit { | ||
| if gas_limit > &ONE_INCH_MAX_GAS { | ||
| return Err(ApiClientError::OutOfBounds { | ||
| param: "gas_limit".to_owned(), | ||
| value: gas_limit.to_string(), | ||
| min: 0.to_string(), | ||
| max: ONE_INCH_MAX_GAS.to_string(), | ||
| } | ||
| .into()); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_parts(parts: &Option<u32>) -> MmResult<(), ApiClientError> { | ||
| if let Some(parts) = parts { | ||
| if parts > &ONE_INCH_MAX_PARTS { | ||
| return Err(ApiClientError::OutOfBounds { | ||
| param: "parts".to_owned(), | ||
| value: parts.to_string(), | ||
| min: 0.to_string(), | ||
| max: ONE_INCH_MAX_PARTS.to_string(), | ||
| } | ||
| .into()); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_main_route_parts(main_route_parts: &Option<u32>) -> MmResult<(), ApiClientError> { | ||
| if let Some(main_route_parts) = main_route_parts { | ||
| if main_route_parts > &ONE_INCH_MAX_MAIN_ROUTE_PARTS { | ||
| return Err(ApiClientError::OutOfBounds { | ||
| param: "main route parts".to_owned(), | ||
| value: main_route_parts.to_string(), | ||
| min: 0.to_string(), | ||
| max: ONE_INCH_MAX_MAIN_ROUTE_PARTS.to_string(), | ||
| } | ||
| .into()); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_complexity_level(complexity_level: &Option<u32>) -> MmResult<(), ApiClientError> { | ||
| if let Some(complexity_level) = complexity_level { | ||
| if complexity_level > &ONE_INCH_MAX_COMPLEXITY_LEVEL { | ||
| return Err(ApiClientError::OutOfBounds { | ||
| param: "complexity level".to_owned(), | ||
| value: complexity_level.to_string(), | ||
| min: 0.to_string(), | ||
| max: ONE_INCH_MAX_COMPLEXITY_LEVEL.to_string(), | ||
| } | ||
| .into()); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
you can move validation logics inside fn validate_params if you don't plan to reuse them. This will also reduce boilerplate code
There was a problem hiding this comment.
tbh I'd like to have an option to break functions like that (even if those functions are not reused), for code structuring
| Ok(()) | ||
| } else { | ||
| Err(MmError::new(ApiIntegrationRpcError::ChainNotSupported)) | ||
| fn api_supports_coin(base: &EthCoin, rel: &EthCoin) -> MmResult<(), ApiIntegrationRpcError> { |
There was a problem hiding this comment.
nit: should be called api_supports_pair
mm2src/coins/lp_coins.rs
Outdated
| let eth_coin = find_erc20_eth_coin(&ctx, &req.coin).await?; | ||
| let amount = wei_from_big_decimal(&req.amount, eth_coin.decimals())?; | ||
| let tx = eth_coin.approve(req.spender, amount).compat().await?; | ||
| Ok(tx.tx_hash_as_bytes()) |
There was a problem hiding this comment.
But is it convenient for GUI when we return tx hash in different formats for different coins
No, we should fix those cases (in a different PR of course). tx hashes and addresses should always be displayed/sent to GUIs with 0x prefix
shamardy
left a comment
There was a problem hiding this comment.
Thanks for the fixes! LGTM!
Please resolve conflicts, I also have one small comment :)
|
@dimxy please open docs PR for this features |
* dev: chore(release): update v2.2.0-beta date (#2277) chore(release): add changelog entries for v2.2.0-beta (#2240) fix(watchers): align taker fee validation retries with makers (#2263) feat(tokens): custom token activation for evm (#2141) use safer subtraction on healthcheck expiration check (#2272) fix(hd-wallet): correctly display evm addr in `get_new_address` response (#2264)
* feat(LRAPI): add 1inch classic swap rpc (#2222) This commit adds initial code to connect to 1inch Liquidity Routing API (LRAPI) provider and two new RPCs to use 1inch classic swap API. It also adds 'approve' and 'allowance' RPCs (for ERC20 tokens). * chore(release): bump mm2 version to 2.3.0-beta (#2285) * improvement(error-handling): main files (#2288) Makes KDF to check main files (config/coins/etc..) before reading them to prevent potential panics. * fix(rpc): remove character check blocking password input (#2287) This commit removes check for <, >, & characters in RPC request bodies that was incorrectly blocking valid password characters in get_mnemonic RPC call. These special characters should be allowed in passwords. This aligns native behavior with WASM implementation. * don't rely on core (#2289) Signed-off-by: onur-ozkan <work@onurozkan.dev> * chore(ctx): replace gstuff constructible with oncelock (#2267) * chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (#2290) * bump libp2p (#2296) Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev> Co-authored-by: dimxy <dimxy@komodoplatform.com> Co-authored-by: Onur Özkan <work@onurozkan.dev> Co-authored-by: Samuel Onoja <samiodev@icloud.com> Co-authored-by: DeckerSU <support@decker.su>
* feat(LRAPI): add 1inch classic swap rpc (#2222) This commit adds initial code to connect to 1inch Liquidity Routing API (LRAPI) provider and two new RPCs to use 1inch classic swap API. It also adds 'approve' and 'allowance' RPCs (for ERC20 tokens). * chore(release): bump mm2 version to 2.3.0-beta (#2285) * improvement(error-handling): main files (#2288) Makes KDF to check main files (config/coins/etc..) before reading them to prevent potential panics. * fix(rpc): remove character check blocking password input (#2287) This commit removes check for <, >, & characters in RPC request bodies that was incorrectly blocking valid password characters in get_mnemonic RPC call. These special characters should be allowed in passwords. This aligns native behavior with WASM implementation. * don't rely on core (#2289) Signed-off-by: onur-ozkan <work@onurozkan.dev> * chore(ctx): replace gstuff constructible with oncelock (#2267) * chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (#2290) * bump libp2p (#2296) Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev> Co-authored-by: dimxy <dimxy@komodoplatform.com> Co-authored-by: Onur Özkan <work@onurozkan.dev> Co-authored-by: Samuel Onoja <samiodev@icloud.com> Co-authored-by: DeckerSU <support@decker.su>
* feat(LRAPI): add 1inch classic swap rpc (#2222) This commit adds initial code to connect to 1inch Liquidity Routing API (LRAPI) provider and two new RPCs to use 1inch classic swap API. It also adds 'approve' and 'allowance' RPCs (for ERC20 tokens). * chore(release): bump mm2 version to 2.3.0-beta (#2285) * improvement(error-handling): main files (#2288) Makes KDF to check main files (config/coins/etc..) before reading them to prevent potential panics. * fix(rpc): remove character check blocking password input (#2287) This commit removes check for <, >, & characters in RPC request bodies that was incorrectly blocking valid password characters in get_mnemonic RPC call. These special characters should be allowed in passwords. This aligns native behavior with WASM implementation. * don't rely on core (#2289) Signed-off-by: onur-ozkan <work@onurozkan.dev> * chore(ctx): replace gstuff constructible with oncelock (#2267) * chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (#2290) * bump libp2p (#2296) Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev> Co-authored-by: dimxy <dimxy@komodoplatform.com> Co-authored-by: Onur Özkan <work@onurozkan.dev> Co-authored-by: Samuel Onoja <samiodev@icloud.com> Co-authored-by: DeckerSU <support@decker.su>
* feat(LRAPI): add 1inch classic swap rpc (#2222) This commit adds initial code to connect to 1inch Liquidity Routing API (LRAPI) provider and two new RPCs to use 1inch classic swap API. It also adds 'approve' and 'allowance' RPCs (for ERC20 tokens). * chore(release): bump mm2 version to 2.3.0-beta (#2285) * improvement(error-handling): main files (#2288) Makes KDF to check main files (config/coins/etc..) before reading them to prevent potential panics. * fix(rpc): remove character check blocking password input (#2287) This commit removes check for <, >, & characters in RPC request bodies that was incorrectly blocking valid password characters in get_mnemonic RPC call. These special characters should be allowed in passwords. This aligns native behavior with WASM implementation. * don't rely on core (#2289) Signed-off-by: onur-ozkan <work@onurozkan.dev> * chore(ctx): replace gstuff constructible with oncelock (#2267) * chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (#2290) * bump libp2p (#2296) Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev> Co-authored-by: dimxy <dimxy@komodoplatform.com> Co-authored-by: Onur Özkan <work@onurozkan.dev> Co-authored-by: Samuel Onoja <samiodev@icloud.com> Co-authored-by: DeckerSU <support@decker.su>
* feat(LRAPI): add 1inch classic swap rpc (#2222) This commit adds initial code to connect to 1inch Liquidity Routing API (LRAPI) provider and two new RPCs to use 1inch classic swap API. It also adds 'approve' and 'allowance' RPCs (for ERC20 tokens). * chore(release): bump mm2 version to 2.3.0-beta (#2285) * improvement(error-handling): main files (#2288) Makes KDF to check main files (config/coins/etc..) before reading them to prevent potential panics. * fix(rpc): remove character check blocking password input (#2287) This commit removes check for <, >, & characters in RPC request bodies that was incorrectly blocking valid password characters in get_mnemonic RPC call. These special characters should be allowed in passwords. This aligns native behavior with WASM implementation. * don't rely on core (#2289) Signed-off-by: onur-ozkan <work@onurozkan.dev> * chore(ctx): replace gstuff constructible with oncelock (#2267) * chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (#2290) * bump libp2p (#2296) Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev> Co-authored-by: dimxy <dimxy@komodoplatform.com> Co-authored-by: Onur Özkan <work@onurozkan.dev> Co-authored-by: Samuel Onoja <samiodev@icloud.com> Co-authored-by: DeckerSU <support@decker.su>
* feat(LRAPI): add 1inch classic swap rpc (#2222) This commit adds initial code to connect to 1inch Liquidity Routing API (LRAPI) provider and two new RPCs to use 1inch classic swap API. It also adds 'approve' and 'allowance' RPCs (for ERC20 tokens). * chore(release): bump mm2 version to 2.3.0-beta (#2285) * improvement(error-handling): main files (#2288) Makes KDF to check main files (config/coins/etc..) before reading them to prevent potential panics. * fix(rpc): remove character check blocking password input (#2287) This commit removes check for <, >, & characters in RPC request bodies that was incorrectly blocking valid password characters in get_mnemonic RPC call. These special characters should be allowed in passwords. This aligns native behavior with WASM implementation. * don't rely on core (#2289) Signed-off-by: onur-ozkan <work@onurozkan.dev> * chore(ctx): replace gstuff constructible with oncelock (#2267) * chore(adex-cli): use "Komodo DeFi Framework" name in adex_cli (#2290) * bump libp2p (#2296) Signed-off-by: onur-ozkan <work@onurozkan.dev> --------- Signed-off-by: onur-ozkan <work@onurozkan.dev> Co-authored-by: dimxy <dimxy@komodoplatform.com> Co-authored-by: Onur Özkan <work@onurozkan.dev> Co-authored-by: Samuel Onoja <samiodev@icloud.com> Co-authored-by: DeckerSU <support@decker.su>
This PR adds:
(This is an ongoing work to add features requested in #1287 in the part regarding "AMM" tech adoption. We need a dedicated issue for that)
To Test: @KomodoPlatform/qa
Docs PR is provided by @dimxy GLEECBTC/komodo-docs-mdx#392
TODO: