Skip to content

earning precompile #2776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions modules/earning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sp-std = { workspace = true }

orml-traits = { workspace = true }
primitives = { workspace = true }
module-support = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true, features = ["std"] }
Expand All @@ -32,6 +33,7 @@ std = [
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"module-support/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
196 changes: 137 additions & 59 deletions modules/earning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ use frame_support::{
traits::{Currency, ExistenceRequirement, LockIdentifier, LockableCurrency, OnUnbalanced, WithdrawReasons},
};
use frame_system::pallet_prelude::*;
use module_support::EarningManager;
use orml_traits::{define_parameters, parameters::ParameterStore, Handler};
use primitives::{
bonding::{self, BondingController},
Balance,
};
use sp_runtime::{traits::Saturating, Permill};
use sp_runtime::{
traits::{Saturating, Zero},
DispatchError, Permill,
};

pub use module::*;

Expand Down Expand Up @@ -138,15 +142,8 @@ pub mod module {
pub fn bond(origin: OriginFor<T>, #[pallet::compact] amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

let change = <Self as BondingController>::bond(&who, amount)?;
let _ = Self::do_bond(&who, amount)?;

if let Some(change) = change {
T::OnBonded::handle(&(who.clone(), change.change))?;
Self::deposit_event(Event::Bonded {
who,
amount: change.change,
});
}
Ok(())
}

Expand All @@ -158,16 +155,7 @@ pub mod module {
pub fn unbond(origin: OriginFor<T>, #[pallet::compact] amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

let unbond_at = frame_system::Pallet::<T>::block_number().saturating_add(T::UnbondingPeriod::get());
let change = <Self as BondingController>::unbond(&who, amount, unbond_at)?;

if let Some(change) = change {
T::OnUnbonded::handle(&(who.clone(), change.change))?;
Self::deposit_event(Event::Unbonded {
who,
amount: change.change,
});
}
let _ = Self::do_unbond(&who, amount);

Ok(())
}
Expand All @@ -180,27 +168,7 @@ pub mod module {
pub fn unbond_instant(origin: OriginFor<T>, #[pallet::compact] amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

let fee_ratio = T::ParameterStore::get(InstantUnstakeFee).ok_or(Error::<T>::NotAllowed)?;

let change = <Self as BondingController>::unbond_instant(&who, amount)?;

if let Some(change) = change {
let amount = change.change;
let fee = fee_ratio.mul_ceil(amount);
let final_amount = amount.saturating_sub(fee);

let unbalance =
T::Currency::withdraw(&who, fee, WithdrawReasons::TRANSFER, ExistenceRequirement::KeepAlive)?;
T::OnUnstakeFee::on_unbalanced(unbalance);

// remove all shares of the change amount.
T::OnUnbonded::handle(&(who.clone(), amount))?;
Self::deposit_event(Event::InstantUnbonded {
who,
amount: final_amount,
fee,
});
}
let _ = Self::do_unbond_instant(&who, amount);

Ok(())
}
Expand All @@ -213,15 +181,7 @@ pub mod module {
pub fn rebond(origin: OriginFor<T>, #[pallet::compact] amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

let change = <Self as BondingController>::rebond(&who, amount)?;

if let Some(change) = change {
T::OnBonded::handle(&(who.clone(), change.change))?;
Self::deposit_event(Event::Rebonded {
who,
amount: change.change,
});
}
let _ = Self::do_rebond(&who, amount)?;

Ok(())
}
Expand All @@ -232,22 +192,95 @@ pub mod module {
pub fn withdraw_unbonded(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;

let change =
<Self as BondingController>::withdraw_unbonded(&who, frame_system::Pallet::<T>::block_number())?;

if let Some(change) = change {
Self::deposit_event(Event::Withdrawn {
who,
amount: change.change,
});
}
let _ = Self::do_withdraw_unbonded(&who);

Ok(())
}
}
}

impl<T: Config> Pallet<T> {}
impl<T: Config> Pallet<T> {
fn do_bond(who: &T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
let change = <Self as BondingController>::bond(who, amount)?;

if let Some(ref change) = change {
T::OnBonded::handle(&(who.clone(), change.change))?;
Self::deposit_event(Event::Bonded {
who: who.clone(),
amount: change.change,
});
}
Ok(change.map_or(Zero::zero(), |c| c.change))
}

fn do_unbond(who: &T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
let unbond_at = frame_system::Pallet::<T>::block_number().saturating_add(T::UnbondingPeriod::get());
let change = <Self as BondingController>::unbond(who, amount, unbond_at)?;

if let Some(ref change) = change {
T::OnUnbonded::handle(&(who.clone(), change.change))?;
Self::deposit_event(Event::Unbonded {
who: who.clone(),
amount: change.change,
});
}

Ok(change.map_or(Zero::zero(), |c| c.change))
}

fn do_unbond_instant(who: &T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
let fee_ratio = T::ParameterStore::get(InstantUnstakeFee).ok_or(Error::<T>::NotAllowed)?;

let change = <Self as BondingController>::unbond_instant(who, amount)?;

if let Some(ref change) = change {
let amount = change.change;
let fee = fee_ratio.mul_ceil(amount);
let final_amount = amount.saturating_sub(fee);

let unbalance =
T::Currency::withdraw(who, fee, WithdrawReasons::TRANSFER, ExistenceRequirement::KeepAlive)?;
T::OnUnstakeFee::on_unbalanced(unbalance);

// remove all shares of the change amount.
T::OnUnbonded::handle(&(who.clone(), amount))?;
Self::deposit_event(Event::InstantUnbonded {
who: who.clone(),
amount: final_amount,
fee,
});
}

Ok(change.map_or(Zero::zero(), |c| c.change))
}

fn do_rebond(who: &T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
let change = <Self as BondingController>::rebond(who, amount)?;

if let Some(ref change) = change {
T::OnBonded::handle(&(who.clone(), change.change))?;
Self::deposit_event(Event::Rebonded {
who: who.clone(),
amount: change.change,
});
}

Ok(change.map_or(Zero::zero(), |c| c.change))
}

fn do_withdraw_unbonded(who: &T::AccountId) -> Result<Balance, DispatchError> {
let change = <Self as BondingController>::withdraw_unbonded(who, frame_system::Pallet::<T>::block_number())?;

if let Some(ref change) = change {
Self::deposit_event(Event::Withdrawn {
who: who.clone(),
amount: change.change,
});
}

Ok(change.map_or(Zero::zero(), |c| c.change))
}
}

impl<T: Config> BondingController for Pallet<T> {
type MinBond = T::MinBond;
Expand Down Expand Up @@ -279,3 +312,48 @@ impl<T: Config> BondingController for Pallet<T> {
}
}
}

impl<T: Config> EarningManager<T::AccountId, Balance, BondingLedgerOf<T>> for Pallet<T> {
type Moment = BlockNumberFor<T>;
type FeeRatio = Permill;

fn bond(who: T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
Self::do_bond(&who, amount)
}

fn unbond(who: T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
Self::do_unbond(&who, amount)
}

fn unbond_instant(who: T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
Self::do_unbond_instant(&who, amount)
}

fn rebond(who: T::AccountId, amount: Balance) -> Result<Balance, DispatchError> {
Self::do_rebond(&who, amount)
}

fn withdraw_unbonded(who: T::AccountId) -> Result<Balance, DispatchError> {
Self::do_withdraw_unbonded(&who)
}

fn get_bonding_ledger(who: T::AccountId) -> BondingLedgerOf<T> {
Self::ledger(who).unwrap_or_default()
}

fn get_instant_unstake_fee() -> Option<Permill> {
T::ParameterStore::get(InstantUnstakeFee)
}

fn get_min_bond() -> Balance {
T::MinBond::get()
}

fn get_unbonding_period() -> BlockNumberFor<T> {
T::UnbondingPeriod::get()
}

fn get_max_unbonding_chunks() -> u32 {
T::MaxUnbondingChunks::get()
}
}
34 changes: 34 additions & 0 deletions modules/support/src/earning.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of Acala.

// Copyright (C) 2020-2024 Acala Foundation.
// 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 sp_runtime::DispatchError;

pub trait EarningManager<AccountId, Balance, BondingLedger> {
type Moment;
type FeeRatio;
fn bond(who: AccountId, amount: Balance) -> Result<Balance, DispatchError>;
fn unbond(who: AccountId, amount: Balance) -> Result<Balance, DispatchError>;
fn unbond_instant(who: AccountId, amount: Balance) -> Result<Balance, DispatchError>;
fn rebond(who: AccountId, amount: Balance) -> Result<Balance, DispatchError>;
fn withdraw_unbonded(who: AccountId) -> Result<Balance, DispatchError>;
fn get_bonding_ledger(who: AccountId) -> BondingLedger;
fn get_min_bond() -> Balance;
fn get_unbonding_period() -> Self::Moment;
fn get_max_unbonding_chunks() -> u32;
fn get_instant_unstake_fee() -> Option<Self::FeeRatio>;
}
2 changes: 2 additions & 0 deletions modules/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use xcm::prelude::*;

pub mod bounded;
pub mod dex;
pub mod earning;
pub mod evm;
pub mod homa;
pub mod honzon;
Expand All @@ -42,6 +43,7 @@ pub mod stable_asset;

pub use crate::bounded::*;
pub use crate::dex::*;
pub use crate::earning::*;
pub use crate::evm::*;
pub use crate::homa::*;
pub use crate::honzon::*;
Expand Down
8 changes: 8 additions & 0 deletions primitives/src/bonding/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ where
self.total
}

pub fn unlocking(&self) -> sp_std::vec::Vec<(Balance, Moment)> {
self.unlocking
.iter()
.cloned()
.map(|chunk| (chunk.value, chunk.unlock_at))
.collect()
}

pub fn unlocking_len(&self) -> usize {
self.unlocking.len()
}
Expand Down
4 changes: 4 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module-prices = { workspace = true }
module-transaction-payment = { workspace = true }
module-nft = { workspace = true }
module-dex = { workspace = true }
module-earning = { workspace = true }
module-evm-accounts = { workspace = true }
module-homa = { workspace = true }
module-asset-registry = { workspace = true, optional = true }
Expand All @@ -81,6 +82,7 @@ wasm-bencher = { workspace = true, optional = true }
orml-nft = { workspace = true, optional = true }
orml-currencies = { workspace = true, optional = true }
orml-rewards = { workspace = true, optional = true }
orml-parameters = { workspace = true }

[dev-dependencies]
orml-utilities = { workspace = true, features = ["std"] }
Expand Down Expand Up @@ -128,12 +130,14 @@ std = [
"orml-tokens/std",
"orml-traits/std",
"orml-xtokens/std",
"orml-parameters/std",

"module-asset-registry/std",
"module-cdp-engine/std",
"module-cdp-treasury/std",
"module-currencies/std",
"module-dex/std",
"module-earning/std",
"module-evm-accounts/std",
"module-evm-bridge/std",
"module-evm/std",
Expand Down
Loading
Loading