Skip to content

Commit

Permalink
Add rpc for lm (#318)
Browse files Browse the repository at this point in the history
* ✨ ($PALLET) Add lm rpc to bifrost

* 🍻 ($PALLET) Compl rpc for lm

* 🐛 ($PALLET) Fix compile error
  • Loading branch information
AllenPocketGamer authored Sep 27, 2021
1 parent e2003bd commit c2b2673
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 4 deletions.
2 changes: 2 additions & 0 deletions node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ bifrost-flexible-fee-rpc = { path = "../../pallets/flexible-fee/rpc" }
bifrost-flexible-fee-rpc-runtime-api = { path = "../../pallets/flexible-fee/rpc/runtime-api" }
bifrost-salp-rpc-api = { path = "../../pallets/salp/rpc" }
bifrost-salp-rpc-runtime-api = { path = "../../pallets/salp/rpc/runtime-api" }
bifrost-liquidity-mining-rpc-api = { path = "../../pallets/liquidity-mining/rpc" }
bifrost-liquidity-mining-rpc-runtime-api = { path = "../../pallets/liquidity-mining/rpc/runtime-api" }
zenlink-protocol-rpc = "*"
zenlink-protocol-runtime-api = "*"
9 changes: 8 additions & 1 deletion node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ use std::sync::Arc;

use bifrost_flexible_fee_rpc::{FeeRpcApi, FlexibleFeeStruct};
use bifrost_flexible_fee_rpc_runtime_api::FlexibleFeeRuntimeApi as FeeRuntimeApi;
use bifrost_liquidity_mining_rpc_api::{LiquidityMiningRpcApi, LiquidityMiningRpcWrapper};
use bifrost_liquidity_mining_rpc_runtime_api::LiquidityMiningRuntimeApi;
use bifrost_salp_rpc_api::{SalpRpcApi, SalpRpcWrapper};
use bifrost_salp_rpc_runtime_api::SalpRuntimeApi;
use node_primitives::{AccountId, Balance, Block, ParaId};
use node_primitives::{AccountId, Balance, Block, ParaId, PoolId};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
pub use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool;
Expand Down Expand Up @@ -69,6 +71,7 @@ where
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: FeeRuntimeApi<Block, AccountId>,
C::Api: SalpRuntimeApi<Block, ParaId, AccountId>,
C::Api: LiquidityMiningRuntimeApi<Block, AccountId, PoolId>,
P: TransactionPool + 'static,
{
let FullDeps { client, .. } = deps;
Expand All @@ -81,5 +84,9 @@ where

io.extend_with(SalpRpcApi::to_delegate(SalpRpcWrapper::new(client.clone())));

io.extend_with(LiquidityMiningRpcApi::to_delegate(LiquidityMiningRpcWrapper::new(
client.clone(),
)));

io
}
2 changes: 1 addition & 1 deletion node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ zenlink-protocol-runtime-api = "*"
# Bifrost rpc
bifrost-flexible-fee-rpc-runtime-api = { path = "../../pallets/flexible-fee/rpc/runtime-api" }
bifrost-salp-rpc-runtime-api = { path = "../../pallets/salp/rpc/runtime-api" }

bifrost-liquidity-mining-rpc-runtime-api = { path = "../../pallets/liquidity-mining/rpc/runtime-api" }

[features]
default = [ "std" ]
Expand Down
4 changes: 4 additions & 0 deletions node/service/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;
#[cfg(feature = "with-asgard-runtime")]
pub use asgard_runtime;
use bifrost_flexible_fee_rpc_runtime_api::FlexibleFeeRuntimeApi as FeeRuntimeApi;
use bifrost_liquidity_mining_rpc_runtime_api::LiquidityMiningRuntimeApi;
#[cfg(feature = "with-bifrost-runtime")]
pub use bifrost_runtime;
use bifrost_salp_rpc_runtime_api::SalpRuntimeApi;
Expand All @@ -33,6 +34,7 @@ use cumulus_client_service::{
prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
};
use cumulus_primitives_core::ParaId;
use node_primitives::PoolId;
pub use node_primitives::{AccountId, Block, BlockNumber, Hash, Header, ParaId as BifrostParaId};
use sc_client_api::ExecutorProvider;
use sc_consensus::LongestChain;
Expand Down Expand Up @@ -189,6 +191,7 @@ where
RuntimeApi::RuntimeApi: sp_consensus_aura::AuraApi<Block, AuraId>,
RuntimeApi::RuntimeApi: FeeRuntimeApi<Block, AccountId>,
RuntimeApi::RuntimeApi: SalpRuntimeApi<Block, BifrostParaId, AccountId>,
RuntimeApi::RuntimeApi: LiquidityMiningRuntimeApi<Block, AccountId, PoolId>,
Executor: NativeExecutionDispatch + 'static,
BIC: FnOnce(
Arc<TFullClient<Block, RuntimeApi, Executor>>,
Expand Down Expand Up @@ -348,6 +351,7 @@ where
RuntimeApi::RuntimeApi: sp_consensus_aura::AuraApi<Block, AuraId>,
RuntimeApi::RuntimeApi: FeeRuntimeApi<Block, AccountId>,
RuntimeApi::RuntimeApi: SalpRuntimeApi<Block, BifrostParaId, AccountId>,
RuntimeApi::RuntimeApi: LiquidityMiningRuntimeApi<Block, AccountId, PoolId>,
Executor: NativeExecutionDispatch + 'static,
{
start_node_impl(
Expand Down
19 changes: 19 additions & 0 deletions pallets/liquidity-mining/rpc/Cargo.toml
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" }
19 changes: 19 additions & 0 deletions pallets/liquidity-mining/rpc/runtime-api/Cargo.toml
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",
]
36 changes: 36 additions & 0 deletions pallets/liquidity-mining/rpc/runtime-api/src/lib.rs
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)>;
}
}
89 changes: 89 additions & 0 deletions pallets/liquidity-mining/rpc/src/lib.rs
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()),
}),
}
}
}
29 changes: 29 additions & 0 deletions pallets/liquidity-mining/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,35 @@ pub mod pallet {
Ok(CurrencyId::LPToken(sym1, discr1, sym2, discr2))
}

