Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add rpc methods for tradeable tokens #531

Merged
merged 8 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pallets/xyk/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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};
use xyk_runtime_api::{RpcAmountsResult, RpcAssetMetadata, XYKRpcResult};

#[rpc(client, server)]
pub trait XykApi<
Expand Down Expand Up @@ -119,6 +119,12 @@ pub trait XykApi<
input_amount: Balance,
at: Option<BlockHash>,
) -> RpcResult<Option<bool>>;

#[method(name = "xyk_get_tradeable_tokens")]
fn get_tradeable_tokens(
&self,
at: Option<BlockHash>,
) -> RpcResult<sp_std::vec::Vec<RpcAssetMetadata<TokenId>>>;
}

pub struct Xyk<C, M> {
Expand Down Expand Up @@ -409,4 +415,19 @@ where
)))
})
}
fn get_tradeable_tokens(
&self,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<RpcAssetMetadata<TokenId>>> {
let api = self.client.runtime_api();
let at = self.client.info().best_hash;

api.get_tradeable_tokens(at).map_err(|e| {
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
1,
"Unable to serve the request",
Some(format!("{:?}", e)),
)))
})
}
}
2 changes: 1 addition & 1 deletion pallets/xyk/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ std = [
"codec/std",
"sp-runtime/std",
"frame-support/std",
'frame-system/std',
'frame-system/std'
]
13 changes: 13 additions & 0 deletions pallets/xyk/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use codec::{Codec, Decode, Encode};
#[cfg(feature = "std")]
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))]
Expand Down Expand Up @@ -34,6 +35,16 @@ pub struct RpcAmountsResult<Balance> {
pub second_asset_amount: 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 RpcAssetMetadata<TokenId> {
pub token_id: TokenId,
pub decimals: u32,
pub name: Vec<u8>,
pub symbol: Vec<u8>,
}

#[cfg(feature = "std")]
fn serialize_as_string<S: Serializer, T: std::fmt::Display>(
t: &T,
Expand Down Expand Up @@ -106,5 +117,7 @@ sp_api::decl_runtime_apis! {
path: sp_std::vec::Vec<TokenId>,
input_amount: Balance,
) -> Option<bool>;

fn get_tradeable_tokens() -> Vec<RpcAssetMetadata<TokenId>>;
}
}
23 changes: 22 additions & 1 deletion runtime/mangata-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{RpcAmountsResult, RpcAssetMetadata, XYKRpcResult};

// Make the WASM binary available.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -1034,6 +1034,27 @@ impl_runtime_apis! {
}
}
}

fn get_tradeable_tokens() -> Vec<RpcAssetMetadata<mangata_types::TokenId>> {
orml_asset_registry::Metadata::<Runtime>::iter()
.filter_map(|(token_id, metadata)| {
if !metadata.name.is_empty()
&& !metadata.symbol.is_empty()
&& metadata.additional.xyk.as_ref().map_or(true, |xyk| !xyk.operations_disabled)
{
let rpc_metadata = RpcAssetMetadata {
token_id: token_id,
decimals: metadata.decimals,
name: metadata.name.clone(),
symbol: metadata.symbol.clone(),
};
Some(rpc_metadata)
} else {
None
}
})
.collect::<Vec<_>>()
}
}

impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
Expand Down
23 changes: 22 additions & 1 deletion runtime/mangata-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{RpcAmountsResult, RpcAssetMetadata, XYKRpcResult};

// Make the WASM binary available.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -1048,6 +1048,27 @@ impl_runtime_apis! {
}
}
}

fn get_tradeable_tokens() -> Vec<RpcAssetMetadata<mangata_types::TokenId>> {
orml_asset_registry::Metadata::<Runtime>::iter()
.filter_map(|(token_id, metadata)| {
if !metadata.name.is_empty()
&& !metadata.symbol.is_empty()
&& metadata.additional.xyk.as_ref().map_or(true, |xyk| !xyk.operations_disabled)
{
let rpc_metadata = RpcAssetMetadata {
token_id: token_id,
decimals: metadata.decimals,
name: metadata.name.clone(),
symbol: metadata.symbol.clone(),
};
Some(rpc_metadata)
} else {
None
}
})
.collect::<Vec<_>>()
}
}

impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
Expand Down