SDK

JavaScript SDK

Last updated March 10, 2026

Genesis JavaScript SDK 的 API 参考。完整教程请参阅 Launch PoolPresale

NPM Package

@metaplex-foundation/genesis

TypeDoc

自动生成的 API 文档

安装

npm install @metaplex-foundation/genesis @metaplex-foundation/umi \
@metaplex-foundation/umi-bundle-defaults @metaplex-foundation/mpl-toolbox \
@metaplex-foundation/mpl-token-metadata

设置

import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { genesis } from '@metaplex-foundation/genesis';
import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata';
const umi = createUmi('https://api.mainnet-beta.solana.com')
.use(genesis())
.use(mplTokenMetadata());

完整的实现示例请参阅 Launch PoolPresale


指令参考

核心

函数描述
initializeV2()创建 Genesis Account 并铸造代币
finalizeV2()锁定配置,激活发行

Buckets

函数描述
addLaunchPoolBucketV2()添加按比例分配的 bucket
addPresaleBucketV2()添加固定价格销售 bucket
addUnlockedBucketV2()添加资金库/接收者 bucket

Launch Pool 操作

函数描述
depositLaunchPoolV2()向 Launch Pool 存入 SOL
withdrawLaunchPoolV2()提取 SOL(存款期间)
claimLaunchPoolV2()领取代币(存款期结束后)

Presale 操作

函数描述
depositPresaleV2()向 Presale 存入 SOL
claimPresaleV2()领取代币(存款期结束后)

管理员

函数描述
triggerBehaviorsV2()执行结束行为
revokeV2()永久撤销铸造和冻结权限

函数签名

initializeV2

await initializeV2(umi, {
baseMint, // Signer - new token keypair
quoteMint, // PublicKey - deposit token (wSOL)
fundingMode, // number - use 0
totalSupplyBaseToken, // bigint - supply with decimals
name, // string - token name
symbol, // string - token symbol
uri, // string - metadata URI
}).sendAndConfirm(umi);

finalizeV2

await finalizeV2(umi, {
baseMint, // PublicKey
genesisAccount, // PublicKey
}).sendAndConfirm(umi);

addLaunchPoolBucketV2

await addLaunchPoolBucketV2(umi, {
genesisAccount, // PublicKey
baseMint, // PublicKey
baseTokenAllocation, // bigint - tokens for this bucket
depositStartCondition, // TimeCondition
depositEndCondition, // TimeCondition
claimStartCondition, // TimeCondition
claimEndCondition, // TimeCondition
minimumDepositAmount, // bigint | null
endBehaviors, // EndBehavior[]
}).sendAndConfirm(umi);

addPresaleBucketV2

await addPresaleBucketV2(umi, {
genesisAccount, // PublicKey
baseMint, // PublicKey
baseTokenAllocation, // bigint
allocationQuoteTokenCap, // bigint - SOL cap (sets price)
depositStartCondition, // TimeCondition
depositEndCondition, // TimeCondition
claimStartCondition, // TimeCondition
claimEndCondition, // TimeCondition
minimumDepositAmount, // bigint | null
depositLimit, // bigint | null - max per user
endBehaviors, // EndBehavior[]
}).sendAndConfirm(umi);

addUnlockedBucketV2

await addUnlockedBucketV2(umi, {
genesisAccount, // PublicKey
baseMint, // PublicKey
baseTokenAllocation, // bigint - usually 0n
recipient, // PublicKey - who can claim
claimStartCondition, // TimeCondition
claimEndCondition, // TimeCondition
}).sendAndConfirm(umi);

depositLaunchPoolV2

await depositLaunchPoolV2(umi, {
genesisAccount, // PublicKey
bucket, // PublicKey
baseMint, // PublicKey
amountQuoteToken, // bigint - lamports
}).sendAndConfirm(umi);

depositPresaleV2

await depositPresaleV2(umi, {
genesisAccount, // PublicKey
bucket, // PublicKey
baseMint, // PublicKey
amountQuoteToken, // bigint - lamports
}).sendAndConfirm(umi);

withdrawLaunchPoolV2

await withdrawLaunchPoolV2(umi, {
genesisAccount, // PublicKey
bucket, // PublicKey
baseMint, // PublicKey
amountQuoteToken, // bigint - lamports
}).sendAndConfirm(umi);

claimLaunchPoolV2