pub fn rewards(
who: AccountIdOf<T>,
pid: PoolId,
) -> Result<Vec<(CurrencyId, BalanceOf<T>)>, ()> {
let pool: PoolInfo<T> = Self::pool(pid).ok_or(())?.try_retire().try_update();
let deposit_data: DepositData<T> =
Self::user_deposit_data(pid, who.clone()).ok_or(())?;

let mut to_rewards = Vec::<(CurrencyId, BalanceOf<T>)>::new();

if let Some(_block_startup) = pool.block_startup {
for (rtoken, reward) in pool.rewards.iter() {
let v_new = reward.gain_avg;
if let Some(gain_avg) = deposit_data.gain_avgs.get(rtoken) {
let v_old = *gain_avg;

let user_deposit: u128 = deposit_data.deposit.saturated_into();
let amount = BalanceOf::<T>::saturated_from(
v_new.saturating_sub(v_old).saturating_mul_int(user_deposit),
);

to_rewards.push((*rtoken, amount));
}
}
}

Ok(to_rewards)
}

#[allow(non_snake_case)]
pub(crate) fn vsAssets(
index: ParaId,
Expand Down
2 changes: 2 additions & 0 deletions runtime/asgard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ bifrost-minter-reward = { path = "../../pallets/minter-reward", default-features
bifrost-runtime-common = { package = "bifrost-runtime-common", path = "../common", default-features = false }
bifrost-salp = { path = "../../pallets/salp", default-features = false }
bifrost-salp-rpc-runtime-api = { path = "../../pallets/salp/rpc/runtime-api", default-features = false }
bifrost-liquidity-mining-rpc-runtime-api = { path = "../../pallets/liquidity-mining/rpc/runtime-api", default-features = false }
bifrost-vsbond-auction = { path = "../../pallets/vsbond-auction", default-features = false }
bifrost-vtoken-mint = { path = "../../pallets/vtoken-mint", default-features = false }
pallet-vesting = { package = "bifrost-vesting", path = "../../pallets/vesting", default-features = false }
Expand Down Expand Up @@ -180,6 +181,7 @@ std = [
"bifrost-vsbond-auction/std",
"bifrost-vtoken-mint/std",
"bifrost-liquidity-mining/std",
"bifrost-liquidity-mining-rpc-runtime-api/std",
"xcm-support/std",
"orml-currencies/std",
"orml-traits/std",
Expand Down
8 changes: 7 additions & 1 deletion runtime/asgard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ use frame_support::{
};
pub use node_primitives::{
AccountId, Amount, Balance, BlockNumber, CurrencyId, ExtraFeeName, Moment, Nonce, ParaId,
ParachainDerivedProxyAccountType, ParachainTransactProxyType, ParachainTransactType,
ParachainDerivedProxyAccountType, ParachainTransactProxyType, ParachainTransactType, PoolId,
RpcContributionStatus, TokenSymbol, TransferOriginType, XcmBaseWeight,
};
// orml imports
Expand Down Expand Up @@ -1616,6 +1616,12 @@ impl_runtime_apis! {
}
}

impl bifrost_liquidity_mining_rpc_runtime_api::LiquidityMiningRuntimeApi<Block, AccountId, PoolId> for Runtime {
fn get_rewards(who: AccountId, pid: PoolId) -> Vec<(CurrencyId, Balance)> {
LiquidityMining::rewards(who, pid).unwrap_or(Vec::new())
}
}

// benchmarks for asgard modules
#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions runtime/bifrost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bifrost-salp = { path = "../../pallets/salp", default-features = false }
bifrost-salp-rpc-runtime-api = { path = "../../pallets/salp/rpc/runtime-api", default-features = false }
pallet-vesting = { package = "bifrost-vesting", path = "../../pallets/vesting", default-features = false }
bifrost-liquidity-mining = { path = "../../pallets/liquidity-mining", default-features = false }
bifrost-liquidity-mining-rpc-runtime-api = { path = "../../pallets/liquidity-mining/rpc/runtime-api", default-features = false }
xcm-support = { path = "../../xcm-support", default-features = false }

# orml
Expand Down Expand Up @@ -163,6 +164,7 @@ std = [
"bifrost-salp/std",
"bifrost-salp-rpc-runtime-api/std",
"bifrost-liquidity-mining/std",
"bifrost-liquidity-mining-rpc-runtime-api/std",
]
with-tracing = ["frame-executive/with-tracing"]

Expand Down
8 changes: 7 additions & 1 deletion runtime/bifrost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use frame_system::{EnsureOneOf, EnsureRoot, RawOrigin};
use hex_literal::hex;
pub use node_primitives::{
AccountId, Amount, Balance, BlockNumber, CurrencyId, ExtraFeeName, Moment, Nonce, ParaId,
ParachainDerivedProxyAccountType, ParachainTransactProxyType, ParachainTransactType,
ParachainDerivedProxyAccountType, ParachainTransactProxyType, ParachainTransactType, PoolId,
RpcContributionStatus, TokenSymbol, TransferOriginType, XcmBaseWeight,
};
// orml imports
Expand Down Expand Up @@ -1333,6 +1333,12 @@ impl_runtime_apis! {
}
}

impl bifrost_liquidity_mining_rpc_runtime_api::LiquidityMiningRuntimeApi<Block, AccountId, PoolId> for Runtime {
fn get_rewards(who: AccountId, pid: PoolId) -> Vec<(CurrencyId, Balance)> {
LiquidityMining::rewards(who, pid).unwrap_or(Vec::new())
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn benchmark_metadata(extra: bool) -> (
Expand Down

0 comments on commit c2b2673

Please sign in to comment.