From 886a817be9f218d9d7666d2a4b23885344d858bf Mon Sep 17 00:00:00 2001 From: Mateusz Nowakowski Date: Thu, 20 Jul 2023 05:56:39 +0000 Subject: [PATCH 1/7] get rid of unnecessary generics and types --- pallets/xyk/rpc/src/lib.rs | 118 ++++++++++++----------------- pallets/xyk/runtime-api/src/lib.rs | 61 +++------------ runtime/mangata-kusama/src/lib.rs | 89 +++++++++------------- runtime/mangata-rococo/src/lib.rs | 89 +++++++++------------- 4 files changed, 130 insertions(+), 227 deletions(-) diff --git a/pallets/xyk/rpc/src/lib.rs b/pallets/xyk/rpc/src/lib.rs index 1c9908c27c..b3bee9d77b 100644 --- a/pallets/xyk/rpc/src/lib.rs +++ b/pallets/xyk/rpc/src/lib.rs @@ -14,63 +14,53 @@ use sp_runtime::traits::{Block as BlockT, MaybeDisplay, MaybeFromStr}; use sp_std::convert::{TryFrom, TryInto}; use std::sync::Arc; pub use xyk_runtime_api::XykApi as XykRuntimeApi; -use xyk_runtime_api::{RpcAmountsResult, XYKRpcResult}; #[rpc(client, server)] -pub trait XykApi< - BlockHash, - Balance, - TokenId, - AccountId, - ResponseTypePrice, - ResponseTypeAmounts, - BalanceOutput, -> -{ +pub trait XykApi { #[method(name = "xyk_calculate_sell_price")] fn calculate_sell_price( &self, - input_reserve: Balance, - output_reserve: Balance, - sell_amount: Balance, + input_reserve: NumberOrHex, + output_reserve: NumberOrHex, + sell_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_buy_price")] fn calculate_buy_price( &self, - input_reserve: Balance, - output_reserve: Balance, - buy_amount: Balance, + input_reserve: NumberOrHex, + output_reserve: NumberOrHex, + buy_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_sell_price_id")] fn calculate_sell_price_id( &self, sold_token_id: TokenId, bought_token_id: TokenId, - sell_amount: Balance, + sell_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_buy_price_id")] fn calculate_buy_price_id( &self, sold_token_id: TokenId, bought_token_id: TokenId, - buy_amount: Balance, + buy_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_get_burn_amount")] fn get_burn_amount( &self, first_asset_id: TokenId, second_asset_id: TokenId, - liquidity_asset_amount: Balance, + liquidity_asset_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult<(Balance, Balance)>; #[method(name = "xyk_get_max_instant_burn_amount")] fn get_max_instant_burn_amount( @@ -78,7 +68,7 @@ pub trait XykApi< user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_get_max_instant_unreserve_amount")] fn get_max_instant_unreserve_amount( @@ -86,7 +76,7 @@ pub trait XykApi< user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_rewards_amount")] fn calculate_rewards_amount( @@ -94,21 +84,21 @@ pub trait XykApi< user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_balanced_sell_amount")] fn calculate_balanced_sell_amount( &self, - total_amount: Balance, - reserve_amount: Balance, + total_amount: NumberOrHex, + reserve_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_is_buy_asset_lock_free")] fn is_buy_asset_lock_free( &self, path: sp_std::vec::Vec, - input_amount: Balance, + input_amount: NumberOrHex, at: Option, ) -> RpcResult>; @@ -116,7 +106,7 @@ pub trait XykApi< fn is_sell_asset_lock_free( &self, path: sp_std::vec::Vec, - input_amount: Balance, + input_amount: NumberOrHex, at: Option, ) -> RpcResult>; } @@ -150,15 +140,7 @@ impl> TryIntoBalance for NumberOrHex { #[async_trait] impl - XykApiServer< - ::Hash, - NumberOrHex, - TokenId, - AccountId, - XYKRpcResult, - RpcAmountsResult, - Balance, - > for Xyk + XykApiServer<::Hash, Balance, TokenId, AccountId> for Xyk where Block: BlockT, C: Send + Sync + 'static, @@ -175,17 +157,17 @@ where output_reserve: NumberOrHex, sell_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - let runtime_api_result = api.calculate_sell_price( + api.calculate_sell_price( at, input_reserve.try_into_balance()?, output_reserve.try_into_balance()?, sell_amount.try_into_balance()?, - ); - runtime_api_result.map_err(|e| { + ) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -200,17 +182,17 @@ where output_reserve: NumberOrHex, buy_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - let runtime_api_result = api.calculate_buy_price( + api.calculate_buy_price( at, input_reserve.try_into_balance()?, output_reserve.try_into_balance()?, buy_amount.try_into_balance()?, - ); - runtime_api_result.map_err(|e| { + ) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -225,17 +207,17 @@ where bought_token_id: TokenId, sell_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - let runtime_api_result = api.calculate_sell_price_id( + api.calculate_sell_price_id( at, sold_token_id, bought_token_id, sell_amount.try_into_balance()?, - ); - runtime_api_result.map_err(|e| { + ) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -250,17 +232,17 @@ where bought_token_id: TokenId, buy_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - let runtime_api_result = api.calculate_buy_price_id( + api.calculate_buy_price_id( at, sold_token_id, bought_token_id, buy_amount.try_into_balance()?, - ); - runtime_api_result.map_err(|e| { + ) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -275,17 +257,17 @@ where second_asset_id: TokenId, liquidity_asset_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult<(Balance, Balance)> { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - let runtime_api_result = api.get_burn_amount( + api.get_burn_amount( at, first_asset_id, second_asset_id, liquidity_asset_amount.try_into_balance()?, - ); - runtime_api_result.map_err(|e| { + ) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -299,7 +281,7 @@ where user: AccountId, liquidity_asset_id: TokenId, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -317,7 +299,7 @@ where user: AccountId, liquidity_asset_id: TokenId, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -335,13 +317,11 @@ where user: AccountId, liquidity_asset_id: TokenId, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - let runtime_api_result = api.calculate_rewards_amount(at, user, liquidity_asset_id); - - runtime_api_result.map_err(|e| { + api.calculate_rewards_amount(at, user, liquidity_asset_id).map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -355,7 +335,7 @@ where total_amount: NumberOrHex, reserve_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; diff --git a/pallets/xyk/runtime-api/src/lib.rs b/pallets/xyk/runtime-api/src/lib.rs index 7138d4e425..01a726c615 100644 --- a/pallets/xyk/runtime-api/src/lib.rs +++ b/pallets/xyk/runtime-api/src/lib.rs @@ -6,49 +6,6 @@ use codec::{Codec, Decode, Encode}; #[cfg(feature = "std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; use sp_runtime::traits::{MaybeDisplay, MaybeFromStr}; -// Workaround for substrate/serde issue -#[derive(Eq, PartialEq, Encode, Decode, Default)] -#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] -pub struct XYKRpcResult { - #[cfg_attr(feature = "std", serde(bound(serialize = "Balance: std::fmt::Display")))] - #[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))] - #[cfg_attr(feature = "std", serde(bound(deserialize = "Balance: std::str::FromStr")))] - #[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))] - pub price: Balance, -} - -#[derive(Eq, PartialEq, Encode, Decode, Default)] -#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] -#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] -pub struct RpcAmountsResult { - #[cfg_attr(feature = "std", serde(bound(serialize = "Balance: std::fmt::Display")))] - #[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))] - #[cfg_attr(feature = "std", serde(bound(deserialize = "Balance: std::str::FromStr")))] - #[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))] - pub first_asset_amount: Balance, - #[cfg_attr(feature = "std", serde(bound(serialize = "Balance: std::fmt::Display")))] - #[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))] - #[cfg_attr(feature = "std", serde(bound(deserialize = "Balance: std::str::FromStr")))] - #[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))] - pub second_asset_amount: Balance, -} - -#[cfg(feature = "std")] -fn serialize_as_string( - t: &T, - serializer: S, -) -> Result { - serializer.serialize_str(&t.to_string()) -} - -#[cfg(feature = "std")] -fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>( - deserializer: D, -) -> Result { - let s = String::deserialize(deserializer)?; - s.parse::().map_err(|_| serde::de::Error::custom("Parse from string failed")) -} sp_api::decl_runtime_apis! { pub trait XykApi where @@ -59,43 +16,43 @@ sp_api::decl_runtime_apis! { input_reserve: Balance, output_reserve: Balance, sell_amount: Balance - ) -> XYKRpcResult; + ) -> Balance; fn calculate_buy_price( input_reserve: Balance, output_reserve: Balance, buy_amount: Balance - ) -> XYKRpcResult; + ) -> Balance; fn calculate_sell_price_id( sold_token_id: TokenId, bought_token_id: TokenId, sell_amount: Balance - ) -> XYKRpcResult; + ) -> Balance; fn calculate_buy_price_id( sold_token_id: TokenId, bought_token_id: TokenId, buy_amount: Balance - ) -> XYKRpcResult; + ) -> Balance; fn get_burn_amount( first_asset_id: TokenId, second_asset_id: TokenId, liquidity_asset_amount: Balance, - ) -> RpcAmountsResult; + ) -> (Balance,Balance); fn get_max_instant_burn_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult; + ) -> Balance; fn get_max_instant_unreserve_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult; + ) -> Balance; fn calculate_rewards_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult; + ) -> Balance; fn calculate_balanced_sell_amount( total_amount: Balance, reserve_amount: Balance, - ) -> XYKRpcResult; + ) -> Balance; fn is_buy_asset_lock_free( path: sp_std::vec::Vec, diff --git a/runtime/mangata-kusama/src/lib.rs b/runtime/mangata-kusama/src/lib.rs index 81799b74cc..1f438b70cb 100644 --- a/runtime/mangata-kusama/src/lib.rs +++ b/runtime/mangata-kusama/src/lib.rs @@ -57,8 +57,6 @@ pub use pallet_sudo_origin; pub use pallet_xyk; // XCM Imports -use xyk_runtime_api::{RpcAmountsResult, XYKRpcResult}; - // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); @@ -835,25 +833,22 @@ impl_runtime_apis! { input_reserve: Balance, output_reserve: Balance, sell_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_sell_price(input_reserve, output_reserve, sell_amount) - .map_err(|e| - { - log::warn!(target:"xyk", "rpc 'XYK::calculate_sell_price' error: '{:?}', returning default value instead", e); - e - } - ).unwrap_or_default() - } + ) -> Balance { + Xyk::calculate_sell_price(input_reserve, output_reserve, sell_amount) + .map_err(|e| + { + log::warn!(target:"xyk", "rpc 'XYK::calculate_sell_price' error: '{:?}', returning default value instead", e); + e + } + ).unwrap_or_default() } fn calculate_buy_price( input_reserve: Balance, output_reserve: Balance, buy_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_buy_price(input_reserve, output_reserve, buy_amount) + ) -> Balance { + Xyk::calculate_buy_price(input_reserve, output_reserve, buy_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price' error: '{:?}', returning default value instead", e); @@ -861,100 +856,88 @@ impl_runtime_apis! { } ).unwrap_or_default() - } } fn calculate_sell_price_id( sold_token_id: TokenId, bought_token_id: TokenId, sell_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_sell_price_id(sold_token_id, bought_token_id, sell_amount) + ) -> Balance { + Xyk::calculate_sell_price_id(sold_token_id, bought_token_id, sell_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_sell_price_id' error: '{:?}', returning default value instead", e); e } ).unwrap_or_default() - } } fn calculate_buy_price_id( sold_token_id: TokenId, bought_token_id: TokenId, buy_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_buy_price_id(sold_token_id, bought_token_id, buy_amount) + ) -> Balance { + Xyk::calculate_buy_price_id(sold_token_id, bought_token_id, buy_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price_id' error: '{:?}', returning default value instead", e); e } ).unwrap_or_default() - } } fn get_burn_amount( first_asset_id: TokenId, second_asset_id: TokenId, liquidity_asset_amount: Balance - ) -> RpcAmountsResult { - match Xyk::get_burn_amount(first_asset_id, second_asset_id, liquidity_asset_amount){ - Ok((first_asset_amount, second_asset_amount)) => RpcAmountsResult{ - first_asset_amount, - second_asset_amount - }, - Err(e) => { - log::warn!(target:"xyk", "rpc 'XYK::get_burn_amount' error: '{:?}', returning default value instead", e); - Default::default() - }, - } + ) -> (Balance, Balance) { + Xyk::get_burn_amount(first_asset_id, second_asset_id, liquidity_asset_amount) + .map_err(|e| + { + log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price_id' error: '{:?}', returning default value instead", e); + e + } + ).unwrap_or_default() } fn get_max_instant_burn_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult { - XYKRpcResult { price: Xyk::get_max_instant_burn_amount(&user, liquidity_asset_id) } + ) -> Balance { + Xyk::get_max_instant_burn_amount(&user, liquidity_asset_id) } fn get_max_instant_unreserve_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult { - XYKRpcResult { price: Xyk::get_max_instant_unreserve_amount(&user, liquidity_asset_id) } + ) -> Balance { + Xyk::get_max_instant_unreserve_amount(&user, liquidity_asset_id) } fn calculate_rewards_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult { - match ProofOfStake::calculate_rewards_amount(user, liquidity_asset_id){ - Ok(claimable_rewards) => XYKRpcResult{ - price:claimable_rewards - }, - Err(e) => { - log::warn!(target:"xyk", "rpc 'XYK::calculate_rewards_amount' error: '{:?}', returning default value instead", e); - Default::default() - }, - } + ) -> Balance { + ProofOfStake::calculate_rewards_amount(user, liquidity_asset_id) + .map_err(|e| + { + log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price_id' error: '{:?}', returning default value instead", e); + e + } + ).unwrap_or_default() } fn calculate_balanced_sell_amount( total_amount: Balance, reserve_amount: Balance, - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_balanced_sell_amount(total_amount, reserve_amount) + ) -> Balance { + Xyk::calculate_balanced_sell_amount(total_amount, reserve_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_balanced_sell_amount' error: '{:?}', returning default value instead", e); e } ).unwrap_or_default() - } } fn is_sell_asset_lock_free( diff --git a/runtime/mangata-rococo/src/lib.rs b/runtime/mangata-rococo/src/lib.rs index 40cdc87560..2ccf727256 100644 --- a/runtime/mangata-rococo/src/lib.rs +++ b/runtime/mangata-rococo/src/lib.rs @@ -57,8 +57,6 @@ pub use pallet_sudo_origin; pub use pallet_xyk; // XCM Imports -use xyk_runtime_api::{RpcAmountsResult, XYKRpcResult}; - // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); @@ -849,25 +847,22 @@ impl_runtime_apis! { input_reserve: Balance, output_reserve: Balance, sell_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_sell_price(input_reserve, output_reserve, sell_amount) - .map_err(|e| - { - log::warn!(target:"xyk", "rpc 'XYK::calculate_sell_price' error: '{:?}', returning default value instead", e); - e - } - ).unwrap_or_default() - } + ) -> Balance { + Xyk::calculate_sell_price(input_reserve, output_reserve, sell_amount) + .map_err(|e| + { + log::warn!(target:"xyk", "rpc 'XYK::calculate_sell_price' error: '{:?}', returning default value instead", e); + e + } + ).unwrap_or_default() } fn calculate_buy_price( input_reserve: Balance, output_reserve: Balance, buy_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_buy_price(input_reserve, output_reserve, buy_amount) + ) -> Balance { + Xyk::calculate_buy_price(input_reserve, output_reserve, buy_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price' error: '{:?}', returning default value instead", e); @@ -875,100 +870,88 @@ impl_runtime_apis! { } ).unwrap_or_default() - } } fn calculate_sell_price_id( sold_token_id: TokenId, bought_token_id: TokenId, sell_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_sell_price_id(sold_token_id, bought_token_id, sell_amount) + ) -> Balance { + Xyk::calculate_sell_price_id(sold_token_id, bought_token_id, sell_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_sell_price_id' error: '{:?}', returning default value instead", e); e } ).unwrap_or_default() - } } fn calculate_buy_price_id( sold_token_id: TokenId, bought_token_id: TokenId, buy_amount: Balance - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_buy_price_id(sold_token_id, bought_token_id, buy_amount) + ) -> Balance { + Xyk::calculate_buy_price_id(sold_token_id, bought_token_id, buy_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price_id' error: '{:?}', returning default value instead", e); e } ).unwrap_or_default() - } } fn get_burn_amount( first_asset_id: TokenId, second_asset_id: TokenId, liquidity_asset_amount: Balance - ) -> RpcAmountsResult { - match Xyk::get_burn_amount(first_asset_id, second_asset_id, liquidity_asset_amount){ - Ok((first_asset_amount, second_asset_amount)) => RpcAmountsResult{ - first_asset_amount, - second_asset_amount - }, - Err(e) => { - log::warn!(target:"xyk", "rpc 'XYK::get_burn_amount' error: '{:?}', returning default value instead", e); - Default::default() - }, - } + ) -> (Balance, Balance) { + Xyk::get_burn_amount(first_asset_id, second_asset_id, liquidity_asset_amount) + .map_err(|e| + { + log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price_id' error: '{:?}', returning default value instead", e); + e + } + ).unwrap_or_default() } fn get_max_instant_burn_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult { - XYKRpcResult { price: Xyk::get_max_instant_burn_amount(&user, liquidity_asset_id) } + ) -> Balance { + Xyk::get_max_instant_burn_amount(&user, liquidity_asset_id) } fn get_max_instant_unreserve_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult { - XYKRpcResult { price: Xyk::get_max_instant_unreserve_amount(&user, liquidity_asset_id) } + ) -> Balance { + Xyk::get_max_instant_unreserve_amount(&user, liquidity_asset_id) } fn calculate_rewards_amount( user: AccountId, liquidity_asset_id: TokenId, - ) -> XYKRpcResult { - match ProofOfStake::calculate_rewards_amount(user, liquidity_asset_id){ - Ok(claimable_rewards) => XYKRpcResult{ - price:claimable_rewards - }, - Err(e) => { - log::warn!(target:"xyk", "rpc 'XYK::calculate_rewards_amount' error: '{:?}', returning default value instead", e); - Default::default() - }, - } + ) -> Balance { + ProofOfStake::calculate_rewards_amount(user, liquidity_asset_id) + .map_err(|e| + { + log::warn!(target:"xyk", "rpc 'XYK::calculate_buy_price_id' error: '{:?}', returning default value instead", e); + e + } + ).unwrap_or_default() } fn calculate_balanced_sell_amount( total_amount: Balance, reserve_amount: Balance, - ) -> XYKRpcResult { - XYKRpcResult { - price: Xyk::calculate_balanced_sell_amount(total_amount, reserve_amount) + ) -> Balance { + Xyk::calculate_balanced_sell_amount(total_amount, reserve_amount) .map_err(|e| { log::warn!(target:"xyk", "rpc 'XYK::calculate_balanced_sell_amount' error: '{:?}', returning default value instead", e); e } ).unwrap_or_default() - } } fn is_sell_asset_lock_free( From b9432e0c753af4b47091548395dbd4611fcb7554 Mon Sep 17 00:00:00 2001 From: Mateusz Nowakowski Date: Mon, 31 Jul 2023 11:55:16 +0000 Subject: [PATCH 2/7] fix compilation issues --- pallets/xyk/rpc/src/lib.rs | 1 + pallets/xyk/runtime-api/src/lib.rs | 2 +- runtime/mangata-kusama/src/lib.rs | 2 +- runtime/mangata-rococo/src/lib.rs | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pallets/xyk/rpc/src/lib.rs b/pallets/xyk/rpc/src/lib.rs index bc7b06fa72..884be6c33b 100644 --- a/pallets/xyk/rpc/src/lib.rs +++ b/pallets/xyk/rpc/src/lib.rs @@ -13,6 +13,7 @@ use sp_rpc::number::NumberOrHex; use sp_runtime::traits::{Block as BlockT, MaybeDisplay, MaybeFromStr}; use sp_std::convert::{TryFrom, TryInto}; use std::sync::Arc; +use xyk_runtime_api::RpcAssetMetadata; pub use xyk_runtime_api::XykApi as XykRuntimeApi; #[rpc(client, server)] diff --git a/pallets/xyk/runtime-api/src/lib.rs b/pallets/xyk/runtime-api/src/lib.rs index 9c4035620d..236509ae58 100644 --- a/pallets/xyk/runtime-api/src/lib.rs +++ b/pallets/xyk/runtime-api/src/lib.rs @@ -7,7 +7,6 @@ use codec::{Codec, Decode, Encode}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use sp_runtime::traits::{MaybeDisplay, MaybeFromStr}; use sp_std::vec::Vec; -} #[derive(Eq, PartialEq, Encode, Decode, Default)] #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] @@ -17,6 +16,7 @@ pub struct RpcAssetMetadata { pub decimals: u32, pub name: Vec, pub symbol: Vec, +} sp_api::decl_runtime_apis! { pub trait XykApi where diff --git a/runtime/mangata-kusama/src/lib.rs b/runtime/mangata-kusama/src/lib.rs index 86d32cd9c1..20787b8b7e 100644 --- a/runtime/mangata-kusama/src/lib.rs +++ b/runtime/mangata-kusama/src/lib.rs @@ -57,7 +57,7 @@ pub use pallet_sudo_origin; pub use pallet_xyk; // XCM Imports -use xyk_runtime_api::{RpcAmountsResult, XYKRpcResult}; +use xyk_runtime_api::RpcAssetMetadata; // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); diff --git a/runtime/mangata-rococo/src/lib.rs b/runtime/mangata-rococo/src/lib.rs index e0626f91a2..ed6d681a92 100644 --- a/runtime/mangata-rococo/src/lib.rs +++ b/runtime/mangata-rococo/src/lib.rs @@ -56,7 +56,7 @@ pub use pallet_sudo_origin; pub use pallet_xyk; // XCM Imports -use xyk_runtime_api::{RpcAmountsResult, XYKRpcResult}; +use xyk_runtime_api::RpcAssetMetadata; // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); From 1ab766ebb63fd7ce05b0510138a00b2281d4dde1 Mon Sep 17 00:00:00 2001 From: Mateusz Nowakowski Date: Thu, 24 Aug 2023 06:26:33 +0000 Subject: [PATCH 3/7] change return types to Type that supports serialization --- pallets/xyk/rpc/src/lib.rs | 92 +++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/pallets/xyk/rpc/src/lib.rs b/pallets/xyk/rpc/src/lib.rs index 884be6c33b..9cdf26b5f5 100644 --- a/pallets/xyk/rpc/src/lib.rs +++ b/pallets/xyk/rpc/src/lib.rs @@ -25,7 +25,7 @@ pub trait XykApi { output_reserve: NumberOrHex, sell_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_buy_price")] fn calculate_buy_price( @@ -34,7 +34,7 @@ pub trait XykApi { output_reserve: NumberOrHex, buy_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_sell_price_id")] fn calculate_sell_price_id( @@ -43,7 +43,7 @@ pub trait XykApi { bought_token_id: TokenId, sell_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_buy_price_id")] fn calculate_buy_price_id( @@ -52,7 +52,7 @@ pub trait XykApi { bought_token_id: TokenId, buy_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_get_burn_amount")] fn get_burn_amount( @@ -61,7 +61,7 @@ pub trait XykApi { second_asset_id: TokenId, liquidity_asset_amount: NumberOrHex, at: Option, - ) -> RpcResult<(Balance, Balance)>; + ) -> RpcResult<(NumberOrHex, NumberOrHex)>; #[method(name = "xyk_get_max_instant_burn_amount")] fn get_max_instant_burn_amount( @@ -69,7 +69,7 @@ pub trait XykApi { user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_get_max_instant_unreserve_amount")] fn get_max_instant_unreserve_amount( @@ -77,7 +77,7 @@ pub trait XykApi { user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_rewards_amount")] fn calculate_rewards_amount( @@ -85,7 +85,7 @@ pub trait XykApi { user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_balanced_sell_amount")] fn calculate_balanced_sell_amount( @@ -93,7 +93,7 @@ pub trait XykApi { total_amount: NumberOrHex, reserve_amount: NumberOrHex, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_is_buy_asset_lock_free")] fn is_buy_asset_lock_free( @@ -154,7 +154,7 @@ where C: ProvideRuntimeApi, C: HeaderBackend, C::Api: XykRuntimeApi, - Balance: Codec + MaybeDisplay + MaybeFromStr + TryFrom, + Balance: Codec + MaybeDisplay + MaybeFromStr + TryFrom + Into, TokenId: Codec + MaybeDisplay + MaybeFromStr, AccountId: Codec + MaybeDisplay + MaybeFromStr, { @@ -164,7 +164,7 @@ where output_reserve: NumberOrHex, sell_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -174,6 +174,7 @@ where output_reserve.try_into_balance()?, sell_amount.try_into_balance()?, ) + .map(Into::::into) .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, @@ -189,7 +190,7 @@ where output_reserve: NumberOrHex, buy_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -199,6 +200,7 @@ where output_reserve.try_into_balance()?, buy_amount.try_into_balance()?, ) + .map(Into::::into) .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, @@ -214,7 +216,7 @@ where bought_token_id: TokenId, sell_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -224,6 +226,7 @@ where bought_token_id, sell_amount.try_into_balance()?, ) + .map(Into::::into) .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, @@ -239,7 +242,7 @@ where bought_token_id: TokenId, buy_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -249,6 +252,7 @@ where bought_token_id, buy_amount.try_into_balance()?, ) + .map(Into::::into) .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, @@ -264,7 +268,7 @@ where second_asset_id: TokenId, liquidity_asset_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult<(Balance, Balance)> { + ) -> RpcResult<(NumberOrHex, NumberOrHex)> { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -274,6 +278,7 @@ where second_asset_id, liquidity_asset_amount.try_into_balance()?, ) + .map(|(val1, val2)| (val1.into(), val2.into())) .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, @@ -288,17 +293,19 @@ where user: AccountId, liquidity_asset_id: TokenId, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - api.get_max_instant_burn_amount(at, user, liquidity_asset_id).map_err(|e| { - JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( - 1, - "Unable to serve the request", - Some(format!("{:?}", e)), - ))) - }) + api.get_max_instant_burn_amount(at, user, liquidity_asset_id) + .map(Into::::into) + .map_err(|e| { + JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( + 1, + "Unable to serve the request", + Some(format!("{:?}", e)), + ))) + }) } fn get_max_instant_unreserve_amount( @@ -306,17 +313,19 @@ where user: AccountId, liquidity_asset_id: TokenId, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - api.get_max_instant_unreserve_amount(at, user, liquidity_asset_id).map_err(|e| { - JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( - 1, - "Unable to serve the request", - Some(format!("{:?}", e)), - ))) - }) + api.get_max_instant_unreserve_amount(at, user, liquidity_asset_id) + .map(Into::::into) + .map_err(|e| { + JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( + 1, + "Unable to serve the request", + Some(format!("{:?}", e)), + ))) + }) } fn calculate_rewards_amount( @@ -324,17 +333,19 @@ where user: AccountId, liquidity_asset_id: TokenId, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; - api.calculate_rewards_amount(at, user, liquidity_asset_id).map_err(|e| { - JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( - 1, - "Unable to serve the request", - Some(format!("{:?}", e)), - ))) - }) + api.calculate_rewards_amount(at, user, liquidity_asset_id) + .map(Into::::into) + .map_err(|e| { + JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( + 1, + "Unable to serve the request", + Some(format!("{:?}", e)), + ))) + }) } fn calculate_balanced_sell_amount( @@ -342,7 +353,7 @@ where total_amount: NumberOrHex, reserve_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult { + ) -> RpcResult { let api = self.client.runtime_api(); let at = self.client.info().best_hash; @@ -351,6 +362,7 @@ where total_amount.try_into_balance()?, reserve_amount.try_into_balance()?, ) + .map(Into::::into) .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, From 9c3fa0b9e30fb3227b986eeb63e3532e7aad21fb Mon Sep 17 00:00:00 2001 From: tenequm Date: Fri, 25 Aug 2023 15:00:17 +0300 Subject: [PATCH 4/7] chore: temporarly add yarn cache cleaning command to fix e2e run issue --- .github/workflows/reusable-e2e-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/reusable-e2e-tests.yml b/.github/workflows/reusable-e2e-tests.yml index 0c23148270..5c0c4be366 100644 --- a/.github/workflows/reusable-e2e-tests.yml +++ b/.github/workflows/reusable-e2e-tests.yml @@ -187,7 +187,7 @@ jobs: - name: Install e2e tests dependencies working-directory: e2eTests - run: yarn + run: yarn cache clean; yarn - name: Run parachain launch working-directory: devops/parachain-launch From efe4874054cd653ea20e78752f55676c51d08915 Mon Sep 17 00:00:00 2001 From: tenequm Date: Fri, 25 Aug 2023 15:25:29 +0300 Subject: [PATCH 5/7] chore: add TODO comment --- .github/workflows/reusable-e2e-tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/reusable-e2e-tests.yml b/.github/workflows/reusable-e2e-tests.yml index 5c0c4be366..6f55bd673b 100644 --- a/.github/workflows/reusable-e2e-tests.yml +++ b/.github/workflows/reusable-e2e-tests.yml @@ -187,6 +187,7 @@ jobs: - name: Install e2e tests dependencies working-directory: e2eTests + # TODO: remove cache clean later run: yarn cache clean; yarn - name: Run parachain launch From e80943cefdf6048b5472a1594b23ca2efad60b90 Mon Sep 17 00:00:00 2001 From: Misha Kolesnik Date: Mon, 25 Sep 2023 14:17:17 +0300 Subject: [PATCH 6/7] CI: Update mangata-node helm chart to v1.3.0 MGX-433 (#601) --- devops/helmfiles/helmfile.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devops/helmfiles/helmfile.yaml b/devops/helmfiles/helmfile.yaml index 95d15a9763..970d97910a 100644 --- a/devops/helmfiles/helmfile.yaml +++ b/devops/helmfiles/helmfile.yaml @@ -14,7 +14,7 @@ environments: --- repositories: - name: mangata-node - url: git+https://github.com/mangata-finance/helm-charts@charts?ref=node-v1.2.0 + url: git+https://github.com/mangata-finance/helm-charts@charts?ref=node-v1.3.0 - name: mangata-relaychain url: git+https://github.com/mangata-finance/helm-charts@charts?ref=relaychain-testnet-v1.0.0 From ff1226bce3c41f7394c02a25285bd9b53a38da33 Mon Sep 17 00:00:00 2001 From: goncer Date: Tue, 26 Sep 2023 11:49:56 +0200 Subject: [PATCH 7/7] Update reusable-e2e-tests.yml (#602) now sdk validates node version. bumped to 18.16.1 --- .github/workflows/reusable-e2e-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/reusable-e2e-tests.yml b/.github/workflows/reusable-e2e-tests.yml index 7e84770078..144dbbc580 100644 --- a/.github/workflows/reusable-e2e-tests.yml +++ b/.github/workflows/reusable-e2e-tests.yml @@ -175,7 +175,7 @@ jobs: - uses: actions/setup-node@v3 with: - node-version: '16' + node-version: '18.16.1' cache: 'yarn' cache-dependency-path: '**/yarn.lock'