Skip to content

Commit

Permalink
system-staking auto payout (#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
SunTiebing authored Dec 2, 2024
1 parent 1d957bc commit fe53050
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions pallets/system-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ pub mod pallet {
vfree: BalanceOf<T>,
shadow: BalanceOf<T>,
},
/// payout error
PayoutFailed { token: CurrencyIdOf<T> },
}

#[pallet::error]
Expand Down Expand Up @@ -355,6 +357,11 @@ pub mod pallet {
// token_info.current_config.exec_delay ===> true
if round.check_delay(n, token_info.current_config.exec_delay) {
Self::process_token_info(pallet_account.clone(), token_info, i).ok();

if let Err(_) = Self::do_payout(i) {
log::error!("System staking auto payout failed, token: {:?}", i);
Self::deposit_event(Event::PayoutFailed { token: i });
}
}
}
}
Expand Down Expand Up @@ -515,51 +522,7 @@ pub mod pallet {
pub fn payout(origin: OriginFor<T>, token: CurrencyIdOf<T>) -> DispatchResultWithPostInfo {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let token_info = <TokenStatus<T>>::get(&token).ok_or(Error::<T>::TokenInfoNotFound)?;

// token_id convert to vtoken_id
let vtoken_id = token.to_vtoken().map_err(|_| Error::<T>::TokenInfoNotFound)?;

let pallet_account: AccountIdOf<T> = T::PalletId::get().into_account_truncating();

// Calculate the revenue generated by vtoken
let vfree_amount = T::MultiCurrency::free_balance(vtoken_id, &pallet_account);
let free_amount = T::VtokenMintingInterface::get_currency_amount_by_v_currency_amount(
token,
vtoken_id,
vfree_amount,
)?;
let token_amount = free_amount.saturating_sub(token_info.system_shadow_amount);

// Calculate the number of benefits converted to vtoken
let vtoken_amount =
T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount(
token,
vtoken_id,
token_amount,
)?;

// Transfer vtoken(benefits) to BenefitReceivingAccount
T::MultiCurrency::transfer(
vtoken_id,
&pallet_account,
&T::BenefitReceivingAccount::get(),
vtoken_amount,
)
.map_err(|_| Error::<T>::PayoutFailed)?;

Self::deposit_event(Event::Payout {
token,
vtoken: vtoken_id,
from: pallet_account,
to: T::BenefitReceivingAccount::get(),
amount: vtoken_amount,
vfree: vfree_amount,
free: free_amount,
shadow: token_info.system_shadow_amount,
});

Ok(().into())
Self::do_payout(token)
}
}
}
Expand Down Expand Up @@ -683,6 +646,53 @@ impl<T: Config> Pallet<T> {
Ok(().into())
}

fn do_payout(token: CurrencyIdOf<T>) -> DispatchResultWithPostInfo {
let token_info = <TokenStatus<T>>::get(&token).ok_or(Error::<T>::TokenInfoNotFound)?;

// token_id convert to vtoken_id
let vtoken_id = token.to_vtoken().map_err(|_| Error::<T>::TokenInfoNotFound)?;

let pallet_account: AccountIdOf<T> = T::PalletId::get().into_account_truncating();

// Calculate the revenue generated by vtoken
let vfree_amount = T::MultiCurrency::free_balance(vtoken_id, &pallet_account);
let free_amount = T::VtokenMintingInterface::get_currency_amount_by_v_currency_amount(
token,
vtoken_id,
vfree_amount,
)?;
let token_amount = free_amount.saturating_sub(token_info.system_shadow_amount);

// Calculate the number of benefits converted to vtoken
let vtoken_amount = T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount(
token,
vtoken_id,
token_amount,
)?;

// Transfer vtoken(benefits) to BenefitReceivingAccount
T::MultiCurrency::transfer(
vtoken_id,
&pallet_account,
&T::BenefitReceivingAccount::get(),
vtoken_amount,
)
.map_err(|_| Error::<T>::PayoutFailed)?;

Self::deposit_event(Event::Payout {
token,
vtoken: vtoken_id,
from: pallet_account,
to: T::BenefitReceivingAccount::get(),
amount: vtoken_amount,
vfree: vfree_amount,
free: free_amount,
shadow: token_info.system_shadow_amount,
});

Ok(().into())
}

// vTokenMinting on_initialize_update_ledger , update pending_redeem_amount -= token_amount ,
// update system_shadow_amount -= token_amount
pub fn on_redeem_success(
Expand Down

0 comments on commit fe53050

Please sign in to comment.