Skip to content
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

Update ve minting #1408

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pallets/buy-back/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ impl bifrost_ve_minting::Config for Runtime {
type TokenType = VeMintingTokenType;
type VeMintingPalletId = VeMintingPalletId;
type IncentivePalletId = IncentivePalletId;
type BuyBackAccount = BuyBackAccount;
type WeightInfo = ();
type BlockNumberToBalance = ConvertInto;
type Week = Week;
Expand Down
2 changes: 2 additions & 0 deletions pallets/clouds-convert/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ parameter_types! {
pub const VeMintingTokenType: CurrencyId = CurrencyId::VToken(TokenSymbol::BNC);
pub VeMintingPalletId: PalletId = PalletId(*b"bf/vemnt");
pub IncentivePalletId: PalletId = PalletId(*b"bf/veict");
pub const BuyBackAccount: PalletId = PalletId(*b"bf/bybck");
pub const Week: BlockNumber = 50400; // a week
pub const MaxBlock: BlockNumber = 10512000; // four years
pub const Multiplier: Balance = 10_u128.pow(12);
Expand All @@ -198,6 +199,7 @@ impl bifrost_ve_minting::Config for Runtime {
type TokenType = VeMintingTokenType;
type VeMintingPalletId = VeMintingPalletId;
type IncentivePalletId = IncentivePalletId;
type BuyBackAccount = BuyBackAccount;
type WeightInfo = ();
type BlockNumberToBalance = ConvertInto;
type Week = Week;
Expand Down
2 changes: 2 additions & 0 deletions pallets/farming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ parameter_types! {
pub const VeMintingTokenType: CurrencyId = CurrencyId::VToken(TokenSymbol::BNC);
pub VeMintingPalletId: PalletId = PalletId(*b"bf/vemnt");
pub IncentivePalletId: PalletId = PalletId(*b"bf/veict");
pub const BuyBackAccount: PalletId = PalletId(*b"bf/bybck");
pub const Week: BlockNumber = 50400; // a week
pub const MaxBlock: BlockNumber = 10512000; // four years
pub const Multiplier: Balance = 10_u128.pow(12);
Expand All @@ -170,6 +171,7 @@ impl bifrost_ve_minting::Config for Runtime {
type TokenType = VeMintingTokenType;
type VeMintingPalletId = VeMintingPalletId;
type IncentivePalletId = IncentivePalletId;
type BuyBackAccount = BuyBackAccount;
type WeightInfo = ();
type BlockNumberToBalance = ConvertInto;
type Week = Week;
Expand Down
55 changes: 39 additions & 16 deletions pallets/ve-minting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub type CurrencyIdOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<

const VE_LOCK_ID: LockIdentifier = *b"vebnclck";
const MARKUP_LOCK_ID: LockIdentifier = *b"vebncmkp";

const VE_MINTING_SYSTEM_POOL_ID: PoolId = 0;
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, Default)]
pub struct VeConfig<Balance, BlockNumber> {
amount: Balance,
Expand Down Expand Up @@ -114,6 +114,9 @@ pub mod pallet {
#[pallet::constant]
type IncentivePalletId: Get<PalletId>;

#[pallet::constant]
type BuyBackAccount: Get<PalletId>;

/// Convert the block number into a balance.
type BlockNumberToBalance: Convert<BlockNumberFor<Self>, BalanceOf<Self>>;

Expand Down Expand Up @@ -188,6 +191,9 @@ pub mod pallet {
PartiallyRefreshed {
asset_id: CurrencyIdOf<T>,
},
NotifyRewardFailed {
rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
},
}

#[pallet::error]
Expand Down Expand Up @@ -306,17 +312,30 @@ pub mod pallet {
ValueQuery,
>;

// #[pallet::hooks]
// impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
// fn on_initialize(n: BlockNumberFor<T>) -> Weight {
// let conf = Self::incentive_configs();
// if n == conf.last_update_time + conf.rewards_duration {
// Self::notify_reward_amount(&conf.incentive_controller, conf.last_reward.clone())
// .unwrap_or_default();
// }
// T::DbWeight::get().writes(1_u64)
// }
// }
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
let conf = IncentiveConfigs::<T>::get(VE_MINTING_SYSTEM_POOL_ID);
if n == conf.period_finish {
if let Some(e) = Self::notify_reward_amount(
VE_MINTING_SYSTEM_POOL_ID,
&conf.incentive_controller,
conf.last_reward.clone(),
)
.err()
{
log::error!(
target: "ve-minting::notify_reward_amount",
"Received invalid justification for {:?}",
e,
);
Self::deposit_event(Event::NotifyRewardFailed { rewards: conf.last_reward });
}
}

T::DbWeight::get().writes(1_u64)
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -398,15 +417,19 @@ pub mod pallet {
rewards: Vec<(CurrencyIdOf<T>, BalanceOf<T>)>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
Self::set_incentive(0, rewards_duration, Some(incentive_from.clone())); // for pool0
Self::notify_reward_amount(0, &Some(incentive_from), rewards) // for pool0
Self::set_incentive(
VE_MINTING_SYSTEM_POOL_ID,
rewards_duration,
Some(incentive_from.clone()),
);
Self::notify_reward_amount(VE_MINTING_SYSTEM_POOL_ID, &Some(incentive_from), rewards)
}

#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::get_rewards())]
pub fn get_rewards(origin: OriginFor<T>) -> DispatchResult {
let exchanger = ensure_signed(origin)?;
Self::get_rewards_inner(0, &exchanger, None) // for pool0
Self::get_rewards_inner(VE_MINTING_SYSTEM_POOL_ID, &exchanger, None)
}

#[pallet::call_index(7)]
Expand Down Expand Up @@ -1079,7 +1102,7 @@ pub mod pallet {
T::MultiCurrency::transfer(
T::TokenType::get(),
who,
&T::VeMintingPalletId::get().into_account_truncating(),
&T::BuyBackAccount::get().into_account_truncating(),
fast.checked_mul_int(value).ok_or(ArithmeticError::Overflow)?,
)?;
}
Expand Down
2 changes: 2 additions & 0 deletions pallets/ve-minting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ parameter_types! {
pub const VeMintingTokenType: CurrencyId = CurrencyId::VToken(TokenSymbol::BNC);
pub VeMintingPalletId: PalletId = PalletId(*b"bf/vemnt");
pub IncentivePalletId: PalletId = PalletId(*b"bf/veict");
pub const BuyBackAccount: PalletId = PalletId(*b"bf/bybck");
pub const Week: BlockNumber = 50400; // a week
pub const MaxBlock: BlockNumber = 10512000; // four years
pub const Multiplier: Balance = 10_u128.pow(12);
Expand All @@ -261,6 +262,7 @@ impl bifrost_ve_minting::Config for Runtime {
type TokenType = VeMintingTokenType;
type VeMintingPalletId = VeMintingPalletId;
type IncentivePalletId = IncentivePalletId;
type BuyBackAccount = BuyBackAccount;
type WeightInfo = ();
type BlockNumberToBalance = ConvertInto;
type Week = Week;
Expand Down
1 change: 1 addition & 0 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,7 @@ impl bifrost_ve_minting::Config for Runtime {
type TokenType = VeMintingTokenType;
type VeMintingPalletId = VeMintingPalletId;
type IncentivePalletId = IncentivePalletId;
type BuyBackAccount = BuyBackAccount;
type WeightInfo = weights::bifrost_ve_minting::BifrostWeight<Runtime>;
type BlockNumberToBalance = ConvertInto;
type Week = Week;
Expand Down