Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feat/add vtoken exchange rate rpc #1274

Merged
merged 6 commits into from
Jun 20, 2024
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

10 changes: 8 additions & 2 deletions node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -89,6 +91,7 @@ where
C::Api: SalpRuntimeApi<Block, ParaId, AccountId>,
C::Api: StablePoolRuntimeApi<Block>,
C::Api: LendMarketApi<Block, AccountId, Balance>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId>,
C::Api: ZenlinkProtocolRuntimeApi<Block, AccountId, AssetId>,
C::Api:
zenlink_stable_amm_runtime_api::StableAmmApi<Block, CurrencyId, Balance, AccountId, PoolId>,
Expand All @@ -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)
}
Expand All @@ -131,6 +135,7 @@ where
C::Api: SalpRuntimeApi<Block, ParaId, AccountId>,
C::Api: VeMintingRuntimeApi<Block, AccountId>,
C::Api: LendMarketApi<Block, AccountId, Balance>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId>,
C::Api: ZenlinkProtocolRuntimeApi<Block, AccountId, AssetId>,
C::Api: StablePoolRuntimeApi<Block>,
C::Api: BlockBuilder<Block>,
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion pallets/vtoken-minting/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
39 changes: 16 additions & 23 deletions pallets/vtoken-minting/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CurrencyId, BlockHash> {
Expand All @@ -53,43 +48,41 @@ pub struct VtokenMintingRpc<C, Block> {
_marker: PhantomData<Block>,
}

impl<C, Block> VtokenMintingRpc<C, Block>
where
Block: BlockT,
C: BlockIdTo<Block>,
{
impl<C, Block> VtokenMintingRpc<C, Block> {
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: PhantomData }
}
}

#[async_trait]
impl<C, Block, CurrencyId> VtokenMintingRpcApiServer<<Block as BlockT>::Hash, AccountId>
impl<C, Block, CurrencyId> VtokenMintingRpcApiServer<CurrencyId, <Block as BlockT>::Hash>
for VtokenMintingRpc<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block> + BlockIdTo<Block>,
C::Api: VeMintingRuntimeApi<Block, AccountId>,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: VtokenMintingRuntimeApi<Block, CurrencyId>,
CurrencyId: Codec,
{
fn get_exchange_rate(
&self,
token_id: Option<CurrencyId>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<(CurrencyId, NumberOrHex)>> {
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<U256, _> = lm_rpc_api.get_exchange_rate(at, token_id, max_epoch);
let rs: Result<Vec<(CurrencyId, U256)>, _> = 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))
}
}
2 changes: 1 addition & 1 deletion pallets/vtoken-minting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down