> For the complete documentation index, see [llms.txt](https://docs.midnight.network/llms.txt)

# Tokens on Midnight

Midnight has one native token, NIGHT, and one native resource, DUST. NIGHT is transferable, used for staking and governance, and generates DUST over time. DUST is a shielded, non-transferable resource that the network consumes to pay transaction fees. Both operate across a dual ledger that supports shielded (private) and unshielded (public) state.

This section covers NIGHT, DUST, and the custom tokens you create in Compact.

## NIGHT and DUST[​](#night-and-dust "Direct link to NIGHT and DUST")

* **NIGHT**: the native utility token. You hold NIGHT for staking, governance, and DUST generation. Unit: STAR (1 NIGHT = 10^6 STAR).
* **DUST**: a shielded, non-transferable fee resource (not a token). Your NIGHT balance generates DUST over time up to a cap, and every transaction consumes it. Unit: SPECK (1 DUST = 10^15 SPECK).

For the economic rationale behind this design, see [Dual-component tokenomics](https://docs.midnight.network/concepts/dual-component-tokenomics.md). For the technical architecture of DUST (generation, decay, registration), see [DUST architecture](https://docs.midnight.network/concepts/dust-architecture.md).

## Shielded and unshielded[​](#shielded-and-unshielded "Direct link to Shielded and unshielded")

Midnight's dual ledger lets tokens exist in two forms:

* **Shielded**: private state where wallet addresses and transaction details stay confidential. DUST is always shielded, as are tokens minted with `mintShieldedToken`.
* **Unshielded**: public state visible on the ledger, similar to transparent blockchains. NIGHT is always unshielded, as are tokens minted with `mintUnshieldedToken`.

A token's privacy comes from its token type, not from the address that holds it. NIGHT is an unshielded token: its balances and transfers are always public, and holding NIGHT at a shielded address does not make it private. There is no mechanism to move a token between shielded and unshielded state, shielded and unshielded tokens are distinct token types, tracked in separate pools. For code examples of shielded and unshielded token transfers in Compact, see [Token transfers](https://docs.midnight.network/examples/contracts/token-transfers.md).

## Custom tokens[​](#custom-tokens "Direct link to Custom tokens")

You can create your own fungible and non-fungible tokens in Compact. The standard library provides `mintShieldedToken` and `mintUnshieldedToken` to mint new tokens, `sendShielded` and `sendUnshielded` to pay tokens out of a contract, and `receiveShielded` and `receiveUnshielded` to take payment into one (see [Receive tokens in a contract](#receive-tokens-in-a-contract)). Token standards on Midnight will come through the MIP (Midnight Improvement Proposal) process. In the meantime, the [OpenZeppelin contracts for Compact](https://docs.midnight.network/sdks/community/openzeppelin-compact-contracts.md) library provides reusable access control and security primitives modeled on the Solidity originals.

To build and move your own tokens end to end, follow the two-part tutorial: [Create and transfer an unshielded token](https://docs.midnight.network/tokens/unshielded-token.md) and [Create and transfer a shielded token](https://docs.midnight.network/tokens/shielded-token.md).

## Receive tokens in a contract[​](#receive-tokens-in-a-contract "Direct link to Receive tokens in a contract")

A contract takes a payment by calling a receive function in a circuit. The receive function doesn't move any funds itself. It adds a condition that the transaction must pay the contract, and the caller's wallet meets that condition when it balances the transaction. The DApp never selects the caller's coins. For an unshielded payment it passes no token data at all. For a shielded payment it passes a description of the new coin, as shown below.

### Unshielded tokens[​](#unshielded-tokens "Direct link to Unshielded tokens")

`receiveUnshielded(color, amount)` makes the transaction pay `amount` of the unshielded token `color` into the contract. For NIGHT, the color is `nativeToken()` and the amount is in STAR:

```
export circuit payEntryFee(amount: Uint<128>): [] {

  receiveUnshielded(nativeToken(), disclose(amount));

}
```

The DApp calls the circuit like any other, with only the arguments the circuit declares. Nothing in the call mentions NIGHT: the caller's wallet adds the inputs that cover the amount when it balances the transaction.

The wallet must also sign the unshielded inputs it adds. A `balanceTx` that goes straight from `balanceUnboundTransaction` to `finalizeRecipe` leaves them unsigned, and the node rejects the transaction with `1010: Invalid Transaction: Custom error: 192` (`InputsSignaturesLengthMismatch`). Sign the recipe with `signRecipe` before you finalize it, as the Private Party tutorial's [`wallet.ts`](https://github.com/midnightntwrk/midnight-examples/blob/main/examples/private-party/src/wallet.ts) does:

```
const recipe = await this.wallet.balanceUnboundTransaction(

  tx,

  { shieldedSecretKeys: this.zswapSecretKeys, dustSecretKey: this.dustSecretKey },

  { ttl },

);

const signed = await this.wallet.signRecipe(

  recipe,

  (payload) => this.unshieldedKeystore.signData(payload),

);

return await this.wallet.finalizeRecipe(signed);
```

The payment and the contract's balance are public. To pay out, check the balance with `unshieldedBalanceGte` and send with `sendUnshielded`.

The [Private Party tutorial](https://docs.midnight.network/tutorials/private-party/smart-contract.md#check-in-crossing-the-boundary) takes an entry fee in NIGHT this way and pays the organizer with `sendUnshielded`.

Receiving NIGHT fails on Mainnet today

With a wallet that signs its unshielded inputs, `payEntryFee` works on Preview, Preprod, and a local network, but it fails on Mainnet. Most Mainnet NIGHT is still held on Cardano as cNIGHT, so the caller's wallet has no NIGHT on Midnight to pay into the contract, and a contract holds none to pay out with `sendUnshielded`. For details, see the caution in the [token transfers example](https://docs.midnight.network/examples/contracts/token-transfers.md).

### Shielded tokens[​](#shielded-tokens "Direct link to Shielded tokens")

A circuit receives a shielded coin as a `ShieldedCoinInfo` argument and calls `receiveShielded` on it. That argument is not one of the caller's wallet coins. It describes a new coin that the transaction creates for the contract:

1. The DApp builds the coin from the token's color, the amount, and a fresh random nonce, and passes it as a circuit argument.
2. `receiveShielded(coin)` requires the transaction to contain a Zswap output that creates that coin for the contract. Midnight.js adds that output to the transaction.
3. The caller's wallet pays for the output when it balances the transaction, spending whichever coins of that color it selects and taking the change back under new nonces. The caller doesn't need a coin of the exact amount.

On the DApp side, build the coin with the ledger's `createShieldedCoinInfo` and pass it to the circuit:

```
import { submitCallTx } from '@midnight-ntwrk/midnight-js-contracts';

import { encodeShieldedCoinInfo } from '@midnight-ntwrk/midnight-js-protocol/compact-runtime';

import { createShieldedCoinInfo } from '@midnight-ntwrk/midnight-js-protocol/ledger';



// providers, compiledContract, and contractAddress come from your DApp's

// setup. tokenColorHex is the token's color as a hex string.

// createShieldedCoinInfo picks a fresh random nonce.

const coin = encodeShieldedCoinInfo(createShieldedCoinInfo(tokenColorHex, 40n));



// If the contract has private state, also pass privateStateId.

await submitCallTx(providers, {

  compiledContract,

  contractAddress,

  circuitId: 'deposit',

  args: [coin],

});
```

Never build the argument from one of the wallet's coins. Its nonce is also in that wallet coin's commitment, so publishing it links the payment to the wallet coin, and whoever minted or sent that coin already knows the nonce.

#### Hold a shielded coin to spend later[​](#hold-a-shielded-coin-to-spend-later "Direct link to Hold a shielded coin to spend later")

A shielded coin has no public balance, so a contract that wants to spend a coin later has to record it. `sendShielded` takes a `QualifiedShieldedCoinInfo`, which adds the coin's Merkle tree index (`mt_index`) to the `ShieldedCoinInfo`. Store the coin in a ledger field of that type, in the same circuit that creates it:

* `insertCoin(key, coin, recipient)` on a `Map<K, QualifiedShieldedCoinInfo>`, or `insertCoin(coin, recipient)` on a `Set<QualifiedShieldedCoinInfo>`.
* `writeCoin(coin, recipient)` on a single `QualifiedShieldedCoinInfo` field.

The recipient is the contract itself, `right<ZswapCoinPublicKey, ContractAddress>(kernel.self())`. At runtime, these operations look up the `mt_index` the ledger assigns to the coin's output, so they only work for a coin created in the current transaction: a coin received with `receiveShielded`, a coin the contract mints to itself, or the change from a partial `sendShielded`. For any other coin, the call throws `Coin commitment not found. Check the coin has been received (or call 'createZswapOutput')`. You don't need the indexer to find the index.

```
export ledger tokenColor: Bytes<32>;

export ledger vault: Map<Bytes<32>, QualifiedShieldedCoinInfo>;



constructor(color: Bytes<32>) {

  tokenColor = disclose(color);

}



export circuit deposit(coin: ShieldedCoinInfo): [] {

  assert(coin.color == tokenColor, "Wrong token");

  assert(coin.value > 0, "Deposit must be worth something");

  // Keys are public and the caller picks the nonce. Without this check, a

  // second coin with the same nonce would overwrite the first one's entry.

  assert(!vault.member(disclose(coin.nonce)), "Nonce already used");



  receiveShielded(disclose(coin));

  vault.insertCoin(

    disclose(coin.nonce),

    disclose(coin),

    right<ZswapCoinPublicKey, ContractAddress>(kernel.self())

  );

}



// Unsafe as written: anyone can withdraw any coin. Add an owner check.

export circuit withdraw(key: Bytes<32>): [] {

  const k = disclose(key);

  assert(vault.member(k), "No such coin");

  const coin = vault.lookup(k);

  sendShielded(coin, left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey()), coin.value);

  vault.remove(k);

}
```

This `withdraw` has no access control, so any caller can name a vault key and take that coin. Before you use the pattern, restrict who can withdraw, for example by having the caller prove knowledge of a secret whose hash the contract stores, as the Private Tip Jar's owner check does. Never authorize with `ownPublicKey()`: the caller supplies it, and nothing proves it.

The `sendShielded` circuit doesn't create the coin ciphertext that tells a recipient about the coin. Midnight.js adds it when it builds the transaction, and only for a recipient whose encryption public key it knows. By default, that is the calling wallet, plus the shielded burn address, which is why burns need no extra setup. To pay anyone else, the DApp passes their key in the `additionalCoinEncPublicKeyMappings` option of the call. Without it, Midnight.js throws `Unable to resolve encryption public key for recipient …`. In practice, a contract pays shielded value to the caller, `ownPublicKey()`. The [Private Tip Jar tutorial](https://docs.midnight.network/tutorials/private-tip-jar/smart-contract.md#tipping) builds this pattern in full, with an owner check on withdrawal and tests that run on a local network.

## Fees[​](#fees "Direct link to Fees")

Every transaction on Midnight requires DUST. On a fresh wallet, DUST generation depends on how you register your NIGHT. Today, most NIGHT reaches Midnight through the cross-chain path (cNIGHT on Cardano), and the registration must finalize on Cardano and then reach a Midnight node, a process that takes about 12 hours. On a local network, DUST generates in about 5 minutes. This delay goes away once native mNIGHT launches. To get started:

* [Fund a wallet](https://docs.midnight.network/guides/acquire-tokens.md) (faucet tNIGHT, Lace registration, and the wallet SDK path)
* [Manage DUST](https://docs.midnight.network/guides/manage-dust.md) (parameters, balances, fee estimates, and several transactions from one wallet)

## Further reading[​](#further-reading "Direct link to Further reading")

* [Dual-component tokenomics](https://docs.midnight.network/concepts/dual-component-tokenomics.md): the economic model behind NIGHT and DUST
* [DUST architecture](https://docs.midnight.network/concepts/dust-architecture.md): generation, decay, registration, and protocol parameters
* [Ledgers](https://docs.midnight.network/concepts/ledgers.md): the UTXO-based ledger and how it represents tokens
* [Token transfers example](https://docs.midnight.network/examples/contracts/token-transfers.md): shielded and unshielded transfer code in Compact
* [Private Party tutorial](https://docs.midnight.network/tutorials/private-party.md): a contract that takes NIGHT entry fees with `receiveUnshielded` and pays them out with `sendUnshielded`
* [Private Tip Jar tutorial](https://docs.midnight.network/tutorials/private-tip-jar.md): a contract that receives shielded tips, stores them with `insertCoin`, and spends them later with `sendShielded`
* [Compact standard library](https://docs.midnight.network/compact/standard-library.md): token management functions
