define-fungible-token
Defining a new fungible token in Clarity smart contracts.
Function Signature
- Input:
token-name
: The name of the fungible token<total-supply>
: (Optional) An unsigned integer representing the total supply of tokens
- Output: Not applicable (definition statement)
Why it matters
The define-fungible-token
function is crucial for:
- Creating new fungible tokens within a smart contract.
- Establishing a token economy or system within your dApp.
- Implementing custom tokens with specific supply constraints.
- Enabling token-related operations like minting, transferring, and burning.
When to use it
Use define-fungible-token
when you need to:
- Create a new fungible token for your smart contract or dApp.
- Implement a token with a fixed total supply.
- Establish a foundation for token-based features in your contract.
- Create utility tokens, governance tokens, or other custom fungible assets.
Best Practices
- Place
define-fungible-token
at the top level of your contract, as it's a definition statement. - Consider carefully whether to specify a total supply or leave it unlimited.
- Use meaningful and descriptive names for your tokens.
- Implement proper access controls for minting and burning operations if required.
Practical Example: Simple Token Creation
Let's implement a basic fungible token with a fixed supply:
This example demonstrates:
- Using
define-fungible-token
to create a new token with a fixed supply of 1,000,000. - Implementing basic token operations like transfer and balance checking.
- Adding a mint function with admin-only access control.
Common Pitfalls
- Forgetting that omitting the total supply parameter allows unlimited minting, if not handled manually.
- Not implementing proper access controls for sensitive operations like minting.
- Overlooking the need for additional functionality like burning or pausing.
Related Functions
ft-transfer?
: Used to transfer tokens between principals.ft-mint?
: Used to create new tokens (if allowed by the token definition).ft-burn?
: Used to destroy tokens, reducing the circulating supply.ft-get-balance
: Used to check the token balance of a principal.ft-get-supply
: Used to get the current total supply of tokens.
Conclusion
The define-fungible-token
function is a fundamental building block for creating token-based systems in Clarity smart contracts. It allows developers to define custom fungible tokens with or without supply constraints. When combined with other token-related functions, it enables the implementation of sophisticated token economies and financial instruments on the Stacks blockchain.