Testing a transfer function
A basic overview to help you write out your checks and balances.
#
GoalLearn how to write tests and improve the correctness of a transfer
function.
#
Use casesTesting a custom transfer function.
#
OverviewTesting each function is an imporant part of developing pallets for production. This guide
steps you through best practices for writing test cases for a basic transfer
function.
#
Stepstransfer
function#
1. Outline the A transfer function has two key elements: subtracting a balance from an account and adding that balance to another account. Here, we'll start by outlining this function:
#
2. Check that the sender has enough balanceThe first thing to verify, is whether the sender has enough balance.
In a separate tests.rs
file, write out this first test case:
#
Configure error handlingTo implement some error check, replace mutate
with try_mutate
to use ensure!
.
This will check whether bal is greater or equal to amount and throw an error message if not:
Run cargo test
from your pallet's directory.
#
3. Check that sending account doesn't go below minimum balance#
4. Check that both tests work togetherUse #[transactional]
to generate a wrapper around both checks:
#
5. Handle dust accountsMake sure that sending and receiving accounts aren't dust accounts. Use T::MinBalance::get()
: