Skip to content

Commit

Permalink
Added new RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
zktony committed Feb 12, 2024
1 parent 8061df8 commit c09d2b5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 103 deletions.
3 changes: 2 additions & 1 deletion pallets/rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ pub mod pallet {
/// * `id`: The reward id.
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::claim())]
pub fn claim(origin: OriginFor<T>, reward_id: u32) -> DispatchResult {
pub fn claim
(origin: OriginFor<T>, reward_id: u32) -> DispatchResult {
let user: T::AccountId = ensure_signed(origin)?;
<Distributor<T>>::mutate(reward_id, user.clone(), |user_reward_info| {
if let Some(reward_info) = <InitializeRewards<T>>::get(reward_id) {
Expand Down
1 change: 0 additions & 1 deletion primitives/orderbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//! This crate contains common types and operations definition required for the `Orderbook` related
//! components.
#![feature(int_roundings)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
2 changes: 0 additions & 2 deletions primitives/polkadex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
//!
//! Low-level types used throughout the Substrate code.
#![feature(int_roundings)]
#![cfg_attr(not(feature = "std"), no_std)]

pub mod assets;
pub mod fees;
pub mod ingress;
pub mod ocex;
pub mod rewards;
pub mod utils;
pub mod withdrawal;

pub use frame_support::storage::bounded_vec::BoundedVec;
Expand Down
94 changes: 0 additions & 94 deletions primitives/polkadex/src/utils.rs

This file was deleted.

1 change: 0 additions & 1 deletion primitives/thea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//! This crate contains common types and operations definition required for the `Thea` related
//! components.
#![feature(duration_constants)]
#![cfg_attr(not(feature = "std"), no_std)]

pub mod types;
Expand Down
28 changes: 24 additions & 4 deletions rpc/swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait PolkadexSwapRpcApi<BlockHash> {
&self,
asset_id1: String,
asset_id2: String,
amount: u128,
amount: String,

This comment has been minimized.

Copy link
@mt-gareth

mt-gareth Feb 24, 2024

Contributor

This broke the polkadex-api package, can you update it or the other.

include_fee: bool,
) -> RpcResult<Option<u128>>;

Expand All @@ -50,9 +50,12 @@ pub trait PolkadexSwapRpcApi<BlockHash> {
&self,
asset_id1: String,
asset_id2: String,
amount: u128,
amount: String,
include_fee: bool,
) -> RpcResult<Option<u128>>;

#[method(name = "tx_getReserves")]
async fn get_reserves(&self, asset_id1: String, asset_id2: String) -> RpcResult<Option<(u128, u128)>>;
}

/// A structure that represents the Polkadex OCEX pallet RPC, which allows querying
Expand Down Expand Up @@ -88,7 +91,7 @@ where
&self,
asset_id1: String,
asset_id2: String,
amount: u128,
amount: String,
include_fee: bool,
) -> RpcResult<Option<u128>> {
let api = self.client.runtime_api();
Expand All @@ -97,6 +100,7 @@ where
AssetId::try_from(asset_id1).map_err(runtime_error_into_rpc_err)?;
let asset_id2: AssetId =
AssetId::try_from(asset_id2).map_err(runtime_error_into_rpc_err)?;
let amount: u128 = amount.parse().map_err(runtime_error_into_rpc_err)?;
let runtime_api_result = api
.quote_price_exact_tokens_for_tokens(at, asset_id1, asset_id2, amount, include_fee)
.map_err(runtime_error_into_rpc_err)?;
Expand All @@ -107,7 +111,7 @@ where
&self,
asset_id1: String,
asset_id2: String,
amount: u128,
amount: String,
include_fee: bool,
) -> RpcResult<Option<u128>> {
let api = self.client.runtime_api();
Expand All @@ -116,11 +120,27 @@ where
AssetId::try_from(asset_id1).map_err(runtime_error_into_rpc_err)?;
let asset_id2: AssetId =
AssetId::try_from(asset_id2).map_err(runtime_error_into_rpc_err)?;
let amount: u128 = amount.parse().map_err(runtime_error_into_rpc_err)?;
let runtime_api_result = api
.quote_price_tokens_for_exact_tokens(at, asset_id1, asset_id2, amount, include_fee)
.map_err(runtime_error_into_rpc_err)?;
Ok(runtime_api_result)
}

async fn get_reserves(
&self,
asset_id1: String,
asset_id2: String,
) -> RpcResult<Option<(u128, u128)>> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;
let asset_id1: AssetId =
AssetId::try_from(asset_id1).map_err(runtime_error_into_rpc_err)?;
let asset_id2: AssetId =
AssetId::try_from(asset_id2).map_err(runtime_error_into_rpc_err)?;
let runtime_api_result = api.get_reserves(at, asset_id1, asset_id2).map_err(runtime_error_into_rpc_err)?;
Ok(runtime_api_result)
}
}

/// Converts a runtime trap into an RPC error.
Expand Down

0 comments on commit c09d2b5

Please sign in to comment.