mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-16 21:21:03 +00:00
NIS should retain funds in reserve (#12928)
* Keep funds with receipt holder * Counterpart is optional * Use named reserves * Tests * Benchmarks * Fixes * Update frame/nis/src/lib.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * Update frame/nis/src/lib.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Update frame/nis/src/tests.rs * Update frame/nis/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/nis/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/nis/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Formatting Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
+263
-49
@@ -169,7 +169,7 @@ pub mod pallet {
|
||||
nonfungible::{Inspect as NonfungibleInspect, Transfer as NonfungibleTransfer},
|
||||
Currency, Defensive, DefensiveSaturating,
|
||||
ExistenceRequirement::AllowDeath,
|
||||
OnUnbalanced, ReservableCurrency,
|
||||
NamedReservableCurrency, OnUnbalanced,
|
||||
},
|
||||
PalletId,
|
||||
};
|
||||
@@ -189,9 +189,10 @@ pub mod pallet {
|
||||
type ReceiptRecordOf<T> = ReceiptRecord<
|
||||
<T as frame_system::Config>::AccountId,
|
||||
<T as frame_system::Config>::BlockNumber,
|
||||
BalanceOf<T>,
|
||||
>;
|
||||
type IssuanceInfoOf<T> = IssuanceInfo<BalanceOf<T>>;
|
||||
type SummaryRecordOf<T> = SummaryRecord<<T as frame_system::Config>::BlockNumber>;
|
||||
type SummaryRecordOf<T> = SummaryRecord<<T as frame_system::Config>::BlockNumber, BalanceOf<T>>;
|
||||
type BidOf<T> = Bid<BalanceOf<T>, <T as frame_system::Config>::AccountId>;
|
||||
type QueueTotalsTypeOf<T> = BoundedVec<(u32, BalanceOf<T>), <T as Config>::QueueCount>;
|
||||
|
||||
@@ -208,7 +209,7 @@ pub mod pallet {
|
||||
type PalletId: Get<PalletId>;
|
||||
|
||||
/// Currency type that this works on.
|
||||
type Currency: ReservableCurrency<Self::AccountId, Balance = Self::CurrencyBalance>;
|
||||
type Currency: NamedReservableCurrency<Self::AccountId, Balance = Self::CurrencyBalance>;
|
||||
|
||||
/// Just the `Currency::Balance` type; we have this item to allow us to constrain it to
|
||||
/// `From<u64>`.
|
||||
@@ -300,6 +301,12 @@ pub mod pallet {
|
||||
/// The maximum proportion which may be thawed and the period over which it is reset.
|
||||
#[pallet::constant]
|
||||
type ThawThrottle: Get<(Perquintill, Self::BlockNumber)>;
|
||||
|
||||
/// The name for the reserve ID.
|
||||
#[pallet::constant]
|
||||
type ReserveId: Get<
|
||||
<Self::Currency as NamedReservableCurrency<Self::AccountId>>::ReserveIdentifier,
|
||||
>;
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
@@ -321,11 +328,13 @@ pub mod pallet {
|
||||
#[derive(
|
||||
Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct ReceiptRecord<AccountId, BlockNumber> {
|
||||
pub struct ReceiptRecord<AccountId, BlockNumber, Balance> {
|
||||
/// The proportion of the effective total issuance.
|
||||
pub proportion: Perquintill,
|
||||
/// The account to whom this receipt belongs.
|
||||
pub who: AccountId,
|
||||
/// The account to whom this receipt belongs and the amount of funds on hold in their
|
||||
/// account for servicing this receipt. If `None`, then it is a communal receipt and
|
||||
/// fungible counterparts have been issued.
|
||||
pub owner: Option<(AccountId, Balance)>,
|
||||
/// The time after which this receipt can be thawed.
|
||||
pub expiry: BlockNumber,
|
||||
}
|
||||
@@ -344,7 +353,7 @@ pub mod pallet {
|
||||
#[derive(
|
||||
Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen,
|
||||
)]
|
||||
pub struct SummaryRecord<BlockNumber> {
|
||||
pub struct SummaryRecord<BlockNumber, Balance> {
|
||||
/// The total proportion over all outstanding receipts.
|
||||
pub proportion_owed: Perquintill,
|
||||
/// The total number of receipts created so far.
|
||||
@@ -353,6 +362,9 @@ pub mod pallet {
|
||||
pub thawed: Perquintill,
|
||||
/// The current thaw period's beginning.
|
||||
pub last_period: BlockNumber,
|
||||
/// The total amount of funds on hold for receipts. This doesn't include the pot or funds
|
||||
/// on hold for bids.
|
||||
pub receipts_on_hold: Balance,
|
||||
}
|
||||
|
||||
pub struct OnEmptyQueueTotals<T>(sp_std::marker::PhantomData<T>);
|
||||
@@ -440,24 +452,28 @@ pub mod pallet {
|
||||
/// The queue for the bid's duration is full and the amount bid is too low to get in
|
||||
/// through replacing an existing bid.
|
||||
BidTooLow,
|
||||
/// Bond index is unknown.
|
||||
Unknown,
|
||||
/// Receipt index is unknown.
|
||||
UnknownReceipt,
|
||||
/// Not the owner of the receipt.
|
||||
NotOwner,
|
||||
/// Bond not yet at expiry date.
|
||||
NotExpired,
|
||||
/// The given bid for retraction is not found.
|
||||
NotFound,
|
||||
UnknownBid,
|
||||
/// The portion supplied is beyond the value of the receipt.
|
||||
TooMuch,
|
||||
PortionTooBig,
|
||||
/// Not enough funds are held to pay out.
|
||||
Unfunded,
|
||||
/// There are enough funds for what is required.
|
||||
Funded,
|
||||
AlreadyFunded,
|
||||
/// The thaw throttle has been reached for this period.
|
||||
Throttled,
|
||||
/// The operation would result in a receipt worth an insignficant value.
|
||||
MakesDust,
|
||||
/// The receipt is already communal.
|
||||
AlreadyCommunal,
|
||||
/// The receipt is already private.
|
||||
AlreadyPrivate,
|
||||
}
|
||||
|
||||
pub(crate) struct WeightCounter {
|
||||
@@ -539,13 +555,17 @@ pub mod pallet {
|
||||
|q| -> Result<(u32, BalanceOf<T>), DispatchError> {
|
||||
let queue_full = q.len() == T::MaxQueueLen::get() as usize;
|
||||
ensure!(!queue_full || q[0].amount < amount, Error::<T>::BidTooLow);
|
||||
T::Currency::reserve(&who, amount)?;
|
||||
T::Currency::reserve_named(&T::ReserveId::get(), &who, amount)?;
|
||||
|
||||
// queue is <Ordered: Lowest ... Highest><Fifo: Last ... First>
|
||||
let mut bid = Bid { amount, who: who.clone() };
|
||||
let net = if queue_full {
|
||||
sp_std::mem::swap(&mut q[0], &mut bid);
|
||||
let _ = T::Currency::unreserve(&bid.who, bid.amount);
|
||||
let _ = T::Currency::unreserve_named(
|
||||
&T::ReserveId::get(),
|
||||
&bid.who,
|
||||
bid.amount,
|
||||
);
|
||||
Self::deposit_event(Event::<T>::BidDropped {
|
||||
who: bid.who,
|
||||
amount: bid.amount,
|
||||
@@ -597,7 +617,7 @@ pub mod pallet {
|
||||
|
||||
let bid = Bid { amount, who };
|
||||
let new_len = Queues::<T>::try_mutate(duration, |q| -> Result<u32, DispatchError> {
|
||||
let pos = q.iter().position(|i| i == &bid).ok_or(Error::<T>::NotFound)?;
|
||||
let pos = q.iter().position(|i| i == &bid).ok_or(Error::<T>::UnknownBid)?;
|
||||
q.remove(pos);
|
||||
Ok(q.len() as u32)
|
||||
})?;
|
||||
@@ -608,7 +628,7 @@ pub mod pallet {
|
||||
qs[queue_index].1.saturating_reduce(bid.amount);
|
||||
});
|
||||
|
||||
T::Currency::unreserve(&bid.who, bid.amount);
|
||||
T::Currency::unreserve_named(&T::ReserveId::get(), &bid.who, bid.amount);
|
||||
Self::deposit_event(Event::BidRetracted { who: bid.who, amount: bid.amount, duration });
|
||||
|
||||
Ok(())
|
||||
@@ -625,7 +645,7 @@ pub mod pallet {
|
||||
let our_account = Self::account_id();
|
||||
let issuance = Self::issuance_with(&our_account, &summary);
|
||||
let deficit = issuance.required.saturating_sub(issuance.holdings);
|
||||
ensure!(!deficit.is_zero(), Error::<T>::Funded);
|
||||
ensure!(!deficit.is_zero(), Error::<T>::AlreadyFunded);
|
||||
T::Deficit::on_unbalanced(T::Currency::deposit_creating(&our_account, deficit));
|
||||
Self::deposit_event(Event::<T>::Funded { deficit });
|
||||
Ok(())
|
||||
@@ -640,27 +660,28 @@ pub mod pallet {
|
||||
/// - `portion`: If `Some`, then only the given portion of the receipt should be thawed. If
|
||||
/// `None`, then all of it should be.
|
||||
#[pallet::call_index(3)]
|
||||
#[pallet::weight(T::WeightInfo::thaw())]
|
||||
pub fn thaw(
|
||||
#[pallet::weight(T::WeightInfo::thaw_private())]
|
||||
pub fn thaw_private(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] index: ReceiptIndex,
|
||||
portion: Option<<T::Counterpart as FungibleInspect<T::AccountId>>::Balance>,
|
||||
maybe_proportion: Option<Perquintill>,
|
||||
) -> DispatchResult {
|
||||
let who = ensure_signed(origin)?;
|
||||
|
||||
// Look for `index`
|
||||
let mut receipt: ReceiptRecordOf<T> =
|
||||
Receipts::<T>::get(index).ok_or(Error::<T>::Unknown)?;
|
||||
Receipts::<T>::get(index).ok_or(Error::<T>::UnknownReceipt)?;
|
||||
// If found, check the owner is `who`.
|
||||
ensure!(receipt.who == who, Error::<T>::NotOwner);
|
||||
let (owner, mut on_hold) = receipt.owner.ok_or(Error::<T>::AlreadyCommunal)?;
|
||||
ensure!(owner == who, Error::<T>::NotOwner);
|
||||
|
||||
let now = frame_system::Pallet::<T>::block_number();
|
||||
ensure!(now >= receipt.expiry, Error::<T>::NotExpired);
|
||||
|
||||
let mut summary: SummaryRecordOf<T> = Summary::<T>::get();
|
||||
|
||||
let proportion = if let Some(counterpart) = portion {
|
||||
let proportion = T::CounterpartAmount::convert_back(counterpart);
|
||||
ensure!(proportion <= receipt.proportion, Error::<T>::TooMuch);
|
||||
let proportion = if let Some(proportion) = maybe_proportion {
|
||||
ensure!(proportion <= receipt.proportion, Error::<T>::PortionTooBig);
|
||||
let remaining = receipt.proportion.saturating_sub(proportion);
|
||||
ensure!(
|
||||
remaining.is_zero() || remaining >= T::MinReceipt::get(),
|
||||
@@ -679,8 +700,6 @@ pub mod pallet {
|
||||
summary.thawed.saturating_accrue(proportion);
|
||||
ensure!(summary.thawed <= throttle, Error::<T>::Throttled);
|
||||
|
||||
T::Counterpart::burn_from(&who, T::CounterpartAmount::convert(proportion))?;
|
||||
|
||||
// Multiply the proportion it is by the total issued.
|
||||
let our_account = Self::account_id();
|
||||
let effective_issuance = Self::issuance_with(&our_account, &summary).effective;
|
||||
@@ -689,13 +708,55 @@ pub mod pallet {
|
||||
receipt.proportion.saturating_reduce(proportion);
|
||||
summary.proportion_owed.saturating_reduce(proportion);
|
||||
|
||||
T::Currency::transfer(&our_account, &who, amount, AllowDeath)
|
||||
.map_err(|_| Error::<T>::Unfunded)?;
|
||||
|
||||
let dropped = receipt.proportion.is_zero();
|
||||
|
||||
if amount > on_hold {
|
||||
T::Currency::unreserve_named(&T::ReserveId::get(), &who, on_hold);
|
||||
let deficit = amount - on_hold;
|
||||
// Try to transfer deficit from pot to receipt owner.
|
||||
summary.receipts_on_hold.saturating_reduce(on_hold);
|
||||
on_hold = Zero::zero();
|
||||
T::Currency::transfer(&our_account, &who, deficit, AllowDeath)
|
||||
.map_err(|_| Error::<T>::Unfunded)?;
|
||||
} else {
|
||||
T::Currency::unreserve_named(&T::ReserveId::get(), &who, amount);
|
||||
on_hold.saturating_reduce(amount);
|
||||
summary.receipts_on_hold.saturating_reduce(amount);
|
||||
if dropped && !on_hold.is_zero() {
|
||||
// Reclaim any remainder:
|
||||
// Transfer `excess` to the pot if we have now fully compensated for the
|
||||
// receipt.
|
||||
//
|
||||
// This will legitimately fail if there is no pot account in existance.
|
||||
// There's nothing we can do about this so we just swallow the error.
|
||||
// This code is not ideal and could fail in the second phase leaving
|
||||
// the system in an invalid state. It can be fixed properly with the
|
||||
// new API in https://github.com/paritytech/substrate/pull/12951
|
||||
//
|
||||
// Below is what it should look like then:
|
||||
// let _ = T::Currency::repatriate_reserved_named(
|
||||
// &T::ReserveId::get(),
|
||||
// &who,
|
||||
// &our_account,
|
||||
// excess,
|
||||
// BalanceStatus::Free,
|
||||
// ).defensive();
|
||||
T::Currency::unreserve_named(&T::ReserveId::get(), &who, on_hold);
|
||||
// It could theoretically be locked, so really we should be using a more
|
||||
// forceful variant. But the alternative `repatriate_reserved_named` will
|
||||
// fail if the destination account doesn't exist. This should be fixed when
|
||||
// we move to the `fungible::*` traits, which should include a force
|
||||
// transfer function to transfer the reserved balance into free balance in
|
||||
// the destination regardless of locks and create it if it doesn't exist.
|
||||
let _ = T::Currency::transfer(&who, &Self::account_id(), on_hold, AllowDeath);
|
||||
summary.receipts_on_hold.saturating_reduce(on_hold);
|
||||
}
|
||||
}
|
||||
|
||||
if dropped {
|
||||
Receipts::<T>::remove(index);
|
||||
} else {
|
||||
receipt.owner = Some((owner, on_hold));
|
||||
Receipts::<T>::insert(index, &receipt);
|
||||
}
|
||||
Summary::<T>::put(&summary);
|
||||
@@ -704,20 +765,159 @@ pub mod pallet {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reduce or remove an outstanding receipt, placing the according proportion of funds into
|
||||
/// the account of the owner.
|
||||
///
|
||||
/// - `origin`: Must be Signed and the account must be the owner of the fungible counterpart
|
||||
/// for receipt `index`.
|
||||
/// - `index`: The index of the receipt.
|
||||
#[pallet::call_index(4)]
|
||||
#[pallet::weight(T::WeightInfo::thaw_communal())]
|
||||
pub fn thaw_communal(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] index: ReceiptIndex,
|
||||
) -> DispatchResult {
|
||||
let who = ensure_signed(origin)?;
|
||||
|
||||
// Look for `index`
|
||||
let receipt: ReceiptRecordOf<T> =
|
||||
Receipts::<T>::get(index).ok_or(Error::<T>::UnknownReceipt)?;
|
||||
// If found, check it is actually communal.
|
||||
ensure!(receipt.owner.is_none(), Error::<T>::NotOwner);
|
||||
let now = frame_system::Pallet::<T>::block_number();
|
||||
ensure!(now >= receipt.expiry, Error::<T>::NotExpired);
|
||||
|
||||
let mut summary: SummaryRecordOf<T> = Summary::<T>::get();
|
||||
|
||||
let (throttle, throttle_period) = T::ThawThrottle::get();
|
||||
if now.saturating_sub(summary.last_period) >= throttle_period {
|
||||
summary.thawed = Zero::zero();
|
||||
summary.last_period = now;
|
||||
}
|
||||
summary.thawed.saturating_accrue(receipt.proportion);
|
||||
ensure!(summary.thawed <= throttle, Error::<T>::Throttled);
|
||||
|
||||
T::Counterpart::burn_from(&who, T::CounterpartAmount::convert(receipt.proportion))?;
|
||||
|
||||
// Multiply the proportion it is by the total issued.
|
||||
let our_account = Self::account_id();
|
||||
let effective_issuance = Self::issuance_with(&our_account, &summary).effective;
|
||||
let amount = receipt.proportion * effective_issuance;
|
||||
|
||||
summary.proportion_owed.saturating_reduce(receipt.proportion);
|
||||
|
||||
// Try to transfer amount owed from pot to receipt owner.
|
||||
T::Currency::transfer(&our_account, &who, amount, AllowDeath)
|
||||
.map_err(|_| Error::<T>::Unfunded)?;
|
||||
|
||||
Receipts::<T>::remove(index);
|
||||
Summary::<T>::put(&summary);
|
||||
|
||||
let e =
|
||||
Event::Thawed { index, who, amount, proportion: receipt.proportion, dropped: true };
|
||||
Self::deposit_event(e);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Make a private receipt communal and create fungible counterparts for its owner.
|
||||
#[pallet::call_index(5)]
|
||||
#[pallet::weight(T::WeightInfo::communify())]
|
||||
pub fn communify(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] index: ReceiptIndex,
|
||||
) -> DispatchResult {
|
||||
let who = ensure_signed(origin)?;
|
||||
|
||||
// Look for `index`
|
||||
let mut receipt: ReceiptRecordOf<T> =
|
||||
Receipts::<T>::get(index).ok_or(Error::<T>::UnknownReceipt)?;
|
||||
|
||||
// Check it's not already communal and make it so.
|
||||
let (owner, on_hold) = receipt.owner.take().ok_or(Error::<T>::AlreadyCommunal)?;
|
||||
|
||||
// If found, check the owner is `who`.
|
||||
ensure!(owner == who, Error::<T>::NotOwner);
|
||||
|
||||
// Unreserve and transfer the funds to the pot.
|
||||
T::Currency::unreserve_named(&T::ReserveId::get(), &who, on_hold);
|
||||
// Transfer `excess` to the pot if we have now fully compensated for the receipt.
|
||||
T::Currency::transfer(&who, &Self::account_id(), on_hold, AllowDeath)
|
||||
.map_err(|_| Error::<T>::Unfunded)?;
|
||||
// TODO #12951: ^^^ The above should be done in a single operation `transfer_on_hold`.
|
||||
|
||||
// Record that we've moved the amount reserved.
|
||||
let mut summary: SummaryRecordOf<T> = Summary::<T>::get();
|
||||
summary.receipts_on_hold.saturating_reduce(on_hold);
|
||||
Summary::<T>::put(&summary);
|
||||
Receipts::<T>::insert(index, &receipt);
|
||||
|
||||
// Mint fungibles.
|
||||
let fung_eq = T::CounterpartAmount::convert(receipt.proportion);
|
||||
let _ = T::Counterpart::mint_into(&who, fung_eq).defensive();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Make a communal receipt private and burn fungible counterparts from its owner.
|
||||
#[pallet::call_index(6)]
|
||||
#[pallet::weight(T::WeightInfo::privatize())]
|
||||
pub fn privatize(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] index: ReceiptIndex,
|
||||
) -> DispatchResult {
|
||||
let who = ensure_signed(origin)?;
|
||||
|
||||
// Look for `index`
|
||||
let mut receipt: ReceiptRecordOf<T> =
|
||||
Receipts::<T>::get(index).ok_or(Error::<T>::UnknownReceipt)?;
|
||||
|
||||
// If found, check there is no owner.
|
||||
ensure!(receipt.owner.is_none(), Error::<T>::AlreadyPrivate);
|
||||
|
||||
// Multiply the proportion it is by the total issued.
|
||||
let mut summary: SummaryRecordOf<T> = Summary::<T>::get();
|
||||
let our_account = Self::account_id();
|
||||
let effective_issuance = Self::issuance_with(&our_account, &summary).effective;
|
||||
let max_amount = receipt.proportion * effective_issuance;
|
||||
// Avoid trying to place more in the account's reserve than we have available in the pot
|
||||
let amount = max_amount.min(T::Currency::free_balance(&our_account));
|
||||
|
||||
// Burn fungible counterparts.
|
||||
T::Counterpart::burn_from(&who, T::CounterpartAmount::convert(receipt.proportion))?;
|
||||
|
||||
// Transfer the funds from the pot to the owner and reserve
|
||||
T::Currency::transfer(&Self::account_id(), &who, amount, AllowDeath)
|
||||
.map_err(|_| Error::<T>::Unfunded)?;
|
||||
T::Currency::reserve_named(&T::ReserveId::get(), &who, amount)?;
|
||||
// TODO: ^^^ The above should be done in a single operation `transfer_and_hold`.
|
||||
|
||||
// Record that we've moved the amount reserved.
|
||||
summary.receipts_on_hold.saturating_accrue(amount);
|
||||
|
||||
receipt.owner = Some((who, amount));
|
||||
|
||||
Summary::<T>::put(&summary);
|
||||
Receipts::<T>::insert(index, &receipt);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Issuance information returned by `issuance()`.
|
||||
#[derive(RuntimeDebug)]
|
||||
#[derive(Debug)]
|
||||
pub struct IssuanceInfo<Balance> {
|
||||
/// The balance held in reserve by this pallet instance.
|
||||
/// The balance held by this pallet instance together with the balances on hold across
|
||||
/// all receipt-owning accounts.
|
||||
pub holdings: Balance,
|
||||
/// The (non-ignored) issuance in the system, not including this pallet's account.
|
||||
pub other: Balance,
|
||||
/// The effective total issuance, hypothetically if all outstanding receipts were thawed at
|
||||
/// present.
|
||||
pub effective: Balance,
|
||||
/// The amount needed to be the pallet instance's account in case all outstanding receipts
|
||||
/// were thawed at present.
|
||||
/// The amount needed to be accessible to this pallet in case all outstanding receipts were
|
||||
/// thawed at present. If it is more than `holdings`, then the pallet will need funding.
|
||||
pub required: Balance,
|
||||
}
|
||||
|
||||
@@ -725,7 +925,7 @@ pub mod pallet {
|
||||
type ItemId = ReceiptIndex;
|
||||
|
||||
fn owner(item: &ReceiptIndex) -> Option<T::AccountId> {
|
||||
Receipts::<T>::get(item).map(|r| r.who)
|
||||
Receipts::<T>::get(item).and_then(|r| r.owner).map(|(who, _)| who)
|
||||
}
|
||||
|
||||
fn attribute(item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
|
||||
@@ -733,6 +933,8 @@ pub mod pallet {
|
||||
match key {
|
||||
b"proportion" => Some(item.proportion.encode()),
|
||||
b"expiry" => Some(item.expiry.encode()),
|
||||
b"owner" => item.owner.as_ref().map(|x| x.0.encode()),
|
||||
b"on_hold" => item.owner.as_ref().map(|x| x.1.encode()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -741,12 +943,28 @@ pub mod pallet {
|
||||
impl<T: Config> NonfungibleTransfer<T::AccountId> for Pallet<T> {
|
||||
fn transfer(index: &ReceiptIndex, destination: &T::AccountId) -> DispatchResult {
|
||||
let mut item = Receipts::<T>::get(index).ok_or(TokenError::UnknownAsset)?;
|
||||
let from = item.who;
|
||||
item.who = destination.clone();
|
||||
let (owner, on_hold) = item.owner.take().ok_or(Error::<T>::AlreadyCommunal)?;
|
||||
|
||||
// TODO: This should all be replaced by a single call `transfer_held`.
|
||||
let shortfall = T::Currency::unreserve_named(&T::ReserveId::get(), &owner, on_hold);
|
||||
if !shortfall.is_zero() {
|
||||
let _ =
|
||||
T::Currency::reserve_named(&T::ReserveId::get(), &owner, on_hold - shortfall);
|
||||
return Err(TokenError::NoFunds.into())
|
||||
}
|
||||
if let Err(e) = T::Currency::transfer(&owner, destination, on_hold, AllowDeath) {
|
||||
let _ = T::Currency::reserve_named(&T::ReserveId::get(), &owner, on_hold);
|
||||
return Err(e)
|
||||
}
|
||||
// This can never fail, and if it somehow does, then we can't handle this gracefully.
|
||||
let _ =
|
||||
T::Currency::reserve_named(&T::ReserveId::get(), destination, on_hold).defensive();
|
||||
|
||||
item.owner = Some((destination.clone(), on_hold));
|
||||
Receipts::<T>::insert(&index, &item);
|
||||
Pallet::<T>::deposit_event(Event::<T>::Transferred {
|
||||
from,
|
||||
to: item.who,
|
||||
from: owner,
|
||||
to: destination.clone(),
|
||||
index: *index,
|
||||
});
|
||||
Ok(())
|
||||
@@ -781,7 +999,8 @@ pub mod pallet {
|
||||
) -> IssuanceInfo<BalanceOf<T>> {
|
||||
let total_issuance =
|
||||
T::Currency::total_issuance().saturating_sub(T::IgnoredIssuance::get());
|
||||
let holdings = T::Currency::free_balance(our_account);
|
||||
let holdings =
|
||||
T::Currency::free_balance(our_account).saturating_add(summary.receipts_on_hold);
|
||||
let other = total_issuance.saturating_sub(holdings);
|
||||
let effective =
|
||||
summary.proportion_owed.left_from_one().saturating_reciprocal_mul(other);
|
||||
@@ -893,7 +1112,7 @@ pub mod pallet {
|
||||
pub(crate) fn process_bid(
|
||||
mut bid: BidOf<T>,
|
||||
expiry: T::BlockNumber,
|
||||
our_account: &T::AccountId,
|
||||
_our_account: &T::AccountId,
|
||||
issuance: &IssuanceInfo<BalanceOf<T>>,
|
||||
remaining: &mut BalanceOf<T>,
|
||||
queue_amount: &mut BalanceOf<T>,
|
||||
@@ -906,10 +1125,8 @@ pub mod pallet {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let amount = bid.amount.saturating_sub(T::Currency::unreserve(&bid.who, bid.amount));
|
||||
if T::Currency::transfer(&bid.who, &our_account, amount, AllowDeath).is_err() {
|
||||
return result
|
||||
}
|
||||
let amount = bid.amount;
|
||||
summary.receipts_on_hold.saturating_accrue(amount);
|
||||
|
||||
// Can never overflow due to block above.
|
||||
remaining.saturating_reduce(amount);
|
||||
@@ -928,12 +1145,9 @@ pub mod pallet {
|
||||
|
||||
let e = Event::Issued { index, expiry, who: who.clone(), amount, proportion };
|
||||
Self::deposit_event(e);
|
||||
let receipt = ReceiptRecord { proportion, who: who.clone(), expiry };
|
||||
let receipt = ReceiptRecord { proportion, owner: Some((who, amount)), expiry };
|
||||
Receipts::<T>::insert(index, receipt);
|
||||
|
||||
// issue the fungible counterpart
|
||||
let fung_eq = T::CounterpartAmount::convert(proportion);
|
||||
let _ = T::Counterpart::mint_into(&who, fung_eq).defensive();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user