From ecc906aa6ec18aeea1cb7664fccdf635b7741433 Mon Sep 17 00:00:00 2001 From: SunTiebing <87381708+SunTiebing@users.noreply.github.com> Date: Thu, 20 Jun 2024 17:57:15 +0800 Subject: [PATCH] Feat/add vtoken exchange rate rpc (#1274) * Add vtoken exchange rate rpc * add the use of Vec * supplement the RPC logic && fix bugs * adjust the RPC exchange rate precision to three decimal places * fix clippy --- Cargo.lock | 18 +++++++++++++ Cargo.toml | 1 + node/rpc/Cargo.toml | 2 ++ node/rpc/src/lib.rs | 10 +++++-- pallets/vtoken-minting/rpc/Cargo.toml | 2 +- pallets/vtoken-minting/rpc/src/lib.rs | 39 +++++++++++---------------- pallets/vtoken-minting/src/lib.rs | 2 +- 7 files changed, 47 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c78bf33f..3647a7b81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1383,6 +1383,8 @@ dependencies = [ "bifrost-stable-pool-rpc-runtime-api", "bifrost-ve-minting-rpc", "bifrost-ve-minting-rpc-runtime-api", + "bifrost-vtoken-minting-rpc", + "bifrost-vtoken-minting-rpc-runtime-api", "jsonrpsee", "lend-market-rpc", "lend-market-rpc-runtime-api", @@ -2013,6 +2015,22 @@ dependencies = [ "staging-xcm-executor", ] +[[package]] +name = "bifrost-vtoken-minting-rpc" +version = "0.8.0" +dependencies = [ + "bifrost-primitives", + "bifrost-vtoken-minting-rpc-runtime-api", + "jsonrpsee", + "parity-scale-codec", + "serde", + "sp-api", + "sp-blockchain", + "sp-core", + "sp-rpc", + "sp-runtime", +] + [[package]] name = "bifrost-vtoken-minting-rpc-runtime-api" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index e18eda964..5fd46544d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ bifrost-salp-rpc = { path = "pallets/salp/rpc" } bifrost-stable-pool-rpc = { path = "pallets/stable-pool/rpc" } bifrost-ve-minting-rpc = { path = "pallets/ve-minting/rpc" } lend-market-rpc = { path = "pallets/lend-market/rpc" } +bifrost-vtoken-minting-rpc = { path = "pallets/vtoken-minting/rpc" } # Bifrost Runtime bifrost-kusama-runtime = { path = "runtime/bifrost-kusama" } diff --git a/node/rpc/Cargo.toml b/node/rpc/Cargo.toml index e4ed12119..bf69260c2 100644 --- a/node/rpc/Cargo.toml +++ b/node/rpc/Cargo.toml @@ -37,4 +37,6 @@ zenlink-protocol-rpc = { workspace = true } zenlink-protocol-runtime-api = { workspace = true } zenlink-stable-amm-rpc = { workspace = true } zenlink-stable-amm-runtime-api = { workspace = true } +bifrost-vtoken-minting-rpc = { workspace = true } +bifrost-vtoken-minting-rpc-runtime-api = { workspace = true } diff --git a/node/rpc/src/lib.rs b/node/rpc/src/lib.rs index 22fb09afa..0fef634c8 100644 --- a/node/rpc/src/lib.rs +++ b/node/rpc/src/lib.rs @@ -44,6 +44,8 @@ use bifrost_stable_pool_rpc::{StablePoolRpc, StablePoolRpcApiServer}; use bifrost_stable_pool_rpc_runtime_api::StablePoolRuntimeApi; use bifrost_ve_minting_rpc::{VeMintingRpc, VeMintingRpcApiServer}; use bifrost_ve_minting_rpc_runtime_api::VeMintingRuntimeApi; +use bifrost_vtoken_minting_rpc::{VtokenMintingRpc, VtokenMintingRpcApiServer}; +use bifrost_vtoken_minting_rpc_runtime_api::VtokenMintingRuntimeApi; use lend_market_rpc::{LendMarket, LendMarketApiServer}; use lend_market_rpc_runtime_api::LendMarketApi; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; @@ -89,6 +91,7 @@ where C::Api: SalpRuntimeApi, C::Api: StablePoolRuntimeApi, C::Api: LendMarketApi, + C::Api: VtokenMintingRuntimeApi, C::Api: ZenlinkProtocolRuntimeApi, C::Api: zenlink_stable_amm_runtime_api::StableAmmApi, @@ -107,7 +110,8 @@ where module.merge(ZenlinkProtocol::new(client.clone()).into_rpc())?; module.merge(StableAmm::new(client.clone()).into_rpc())?; module.merge(StablePoolRpc::new(client.clone()).into_rpc())?; - module.merge(LendMarket::new(client).into_rpc())?; + module.merge(LendMarket::new(client.clone()).into_rpc())?; + module.merge(VtokenMintingRpc::new(client).into_rpc())?; Ok(module) } @@ -131,6 +135,7 @@ where C::Api: SalpRuntimeApi, C::Api: VeMintingRuntimeApi, C::Api: LendMarketApi, + C::Api: VtokenMintingRuntimeApi, C::Api: ZenlinkProtocolRuntimeApi, C::Api: StablePoolRuntimeApi, C::Api: BlockBuilder, @@ -148,7 +153,8 @@ where module.merge(VeMintingRpc::new(client.clone()).into_rpc())?; module.merge(ZenlinkProtocol::new(client.clone()).into_rpc())?; module.merge(StablePoolRpc::new(client.clone()).into_rpc())?; - module.merge(LendMarket::new(client).into_rpc())?; + module.merge(LendMarket::new(client.clone()).into_rpc())?; + module.merge(VtokenMintingRpc::new(client).into_rpc())?; Ok(module) } diff --git a/pallets/vtoken-minting/rpc/Cargo.toml b/pallets/vtoken-minting/rpc/Cargo.toml index ccbf9c67c..080396f47 100644 --- a/pallets/vtoken-minting/rpc/Cargo.toml +++ b/pallets/vtoken-minting/rpc/Cargo.toml @@ -14,4 +14,4 @@ sp-blockchain = { workspace = true } sp-core = { workspace = true } sp-rpc = { workspace = true } bifrost-primitives = { workspace = true } -bifrost-ve-minting-rpc-runtime-api = { workspace = true } +bifrost-vtoken-minting-rpc-runtime-api = { workspace = true } diff --git a/pallets/vtoken-minting/rpc/src/lib.rs b/pallets/vtoken-minting/rpc/src/lib.rs index d820529dd..301d82f26 100644 --- a/pallets/vtoken-minting/rpc/src/lib.rs +++ b/pallets/vtoken-minting/rpc/src/lib.rs @@ -18,23 +18,18 @@ use std::{marker::PhantomData, sync::Arc}; -use bifrost_primitives::Balance; -pub use bifrost_vtoken_minting_rpc_runtime_api::{self as runtime_api, VtokenMintingRuntimeApi}; +pub use bifrost_vtoken_minting_rpc_runtime_api::VtokenMintingRuntimeApi; use jsonrpsee::{ core::{async_trait, RpcResult}, proc_macros::rpc, - types::error::{CallError, ErrorCode, ErrorObject}, + types::error::{ErrorCode, ErrorObject}, }; use parity_scale_codec::Codec; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_core::U256; use sp_rpc::number::NumberOrHex; -use sp_runtime::{ - generic::BlockId, - traits::{Block as BlockT, BlockIdTo}, - SaturatedConversion, -}; +use sp_runtime::traits::Block as BlockT; #[rpc(client, server)] pub trait VtokenMintingRpcApi { @@ -53,23 +48,19 @@ pub struct VtokenMintingRpc { _marker: PhantomData, } -impl VtokenMintingRpc -where - Block: BlockT, - C: BlockIdTo, -{ +impl VtokenMintingRpc { pub fn new(client: Arc) -> Self { Self { client, _marker: PhantomData } } } #[async_trait] -impl VtokenMintingRpcApiServer<::Hash, AccountId> +impl VtokenMintingRpcApiServer::Hash> for VtokenMintingRpc where Block: BlockT, - C: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend + BlockIdTo, - C::Api: VeMintingRuntimeApi, + C: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, + C::Api: VtokenMintingRuntimeApi, CurrencyId: Codec, { fn get_exchange_rate( @@ -77,19 +68,21 @@ where token_id: Option, at: Option<::Hash>, ) -> RpcResult> { - let lm_rpc_api = self.client.runtime_api(); + let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - let rs: Result = lm_rpc_api.get_exchange_rate(at, token_id, max_epoch); + let rs: Result, _> = api.get_exchange_rate(at, token_id); match rs { - Ok(epoch) => Ok(NumberOrHex::Hex(epoch.into())), - Err(e) => Err(CallError::Custom(ErrorObject::owned( + Ok(data) => Ok(data + .into_iter() + .map(|(token, rate)| (token, NumberOrHex::Hex(rate.into()))) + .collect()), + Err(e) => Err(ErrorObject::owned( ErrorCode::InternalError.code(), - "Failed to get exchange rate.", + "Failed to get find_block_epoch.", Some(format!("{:?}", e)), - ))), + )), } - .map_err(|e| jsonrpsee::core::Error::Call(e)) } } diff --git a/pallets/vtoken-minting/src/lib.rs b/pallets/vtoken-minting/src/lib.rs index ab9147878..d91d11e40 100644 --- a/pallets/vtoken-minting/src/lib.rs +++ b/pallets/vtoken-minting/src/lib.rs @@ -1934,7 +1934,7 @@ pub mod pallet { match token_id { Some(token_id) => { - let vtoken_amount = Self::get_vtoken_amount(token_id, 1u128)?; + let vtoken_amount = Self::get_vtoken_amount(token_id, 1000u128)?; result.push((token_id, vtoken_amount)); }, None =>