Simple staking pallet
Build a composable staking pallet for your runtime.
#
GoalWrite a pallet that allows users to lock funds.
#
Use cases- Staking.
- Conviction voting.
#
OverviewThe LockableCurrency
trait can be very useful in the context of economic systems that enforce accountability by collateralizing fungible resources.
Substrate's staking pallet makes use of the same trait to handle locked funds in time-based increments. In
this guide, we will make use of it in our own custom pallet.
#
Steps#
1. Declare relevant importsImport the following traits:
LockIdentifier
constant#
2.Declare the In order to use LockableCurrency
, we need to declare a LockIdentifier
(must be 8 characters long):
#
3. Define the required typesDefine the lockable currency type:
The new BalanceOf<T>
type satisfies the type constraints of Self::Balance
for all provided
methods inCurrency
.
#
4. Write out the required functionsOur pallet must contain the following key functions to ensure it handles the basic logical components for locking and unlocking a currency for a period of time. These are:
fn set_lock
: Locks the specified amount of tokens from the caller.fn extend_lock
: Extends the lock period.fn remove_lock
: Releases all locked tokens.
fn lock_capital
#
Call the set_lock()
method from Currency
:
fn extend_lock
#
Call the extend_lock()
method from Currency
:
fn unlock_all
#
Call the remove_lock()
method from Currency
:
#
ExamplesFurther learning ๐ก
- Using
LockIdentifier
. Use this to define logic within the runtime that schedules locking, unlocking, or extends existing locks.- Composability. Use this basic scheme when writing other pallets that borrow useful methods from mainstream traits.