await claimLaunchPoolV2(umi, {
genesisAccount, // PublicKey
bucket, // PublicKey
baseMint, // PublicKey
recipient, // PublicKey
}).sendAndConfirm(umi);

claimPresaleV2

await claimPresaleV2(umi, {
genesisAccount, // PublicKey
bucket, // PublicKey
baseMint, // PublicKey
recipient, // PublicKey
}).sendAndConfirm(umi);

triggerBehaviorsV2

await triggerBehaviorsV2(umi, {
genesisAccount, // PublicKey
primaryBucket, // PublicKey
baseMint, // PublicKey
})
.addRemainingAccounts([/* destination bucket + its quote token account */])
.sendAndConfirm(umi);

revokeV2

await revokeV2(umi, {
genesisAccount, // PublicKey
baseMint, // PublicKey
revokeMintAuthority, // boolean
revokeFreezeAuthority, // boolean
}).sendAndConfirm(umi);

PDA 辅助函数

函数种子
findGenesisAccountV2Pda()baseMint, genesisIndex
findLaunchPoolBucketV2Pda()genesisAccount, bucketIndex
findPresaleBucketV2Pda()genesisAccount, bucketIndex
findUnlockedBucketV2Pda()genesisAccount, bucketIndex
findLaunchPoolDepositV2Pda()bucket, recipient
findPresaleDepositV2Pda()bucket, recipient
const [genesisAccountPda] = findGenesisAccountV2Pda(umi, { baseMint: mint.publicKey, genesisIndex: 0 });
const [bucketPda] = findLaunchPoolBucketV2Pda(umi, { genesisAccount: genesisAccountPda, bucketIndex: 0 });
const [depositPda] = findLaunchPoolDepositV2Pda(umi, { bucket: bucketPda, recipient: wallet });

获取函数

Genesis 账户

Genesis 账户存储包括发行类型在内的顶层发行状态。后端 crank 在创建后通过 setLaunchTypeV2 指令在链上设置 launchType 字段,因此在 crank 处理之前,该值可能为 Uninitialized(0)。

函数返回值
fetchGenesisAccountV2()Genesis 账户状态(不存在时抛出错误)
safeFetchGenesisAccountV2()Genesis 账户状态或 null
fetchGenesisAccountV2FromSeeds()通过 PDA 种子(baseMintgenesisIndex)获取
safeFetchGenesisAccountV2FromSeeds()同上,不存在时返回 null
fetchAllGenesisAccountV2()批量获取多个 Genesis 账户
import {
fetchGenesisAccountV2,
fetchGenesisAccountV2FromSeeds,
findGenesisAccountV2Pda,
LaunchType,
} from '@metaplex-foundation/genesis';
// 通过 PDA 地址获取
const [genesisAccountPda] = findGenesisAccountV2Pda(umi, {
baseMint: mintAddress,
genesisIndex: 0,
});
const account = await fetchGenesisAccountV2(umi, genesisAccountPda);
console.log(account.data.launchType); // 0 = Uninitialized, 1 = Project, 2 = Meme
// 直接通过种子获取
const account2 = await fetchGenesisAccountV2FromSeeds(umi, {
baseMint: mintAddress,
genesisIndex: 0,
});
// 检查发行类型
if (account2.data.launchType === LaunchType.Meme) {
console.log('This is a memecoin launch');
} else if (account2.data.launchType === LaunchType.Project) {
console.log('This is a project launch');
}

Genesis 账户字段: authoritybaseMintquoteMinttotalSupplyBaseTokentotalAllocatedSupplyBaseTokentotalProceedsQuoteTokenfundingModelaunchTypebucketCountfinalized

GPA 构建器 — 按发行类型查询

使用 getGenesisAccountV2GpaBuilder() 查询按链上字段过滤的所有 Genesis 账户。这使用 Solana 的字节级过滤器 getProgramAccounts RPC 方法进行高效查找。

import {
getGenesisAccountV2GpaBuilder,
LaunchType,
} from '@metaplex-foundation/genesis';
// 获取所有 Meme 币发行
const memecoins = await getGenesisAccountV2GpaBuilder(umi)
.whereField('launchType', LaunchType.Meme)
.getDeserialized();
// 获取所有项目发行
const projects = await getGenesisAccountV2GpaBuilder(umi)
.whereField('launchType', LaunchType.Project)
.getDeserialized();
// 按多个字段过滤
const finalizedMemecoins = await getGenesisAccountV2GpaBuilder(umi)
.whereField('launchType', LaunchType.Meme)
.whereField('finalized', true)
.getDeserialized();
for (const account of memecoins) {
console.log(account.publicKey, account.data.baseMint, account.data.launchType);
}

launchType 在发行创建后由后端 crank 追溯设置。最近创建的发行可能会显示 LaunchType.Uninitialized(0),直到 crank 处理完毕。

Bucket 和存款

函数返回值
fetchLaunchPoolBucketV2()Bucket 状态(不存在时抛出错误)
safeFetchLaunchPoolBucketV2()Bucket 状态或 null
fetchPresaleBucketV2()Bucket 状态(不存在时抛出错误)
safeFetchPresaleBucketV2()Bucket 状态或 null
fetchLaunchPoolDepositV2()存款状态(不存在时抛出错误)
safeFetchLaunchPoolDepositV2()存款状态或 null
fetchPresaleDepositV2()存款状态(不存在时抛出错误)
safeFetchPresaleDepositV2()存款状态或 null
const bucket = await fetchLaunchPoolBucketV2(umi, bucketPda);
const deposit = await safeFetchLaunchPoolDepositV2(umi, depositPda); // null if not found

Bucket 状态字段: quoteTokenDepositTotaldepositCountclaimCountbucket.baseTokenAllocation

存款状态字段: amountQuoteTokenclaimed


类型

LaunchType

由后端 crank 通过 setLaunchTypeV2 指令追溯设置的链上发行类别。

enum LaunchType {
Uninitialized = 0, // 尚未由 crank 设置
Project = 1, // 结构化项目代币发行
Meme = 2, // 社区 Meme 币发行
}

Integration APIs 以字符串形式返回('project''memecoin''custom'),而链上 SDK 使用上述数字枚举。

GenesisAccountV2

Genesis 发行的顶层链上账户。每个代币铸币地址、每个发行索引对应一个账户。

{
key: Key;
bump: number;
index: number; // Genesis 索引(通常为 0)
finalized: boolean; // finalizeV2() 后为 true
authority: PublicKey; // 发行创建者
baseMint: PublicKey; // 正在发行的代币
quoteMint: PublicKey; // 存款代币(例如 wSOL)
totalSupplyBaseToken: bigint; // 代币总供应量
totalAllocatedSupplyBaseToken: bigint; // 分配给 bucket 的供应量
totalProceedsQuoteToken: bigint; // 收集的总存款额
fundingMode: number; // 资金模式(0)
launchType: number; // 0 = 未初始化, 1 = 项目, 2 = Meme
bucketCount: number; // Bucket 数量
}

账户大小:136 字节。PDA 种子:["genesis_v2", baseMint, genesisIndex]

TimeCondition

{
__kind: 'TimeAbsolute',
padding: Array(47).fill(0),
time: bigint, // Unix timestamp (seconds)
triggeredTimestamp: null,
}

EndBehavior

{
__kind: 'SendQuoteTokenPercentage',
padding: Array(4).fill(0),
destinationBucket: PublicKey,
percentageBps: number, // 10000 = 100%
processed: false,
}

常量

常量
WRAPPED_SOL_MINTSo11111111111111111111111111111111111111112

常见错误

错误原因
insufficient fundsSOL 不足以支付费用
already initializedGenesis Account 已存在
already finalized最终化后无法修改
deposit period not active不在存款窗口内
claim period not active不在领取窗口内

常见问题

什么是 Umi,为什么需要它?

Umi 是 Metaplex 的 Solana JavaScript 框架。它提供了统一的接口来构建交易、管理签名者以及与 Metaplex 程序交互。

我可以在浏览器中使用 Genesis SDK 吗?

可以。SDK 在 Node.js 和浏览器环境中都可以使用。在浏览器中,请使用钱包适配器进行签名,而不是密钥对文件。

fetch 和 safeFetch 有什么区别?

fetch 在账户不存在时会抛出错误。safeFetch 则返回 null,适用于检查账户是否存在。

如何获取代币的发行类型?

使用代币的铸币地址通过 fetchGenesisAccountV2FromSeeds() 获取 GenesisAccountV2 账户。launchType 字段返回 0(未初始化)、1(项目)或 2(Meme)。要查询特定类型的所有发行,请使用 GPA 构建器。或者,Integration APIs 在 REST 响应中以字符串形式返回发行类型。

如何处理交易错误?

sendAndConfirm 调用包装在 try/catch 块中。检查错误消息以了解具体的失败原因。


下一步

完整的实现教程: