Token module
The token module handles all logic related to balance. Specifically the following criteria:
-
Validating and subtracting fees for all transactions.
-
Checking the minimum remaining balance requirement.
-
Giving block rewards to the block generator.
-
Transferring account balances.
Name |
|
||||
ID |
|
||||
Assets |
|
||||
Reducers |
|||||
Actions |
none |
||||
Events |
none |
Table of Contents
Account schema
The token module adds a new property balance
under the key token
to every account in the network as follows:
{
type: 'object',
properties: {
balance: {
fieldNumber: 1,
dataType: 'uint64',
},
},
default: {
balance: BigInt(0),
},
}
Transactions
The following transaction assets are provided by the token module.
TransferAsset
Allows the possibility to execute a transfer transaction, which transfers tokens from one account to another.
Schema
{
$id: 'lisk/transfer-asset',
title: 'Transfer transaction asset',
type: 'object',
required: ['amount', 'recipientAddress', 'data'],
properties: {
amount: {
dataType: 'uint64',
fieldNumber: 1,
},
recipientAddress: {
dataType: 'bytes',
fieldNumber: 2,
minLength: 20,
maxLength: 20,
},
data: {
dataType: 'string',
fieldNumber: 3,
minLength: 0,
maxLength: 64,
},
},
}
Reducers
credit()
Credits a specific amount of tokens to an account.
credit()
async (params: Record<string, unknown>, stateStore: StateStore): Promise<void> => {
const { address, amount } = params;
if (!Buffer.isBuffer(address)) {
throw new Error('Address must be a buffer');
}
if (typeof amount !== 'bigint') {
throw new Error('Amount must be a bigint');
}
if (amount <= BigInt(0)) {
throw new Error('Amount must be a positive bigint.');
}
const account = await stateStore.account.getOrDefault<TokenAccount>(address);
account.token.balance += amount;
if (account.token.balance < this._minRemainingBalance) {
throw new Error(
`Remaining balance must be greater than ${this._minRemainingBalance.toString()}`,
);
}
await stateStore.account.set(address, account);
},
debit()
Debits a specific amount of tokens from an account.
debit()
async (params: Record<string, unknown>, stateStore: StateStore): Promise<void> => {
const { address, amount } = params;
if (!Buffer.isBuffer(address)) {
throw new Error('Address must be a buffer');
}
if (typeof amount !== 'bigint') {
throw new Error('Amount must be a bigint');
}
if (amount <= BigInt(0)) {
throw new Error('Amount must be a positive bigint.');
}
const account = await stateStore.account.getOrDefault<TokenAccount>(address);
account.token.balance -= amount;
if (account.token.balance < this._minRemainingBalance) {
throw new Error(
`Remaining balance must be greater than ${this._minRemainingBalance.toString()}`,
);
}
await stateStore.account.set(address, account);
},
getBalance()
Get the balance of an specific account.
getBalance()
async (
params: Record<string, unknown>,
stateStore: StateStore,
): Promise<bigint> => {
const { address } = params;
if (!Buffer.isBuffer(address)) {
throw new Error('Address must be a buffer');
}
const account = await stateStore.account.getOrDefault<TokenAccount>(address);
return account.token.balance;
},