# Build an NFT Collection Frontend

ChainRender delivers the **entire frontend** for an NFT collection — mint, gallery, my-collection, and the on-chain
art itself — driven by *your* ERC-721 backend. This guide shows the pattern. It's the same architecture as the
flagship swap app (read live on-chain state + declared write actions), applied to a collection.

## The division of labour (honest)

ChainRender is a fully-on-chain **UI/rendering** framework, not an NFT protocol. So:

- **You bring the ERC-721** — mint, ownership, trading, and an **on-chain `tokenURI`** (the art as `data:` SVG/JSON).
  Use OpenZeppelin, ERC-721A, or your own contract.
- **ChainRender builds the frontend** — it *reads* your contract's state live, *renders* the UI and the on-chain
  art, and turns each user action (mint, reveal, transfer) into a declared, gate-checked transaction to sign.

Nothing here is minted or owned by ChainRender. It's the trustless frontend, exactly like the flagship is the
frontend for a DEX.

## What you get, Grade A

A multi-view app — **Mint / Gallery / My NFTs** — where:

- **Live reads** (`totalSupply`, `mintPrice`, `saleActive`, `balanceOf`) come from *your* contract via the
  `staticread` **CallResolver** — any `view` function, rendered live on-chain.
- **The Mint button** is a **declared action** → your `mint(uint256)`, carrying the mint price
  (the `TRANSFER_VALUE` capability). The runtime constructs the exact calldata; the user sees the decoded action
  before signing; the button can only ever call what it declares.
- **Each token's art** renders through *your* `tokenURI` via the **`NftImageResolver`**, which decodes the on-chain
  JSON/SVG and verifies provenance per token — off-chain pointers are refused, never laundered as on-chain.
- **Grade A**: every read is on-chain, the art is on-chain, no off-chain fetch.

## The shape

The fastest start is the visual builder's **NFT Collection (mint + gallery)** template (Templates → NFT Collection):
the three views below, wired as tabs, ready for your addresses. The same app in code, with the SDK:

```ts
import { compileComponent, compose, reads, checkNode, grade, type Node } from "@chainrender/client";
import { encodeAbiParameters } from "viem";

const COLLECTION = "0x…" as `0x${string}`;        // your ERC-721 (on the same chain as the resolvers)
const CALL_RESOLVER = "0x…" as `0x${string}`;     // CallResolver on that chain (see Deployed Contracts)
const NFT_IMAGE_RESOLVER = "0x…" as `0x${string}`; // NftImageResolver on that chain

// a live read of any view function on your contract (the `staticread` slot, Grade A)
const read = (signature: string, args: unknown[] = [], fmt = {}) =>
  reads.staticReadSlot(CALL_RESOLVER, { target: COLLECTION, callData: reads.callData(signature, args), ...fmt }) as Node;

// one token's on-chain art: arg = abi.encode(address nft, uint256 tokenId)
const art = (tokenId: bigint): Node => ({
  type: "image", bind: "nftImage", resolver: NFT_IMAGE_RESOLVER, alt: `Token ${tokenId}`, provenance: "onchain-vector-nft",
  arg: encodeAbiParameters([{ type: "address" }, { type: "uint256" }], [COLLECTION, tokenId]),
});

const mint = compose.box([
  compose.heading("Mint"),
  compose.box([read("totalSupply()"), read("mintPrice()", [], { format: reads.FMT.FIXED, decimals: 18, fracDigits: 4 })], { direction: "row" }),
  compose.input({ inputType: "number", placeholder: "Quantity", name: "amount" }), // fills the button's "$amount"
  // a payable action: `value` is the wei sent; the user's wallet is the final gate
  { type: "button", content: "Mint", variant: "primary", action: { to: COLLECTION, sig: "mint(uint256)", value: "10000000000000000", args: "$amount" } } as Node,
], { direction: "column" });
const gallery = compose.box([compose.heading("Collection"), compose.box([0n, 1n, 2n, 3n].map(art), { direction: "row" })], { direction: "column" });
// the viewer's own transfers of this collection: filled in the viewer after the person opts in (⚙, see below)
const mine = compose.box([
  compose.heading("My recent mints and transfers"),
  { type: "slot", bind: "transfers", argValues: { kind: "nft-transfers", account: "$viewer", token: COLLECTION, blocks: "45000", limit: "20" } } as Node,
  compose.text("Covers the most recent 45,000 blocks, not all time."),
], { direction: "column" });

for (const v of [mint, gallery]) console.log(checkNode(v).ok, grade(v).letter); // true "A"
console.log(grade(mine).aboveFloor); // ⚙ transfer-history, over a floor that grades A

// compile each view, then combine them as tabs (each view is a component hash)
const app = compose.multiview.buildTabs([mint, gallery, mine].map((v) => compileComponent(v).hash), { name: "Cryptic Organisms" });
// app.built.encoded → register in the MultiViewRouter; see Build a Multi-View App
```

## The three views

**Mint** — collection name + tagline, a live stat row (minted / max / price / sale status, all read from your
contract), a quantity input, and the payable Mint button.

**Gallery** — a responsive grid; each card renders a token's on-chain art from your `tokenURI`.

**My recent mints and transfers** — the viewer's own transfers of this collection, read by the viewer from the
chain's logs after the person chooses to show their wallet's data ([viewer reads](./viewer-reads.md)). Everyone
else, and the on-chain render, sees "Connect your wallet to see your recent transfers". That view is ⚙ Above-floor;
report it that way. A connected wallet's **token count** (`balanceOf(viewer)`) is not available: no viewer-read kind
reads an ERC-721 balance, and a fixed-address `staticread` seals its address into the app's hash at registration.

## Wiring it to your contract

1. Deploy your **ERC-721** with an **on-chain `tokenURI`** (the art as a `data:` payload).
2. Deploy (or reuse) a **`CallResolver`** and an **`NftImageResolver`**.
3. Plug their addresses into the config. If your view getters differ from the standard names
   (`totalSupply()`, `mintPrice()`, `saleActive()`), adjust the selectors — the builder documents which it assumes.
4. Compile each view to a component hash, combine with `buildTabs`, and deploy. Register a subname if you want a
   `yourcollection.chainrender.eth` address.

## Trading, and the OpenSea question

- **On-chain trade functions** on *your* contract (list, buy, transfer) are just more declared actions — add a
  button for each.
- **Secondary marketplace**: once your ERC-721 + on-chain `tokenURI` exist, **OpenSea and other sites display the
  collection automatically** (the on-chain SVG shows everywhere). Link out to OpenSea for the open market.
- **Interactive art**: the p5/three interactive version lives in *your ChainRender app* (the premium experience).
  On OpenSea, the fully-on-chain **SVG poster** displays; a self-contained interactive `animation_url` is optional
  extra work on your side. See **[Rich Media: p5.js & three.js](../concepts/rich-media-p5-three.md)**.

## Try it in the builder

The **[Visual Builder](https://chainrender.org/builder)** ships an **"NFT Collection (mint + gallery)"** template —
load it to see the multi-view app, adapt the addresses, grade it, and deploy.

## Related
- **[Declare an Action](./declare-action.md)** · **[Bind Live On-Chain Data](./bind-data.md)** ·
  **[Multi-View App](./multi-view-app.md)** · **[On-Chain NFT Media](../concepts/visual-media.md)**
