Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 88a1c51
Author: 1xstj <106580853+1xstj@users.noreply.github.com>
Date:   Mon Feb 10 20:02:38 2025 +0530

    [fix] Switch from Percent to Perbill (#914)

    * wip : fix e2e test

    * switch to Perbill for more precision

    * cleanup tests

commit 01fc07f
Author: Alex <69764315+Serial-ATA@users.noreply.github.com>
Date:   Wed Feb 5 15:48:26 2025 -0500

    chore: v1.2.8 (#911)

commit 6626da4
Author: Alex <69764315+Serial-ATA@users.noreply.github.com>
Date:   Wed Feb 5 13:30:24 2025 -0500

    chore: bump subxt to 0.39.0 (#910)

    * chore: bump subxt to 0.39.0

    * chore: bump tangle-subxt to 0.11.0
  • Loading branch information
shekohex committed Feb 10, 2025
1 parent 0fe6a28 commit 0ea64c8
Show file tree
Hide file tree
Showing 20 changed files with 20,254 additions and 19,097 deletions.
9,223 changes: 7,177 additions & 2,046 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "1.2.7"
version = "1.2.8"
authors = ["Tangle Foundation."]
edition = "2021"
license = "Unlicense"
Expand Down Expand Up @@ -381,9 +381,9 @@ xcm = { package = "staging-xcm", git = "https://github.com/paritytech/polkadot-s
polkadot-parachain-primitives = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2407", default-features = false }

# Subxt
subxt = { version = "0.38.0", default-features = false }
subxt-core = { version = "0.38.0", default-features = false }
subxt-signer = { version = "0.38.0", default-features = false }
subxt = { version = "0.39.0", default-features = false }
subxt-core = { version = "0.39.0", default-features = false }
subxt-signer = { version = "0.39.0", default-features = false }

[profile.release]
panic = "unwind"
27 changes: 21 additions & 6 deletions node/tests/evm_restaking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ fn lrt_deposit_withdraw_erc20() {
}

#[test]
fn lrt_rewards() {
fn mad_rewards() {
run_mad_test(|t| async move {
let alice = TestAccount::Alice;
let alice_provider = alloy_provider_with_wallet(&t.provider, alice.evm_wallet());
Expand Down Expand Up @@ -963,17 +963,32 @@ fn lrt_rewards() {
assert!(approve_result.status());
info!("Approved {} WETH for deposit in LRT", format_ether(deposit_amount));

// Deposit WETH to LRT
let lrt = TangleLiquidRestakingVault::new(lrt_address, &bob_provider);
let deposit_result = lrt
.deposit(deposit_amount, bob.address())
// // Deposit WETH to LRT
// let lrt = TangleLiquidRestakingVault::new(lrt_address, &bob_provider);
// let deposit_result = lrt
// .deposit(deposit_amount, bob.address())
// .send()
// .await?
// .with_timeout(Some(Duration::from_secs(5)))
// .get_receipt()
// .await?;
// assert!(deposit_result.status());
// info!("Deposited {} WETH in LRT", format_ether(deposit_amount));

// Delegate assets
let precompile = MultiAssetDelegation::new(MULTI_ASSET_DELEGATION, &bob_provider);
let deposit_amount = U256::from(100_000_000u128);

// Deposit and delegate using asset ID
let deposit_result = precompile
.deposit(U256::from(t.usdc_asset_id), Address::ZERO, U256::from(deposit_amount), 0)
.from(bob.address())
.send()
.await?
.with_timeout(Some(Duration::from_secs(5)))
.get_receipt()
.await?;
assert!(deposit_result.status());
info!("Deposited {} WETH in LRT", format_ether(deposit_amount));

// Wait for one year to pass
wait_for_more_blocks(&t.provider, 51).await;
Expand Down
1 change: 0 additions & 1 deletion pallets/multi-asset-delegation/src/functions/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,6 @@ impl<T: Config> Pallet<T> {
*operator_updates.entry(delegation_key).or_default() += request.amount;
indices_to_remove.push(idx);
}
println!("indices_to_remove: {:?}", indices_to_remove);
ensure!(!indices_to_remove.is_empty(), Error::<T>::BondLessNotReady);
Ok((deposit_updates, delegation_updates, operator_updates, indices_to_remove))
}
Expand Down
8 changes: 4 additions & 4 deletions pallets/rewards/fuzzer/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use crate::runtime::*;
use frame_support::pallet_prelude::*;
use pallet_rewards::{Call as RewardsCall, Config, Error, RewardConfigForAssetVault};
use sp_runtime::{traits::Zero, Percent};
use sp_runtime::{traits::Zero, Perbill};
use tangle_primitives::types::rewards::LockMultiplier;

#[derive(Debug)]
Expand Down Expand Up @@ -126,7 +126,7 @@ impl RewardsCallGenerator {
boost_multiplier: Option<u32>,
) -> RewardsCall<Runtime> {
let config = RewardConfigForAssetVault {
apy: Percent::from_percent(apy.min(100)),
apy: Perbill::from_percent(apy.min(100)),
deposit_cap,
incentive_cap,
boost_multiplier: boost_multiplier.map(|m| m.min(500)), // Cap at 5x
Expand Down Expand Up @@ -155,7 +155,7 @@ impl RewardsCallExecutor {
boost_multiplier: Option<u32>,
) -> DispatchResultWithPostInfo {
let config = RewardConfigForAssetVault {
apy: Percent::from_percent(apy.min(100)),
apy: Perbill::from_percent(apy.min(100)),
deposit_cap,
incentive_cap,
boost_multiplier: boost_multiplier.map(|m| m.min(500)), // Cap at 5x
Expand Down Expand Up @@ -202,7 +202,7 @@ impl RewardsCallVerifier {
) {
// Verify that config was updated by checking storage
if let Some(config) = RewardConfigStorage::<Runtime>::get(vault_id) {
config.apy == Percent::from_percent(apy.min(100))
config.apy == Perbill::from_percent(apy.min(100))
&& config.deposit_cap == deposit_cap
&& config.incentive_cap == incentive_cap
&& config.boost_multiplier == boost_multiplier.map(|m| m.min(500))
Expand Down
4 changes: 2 additions & 2 deletions pallets/rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn setup_vault<T: Config>() -> (T::VaultId, T::AccountId) {

// Setup reward config
let reward_config = RewardConfigForAssetVault {
apy: Percent::from_percent(10),
apy: Perbill::from_percent(10),
deposit_cap: balance,
incentive_cap: balance,
boost_multiplier: Some(150),
Expand Down Expand Up @@ -73,7 +73,7 @@ benchmarks! {
update_vault_reward_config {
let (vault_id, _) = setup_vault::<T>();
let new_config = RewardConfigForAssetVault {
apy: Percent::from_percent(20),
apy: Perbill::from_percent(20),
deposit_cap: BalanceOf::<T>::from(2000u32),
incentive_cap: BalanceOf::<T>::from(2000u32),
boost_multiplier: Some(200),
Expand Down
20 changes: 10 additions & 10 deletions pallets/rewards/src/functions/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::prelude::vec;
use sp_runtime::{
traits::{CheckedMul, Saturating, Zero},
DispatchError, DispatchResult, Percent, SaturatedConversion,
DispatchError, DispatchResult, Perbill, SaturatedConversion,
};
use sp_std::vec::Vec;
use tangle_primitives::{
Expand Down Expand Up @@ -168,7 +168,7 @@ impl<T: Config> Pallet<T> {
/// The goal is to ensure the APY is proportional to the total deposit.
///
/// # Returns
/// * `Ok(Percent)` - The normalized APY
/// * `Ok(Perbill)` - The normalized APY
/// * `Err(DispatchError)` - If any arithmetic operation overflows
///
/// # Arguments
Expand All @@ -178,15 +178,15 @@ impl<T: Config> Pallet<T> {
pub fn calculate_propotional_apy(
total_deposit: BalanceOf<T>,
deposit_cap: BalanceOf<T>,
original_apy: Percent,
) -> Option<Percent> {
original_apy: Perbill,
) -> Option<Perbill> {
if deposit_cap.is_zero() {
return None;
}

log::debug!(target: LOG_TARGET, "calculate_propotional_apy : total_deposit: {:?}, deposit_cap: {:?}, original_apy: {:?}",
total_deposit, deposit_cap, original_apy);
let propotion = Percent::from_rational(total_deposit, deposit_cap);
let propotion = Perbill::from_rational(total_deposit, deposit_cap);
original_apy.checked_mul(&propotion)
}

Expand All @@ -213,20 +213,20 @@ impl<T: Config> Pallet<T> {
fn calculate_decay_factor(
current_block: BlockNumberFor<T>,
last_claim_block: BlockNumberFor<T>,
) -> Percent {
) -> Perbill {
let blocks_since_last_claim = current_block.saturating_sub(last_claim_block);
let start_period = DecayStartPeriod::<T>::get();

// If we haven't reached the decay period yet, no decay
if blocks_since_last_claim <= start_period {
return Percent::from_percent(100);
return Perbill::from_percent(100);
}

let decay_rate = DecayRate::<T>::get();
let decay_percent = 100_u8.saturating_sub(decay_rate.deconstruct());
let decay_percentage = 100_u32.saturating_sub(decay_rate.deconstruct());

// Ensure we don't decay below 90%
Percent::from_percent(decay_percent.max(90))
Perbill::from_percent(decay_percentage.max(90_u32))
}

/// Calculates rewards for deposits considering both unlocked amounts and locked amounts with
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<T: Config> Pallet<T> {

let mut total_rewards_to_be_paid_to_user = BalanceOf::<T>::zero();
for (score, blocks) in user_rewards_score_by_blocks {
let user_proportion = Percent::from_rational(score, total_asset_score);
let user_proportion = Perbill::from_rational(score, total_asset_score);
log::debug!(target: LOG_TARGET, "user_proportion: {:?}", user_proportion);
let user_reward_per_block = user_proportion.mul_floor(total_reward_per_block);

Expand Down
22 changes: 11 additions & 11 deletions pallets/rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! ## Reward Vaults
//!
//! Each vault is identified by a unique `VaultId` and has its own reward configuration:
//! - `apy`: Annual Percentage Yield for the vault
//! - `apy`: Annual Perbillage Yield for the vault
//! - `deposit_cap`: Maximum amount that can be deposited
//! - `incentive_cap`: Maximum amount of incentives that can be distributed
//! - `boost_multiplier`: Optional multiplier to boost rewards
Expand Down Expand Up @@ -90,7 +90,7 @@ pub mod pallet {
PalletId,
};
use frame_system::pallet_prelude::*;
use sp_runtime::{traits::AccountIdConversion, Percent};
use sp_runtime::{traits::AccountIdConversion, Perbill};
use tangle_primitives::rewards::LockMultiplier;

#[pallet::config]
Expand Down Expand Up @@ -216,15 +216,15 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn decay_rate)]
/// Per-block decay rate in basis points (1/10000). e.g., 1 = 0.01% per block
pub type DecayRate<T: Config> = StorageValue<_, Percent, ValueQuery>;
pub type DecayRate<T: Config> = StorageValue<_, Perbill, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Rewards have been claimed by an account
RewardsClaimed { account: T::AccountId, asset: Asset<T::AssetId>, amount: BalanceOf<T> },
/// Event emitted when an incentive APY and cap are set for a reward vault
IncentiveAPYAndCapSet { vault_id: T::VaultId, apy: sp_runtime::Percent, cap: BalanceOf<T> },
IncentiveAPYAndCapSet { vault_id: T::VaultId, apy: sp_runtime::Perbill, cap: BalanceOf<T> },
/// Event emitted when a blueprint is whitelisted for rewards
BlueprintWhitelisted { blueprint_id: BlueprintId },
/// Asset has been updated to reward vault
Expand Down Expand Up @@ -258,7 +258,7 @@ pub mod pallet {
total_deposit: BalanceOf<T>,
},
/// Decay configuration was updated
DecayConfigUpdated { start_period: BlockNumberFor<T>, rate: Percent },
DecayConfigUpdated { start_period: BlockNumberFor<T>, rate: Perbill },
/// The number of blocks for APY calculation has been updated
ApyBlocksUpdated { blocks: BlockNumberFor<T> },
}
Expand Down Expand Up @@ -314,7 +314,7 @@ pub mod pallet {
/// Number of blocks after which decay starts
pub decay_start_period: BlockNumberFor<T>,
/// Per-block decay rate in basis points
pub decay_rate: Percent,
pub decay_rate: Perbill,
}

impl<T: Config> Default for GenesisConfig<T> {
Expand All @@ -325,7 +325,7 @@ pub mod pallet {
// Default to 30 days worth of blocks
decay_start_period: BlockNumberFor::<T>::from(432000u32),
// Default to 1% per block
decay_rate: Percent::from_percent(1),
decay_rate: Perbill::from_percent(1),
}
}
}
Expand Down Expand Up @@ -420,7 +420,7 @@ pub mod pallet {
/// * `origin` - Origin of the call, must pass `ForceOrigin` check
/// * `vault_id` - The ID of the vault to update
/// * `new_config` - The new reward configuration containing:
/// * `apy` - Annual Percentage Yield for the vault
/// * `apy` - Annual Perbillage Yield for the vault
/// * `deposit_cap` - Maximum amount that can be deposited
/// * `incentive_cap` - Maximum amount of incentives that can be distributed
/// * `boost_multiplier` - Optional multiplier to boost rewards
Expand Down Expand Up @@ -464,7 +464,7 @@ pub mod pallet {
/// * `origin` - Origin of the call, must pass `ForceOrigin` check
/// * `vault_id` - The ID of the vault to update
/// * `new_config` - The new reward configuration containing:
/// * `apy` - Annual Percentage Yield for the vault
/// * `apy` - Annual Perbillage Yield for the vault
/// * `deposit_cap` - Maximum amount that can be deposited
/// * `incentive_cap` - Maximum amount of incentives that can be distributed
/// * `boost_multiplier` - Optional multiplier to boost rewards
Expand Down Expand Up @@ -507,12 +507,12 @@ pub mod pallet {
pub fn update_decay_config(
origin: OriginFor<T>,
start_period: BlockNumberFor<T>,
rate: Percent,
rate: Perbill,
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;

// Ensure rate is reasonable (max 10% decay)
ensure!(rate <= Percent::from_percent(10), Error::<T>::InvalidDecayRate);
ensure!(rate <= Perbill::from_percent(10), Error::<T>::InvalidDecayRate);

DecayStartPeriod::<T>::put(start_period);
DecayRate::<T>::put(rate);
Expand Down
2 changes: 1 addition & 1 deletion pallets/rewards/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Tangle. If not, see <http://www.gnu.org/licenses/>.
use crate::{mock::*, types::*, AssetAction, Error, Pallet as RewardsPallet};
use frame_support::assert_err;
use sp_runtime::{DispatchError, Percent};
use sp_runtime::{DispatchError, Perbill};
use tangle_primitives::services::Asset;

pub mod apy_calc;
Expand Down
Loading

0 comments on commit 0ea64c8

Please sign in to comment.