DEV Community

Neeraj Choubisa
Neeraj Choubisa Subscriber

Posted on

Building a Fungible Token in Compact (Midnight)

Midnight Network Challenge: Enhance the Ecosystem

The rise of privacy-focused blockchains like Midnight is introducing developers to a new paradigm of smart contract development using Compact language.

If you are coming from Solidity, things may feel unfamiliar at first, especially concepts like inheritance, storage, and function definitions.

In this article, you will learn how fungible tokens work in Compact, why inheritance does not exist, how to build your own token step by step, and how to fix common errors.

Understanding Compact

Compact does not support inheritance like Solidity.

Instead of writing:
contract MyToken is ERC20

Compact uses modules and composition. This means you import functionality and explicitly use it. There is no hidden behavior.

Understanding the FungibleToken Module

The FungibleToken module provides core token functionality such as balances tracking, allowances, transfers, minting and burning, and metadata like name, symbol, and decimals.

There are a few differences compared to ERC20:
It uses Uint<128> instead of uint256
There are no events
Contract to contract transfers are not supported yet

Step 1: Import the Module

import "./token/FungibleToken.compact" prefix FT_;
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Your Token Contract

pragma language_version >= 0.21.0;

import "./token/FungibleToken.compact" prefix FT_;

contract CustomFungibleToken {

  circuit init(): [] {
    FT_.initialize("Nikku Token", "NIK", 18);

    const owner = left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey());
    FT_._mint(owner, 1000000);
  }

  circuit transfer(
    to: Either<ZswapCoinPublicKey, ContractAddress>,
    amount: Uint<128>
  ): Boolean {
    return FT_.transfer(to, amount);
  }

  circuit balanceOf(
    account: Either<ZswapCoinPublicKey, ContractAddress>
  ): Uint<128> {
    return FT_.balanceOf(account);
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Compile the Contract

Run the following command:

npm run compile
Enter fullscreen mode Exit fullscreen mode

Key Concepts to Remember

Compact does not support inheritance.
You must use circuit instead of function definitions.
Modules manage their own storage.
Initialization is required and must not be skipped.

Current Limitations

Contract to contract calls are not supported.
Events are not available.
Only 128 bit integers are supported.

Mental Model

Instead of thinking that your contract extends ERC20, think of it as using a token module and calling its functions.

What You Can Build Next

You can extend this basic token to add mint restrictions, implement fees, or build more advanced applications on Midnight.

Final Thoughts

Compact encourages explicit and predictable contract design. While it may take some time to adjust, it leads to better security and modularity.

Once you understand this pattern, building on Midnight becomes much easier.


About Author ?

Hi, I’m Nikku.Dev, a Full Stack Blockchain Developer and builder. I work on smart contracts, DeFi systems, and Web3 applications, focusing on building real, scalable products.

Portfolio: https://nikkudotdev.vercel.app/

GitHub: https://github.com/Kali-Decoder

Top comments (0)