referrerpolicy=no-referrer-when-downgrade

frame_support/traits/tokens/currency/
lockable.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! The lockable currency trait and some associated types.
19
20use super::{super::misc::WithdrawReasons, Currency};
21use crate::{dispatch::DispatchResult, traits::misc::Get};
22
23/// An identifier for a lock. Used for disambiguating different locks so that
24/// they can be individually replaced or removed.
25pub type LockIdentifier = [u8; 8];
26
27/// A currency whose accounts can have liquidity restrictions.
28pub trait LockableCurrency<AccountId>: Currency<AccountId> {
29	/// The quantity used to denote time; usually just a `BlockNumber`.
30	type Moment;
31
32	/// The maximum number of locks a user should have on their account.
33	type MaxLocks: Get<u32>;
34
35	/// Create a new balance lock on account `who`.
36	///
37	/// If the new lock is valid (i.e. not already expired), it will push the struct to
38	/// the `Locks` vec in storage. Note that you can lock more funds than a user has.
39	///
40	/// If the lock `id` already exists, this will update it.
41	fn set_lock(
42		id: LockIdentifier,
43		who: &AccountId,
44		amount: Self::Balance,
45		reasons: WithdrawReasons,
46	);
47
48	/// Changes a balance lock (selected by `id`) so that it becomes less liquid in all
49	/// parameters or creates a new one if it does not exist.
50	///
51	/// Calling `extend_lock` on an existing lock `id` differs from `set_lock` in that it
52	/// applies the most severe constraints of the two, while `set_lock` replaces the lock
53	/// with the new parameters. As in, `extend_lock` will set:
54	/// - maximum `amount`
55	/// - bitwise mask of all `reasons`
56	fn extend_lock(
57		id: LockIdentifier,
58		who: &AccountId,
59		amount: Self::Balance,
60		reasons: WithdrawReasons,
61	);
62
63	/// Remove an existing lock.
64	fn remove_lock(id: LockIdentifier, who: &AccountId);
65}
66
67/// A inspect interface for a currency whose accounts can have liquidity restrictions.
68pub trait InspectLockableCurrency<AccountId>: LockableCurrency<AccountId> {
69	/// Amount of funds locked for `who` associated with `id`.
70	fn balance_locked(id: LockIdentifier, who: &AccountId) -> Self::Balance;
71}
72
73/// A vesting schedule over a currency. This allows a particular currency to have vesting limits
74/// applied to it.
75pub trait VestingSchedule<AccountId> {
76	/// The quantity used to denote time; usually just a `BlockNumber`.
77	type Moment;
78
79	/// The currency that this schedule applies to.
80	type Currency: Currency<AccountId>;
81
82	/// Get the amount that is currently being vested and cannot be transferred out of this account.
83	/// Returns `None` if the account has no vesting schedule.
84	fn vesting_balance(who: &AccountId)
85		-> Option<<Self::Currency as Currency<AccountId>>::Balance>;
86
87	/// Adds a vesting schedule to a given account.
88	///
89	/// If the account has `MaxVestingSchedules`, an Error is returned and nothing
90	/// is updated.
91	///
92	/// Is a no-op if the amount to be vested is zero.
93	///
94	/// NOTE: This doesn't alter the free balance of the account.
95	fn add_vesting_schedule(
96		who: &AccountId,
97		locked: <Self::Currency as Currency<AccountId>>::Balance,
98		per_block: <Self::Currency as Currency<AccountId>>::Balance,
99		starting_block: Self::Moment,
100	) -> DispatchResult;
101
102	/// Checks if `add_vesting_schedule` would work against `who`.
103	fn can_add_vesting_schedule(
104		who: &AccountId,
105		locked: <Self::Currency as Currency<AccountId>>::Balance,
106		per_block: <Self::Currency as Currency<AccountId>>::Balance,
107		starting_block: Self::Moment,
108	) -> DispatchResult;
109
110	/// Remove a vesting schedule for a given account.
111	///
112	/// NOTE: This doesn't alter the free balance of the account.
113	fn remove_vesting_schedule(who: &AccountId, schedule_index: u32) -> DispatchResult;
114}
115
116/// A vested transfer over a currency. This allows a transferred amount to vest over time.
117pub trait VestedTransfer<AccountId> {
118	/// The quantity used to denote time; usually just a `BlockNumber`.
119	type Moment;
120
121	/// The currency that this schedule applies to.
122	type Currency: Currency<AccountId>;
123
124	/// Execute a vested transfer from `source` to `target` with the given schedule:
125	/// 	- `locked`: The amount to be transferred and for the vesting schedule to apply to.
126	/// 	- `per_block`: The amount to be unlocked each block. (linear vesting)
127	/// 	- `starting_block`: The block where the vesting should start. This block can be in the past
128	///    or future, and should adjust when the tokens become available to the user.
129	///
130	/// Example: Assume we are on block 100. If `locked` amount is 100, and `per_block` is 1:
131	/// 	- If `starting_block` is 0, then the whole 100 tokens will be available right away as the
132	///    vesting schedule started in the past and has fully completed.
133	/// 	- If `starting_block` is 50, then 50 tokens are made available right away, and 50 more
134	///    tokens will unlock one token at a time until block 150.
135	/// 	- If `starting_block` is 100, then each block, 1 token will be unlocked until the whole
136	///    balance is unlocked at block 200.
137	/// 	- If `starting_block` is 200, then the 100 token balance will be completely locked until
138	///    block 200, and then start to unlock one token at a time until block 300.
139	fn vested_transfer(
140		source: &AccountId,
141		target: &AccountId,
142		locked: <Self::Currency as Currency<AccountId>>::Balance,
143		per_block: <Self::Currency as Currency<AccountId>>::Balance,
144		starting_block: Self::Moment,
145	) -> DispatchResult;
146}
147
148// An no-op implementation of `VestedTransfer` for pallets that require this trait, but users may
149// not want to implement this functionality
150pub struct NoVestedTransfers<C> {
151	phantom: core::marker::PhantomData<C>,
152}
153
154impl<AccountId, C: Currency<AccountId>> VestedTransfer<AccountId> for NoVestedTransfers<C> {
155	type Moment = ();
156	type Currency = C;
157
158	fn vested_transfer(
159		_source: &AccountId,
160		_target: &AccountId,
161		_locked: <Self::Currency as Currency<AccountId>>::Balance,
162		_per_block: <Self::Currency as Currency<AccountId>>::Balance,
163		_starting_block: Self::Moment,
164	) -> DispatchResult {
165		Err(sp_runtime::DispatchError::Unavailable.into())
166	}
167}