Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Non-Interactive Staking #12610

Merged
merged 57 commits into from
Dec 5, 2022
Merged
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
d2a1c7b
Improve naming.
gavofyork Nov 2, 2022
a1748df
More improvements to naming
gavofyork Nov 2, 2022
8394666
Fungible counterpart
gavofyork Nov 2, 2022
c81e4a4
Shared pot instead of reserve
gavofyork Nov 2, 2022
9a78199
Transferable receipts
gavofyork Nov 2, 2022
556aee9
Better naming
gavofyork Nov 2, 2022
326bed5
Use u128 for counterpart
gavofyork Nov 3, 2022
fb9857f
Partial thawing
gavofyork Nov 3, 2022
b232a35
Docs
gavofyork Nov 3, 2022
66fa13b
Remove AdminOrigin
gavofyork Nov 3, 2022
3855ed0
Integrate into Kitchen Sink
gavofyork Nov 3, 2022
ed21e76
Thaw throttling
gavofyork Nov 3, 2022
c2fd94e
Remove todo
gavofyork Nov 3, 2022
ec6b594
Docs
gavofyork Nov 3, 2022
1442179
Fix benchmarks
gavofyork Nov 3, 2022
86ceb01
Building
gavofyork Nov 4, 2022
54609be
Tests work
gavofyork Nov 4, 2022
085996b
New benchmarks
gavofyork Nov 5, 2022
077afbe
Benchmarking tests
gavofyork Nov 5, 2022
3ca6918
Test new defensive_saturating_* functions
ggwpez Nov 5, 2022
25d80d3
fmt
ggwpez Nov 5, 2022
9ea6681
Formatting
gavofyork Nov 6, 2022
be66334
Update frame/nis/src/lib.rs
gavofyork Nov 6, 2022
471c83b
Apply suggestions from code review
gavofyork Nov 6, 2022
c668161
Merge branch 'gav-ref-nis' of github.com:paritytech/substrate into ga…
gavofyork Nov 6, 2022
a44dc75
Events added
gavofyork Nov 6, 2022
f89b093
Fix kitchensink
gavofyork Nov 6, 2022
f5a3e3d
Update frame/nis/src/lib.rs
gavofyork Nov 8, 2022
1474a75
Review niggles
gavofyork Nov 10, 2022
376132d
Merge branch 'gav-ref-nis' of github.com:paritytech/substrate into ga…
gavofyork Nov 11, 2022
bafbdff
Merge remote-tracking branch 'origin/master' into gav-ref-nis
gavofyork Nov 11, 2022
ef9a239
Remove genesis build requirements
gavofyork Nov 11, 2022
207a4b5
Grumbles
gavofyork Nov 14, 2022
e395e84
Merge remote-tracking branch 'origin/master' into gav-ref-nis
gavofyork Nov 14, 2022
6a85012
Fixes
gavofyork Nov 14, 2022
d11f04e
Fixes
gavofyork Nov 14, 2022
8b47006
Fixes
gavofyork Nov 14, 2022
2a59d93
Update frame/nis/src/lib.rs
gavofyork Nov 22, 2022
cd45d8a
Update primitives/runtime/src/traits.rs
gavofyork Nov 22, 2022
d201b05
Merge remote-tracking branch 'origin/master' into gav-ref-nis
gavofyork Nov 22, 2022
990f280
Formatting
gavofyork Nov 22, 2022
15bd0ac
Fixes
gavofyork Nov 23, 2022
9578bf0
Fix node genesis config
ggwpez Nov 23, 2022
ecf4bc1
Fix node chain specs
ggwpez Nov 23, 2022
0a57aed
Use free asset ID as counterpart
ggwpez Nov 23, 2022
6e13e91
Account for rounding errors in fund_deficit bench
ggwpez Nov 23, 2022
45c58b4
clippy
ggwpez Nov 23, 2022
e4499b4
fmt
ggwpez Nov 23, 2022
335ca07
Fix
gavofyork Nov 27, 2022
314728f
Rename
gavofyork Nov 27, 2022
ab31838
Merge branch 'gav-ref-nis' of github.com:paritytech/substrate into ga…
gavofyork Nov 27, 2022
57402c0
Merge remote-tracking branch 'origin/master' into gav-ref-nis
gavofyork Nov 27, 2022
52e6430
Fixes
gavofyork Nov 27, 2022
5817e46
Fixes
gavofyork Nov 27, 2022
fd2575a
Merge branch 'master' into gav-ref-nis
gavofyork Dec 1, 2022
14a4085
Merge remote-tracking branch 'origin/master' into gav-ref-nis
gavofyork Dec 2, 2022
2a18a46
Formatting
gavofyork Dec 2, 2022
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
Prev Previous commit
Next Next commit
Docs
  • Loading branch information
