smart-contracts
: working with tokens
Overview
The NilTokenBase.sol
contract allows for creating new custom tokens to be deployed on =nil;
To do so, simply inherit a new smart contract from NilTokenBase.sol
and deploy it.
import "@nilfoundation/smart-contracts/contracts/NilTokenBase.sol";
contract NewToken is NilTokenBase {}
NilTokenBase.sol
provides internal methods for burning, minting and sending tokens, as well as onlyExternal
wrappers over these methods.
FT example
It is possible to implement a simple fungible token contract by inheriting from NilTokenBase.sol
:
pragma solidity ^0.8.0;
import "@nilfoundation/smart-contracts/contracts/Nil.sol";
import "@nilfoundation/smart-contracts/contracts/NilTokenBase.sol";
/**
* @title FT
* @author =nil; Foundation
* @notice The contract represents a simple fungible token.
*/
contract FT is NilTokenBase {
/**
* @notice A public 'wrapper' over mintTokenInternal().
*/
function mintFT(uint256 amount) public {
mintTokenInternal(amount);
}
/**
* @notice The function sends the FT to the provided address.
* @param dst The address to which the FT must be sent.
*/
function sendFT(address dst, uint256 amount) public {
uint currentBalance = getTokenTotalSupply();
require(amount <= currentBalance, "Insufficient balance");
Nil.Token[] memory ft = new Nil.Token[](1);
ft[0].id = getTokenId();
ft[0].amount = amount;
Nil.asyncCallWithTokens(
dst,
msg.sender,
msg.sender,
0,
Nil.FORWARD_REMAINING,
0,
ft,
""
);
}
/**
*
* @notice The empty override ensures that the FT can only be minted via mintFT().
*/
function mintToken(uint256 amount) public override onlyExternal {}
}