-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ ($PALLET) Add lm rpc to bifrost * 🍻 ($PALLET) Compl rpc for lm * 🐛 ($PALLET) Fix compile error
- Loading branch information
1 parent
e2003bd
commit c2b2673
Showing
13 changed files
with
225 additions
and
4 deletions.
There are no files selected for viewing
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,19 @@ | ||
[package] | ||
name = "bifrost-liquidity-mining-rpc-api" | ||
version = "0.8.0" | ||
authors = ["Allen Pocket <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serde = { version = "1.0.124", features = ["derive"] } | ||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } | ||
jsonrpc-core = "15.1.0" | ||
jsonrpc-core-client = "15.1.0" | ||
jsonrpc-derive = "15.1.0" | ||
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" } | ||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" } | ||
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" } | ||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" } | ||
sp-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" } | ||
node-primitives = { path = "../../../node/primitives", default-features = false } | ||
bifrost-liquidity-mining-rpc-runtime-api = { path = "./runtime-api" } |
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,19 @@ | ||
[package] | ||
name = "bifrost-liquidity-mining-rpc-runtime-api" | ||
version = "0.8.0" | ||
authors = ["Allen Pocket <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } | ||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9", default-features = false } | ||
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9", default-features = false } | ||
node-primitives = { path = "../../../../node/primitives", default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"sp-api/std", | ||
"node-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,36 @@ | ||
// This file is part of Bifrost. | ||
|
||
// Copyright (C) 2019-2021 Liebi Technologies (UK) 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 codec::Codec; | ||
use node_primitives::{Balance, CurrencyId}; | ||
use sp_api::decl_runtime_apis; | ||
use sp_std::vec::Vec; | ||
|
||
decl_runtime_apis! { | ||
pub trait LiquidityMiningRuntimeApi<AccountId, PoolId> where | ||
AccountId: Codec, | ||
PoolId: Codec, | ||
{ | ||
fn get_rewards( | ||
who: AccountId, | ||
pid: PoolId, | ||
) -> Vec<(CurrencyId, Balance)>; | ||
} | ||
} |
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,89 @@ | ||
// This file is part of Bifrost. | ||
|
||
// Copyright (C) 2019-2021 Liebi Technologies (UK) 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}; | ||
|
||
pub use bifrost_liquidity_mining_rpc_runtime_api::{ | ||
self as runtime_api, LiquidityMiningRuntimeApi, | ||
}; | ||
use codec::Codec; | ||
use jsonrpc_core::{Error as RpcError, ErrorCode, Result as JsonRpcResult}; | ||
use jsonrpc_derive::rpc; | ||
use node_primitives::{Balance, CurrencyId}; | ||
use sp_api::ProvideRuntimeApi; | ||
use sp_blockchain::HeaderBackend; | ||
use sp_rpc::number::NumberOrHex; | ||
use sp_runtime::{generic::BlockId, traits::Block as BlockT, SaturatedConversion}; | ||
|
||
#[rpc] | ||
pub trait LiquidityMiningRpcApi<BlockHash, AccountId, PoolId> { | ||
/// rpc method for getting current rewards | ||
#[rpc(name = "liquidityMining_getRewards")] | ||
fn get_rewards( | ||
&self, | ||
who: AccountId, | ||
pid: PoolId, | ||
at: Option<BlockHash>, | ||
) -> JsonRpcResult<Vec<(CurrencyId, NumberOrHex)>>; | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct LiquidityMiningRpcWrapper<C, Block> { | ||
client: Arc<C>, | ||
_marker: PhantomData<Block>, | ||
} | ||
|
||
impl<C, Block> LiquidityMiningRpcWrapper<C, Block> { | ||
pub fn new(client: Arc<C>) -> Self { | ||
Self { client, _marker: PhantomData } | ||
} | ||
} | ||
|
||
impl<C, Block, AccountId, PoolId> LiquidityMiningRpcApi<<Block as BlockT>::Hash, AccountId, PoolId> | ||
for LiquidityMiningRpcWrapper<C, Block> | ||
where | ||
Block: BlockT, | ||
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>, | ||
C::Api: LiquidityMiningRuntimeApi<Block, AccountId, PoolId>, | ||
AccountId: Codec, | ||
PoolId: Codec, | ||
{ | ||
fn get_rewards( | ||
&self, | ||
who: AccountId, | ||
pid: PoolId, | ||
at: Option<<Block as BlockT>::Hash>, | ||
) -> JsonRpcResult<Vec<(CurrencyId, NumberOrHex)>> { | ||
let lm_rpc_api = self.client.runtime_api(); | ||
let at = BlockId::<Block>::hash(at.unwrap_or_else(|| self.client.info().best_hash)); | ||
|
||
let rs: Result<Vec<(CurrencyId, Balance)>, _> = lm_rpc_api.get_rewards(&at, who, pid); | ||
|
||
match rs { | ||
Ok(rewards) => Ok(rewards | ||
.into_iter() | ||
.map(|(token, amount)| (token, NumberOrHex::Number(amount.saturated_into()))) | ||
.collect()), | ||
Err(e) => Err(RpcError { | ||
code: ErrorCode::InternalError, | ||
message: "Failed to get lm rewards.".to_owned(), | ||
data: Some(format!("{:?}", e).into()), | ||
}), | ||
} | ||
} | ||
} |
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