SDK

JavaScript SDK

Last updated October 12, 2026

Genesis JavaScript SDK 的 API 参考。完整教程请参阅 Launch Pool 或 Presale。

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 Pool 或 Presale。


指令参考

核心

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

Buckets

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

Launch Pool 操作

函数描述
depositLaunchPoolV2()向 Launch Pool 存入 SOL
withdrawLaunchPoolV2()提取 SOL(存款期间)
claimLaunchPoolV2()领取代币(存款期结束后)
refundLaunchPoolV2()退还存款(未达门槛或超过软顶)

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, // { amount: bigint } | null
minimumQuoteTokenThreshold, // { amount: bigint } | null - floor, launch fails below it
softCap, // { amount: bigint } | null - REQUIRED, ceiling on quote kept
endBehaviors, // EndBehavior[]
}).sendAndConfirm(umi);

自 @metaplex-foundation/genesis 0.42.0 起,softCap 为必填参数。与其他 Launch Pool 扩展不同,它在生成的序列化器中没有默认值,因此当发行没有上限时,请显式传入 softCap: null。行为说明请参阅 Launch Pool 软顶。

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);

refundLaunchPoolV2

在存款窗口关闭后退还 Launch Pool 存款。退款金额由程序计算,因此没有金额参数:未达到 minimumQuoteTokenThreshold 时退还全部存款,超过 softCap 时仅退还超出部分,并保留存款人的代币分配不变。

await refundLaunchPoolV2(umi, {
genesisAccount, // PublicKey
bucket, // PublicKey
baseMint, // PublicKey
recipient, // PublicKey | Signer - depositor; base token account closed if they sign
}).sendAndConfirm(umi);

只有 payer 需要签名,因此任何人都可以代表存款人无需许可地触发退款。在存款窗口关闭前调用会返回 LaunchPoolNotEnded;在门槛已达到且未超过上限时调用会返回 LaunchPoolThresholdMet;重复调用会返回 DepositAlreadyRefunded。

claimPresaleV2

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

triggerBehaviorsV2

处理主存储桶上配置的结束行为,将收集的资金转移到 endBehaviors 中定义的目标存储桶。

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
baseTokenProgram, // PublicKey - required, e.g. SPL_TOKEN_PROGRAM_ID from mpl-toolbox
revokeMintAuthority, // boolean
revokeFreezeAuthority, // boolean
padding: Array(5).fill(0), // required, no default
}).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 种子(baseMint、genesisIndex)获取
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, 3 = LaunchPoolV1
// 直接通过种子获取
const account2 = await fetchGenesisAccountV2FromSeeds(umi, {
baseMint: mintAddress,
genesisIndex: 0,
});
// 检查发行类型
if (account2.data.launchType === LaunchType.LaunchPoolV1) {
console.log('This is a launch pool');
}

Genesis 账户字段: authority、baseMint、quoteMint、totalSupplyBaseToken、totalAllocatedSupplyBaseToken、totalProceedsQuoteToken、fundingMode、launchType、bucketCount、finalized

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

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

import {
getGenesisAccountV2GpaBuilder,
LaunchType,
} from '@metaplex-foundation/genesis';
// 获取所有 Launch Pool 发行
const launchpools = await getGenesisAccountV2GpaBuilder(umi)
.whereField('launchType', LaunchType.LaunchPoolV1)
.getDeserialized();
// 按多个字段过滤
const finalizedLaunchpools = await getGenesisAccountV2GpaBuilder(umi)
.whereField('launchType', LaunchType.LaunchPoolV1)
.whereField('finalized', true)
.getDeserialized();
for (const account of launchpools) {
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 状态字段: quoteTokenDepositTotal、weightedQuoteTokenTotal、depositCount、claimCount、refundCount、bucket.baseTokenAllocation、extensions

存款状态字段: amountQuoteToken、weightedQuoteToken、claimed、refunded


类型

LaunchPoolV2Extensions

Launch Pool bucket 上的可选保护机制,从 bucket.extensions 读取。每个字段都是 Umi Option,因此读取值之前请使用 unwrapOption() 或检查 .__option。

{
backendSigner: Option<BackendSigner>;
depositPenalty: Option<LinearBpsScheduleV2>;
withdrawPenalty: Option<LinearBpsScheduleV2>;
bonusSchedule: Option<LinearBpsScheduleV2>;
depositLimit: Option<DepositLimit>; // { limit: bigint }
allowlist: Option<Allowlist>;
claimSchedule: Option<ClaimSchedule>;
minimumDepositAmount: Option<MinimumDepositAmount>; // { amount: bigint }
minimumQuoteTokenThreshold: Option<MinimumQuoteTokenThreshold>; // { amount: bigint } - floor
softCap: Option<SoftCap>; // { amount: bigint } - ceiling
}

可以使用 addLaunchPoolBucketV2Extensions() 和 removeLaunchPoolBucketV2Extensions() 单独添加或移除扩展,并通过 LaunchPoolV2ExtensionType 选择字段。Genesis 账户最终化之后,这两个操作都会被拒绝。

LaunchType

由后端 crank 通过 setLaunchTypeV2 指令追溯设置的链上发行类型。发行类型代表发行使用的底层机制。

enum LaunchType {
Uninitialized = 0, // 尚未由 crank 设置
LaunchPoolV1 = 3, // Launch Pool(按比例分配)
}

Metaplex API 以字符串形式返回('launchpool'),而链上 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 = 未初始化, 3 = LaunchPoolV1
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不在领取窗口内
InvalidSoftCap (221)softCap.amount 为零 — 请传入 softCap: null 而不是 { amount: 0n }
SoftCapBelowThreshold (222)softCap.amount 低于 minimumQuoteTokenThreshold.amount
LaunchPoolNotEnded在存款窗口关闭前调用了 refundLaunchPoolV2()
LaunchPoolThresholdMet (173)在门槛已达到且未超过软顶时请求了退款
DepositAlreadyRefunded对同一笔存款调用了两次 refundLaunchPoolV2()

常见问题

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

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

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

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

fetch 和 safeFetch 有什么区别?

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

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

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

如何处理交易错误?

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


下一步

完整的实现教程: