功能

创建Bubblegum树

Last updated February 24, 2026

Summary

Creating a Bubblegum Tree is the first step before minting compressed NFTs. This page covers how to create and fetch the two required on-chain accounts: the Merkle Tree account and the TreeConfigV2 PDA.

  • Create a Bubblegum Tree with configurable max depth, max buffer size, and canopy depth
  • Choose tree parameters based on your project's cNFT capacity needs (16K to 1B+ cNFTs)
  • Fetch merkle tree and tree config account data after creation
  • Understand the cost tradeoffs for different tree configurations

介绍

虽然压缩NFT的数据存储在交易中而不是链上账户中,但我们仍然需要一些链上账户来跟踪默克尔树及其配置。因此,在开始铸造压缩NFT之前,我们需要创建两个账户:

  • 默克尔树账户。此账户持有一个通用默克尔树,可用于验证任何类型数据的真实性。它由MPL Account Compression程序拥有,该程序从SPL Account Compression程序分叉。在我们的案例中,我们将使用它来验证压缩NFT的真实性。
  • TreeConfigV2账户。第二个账户是从默克尔树账户地址派生的PDA。它允许我们存储特定于压缩NFT的默克尔树额外配置——例如,树创建者、已铸造cNFT数量等。

有了这两个账户,我们就有了开始铸造压缩NFT所需的一切。请注意,我们将带有关联Tree Config账户的默克尔树账户称为Bubblegum树

创建Bubblegum树

现在让我们看看如何创建这两个账户来创建Bubblegum树。幸运的是,我们的库通过提供创建树操作使这个过程变得简单,该操作为我们处理一切。此操作接受各种参数——大多数是可选的——允许我们根据需要自定义Bubblegum树。最重要的参数是:

  • 默克尔树:一个新生成的签名者,将用于创建默克尔树账户。然后可以通过此地址访问默克尔树账户。
  • 树创建者:能够管理Bubblegum树和铸造压缩NFT的账户地址。
  • 最大深度最大缓冲区大小最大深度参数用于计算默克尔树可以容纳的最大叶子数——因此也是压缩NFT数。此最大值按2^maxDepth计算。最大缓冲区大小参数指示默克尔树的最小并发限制。换句话说,它定义了树中可以并行发生多少更改。这两个参数不能任意选择,必须从预定义的值集中选择,如下表所示。

以下是我们推荐的与Solana生态系统兼容的树设置。

cNFT数量树深度树冠深度并发缓冲区树成本每个cNFT成本
16,384148640.33580.00002550
65,5361610640.70690.00001579
262,1441812642.10420.00001303
1,048,576201310248.50120.00001311
16,777,2162415204826.12010.00000656
67,108,8642617204870.82130.00000606
1,073,741,8243017204872.64680.00000507

树的最大深度如下:

  • 公开:Bubblegum树是否应该公开。如果是公开的,任何人都可以从中铸造压缩NFT。否则,只有树创建者或树委托人(如委托cNFT中讨论的)才能铸造压缩NFT。

以下是如何使用我们的库创建Bubblegum树:

创建Bubblegum树

import { generateSigner } from '@metaplex-foundation/umi'
import { createTreeV2 } from '@metaplex-foundation/mpl-bubblegum'
const merkleTree = generateSigner(umi)
const builder = await createTreeV2(umi, {
merkleTree,
maxBufferSize: 64,
maxDepth: 14,
})
await builder.sendAndConfirm(umi)

默认情况下,树创建者设置为Umi身份,公开参数设置为false。但是,可以如下例所示自定义这些参数。

const customTreeCreator = generateSigner(umi)
const builder = await createTreeV2(umi, {
// ...
treeCreator: customTreeCreator,
public: true,
})

获取Bubblegum树

由于Bubblegum树由两个链上账户组成,让我们看看如何获取它们中的任何一个。

获取默克尔树

默克尔树账户包含关于树的各种信息,例如:

  • 树头,存储最大深度最大缓冲区大小、树的权限以及树创建时的创建Slot
  • 本身,存储关于树的低级信息,例如其更改日志(或根)、其序列号等。我们在本文档的专门页面中更多地讨论并发默克尔树。
  • 树冠,如默克尔树树冠页面中讨论的。

以下是如何使用我们的库获取所有这些数据:

获取默克尔树

import {
fetchMerkleTree,
} from "@metaplex-foundation/mpl-account-compression";
const merkleTreeAccount = await fetchMerkleTree(umi, merkleTree)

获取Tree Config

Tree Config账户包含特定于压缩NFT的数据。它存储:

  • Bubblegum树的树创建者
  • Bubblegum树的树委托人(如果有)。否则,设置为树创建者
  • Bubblegum树的总容量,即可以从树中铸造的cNFT最大数量。
  • 已铸造数量,跟踪铸造到树中的cNFT数量。这个值很重要,因为它被用作操作的Nonce("一次性使用数字")值,以确保默克尔树叶子是唯一的。因此,这个nonce充当资产的树范围唯一标识符。
  • 是否公开参数,指示是否任何人都可以从树中铸造cNFT。
  • 是否可解压仅对Bubblegum V1有效。
  • 版本是可以使用的LeafSchema版本。

以下是如何使用我们的库获取所有这些数据:

获取Tree Config

import { fetchTreeConfigFromSeeds } from '@metaplex-foundation/mpl-bubblegum';
const treeConfig = await fetchTreeConfigFromSeeds(umi, { merkleTree });

Notes

  • Tree parameters (max depth, max buffer size, canopy depth) are immutable after creation. Choose carefully based on your project's needs.
  • Larger trees cost more in rent but have a lower per-cNFT cost. See the recommended settings table above for cost estimates.
  • The Tree Creator is stored in the TreeConfigV2 account and can delegate minting authority to another account (see Delegating Trees).
  • Public trees allow anyone to mint. Private trees restrict minting to the Tree Creator or Tree Delegate.

FAQ

Glossary

TermDefinition
Bubblegum TreeThe combination of a Merkle Tree account and its associated TreeConfigV2 PDA
Merkle Tree AccountThe on-chain account holding the merkle tree data, owned by the MPL Account Compression Program
TreeConfigV2A PDA derived from the Merkle Tree address, storing Bubblegum-specific config (creator, delegate, capacity, mint count, public flag)
Max DepthThe maximum depth of the merkle tree, determining capacity as 2^maxDepth
Max Buffer SizeThe number of change log entries stored, determining how many concurrent modifications the tree supports per block
Canopy DepthThe number of upper tree levels cached on-chain, reducing proof sizes in transactions
Tree CreatorThe account that created the tree and has authority to manage it and mint cNFTs
Tree DelegateAn account authorized by the Tree Creator to mint cNFTs on their behalf
Previous
Rust