diff --git a/.github/workflows/reusable-e2e-tests.yml b/.github/workflows/reusable-e2e-tests.yml index 946b5f86d6..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' @@ -189,7 +189,8 @@ jobs: - name: Install e2e tests dependencies working-directory: e2eTests - run: yarn + # TODO: remove cache clean later + run: yarn cache clean; yarn - name: Run parachain launch working-directory: devops/parachain-launch 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 diff --git a/pallets/xyk/rpc/src/lib.rs b/pallets/xyk/rpc/src/lib.rs index 5a65f6f06d..5f96d56cb6 100644 --- a/pallets/xyk/rpc/src/lib.rs +++ b/pallets/xyk/rpc/src/lib.rs @@ -16,64 +16,55 @@ use sp_runtime::{ }; use sp_std::convert::{TryFrom, TryInto}; use std::sync::Arc; +use xyk_runtime_api::RpcAssetMetadata; pub use xyk_runtime_api::XykApi as XykRuntimeApi; -use xyk_runtime_api::{RpcAmountsResult, RpcAssetMetadata, 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<(NumberOrHex, NumberOrHex)>; #[method(name = "xyk_get_max_instant_burn_amount")] fn get_max_instant_burn_amount( @@ -81,7 +72,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( @@ -89,7 +80,7 @@ pub trait XykApi< user: AccountId, liquidity_asset_id: TokenId, at: Option, - ) -> RpcResult; + ) -> RpcResult; #[method(name = "xyk_calculate_rewards_amount")] fn calculate_rewards_amount( @@ -97,15 +88,15 @@ 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_get_liq_tokens_for_trading")] fn get_liq_tokens_for_trading(&self, at: Option) -> RpcResult>; @@ -114,7 +105,7 @@ pub trait XykApi< fn is_buy_asset_lock_free( &self, path: sp_std::vec::Vec, - input_amount: Balance, + input_amount: NumberOrHex, at: Option, ) -> RpcResult>; @@ -122,7 +113,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>; @@ -162,22 +153,14 @@ 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, 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, { @@ -187,17 +170,18 @@ 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(Into::::into) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -212,17 +196,18 @@ 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(Into::::into) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -237,17 +222,18 @@ 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(Into::::into) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -262,17 +248,18 @@ 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(Into::::into) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -287,17 +274,18 @@ where second_asset_id: TokenId, liquidity_asset_amount: NumberOrHex, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult<(NumberOrHex, NumberOrHex)> { 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(|(val1, val2)| (val1.into(), val2.into())) + .map_err(|e| { JsonRpseeError::Call(CallError::Custom(ErrorObject::owned( 1, "Unable to serve the request", @@ -311,17 +299,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( @@ -329,17 +319,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( @@ -347,19 +339,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; - let runtime_api_result = api.calculate_rewards_amount(at, user, liquidity_asset_id); - - runtime_api_result.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( @@ -367,7 +359,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; @@ -376,6 +368,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, diff --git a/pallets/xyk/runtime-api/src/lib.rs b/pallets/xyk/runtime-api/src/lib.rs index 9af1f6bfc2..2f42b4e2c5 100644 --- a/pallets/xyk/runtime-api/src/lib.rs +++ b/pallets/xyk/runtime-api/src/lib.rs @@ -7,33 +7,6 @@ use codec::{Codec, Decode, Encode}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use sp_runtime::traits::{MaybeDisplay, MaybeFromStr}; use sp_std::vec::Vec; -// 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, -} #[derive(Eq, PartialEq, Encode, Decode, Default)] #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] @@ -45,22 +18,6 @@ pub struct RpcAssetMetadata { pub symbol: Vec, } -#[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 Balance: Codec + MaybeDisplay + MaybeFromStr, @@ -70,43 +27,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 get_liq_tokens_for_trading( ) -> Vec; fn is_buy_asset_lock_free( diff --git a/runtime/mangata-kusama/src/lib.rs b/runtime/mangata-kusama/src/lib.rs index bcf96473ee..04a18a4b14 100644 --- a/runtime/mangata-kusama/src/lib.rs +++ b/runtime/mangata-kusama/src/lib.rs @@ -57,8 +57,7 @@ pub use pallet_sudo_origin; pub use pallet_xyk; // XCM Imports -use xyk_runtime_api::{RpcAmountsResult, RpcAssetMetadata, XYKRpcResult}; - +use xyk_runtime_api::RpcAssetMetadata; // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); @@ -864,25 +863,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); @@ -890,100 +886,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 get_liq_tokens_for_trading() -> Vec { diff --git a/runtime/mangata-rococo/src/lib.rs b/runtime/mangata-rococo/src/lib.rs index 164c6fcb78..3920a8016d 100644 --- a/runtime/mangata-rococo/src/lib.rs +++ b/runtime/mangata-rococo/src/lib.rs @@ -56,8 +56,7 @@ pub use pallet_sudo_origin; pub use pallet_xyk; // XCM Imports -use xyk_runtime_api::{RpcAmountsResult, RpcAssetMetadata, XYKRpcResult}; - +use xyk_runtime_api::RpcAssetMetadata; // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); @@ -881,25 +880,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); @@ -907,100 +903,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 get_liq_tokens_for_trading() -> Vec {