Implement Lockable Currency
This guide shows you how to write a pallet that allows users to lock funds for staking and voting. The LockableCurrency
trait is useful in the context of economic systems that enforce accountability by collateralizing fungible resources. You can use the Substrate staking pallet to manage locked funds in time-based increments.
In this guide, we will implement the set_lock
, extend_lock
and remove_lock
methods in our own custom pallet.
Before you begin
You will need to have a pallet already integrated in a runtime to follow this guide. This guide assumes you are using a runtime that contains the Balances pallet to handle the accounts and balances for your chain. You can use the template pallet in the node template to follow.
Declare the necessary dependencies
The methods from LockableCurrency
require us to import a few traits from frame_support
.
Ensure you have the following traits imported in the top section of your pallet:
Declare the
LockIdentifier
constant.To use
LockableCurrency
, you must declare aLockIdentifier
. Given that it requires a[u8; 8]
type, this identifier must be eight characters long.You will need this to declare the methods we'll be including later on.
Declare your custom pallet types
Defining your custom configuration type will allow your pallet to inherit the methods we want to implement.
Define the lockable currency type, let's call it
StakeCurrency
:Specify the new type for your runtime:
Passing in
Balances
ensures that your pallet'sLockableCurrency
methods have the same understanding ofBalance
than the pallet that handles accounts balances of your blockchain.
Create dispatchable functions using the required methods
The required methods are:
fn lock_capital
: Locks the specified amount of tokens from the caller.fn extend_lock
: Extends the lock period.fn unlock_all
: Releases all locked tokens.
Write
lock_capital
, usingT::StakeCurrency::set_lock
:Write
extend_lock
, usingT::StakeCurrency::extend_lock
:Write
unlock_all
, usingT::StakeCurrency::remove_lock
:Write the events for all three dispatchables:
Examples
lockable-currency
example pallet
Related material
Last updated