-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vtoken exchange rate rpc (#1260)
- Loading branch information
1 parent
5b892b4
commit 66b0ab0
Showing
26 changed files
with
270 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "bifrost-vtoken-minting-rpc" | ||
version = "0.8.0" | ||
authors = ["Tiebing <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
serde = { workspace = true, features = ["derive"] } | ||
parity-scale-codec = { workspace = true, features = ["derive"] } | ||
jsonrpsee = { workspace = true, features = ["server", "macros"] } | ||
sp-api = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-blockchain = { workspace = true } | ||
sp-core = { workspace = true } | ||
sp-rpc = { workspace = true } | ||
bifrost-primitives = { workspace = true } | ||
bifrost-ve-minting-rpc-runtime-api = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "bifrost-vtoken-minting-rpc-runtime-api" | ||
version = "0.8.0" | ||
authors = ["Tiebing <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
parity-scale-codec = { workspace = true, features = ["derive"] } | ||
sp-std = { workspace = true } | ||
sp-api = { workspace = true } | ||
sp-core = { workspace = true } | ||
bifrost-primitives = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"sp-api/std", | ||
"bifrost-primitives/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This file is part of Bifrost. | ||
|
||
// Copyright (C) Liebi Technologies PTE. LTD. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use parity_scale_codec::Codec; | ||
use sp_api::decl_runtime_apis; | ||
use sp_core::U256; | ||
|
||
decl_runtime_apis! { | ||
pub trait VtokenMintingRuntimeApi<CurrencyId> where CurrencyId: Codec | ||
{ | ||
fn get_exchange_rate(token_id: Option<CurrencyId>) -> Vec<(CurrencyId, U256)>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// This file is part of Bifrost. | ||
|
||
// Copyright (C) Liebi Technologies PTE. LTD. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use std::{marker::PhantomData, sync::Arc}; | ||
|
||
use bifrost_primitives::Balance; | ||
pub use bifrost_vtoken_minting_rpc_runtime_api::{self as runtime_api, VtokenMintingRuntimeApi}; | ||
use jsonrpsee::{ | ||
core::{async_trait, RpcResult}, | ||
proc_macros::rpc, | ||
types::error::{CallError, 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, | ||
}; | ||
|
||
#[rpc(client, server)] | ||
pub trait VtokenMintingRpcApi<CurrencyId, BlockHash> { | ||
/// rpc method for getting vtoken exchange rate | ||
#[method(name = "vtoken_minting_getExchangeRate")] | ||
fn get_exchange_rate( | ||
&self, | ||
asset_id: Option<CurrencyId>, | ||
at: Option<BlockHash>, | ||
) -> RpcResult<Vec<(CurrencyId, NumberOrHex)>>; | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct VtokenMintingRpc<C, Block> { | ||
client: Arc<C>, | ||
_marker: PhantomData<Block>, | ||
} | ||
|
||
impl<C, Block> VtokenMintingRpc<C, Block> | ||
where | ||
Block: BlockT, | ||
C: BlockIdTo<Block>, | ||
{ | ||
pub fn new(client: Arc<C>) -> Self { | ||
Self { client, _marker: PhantomData } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<C, Block, CurrencyId> VtokenMintingRpcApiServer<<Block as BlockT>::Hash, AccountId> | ||
for VtokenMintingRpc<C, Block> | ||
where | ||
Block: BlockT, | ||
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block> + BlockIdTo<Block>, | ||
C::Api: VeMintingRuntimeApi<Block, AccountId>, | ||
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 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); | ||
|
||
match rs { | ||
Ok(epoch) => Ok(NumberOrHex::Hex(epoch.into())), | ||
Err(e) => Err(CallError::Custom(ErrorObject::owned( | ||
ErrorCode::InternalError.code(), | ||
"Failed to get exchange rate.", | ||
Some(format!("{:?}", e)), | ||
))), | ||
} | ||
.map_err(|e| jsonrpsee::core::Error::Call(e)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.