From 6e356629784e0f152dbb33bab0227ace34debdd3 Mon Sep 17 00:00:00 2001 From: Leouarz Date: Fri, 25 Oct 2024 12:00:13 +0200 Subject: [PATCH] remove runtime api --- Cargo.lock | 11 --- Cargo.toml | 2 - pallets/fusion/runtime-api/Cargo.toml | 21 ----- pallets/fusion/runtime-api/src/lib.rs | 34 -------- pallets/fusion/src/lib.rs | 118 +++----------------------- runtime/Cargo.toml | 2 - runtime/src/apis.rs | 41 +-------- 7 files changed, 17 insertions(+), 212 deletions(-) delete mode 100644 pallets/fusion/runtime-api/Cargo.toml delete mode 100644 pallets/fusion/runtime-api/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9722ecf28..086a3d7fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2168,7 +2168,6 @@ dependencies = [ "pallet-collective", "pallet-election-provider-multi-phase", "pallet-fusion", - "pallet-fusion-runtime-api", "pallet-grandpa", "pallet-identity", "pallet-im-online", @@ -6147,16 +6146,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "pallet-fusion-runtime-api" -version = "1.0.0" -dependencies = [ - "pallet-fusion", - "parity-scale-codec", - "sp-api", - "sp-staking", -] - [[package]] name = "pallet-grandpa" version = "28.0.0" diff --git a/Cargo.toml b/Cargo.toml index f9d2158b9..a1a159788 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ members = [ "base", "pallets/dactr", "pallets/fusion", - "pallets/fusion/runtime-api", "pallets/mandate", "pallets/system", "pallets/vector", @@ -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 } diff --git a/pallets/fusion/runtime-api/Cargo.toml b/pallets/fusion/runtime-api/Cargo.toml deleted file mode 100644 index 6605d857a..000000000 --- a/pallets/fusion/runtime-api/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "pallet-fusion-runtime-api" -version = "1.0.0" -edition = "2021" -description = "Runtime API for pallet fusion" - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - -[dependencies] -codec = { features = ["derive"], workspace = true } -sp-api = { workspace = true } -pallet-fusion = { workspace = true } -sp-staking = { workspace = true, default-features = false, features = [ - "serde", -] } - - -[features] -default = ["std"] -std = ["codec/std", "pallet-fusion/std", "sp-api/std"] diff --git a/pallets/fusion/runtime-api/src/lib.rs b/pallets/fusion/runtime-api/src/lib.rs deleted file mode 100644 index 44cf6c9a6..000000000 --- a/pallets/fusion/runtime-api/src/lib.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -use codec::Codec; -use pallet_fusion::types::*; -use sp_staking::EraIndex; - -sp_api::decl_runtime_apis! { - pub trait FusionApi - where - AccountId: Codec, - Balance: Codec, - { - /// Return the amount of pending rewards for the user alongside the concerned pools and eras for each pool. - fn api_pending_rewards(who: EvmAddress, pool_id: PoolId, era: EraIndex) -> Balance; - - /// Convert an AVAIL amount to a specified external currency. - fn api_avail_to_currency(currency_id: CurrencyId, avail_amount: Balance, era: Option) -> FusionCurrencyBalance; - - /// Convert a specified external currency amount to AVAIL. - fn api_currency_to_avail(currency_id: CurrencyId, currency_amount: FusionCurrencyBalance, era: Option) -> Balance; - - /// Convert points to the equivalent amount in a specified external currency for a given pool. - fn api_points_to_currency(pool_id: PoolId, points: Points) -> FusionCurrencyBalance; - - /// Convert a specified external currency amount to points for a given pool. - fn api_currency_to_points(pool_id: PoolId, currency_amount: FusionCurrencyBalance) -> Points; - - /// Convert points to the equivalent AVAIL amount for a given pool. - fn api_points_to_avail(pool_id: PoolId, points: Points, era: Option) -> Balance; - - /// Convert an AVAIL amount to points for a given pool. - fn api_avail_to_points(pool_id: PoolId, avail_amount: Balance, era: Option) -> Points; - } -} diff --git a/pallets/fusion/src/lib.rs b/pallets/fusion/src/lib.rs index 5eb324e90..17b994450 100644 --- a/pallets/fusion/src/lib.rs +++ b/pallets/fusion/src/lib.rs @@ -1901,20 +1901,25 @@ impl Pallet { return Ok(()); }; - // Fetch avail pool + // Checks before calling stake let avail_pool = FusionPools::::get(AVAIL_POOL_ID).ok_or(Error::::PoolNotFound)?; + let has_avail_membership = + FusionMemberships::::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::::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)?; @@ -2372,101 +2377,6 @@ impl Pallet { } Ok(user_extra_rewards_balance) } - - // FusionApi implementation - pub fn api_pending_rewards( - evm_address: EvmAddress, - pool_id: PoolId, - era: EraIndex, - ) -> Result, DispatchError> { - ensure!( - !ClaimedRewards::::contains_key(era, (pool_id, evm_address)), - Error::::AlreadyClaimed - ); - - let Some(era_rewards) = FusionEraRewards::::get(era, pool_id) else { - return Ok(BalanceOf::::zero()); - }; - - let Some(exposure) = FusionExposures::::get(era, pool_id) else { - return Ok(BalanceOf::::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, - era: Option, - ) -> Result { - let currency = - FusionCurrencies::::get(currency_id).ok_or(Error::::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, - ) -> Result, DispatchError> { - let currency = - FusionCurrencies::::get(currency_id).ok_or(Error::::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 { - let pool = FusionPools::::get(pool_id).ok_or(Error::::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 { - let pool = FusionPools::::get(pool_id).ok_or(Error::::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, - ) -> Result, DispatchError> { - let pool = FusionPools::::get(pool_id).ok_or(Error::::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, - era: Option, - ) -> Result { - let pool = FusionPools::::get(pool_id).ok_or(Error::::PoolNotFound)?; - let points_value = pool.avail_to_points(avail_amount, None, era)?; - - Ok(points_value) - } } impl FusionExt> for Pallet { diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index fd1ab78b4..1a491bf6c 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -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 } @@ -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", diff --git a/runtime/src/apis.rs b/runtime/src/apis.rs index bdd73c93c..09e36a6a3 100644 --- a/runtime/src/apis.rs +++ b/runtime/src/apis.rs @@ -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::{ @@ -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; @@ -572,38 +571,4 @@ impl_runtime_apis! { build_config::(config) } } - - impl pallet_fusion_runtime_api::FusionApi 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, - ) -> 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) -> 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) -> 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) -> Points { - Fusion::api_avail_to_points(pool_id, avail_amount, era).unwrap_or_default() - } - } }