Skip to content

Commit

Permalink
remove runtime api
Browse files Browse the repository at this point in the history
  • Loading branch information
Leouarz committed Oct 25, 2024
1 parent d20649b commit 6e35662
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 212 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ members = [
"base",
"pallets/dactr",
"pallets/fusion",
"pallets/fusion/runtime-api",
"pallets/mandate",
"pallets/system",
"pallets/vector",
Expand Down Expand Up @@ -36,7 +35,6 @@ kate-recovery = { git = "https://github.com/availproject/avail-core", tag = "cor
avail-base = { path = "base", default-features = false }
da-control = { path = "pallets/dactr", default-features = false }
pallet-fusion = { path = "pallets/fusion", default-features = false }
pallet-fusion-runtime-api = { path = "pallets/fusion/runtime-api", default-features = false }
pallet-mandate = { path = "pallets/mandate", default-features = false }
pallet-vector = { path = "pallets/vector", default-features = false }
da-runtime = { path = "runtime", default-features = false }
Expand Down
21 changes: 0 additions & 21 deletions pallets/fusion/runtime-api/Cargo.toml

This file was deleted.

34 changes: 0 additions & 34 deletions pallets/fusion/runtime-api/src/lib.rs

This file was deleted.

118 changes: 14 additions & 104 deletions pallets/fusion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,20 +1901,25 @@ impl<T: Config> Pallet<T> {
return Ok(());
};

// Fetch avail pool
// Checks before calling stake
let avail_pool =
FusionPools::<T>::get(AVAIL_POOL_ID).ok_or(Error::<T>::PoolNotFound)?;
let has_avail_membership =
FusionMemberships::<T>::get(evm_address, AVAIL_POOL_ID).is_some();
let can_stake_to_pool = avail_pool.state == FusionPoolState::Open
|| (avail_pool.state == FusionPoolState::Blocked && has_avail_membership);
let wont_overflow_maximum_amount = avail_currency
.total_staked_native
.saturating_add(avail_in_currency)
<= avail_currency.max_amount;
let wont_overflow_maximum_members = has_avail_membership
|| (avail_pool.members.len() as u32) < T::MaxMembersPerPool::get();

if membership.is_compounding
&& (avail_pool.state == FusionPoolState::Open
|| (avail_pool.state == FusionPoolState::Blocked
&& FusionMemberships::<T>::get(evm_address, AVAIL_POOL_ID).is_some()))
&& !avail_currency.is_destroyed
&& avail_currency
.total_staked_native
.saturating_add(avail_in_currency)
<= avail_currency.max_amount
&& avail_in_currency > 0
&& can_stake_to_pool
&& wont_overflow_maximum_amount
&& wont_overflow_maximum_members
{
// At this point this should never fail except in case of arithmetic errors which is ok
Self::do_stake(evm_address, AVAIL_POOL_ID, avail_in_currency, true)?;
Expand Down Expand Up @@ -2372,101 +2377,6 @@ impl<T: Config> Pallet<T> {
}
Ok(user_extra_rewards_balance)
}

// FusionApi implementation
pub fn api_pending_rewards(
evm_address: EvmAddress,
pool_id: PoolId,
era: EraIndex,
) -> Result<BalanceOf<T>, DispatchError> {
ensure!(
!ClaimedRewards::<T>::contains_key(era, (pool_id, evm_address)),
Error::<T>::AlreadyClaimed
);

let Some(era_rewards) = FusionEraRewards::<T>::get(era, pool_id) else {
return Ok(BalanceOf::<T>::zero());
};

let Some(exposure) = FusionExposures::<T>::get(era, pool_id) else {
return Ok(BalanceOf::<T>::zero());
};

let (user_reward_balance, user_points) =
Self::compute_basic_rewards(evm_address, &exposure, &era_rewards)?;

let extra_rewards =
Self::compute_extra_rewards(evm_address, &exposure, &era_rewards, user_points)?;

// Using pools Ids,
Ok(user_reward_balance.saturating_add(extra_rewards))
}

pub fn api_avail_to_currency(
currency_id: CurrencyId,
avail_amount: BalanceOf<T>,
era: Option<EraIndex>,
) -> Result<FusionCurrencyBalance, DispatchError> {
let currency =
FusionCurrencies::<T>::get(currency_id).ok_or(Error::<T>::CurrencyNotFound)?;
let currency_value = currency.avail_to_currency(avail_amount, era)?;

Ok(currency_value)
}

pub fn api_currency_to_avail(
currency_id: CurrencyId,
currency_amount: FusionCurrencyBalance,
era: Option<EraIndex>,
) -> Result<BalanceOf<T>, DispatchError> {
let currency =
FusionCurrencies::<T>::get(currency_id).ok_or(Error::<T>::CurrencyNotFound)?;
let avail_value = currency.currency_to_avail(currency_amount, era, None)?;

Ok(avail_value)
}

pub fn api_points_to_currency(
pool_id: PoolId,
points: Points,
) -> Result<FusionCurrencyBalance, DispatchError> {
let pool = FusionPools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let currency_value = pool.points_to_currency(points, None)?;

Ok(currency_value)
}

pub fn api_currency_to_points(
pool_id: PoolId,
currency_amount: FusionCurrencyBalance,
) -> Result<Points, DispatchError> {
let pool = FusionPools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let points_value = pool.currency_to_points(currency_amount, None)?;

Ok(points_value)
}

pub fn api_points_to_avail(
pool_id: PoolId,
points: Points,
era: Option<EraIndex>,
) -> Result<BalanceOf<T>, DispatchError> {
let pool = FusionPools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let avail_value = pool.points_to_avail(points, None, era)?;

Ok(avail_value)
}

pub fn api_avail_to_points(
pool_id: PoolId,
avail_amount: BalanceOf<T>,
era: Option<EraIndex>,
) -> Result<Points, DispatchError> {
let pool = FusionPools::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
let points_value = pool.avail_to_points(avail_amount, None, era)?;

Ok(points_value)
}
}

impl<T: Config> FusionExt<T::AccountId, BalanceOf<T>> for Pallet<T> {
Expand Down
2 changes: 0 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ kate = { workspace = true, default-features = false }

da-control = { workspace = true, default-features = false }
pallet-fusion = { workspace = true, default-features = false }
pallet-fusion-runtime-api = { workspace = true, default-features = false }
pallet-mandate = { workspace = true, default-features = false }
pallet-vector = { workspace = true, default-features = false }

Expand Down Expand Up @@ -166,7 +165,6 @@ std = [
"pallet-collective/std",
"pallet-election-provider-multi-phase/std",
"pallet-fusion/std",
"pallet-fusion-runtime-api/std",
"pallet-grandpa/std",
"pallet-identity/std",
"pallet-im-online/std",
Expand Down
41 changes: 3 additions & 38 deletions runtime/src/apis.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::kate::{Error as RTKateError, GDataProof, GRow};
use crate::{
constants, mmr, version::VERSION, AccountId, AuthorityDiscovery, Babe, Block, BlockNumber,
EpochDuration, Executive, Fusion, Grandpa, Historical, Index, InherentDataExt, Mmr,
NominationPools, OpaqueMetadata, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys,
Staking, System, TransactionPayment, LOG_TARGET,
EpochDuration, Executive, Grandpa, Historical, Index, InherentDataExt, Mmr, NominationPools,
OpaqueMetadata, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys, Staking, System,
TransactionPayment, LOG_TARGET,
};
use avail_base::{HeaderExtensionBuilderData, ProvidePostInherent};
use avail_core::{
Expand All @@ -20,7 +20,6 @@ use frame_support::{
traits::KeyOwnerProofSystem,
weights::Weight,
};
use pallet_fusion::types::*;
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use sp_api::{decl_runtime_apis, impl_runtime_apis};
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
Expand Down Expand Up @@ -572,38 +571,4 @@ impl_runtime_apis! {
build_config::<RuntimeGenesisConfig>(config)
}
}

impl pallet_fusion_runtime_api::FusionApi<Block, AccountId, Balance> for Runtime {
fn api_pending_rewards(who: EvmAddress, pool_id: PoolId, era: EraIndex) -> Balance {
Fusion::api_pending_rewards(who, pool_id, era).unwrap_or(Balance::default())
}

fn api_avail_to_currency(
currency_id: CurrencyId,
avail_amount: Balance,
era: Option<EraIndex>,
) -> FusionCurrencyBalance {
Fusion::api_avail_to_currency(currency_id, avail_amount, era).unwrap_or_default()
}

fn api_currency_to_avail(currency_id: CurrencyId, currency_amount: FusionCurrencyBalance, era: Option<EraIndex>) -> Balance {
Fusion::api_currency_to_avail(currency_id, currency_amount, era).unwrap_or_default()
}

fn api_points_to_currency(pool_id: PoolId, points: Points) -> FusionCurrencyBalance {
Fusion::api_points_to_currency(pool_id, points).unwrap_or_default()
}

fn api_currency_to_points(pool_id: PoolId, currency_amount: FusionCurrencyBalance) -> Points {
Fusion::api_currency_to_points(pool_id, currency_amount).unwrap_or_default()
}

fn api_points_to_avail(pool_id: PoolId, points: Points, era: Option<EraIndex>) -> Balance {
Fusion::api_points_to_avail(pool_id, points, era).unwrap_or_default()
}

fn api_avail_to_points(pool_id: PoolId, avail_amount: Balance, era: Option<EraIndex>) -> Points {
Fusion::api_avail_to_points(pool_id, avail_amount, era).unwrap_or_default()
}
}
}

0 comments on commit 6e35662

Please sign in to comment.