gavofyork committed Nov 3, 2022
commit ec6b59497ef0ae97494c02b69ff6e131a71851ea
104 changes: 57 additions & 47 deletions frame/nis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,13 @@ pub mod pallet {
/// the issuance with which we determine the thawed value of a given proportion.
type IgnoredIssuance: Get<BalanceOf<Self>>;

/// The accounting system for the fungible counterpart tokens.
type Counterpart: FungibleMutate<Self::AccountId>;
gavofyork marked this conversation as resolved.
Show resolved Hide resolved

/// The system to convert an overall proportion of issuance into a number of fungible
/// counterpart tokens.
///
/// In general it's best to use `WithMaximumOf`.
type CounterpartAmount: ConvertBack<
Perquintill,
<Self::Counterpart as FungibleInspect<Self::AccountId>>::Balance,
Expand Down Expand Up @@ -310,16 +315,16 @@ pub mod pallet {
pub struct ReceiptRecord<AccountId, BlockNumber> {
/// The proportion of the effective total issuance.
pub proportion: Perquintill,
/// The account to whom this bond belongs.
/// The account to whom this receipt belongs.
pub who: AccountId,
/// The time after which this bond can be redeemed for the proportional amount of balance.
/// The time after which this receipt can be thawed.
pub expiry: BlockNumber,
}

/// An index for a bond.
pub type ActiveIndex = u32;
/// An index for a receipt.
pub type ReceiptIndex = u32;

/// Overall information package on the active bonds.
/// Overall information package on the outstanding receipts.
///
/// The way of determining the net issuance (i.e. after factoring in all maturing frozen funds)
/// is:
Expand All @@ -331,10 +336,10 @@ pub mod pallet {
Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen,
)]
pub struct SummaryRecord<BlockNumber> {
/// The proportion of funds that the `frozen` balance represents to total issuance.
/// The total proportion over all outstanding receipts.
pub proportion_owed: Perquintill,
/// The total number of bonds issued so far.
pub index: ActiveIndex,
/// The total number of receipts created so far.
pub index: ReceiptIndex,
/// The amount (as a proportion of ETI) which has been thawed in this period so far.
pub thawed: Perquintill,
/// The current thaw period's beginning.
Expand All @@ -350,7 +355,7 @@ pub mod pallet {
pub type QueueTotals<T: Config> =
StorageValue<_, BoundedVec<(u32, BalanceOf<T>), T::QueueCount>, ValueQuery>;

/// The queues of bids ready to become bonds. Indexed by duration (in `Period`s).
/// The queues of bids. Indexed by duration (in `Period`s).
#[pallet::storage]
pub type Queues<T: Config> = StorageMap<
_,
Expand All @@ -360,14 +365,14 @@ pub mod pallet {
ValueQuery,
>;

/// Information relating to the bonds currently active.
/// Summary information over the general state.
#[pallet::storage]
pub type Summary<T> = StorageValue<_, SummaryRecordOf<T>, ValueQuery>;

/// The currently active bonds, indexed according to the order of creation.
/// The currently outstanding receipts, indexed according to the order of creation.
#[pallet::storage]
pub type Receipts<T> =
StorageMap<_, Blake2_128Concat, ActiveIndex, ReceiptRecordOf<T>, OptionQuery>;
StorageMap<_, Blake2_128Concat, ReceiptIndex, ReceiptRecordOf<T>, OptionQuery>;

#[pallet::genesis_config]
#[derive(Default)]
Expand All @@ -394,7 +399,7 @@ pub mod pallet {
/// A bid was accepted. The balance may not be released until expiry.
Issued {
/// The identity of the receipt.
index: ActiveIndex,
index: ReceiptIndex,
/// The block number at which the receipt may be thawed.
expiry: T::BlockNumber,
/// The owner of the receipt.
Expand All @@ -404,10 +409,10 @@ pub mod pallet {
/// The amount of funds which were debited from the owner.
amount: BalanceOf<T>,
},
/// An expired bond has been thawed.
/// An receipt has been (at least partially) thawed.
Thawed {
/// The identity of the receipt.
index: ActiveIndex,
index: ReceiptIndex,
/// The owner.
who: T::AccountId,
/// The proportion of the effective total issuance by which the owner was debited.
Expand All @@ -432,13 +437,13 @@ pub mod pallet {
BidTooLow,
/// Bond index is unknown.
Unknown,
/// Not the owner of the bond.
/// Not the owner of the receipt.
NotOwner,
/// Bond not yet at expiry date.
NotExpired,
/// The given bid for retraction is not found.
NotFound,
/// The portion supplied is beyond the value of the bond.
/// The portion supplied is beyond the value of the receipt.
TooMuch,
/// Not enough funds are held to pay out.
Unfunded,
Expand All @@ -463,15 +468,14 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Place a bid for a bond.
/// Place a bid.
///
/// Origin must be Signed, and account must have at least `amount` in free balance.
///
/// - `amount`: The amount of the bid; these funds will be reserved. If the bid is
/// successfully elevated into a bond, then these funds will continue to be
/// reserved until the bond thaws. Must be at least `MinBid`.
/// - `duration`: The number of periods before which the bond may be thawed.
/// Must be greater than 1 and no more than `QueueCount`.
/// - `amount`: The amount of the bid; these funds will be reserved, and if/when
/// consolidated, removed. Must be at least `MinBid`.
/// - `duration`: The number of periods before which the newly consolidated bid may be
/// thawed. Must be greater than 1 and no more than `QueueCount`.
///
/// Complexities:
/// - `Queues[duration].len()` (just take max).
Expand Down Expand Up @@ -578,17 +582,18 @@ pub mod pallet {
Ok(())
gavofyork marked this conversation as resolved.
Show resolved Hide resolved
}

/// Remove an active but expired bond. Reserved funds under bond are freed and balance is
/// adjusted to ensure that the newly freed amount is equivalent to the original amount in
/// terms of the proportion of effective total issued funds.
///
/// Origin must be Signed and the account must be the owner of the bond of the given index.
/// Reduce or remove an outstanding receipt, placing the according proportion of funds into
/// the account of the owner.
///
/// - `index`: The index of the bond to be thawed.
/// - `origin`: Must be Signed and the account must be the owner of the receipt `index` as
gavofyork marked this conversation as resolved.
Show resolved Hide resolved
/// well as any fungible counterpart.
/// - `index`: The index of the receipt.
/// - `portion`: If `Some`, then only the given portion of the receipt should be thawed. If
/// `None`, then all of it should be.
#[pallet::weight(T::WeightInfo::thaw())]
pub fn thaw(
origin: OriginFor<T>,
#[pallet::compact] index: ActiveIndex,
#[pallet::compact] index: ReceiptIndex,
portion: Option<<T::Counterpart as FungibleInspect<T::AccountId>>::Balance>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand Down Expand Up @@ -653,23 +658,22 @@ pub mod pallet {

/// Issuance information returned by `issuance()`.
pub struct IssuanceInfo<Balance> {
/// The balance held in reserve over all active bonds.
/// The balance held in reserve by this pallet instance.
pub holdings: Balance,
/// The issuance not held in reserve for active bonds. Together with `reserved` this sums
/// to `Currency::total_issuance`.
/// The (non-ignored) issuance in the system, not including this pallet's account.
pub other: Balance,
/// The balance that `reserved` is effectively worth, at present. This is not issued funds
/// and could be less than `reserved` (though in most cases should be greater).
/// The effective total issuance, hypothetically if all outstanding receipts were thawed at
/// present.
pub effective: Balance,
/// The balance that `reserved` is effectively worth, at present. This is not issued funds
/// and could be less than `reserved` (though in most cases should be greater).
/// The amount needed to be the pallet instance's account in case all outstanding receipts
/// were thawed at present.
pub required: Balance,
}

impl<T: Config> NonfungibleInspect<T::AccountId> for Pallet<T> {
type ItemId = ActiveIndex;
type ItemId = ReceiptIndex;

fn owner(item: &ActiveIndex) -> Option<T::AccountId> {
fn owner(item: &ReceiptIndex) -> Option<T::AccountId> {
Receipts::<T>::get(item).map(|r| r.who)
}

Expand All @@ -684,7 +688,7 @@ pub mod pallet {
}

impl<T: Config> NonfungibleTransfer<T::AccountId> for Pallet<T> {
fn transfer(index: &ActiveIndex, destination: &T::AccountId) -> DispatchResult {
fn transfer(index: &ReceiptIndex, destination: &T::AccountId) -> DispatchResult {
let mut item = Receipts::<T>::get(index).ok_or(TokenError::UnknownAsset)?;
item.who = destination.clone();
Receipts::<T>::insert(index, item);
gavofyork marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -701,16 +705,19 @@ pub mod pallet {
T::PalletId::get().into_account_truncating()
}

/// Returns information on the issuance of bonds.
///
/// Also provides the summary and this pallet's account ID.
/// Returns information on the issuance within the system.
pub fn issuance() -> IssuanceInfo<BalanceOf<T>> {
Self::issuance_with(&Self::account_id(), &Summary::<T>::get())
}

/// Returns information on the issuance of bonds.
/// Returns information on the issuance within the system
///
/// Also provides the summary and this pallet's account ID.
/// This function is equivalent to `issuance`, except that it accepts arguments rather than
/// queries state. If the arguments are already known, then this may be slightly more
/// performant.
///
/// - `our_account`: The value returned by `Self::account_id()`.
/// - `summary`: The value returned by `Summary::<T>::get()`.
pub fn issuance_with(
our_account: &T::AccountId,
summary: &SummaryRecordOf<T>,
Expand All @@ -725,8 +732,11 @@ pub mod pallet {
IssuanceInfo { holdings, other, effective, required }
}

/// Attempt to enlarge our bond-set from bids in order to satisfy our desired target amount
/// of funds frozen into bonds.
/// Process some bids into receipts in line with the pallet's configuration, especially
/// `Target`.
///
/// Returns the weight used.
// TODO: Accept max_weight, not max_bids.
pub fn pursue_target(max_bids: u32) -> Weight {
let summary: SummaryRecordOf<T> = Summary::<T>::get();
let target = T::Target::get();
Expand Down