NAV(净资产值)定价时点是交易日内基金份额定价和订单执行的固定时间节点。传统基金通常每天仅在收盘时设置一个定价时点,而 Solana 凭借其高速度和低成本,可支持日内多个定价时点,为投资者提供更大的灵活性。
什么是 NAV?
基金是由众多投资者共同持有的资产池。**NAV(净资产值)**即基金一份份额的价格。可以这样理解:
NAV = (Total Value of Everything in the Fund - liabilities) ÷ Number of Shares
例如,若某基金持有 1000 万美元资产,且有 1000 万份份额在外流通,则每份份额价值 1.00 美元。
NAV 为何重要?
- 买入份额(认购):您按当前 NAV 支付。若 NAV 为 1.02 美元,投资 102 美元可获得 100 份份额。
- 卖出份额(赎回):您按当前 NAV 收款。若以 NAV 1.02 美元赎回 100 份份额,可获得 102 美元。
货币市场基金通常维持约 1.00 美元的稳定 NAV,根据所得利息会有小幅波动。
问题所在
传统上,当您投资货币市场基金时,有两件事会在不同时间发生:
- 提交订单 → 等待收盘
- 计算 NAV → 在 T+1 或 T+2 日发行/赎回份额
这造成了以下几个问题:
- 执行延迟:上午提交的订单需等到下午 4 点才能处理
- 结算滞后:资金在 1-2 天内处于冻结状态
- 灵活性有限:每天仅有一次交易机会
- 价格陈旧:从下单到执行期间 NAV 可能已过时
解决方案
Solana 上的 NAV 定价时点支持多个每日结算窗口,并实现原子化执行。订单排队后在每个定价时点以当前 NAV 即时执行。
┌─────────────────────────────────────────────────────────────┐│ MULTIPLE DAILY NAV STRIKES │├─────────────────────────────────────────────────────────────┤│ ││ 9:30 AM 12:00 PM 2:30 PM 4:00 PM ││ │ │ │ │ ││ ▼ ▼ ▼ ▼ ││ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ││ │STRIKE│ │STRIKE│ │STRIKE│ │STRIKE│ ││ │NAV=$1│ │NAV=$1│ │NAV=$1│ │NAV=$1│ ││ │.0012 │ │.0015 │ │.0018 │ │.0020 │ ││ └─────┘ └─────┘ └─────┘ └─────┘ ││ │ │ │ │ ││ ▼ ▼ ▼ ▼ ││ Process Process Process Process ││ Orders Orders Orders Orders ││ ││ ✅ Atomic settlement at each strike ││ ✅ NAV stored onchain in token metadata ││ ✅ Less stale prices ││ │└─────────────────────────────────────────────────────────────┘
本指南演示如何在 Solana 上使用 SPL Token 2022 扩展实现 NAV 定价时点,以实现合规的基金份额发行,并使用标准 USDC 进行结算,全程无需编写自定义 Rust 程序。
教育参考实现
您可以使用本实现的 源代码在本地体验 NAV 定价时点。
本指南仅供探索和教育目的提供参考实现。请勿将此代码直接用于生产环境,除非完成以下工作:
- 全面的安全审计
- 完善的密钥管理系统
- 合规监管审查
- 法律咨询
- 充分的测试与修改
架构概览
NAV 定价时点系统由以下核心组件构成:
- 基金份额代币:SPL Token 2022,元数据中存储链上 NAV
- 结算货币:标准 USDC(现有 SPL 代币)
- 基金管理员:通过委托权限编排定价时点
- 白名单系统:控制哪些地址可持有基金份额
关键设计原则
- 无需自定义程序:仅使用 SPL Token 2022 扩展和标准 USDC
- 原子化结算:单笔交易确保认购/赎回整体完成或整体失败
- 默认冻结状态:份额需经显式白名单审批以满足合规要求
- 委托权限:基金管理员无需托管即可操作
- 链上 NAV:当前价格存储于代币元数据中,可公开验证
阶段一:基金设置
- 创建带有元数据扩展的基金份额代币
- 将 NAV 初始化为 1.00 美元
- 设置每日定价时点计划
- 将机构投资者加入白名单
阶段二:定价时点前准备期
- 投资者提交认购/赎回申请
- 订单排队等待下一个定价时点
- 基金计算初步 NAV
- 执行风险检查
阶段三:NAV 定价时点执行
At Strike Time (e.g., 14:30):├── Calculate final NAV from underlying assets├── Update onchain NAV in metadata├── Process all pending subscriptions atomically│ └── USDC → Fund Shares at exact NAV├── Process all pending redemptions atomically│ └── Fund Shares → USDC at exact NAV└── Emit strike completion event✅ STRIKE COMPLETE (all trades at same NAV)
阶段四:定价时点后处理
- 生成交易确认单
- 更新基金构成
- 准备下一个定价时点
- 与托管方进行对账
委托权限模式
投资者预先授权基金管理员转移其代币,管理员在定价时点执行操作,无需投资者签名:
┌──────────┐ delegates ┌────────────────────┐ delegates ┌──────────┐│ Investor │ ───────────────→ │ Fund Administrator │ ←────────────── │ Investor ││ A │ (USDC) │ (trusted) │ (shares) │ B │└──────────┘ │ │ └──────────┘│ executes strike ││ (atomic txns) │└────────────────────┘
认购流程(USDC → 基金份额)
Investor wants to invest $100,000 USDCCurrent NAV = $1.000234 per share────────────────────────────────────1. Investor approves $100,000 USDC delegation2. At strike time, atomic transaction:├── Transfer $100,000 USDC from investor├── Calculate shares: 100,000 / 1.000234 = 99,976.61└── Mint 99,976.61 fund shares to investor3. Settlement complete in <1 second
赎回流程(基金份额 → USDC)
Investor wants to redeem 50,000 sharesCurrent NAV = $1.000234 per share────────────────────────────────────1. Investor approves 50,000 shares delegation2. At strike time, atomic transaction:├── Burn 50,000 fund shares from investor├── Calculate USDC: 50,000 × 1.000234 = $50,011.70└── Transfer $50,011.70 USDC to investor3. Settlement complete in <1 second
为何选择 Solana 实现 NAV 定价时点?
Solana 的架构相较于传统基金结算提供了显著优势:
| 对比维度 | 传统方式(T+1) | Solana NAV 定价时点 |
|---|---|---|
| 结算逻辑 | 转账代理机构 | 原子化交易打包 |
| 结算时间 | 1-2 天 | <1 秒 |
| 交易成本 | 50-200 美元 | <0.01 美元 |
| 每日定价时点数 | 1 次 | 4 次以上(可配置) |
| 价格陈旧风险 | 高 | 较低 |
| NAV 透明度 | 日终报告 | 链上实时可查 |
Solana 的原子化交易消除了中间环节,同时提供即时、低成本、安全的结算,并实现链上 NAV 透明度。
使用 Token 2022 扩展的基金代币
SPL Token 2022 提供了强大的扩展功能,无需自定义程序即可实现合规的基金份额发行:
基金份额所需的核心扩展
- 默认账户状态扩展:将所有新 token account 默认设为冻结状态,需经显式白名单审批(KYC/AML 合规)
- 元数据扩展:将 NAV、定价时点计划和 AUM 存储于链上,确保透明度
权限配置
- 铸造权限:基金管理员(控制份额发行)
- 冻结权限:基金管理员(管理白名单)
- 更新权限:基金管理员(更新 NAV 元数据)
默认冻结状态对于监管合规至关重要。它确保只有经过显式白名单审批(KYC 验证)的地址才能接收和持有基金份额。
链上元数据字段
{"name": "Example Money Market Fund","symbol": "EX-MMF","currentNAV": "1.000234","lastStrikeTime": "2024-01-15T14:30:00Z","strikeSchedule": "[\"09:30\", \"12:00\", \"14:30\", \"16:00\"]","totalAUM": "50000000.00","fundType": "Money Market Fund"}
完整的 NAV 定价时点实现
搭建 NAV 定价时点引擎
请注意,本指南使用 @solana/kit 库来创建 NAV 定价时点引擎。您也可以在
源代码中找到 web3.js 的实现版本。首先,创建负责处理所有基金操作的核心引擎类:
/*** NAV Strikes Engine - Solana Kit Reference Implementation** This implementation demonstrates NAV strikes for money market funds on Solana using:* - SPL Token 2022 with Default Account State extension for fund shares* - Standard USDC for settlement* - Atomic transactions for subscription/redemption* - Delegated authority pattern for fund administrator** Built with @solana/kit (web3.js 2.0)** ⚠️ IMPORTANT: This is a reference implementation for educational purposes.* Do NOT use in production without proper audits and security reviews.*/import {Address,airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,KeyPairSigner,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners,Rpc,RpcSubscriptions,SolanaRpcApi,SolanaRpcSubscriptionsApi} from "@solana/kit";import { getCreateAccountInstruction } from "@solana-program/system";import {TOKEN_PROGRAM_ADDRESS,getApproveInstruction,getTransferCheckedInstruction,getMintToInstruction,getBurnInstruction,findAssociatedTokenPda,getCreateAssociatedTokenInstruction} from "@solana-program/token";import {TOKEN_2022_PROGRAM_ADDRESS,getInitializeMintInstruction,getInitializeTokenMetadataInstruction,getUpdateTokenMetadataFieldInstruction,tokenMetadataField,getThawAccountInstruction,getFreezeAccountInstruction,AccountState,getMintSize,extension,getPreInitializeInstructionsForMintExtensions,fetchMaybeToken} from "@solana-program/token-2022";import { pack } from "@solana/spl-token-metadata";import { PublicKey } from "@solana/web3.js";// TLV (Type-Length-Value) sizes for Token-2022 extensionsconst TYPE_SIZE = 2;const LENGTH_SIZE = 2;import type {FundTokenConfig,FundState,SubscriptionParams,RedemptionParams,SubscriptionResult,RedemptionResult,StrikeResult,StrikeOrder,SolanaClient} from "./types";/*** Cluster type for explorer links*/export type ClusterType = "mainnet-beta" | "devnet" | "testnet" | "localnet";/*** Generate Solana Explorer link for a transaction*/export function getExplorerLink(signature: string,cluster: ClusterType = "localnet"): string {const baseUrl = "https://explorer.solana.com/tx";if (cluster === "localnet") {return `${baseUrl}/${signature}?cluster=custom&customUrl=http%3A%2F%2Flocalhost%3A8899`;}return `${baseUrl}/${signature}?cluster=${cluster}`;}/*** Generate Solana Explorer link for an account/address*/export function getAddressExplorerLink(addr: string,cluster: ClusterType = "localnet"): string {const baseUrl = "https://explorer.solana.com/address";if (cluster === "localnet") {return `${baseUrl}/${addr}?cluster=custom&customUrl=http%3A%2F%2Flocalhost%3A8899`;}return `${baseUrl}/${addr}?cluster=${cluster}`;}/*** Create a Solana client for Kit*/export async function createSolanaClient(rpcUrl: string = "http://127.0.0.1:8899",wsUrl: string = "ws://127.0.0.1:8900"): Promise<SolanaClient> {const rpc = createSolanaRpc(rpcUrl);const rpcSubscriptions = createSolanaRpcSubscriptions(wsUrl);const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({rpc,rpcSubscriptions});return {rpc,rpcSubscriptions,sendAndConfirmTransaction};}/*** NAV Strike Engine - Solana Kit Version** Manages the lifecycle of a money market fund on Solana:* - Creates fund tokens with Token 2022 extensions* - Updates NAV at scheduled strike times* - Processes subscription and redemption orders atomically*/export class NAVStrikeEngine {private client: SolanaClient;private fundAdministrator: KeyPairSigner;private cluster: ClusterType;// Fund stateprivate currentNAV: number = 1.0;private totalAUM: number = 0;private totalSharesOutstanding: number = 0;private strikeSchedule: string[] = [];private lastStrikeTime: Date = new Date();// Order queueprivate pendingOrders: StrikeOrder[] = [];private orderCounter: number = 0;constructor(client: SolanaClient,fundAdministrator: KeyPairSigner,cluster: ClusterType = "localnet") {this.client = client;this.fundAdministrator = fundAdministrator;this.cluster = cluster;}/*** Helper to send and confirm transactions with correct typing* Works around stricter types in @solana/kit v5+*/// eslint-disable-next-line @typescript-eslint/no-explicit-anyprivate async sendTransaction(signedTx: any,commitment: "confirmed" | "finalized" = "confirmed"): Promise<void> {await this.client.sendAndConfirmTransaction(signedTx, { commitment });}/*** Get explorer link for a transaction signature*/getTxExplorerLink(signature: string): string {return getExplorerLink(signature, this.cluster);}/*** Get explorer link for an address*/getAddressLink(addr: string): string {return getAddressExplorerLink(addr, this.cluster);}/*** Creates a fund share token using Token-2022 with Default Account State and Metadata extensions* Fund shares are frozen by default and require whitelisting* Metadata stores NAV and fund information onchain*/async createFundToken(issuer: KeyPairSigner,config: FundTokenConfig): Promise<Address> {console.log("\n╔══════════════════════════════════════════════════════════════╗");console.log("║ NAV STRIKE - FUND CREATION (Kit) ║");console.log("╚══════════════════════════════════════════════════════════════╝");console.log(`\n🏗️ Creating fund token with Token-2022 + Metadata...`);console.log(` Name: ${config.name}`);console.log(` Symbol: ${config.symbol}`);console.log(` Initial NAV: $${config.initialNAV.toFixed(6)}`);console.log(` Strike Schedule: ${config.strikeSchedule.join(", ")}`);// Generate new keypair for the mintconst mint = await generateKeyPairSigner();const decimals = config.decimals ?? 6;// Define extensionsconst defaultAccountStateExtension = extension("DefaultAccountState", {state: AccountState.Frozen});const metadataPointerExtension = extension("MetadataPointer", {authority: this.fundAdministrator.address,metadataAddress: mint.address});const extensions = [defaultAccountStateExtension, metadataPointerExtension];// Calculate mint size without metadataconst baseMintSize = getMintSize(extensions);// Create metadata object to calculate EXACT size using pack()// Note: PublicKey is only used here for size calculation, not for transaction buildingconst metadataForSizing = {mint: new PublicKey(mint.address),name: config.name,symbol: config.symbol,uri: config.uri || "",additionalMetadata: [["icon-uri", "link to icon"], // Max NAV format["currentNAV", "999999.999999"], // Max NAV format["lastStrikeTime", "2099-12-31T23:59:59.999Z"], // Max ISO timestamp["strikeSchedule", JSON.stringify(config.strikeSchedule)],["totalAUM", "999999999999999.99"], // Max AUM (quadrillions)["fundType", "Money Market Fund"]] as [string, string][]};// Calculate exact metadata size using pack()const metadataLen = pack(metadataForSizing).length;// MetadataExtension TLV overhead: 2 bytes for type, 2 bytes for lengthconst metadataExtensionOverhead = TYPE_SIZE + LENGTH_SIZE;// Add safety buffer for any encoding overheadconst totalSpace =baseMintSize + metadataLen + metadataExtensionOverhead + 100;// Get rent for total spaceconst mintRent = await this.client.rpc.getMinimumBalanceForRentExemption(BigInt(totalSpace)).send();// Build create account instruction (with base size, but extra rent for metadata)const createAccountIx = getCreateAccountInstruction({payer: issuer,newAccount: mint,lamports: lamports(mintRent),space: baseMintSize,programAddress: TOKEN_2022_PROGRAM_ADDRESS});// Get extension initialization instructionsconst preInitIxs = getPreInitializeInstructionsForMintExtensions(mint.address,extensions);// Initialize mint instruction (Token 2022)const initMintIx = getInitializeMintInstruction({mint: mint.address,decimals,mintAuthority: this.fundAdministrator.address,freezeAuthority: this.fundAdministrator.address});// Initialize metadata instructionconst initMetadataIx = getInitializeTokenMetadataInstruction({metadata: mint.address,updateAuthority: this.fundAdministrator.address,mint: mint.address,mintAuthority: this.fundAdministrator,name: config.name,symbol: config.symbol,uri: ""});// Build custom metadata field update instructionsconst initialMetadata: [string, string][] = [["currentNAV", config.initialNAV.toFixed(6)],["lastStrikeTime", new Date().toISOString()],["strikeSchedule", JSON.stringify(config.strikeSchedule)],["totalAUM", "0.00"],["fundType", "Money Market Fund"]];const updateFieldIxs = initialMetadata.map(([key, value]) =>getUpdateTokenMetadataFieldInstruction({metadata: mint.address,updateAuthority: this.fundAdministrator,field: tokenMetadataField("Key", [key]),value}));// Get latest blockhashconst { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();// Build transaction messageconst transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(issuer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) =>appendTransactionMessageInstructions([createAccountIx,...preInitIxs,initMintIx,initMetadataIx,...updateFieldIxs],tx));// Sign and sendconst signedTransaction =await signTransactionMessageWithSigners(transactionMessage);const signature = getSignatureFromTransaction(signedTransaction);await this.sendTransaction(signedTransaction);// Update internal statethis.currentNAV = config.initialNAV;this.strikeSchedule = config.strikeSchedule;this.lastStrikeTime = new Date();console.log(`\n✅ Fund token created: ${mint.address}`);console.log(` Mint Authority: ${this.fundAdministrator.address}`);console.log(` Freeze Authority: ${this.fundAdministrator.address}`);console.log(` Default State: FROZEN (requires whitelisting)`);console.log(` ✨ Metadata: ON-CHAIN`);console.log(` - NAV: $${config.initialNAV.toFixed(6)}`);console.log(` - Schedule: ${config.strikeSchedule.join(", ")}`);console.log(` 🔗 Token: ${this.getAddressLink(mint.address)}`);console.log(` 🔗 Tx: ${this.getTxExplorerLink(signature)}`);return mint.address;}
动态元数据更新
对于基金份额,currentNAV、lastStrikeTime 和 totalAUM 字段在每次 NAV 定价时点时使用
getUpdateTokenMetadataFieldInstruction 在链上更新。这提供了:
- 实时 NAV 可见性,所有参与方均可查看
- 所有 NAV 更新的链上审计追踪
- 与可读取链上元数据的 DeFi 协议的集成
/*** Updates NAV onchain in token metadata*/async updateNAV(fundMint: Address, newNAV: number): Promise<string> {const previousNAV = this.currentNAV;this.currentNAV = newNAV;this.lastStrikeTime = new Date();// Build update field instructionsconst updateNavIx = getUpdateTokenMetadataFieldInstruction({metadata: fundMint,updateAuthority: this.fundAdministrator,field: tokenMetadataField("Key", ["currentNAV"]),value: newNAV.toFixed(6)});const updateTimeIx = getUpdateTokenMetadataFieldInstruction({metadata: fundMint,updateAuthority: this.fundAdministrator,field: tokenMetadataField("Key", ["lastStrikeTime"]),value: this.lastStrikeTime.toISOString()});const updateAumIx = getUpdateTokenMetadataFieldInstruction({metadata: fundMint,updateAuthority: this.fundAdministrator,field: tokenMetadataField("Key", ["totalAUM"]),value: this.totalAUM.toFixed(2)});const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();const transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(this.fundAdministrator, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) =>appendTransactionMessageInstructions([updateNavIx, updateTimeIx, updateAumIx],tx));const signedTransaction =await signTransactionMessageWithSigners(transactionMessage);const signature = getSignatureFromTransaction(signedTransaction);await this.sendTransaction(signedTransaction);const navChange = ((newNAV - previousNAV) / previousNAV) * 100;const changeSymbol = navChange >= 0 ? "▲" : "▼";console.log(` NAV Updated: $${previousNAV.toFixed(6)} → $${newNAV.toFixed(6)} (${changeSymbol}${Math.abs(navChange).toFixed(4)}%)`);console.log(` 🔗 Explorer: ${this.getTxExplorerLink(signature)}`);return signature;}/*** Whitelists an investor by creating their fund account and thawing it*/async whitelistInvestor(fundMint: Address,investor: Address,payer: KeyPairSigner): Promise<Address> {console.log(`\n🔓 Whitelisting investor: ${investor.slice(0, 20)}...`);// Find the ATAconst [ata] = await findAssociatedTokenPda({mint: fundMint,owner: investor,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const maybeToken = await fetchMaybeToken(this.client.rpc, ata, {commitment: "confirmed"});const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();if (!maybeToken.exists) {// Create ATAconst createAtaIx = getCreateAssociatedTokenInstruction({payer,owner: investor,mint: fundMint,ata,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const createAtaMsg = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(payer, tx),(tx) =>setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions([createAtaIx], tx));const signedCreateAta =await signTransactionMessageWithSigners(createAtaMsg);await this.sendTransaction(signedCreateAta);}// Thaw the accountconst thawIx = getThawAccountInstruction({account: ata,mint: fundMint,owner: this.fundAdministrator});const thawMsg = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(payer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions([thawIx], tx));const signedThaw = await signTransactionMessageWithSigners(thawMsg);await this.sendTransaction(signedThaw);console.log(` Account: ${ata}`);console.log(`✅ Investor whitelisted and account thawed`);return ata;}/*** Removes an investor from whitelist by freezing their account*/async removeFromWhitelist(fundMint: Address,investorFundAccount: Address,payer: KeyPairSigner): Promise<void> {console.log(`\n🔒 Removing from whitelist: ${investorFundAccount}`);const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();const freezeIx = getFreezeAccountInstruction({account: investorFundAccount,mint: fundMint,owner: this.fundAdministrator});const freezeMsg = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(payer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions([freezeIx], tx));const signedFreeze = await signTransactionMessageWithSigners(freezeMsg);await this.sendTransaction(signedFreeze);console.log(`✅ Account frozen and removed from whitelist`);}/*** Delegates USDC authority to fund administrator for subscription*/async delegateUSDCForSubscription(investor: KeyPairSigner,usdcMint: Address,amount: number): Promise<void> {const [investorUSDC] = await findAssociatedTokenPda({mint: usdcMint,owner: investor.address,tokenProgram: TOKEN_PROGRAM_ADDRESS});console.log(`\n🤝 Delegating USDC for subscription...`);console.log(` Amount: $${amount.toLocaleString()}`);const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();const approveIx = getApproveInstruction({source: investorUSDC,delegate: this.fundAdministrator.address,owner: investor,amount: BigInt(amount * 1e6)});const approveMsg = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(investor, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions([approveIx], tx));const signedApprove = await signTransactionMessageWithSigners(approveMsg);await this.sendTransaction(signedApprove);console.log(`✅ USDC delegation approved`);}/*** Delegates fund shares authority to administrator for redemption*/async delegateSharesForRedemption(investor: KeyPairSigner,fundMint: Address,shareAmount: number): Promise<void> {const [investorShares] = await findAssociatedTokenPda({mint: fundMint,owner: investor.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});console.log(`\n🤝 Delegating shares for redemption...`);console.log(` Shares: ${shareAmount.toLocaleString()}`);const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();const approveIx = getApproveInstruction({source: investorShares,delegate: this.fundAdministrator.address,owner: investor,amount: BigInt(Math.floor(shareAmount * 1e6))},{ programAddress: TOKEN_2022_PROGRAM_ADDRESS });const approveMsg = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(investor, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions([approveIx], tx));const signedApprove = await signTransactionMessageWithSigners(approveMsg);await this.sendTransaction(signedApprove);console.log(`✅ Share delegation approved`);}/*** Executes atomic subscription: USDC → Fund Shares at current NAV*/async processSubscription(params: SubscriptionParams): Promise<SubscriptionResult> {const { fundMint, usdcMint, investor, usdcAmount } = params;// Calculate shares at current NAVconst sharesToMint = usdcAmount / this.currentNAV;const shortAddr = `${investor.slice(0, 4)}...${investor.slice(-4)}`;console.log(`\n⚡ Processing subscription...`);console.log(` Investor: ${shortAddr}`);console.log(` USDC Amount: $${usdcAmount.toLocaleString()}`);console.log(` NAV: $${this.currentNAV.toFixed(6)}`);console.log(` Shares to mint: ${sharesToMint.toFixed(6)}`);// Get token accountsconst [investorUSDC] = await findAssociatedTokenPda({mint: usdcMint,owner: investor,tokenProgram: TOKEN_PROGRAM_ADDRESS});const [fundUSDC] = await findAssociatedTokenPda({mint: usdcMint,owner: this.fundAdministrator.address,tokenProgram: TOKEN_PROGRAM_ADDRESS});const [investorShares] = await findAssociatedTokenPda({mint: fundMint,owner: investor,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();// Build atomic transaction with both instructions// 1. Transfer USDC from investor to fund (using delegated authority)const transferUSDCIx = getTransferCheckedInstruction({source: investorUSDC,mint: usdcMint,destination: fundUSDC,authority: this.fundAdministrator,amount: BigInt(Math.floor(usdcAmount * 1e6)),decimals: 6});// 2. Mint fund shares to investorconst mintSharesIx = getMintToInstruction({mint: fundMint,token: investorShares,mintAuthority: this.fundAdministrator,amount: BigInt(Math.floor(sharesToMint * 1e6))},{ programAddress: TOKEN_2022_PROGRAM_ADDRESS });const txMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(this.fundAdministrator, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) =>appendTransactionMessageInstructions([transferUSDCIx, mintSharesIx], tx) // <- Both happen at the same time);const signedTx = await signTransactionMessageWithSigners(txMessage);const signature = getSignatureFromTransaction(signedTx);await this.sendTransaction(signedTx);// Update fund statethis.totalAUM += usdcAmount;this.totalSharesOutstanding += sharesToMint;console.log(`✅ SUBSCRIPTION SETTLED ATOMICALLY`);console.log(` Shares issued: ${sharesToMint.toFixed(6)}`);console.log(` New AUM: $${this.totalAUM.toLocaleString()}`);console.log(` 🔗 Explorer: ${this.getTxExplorerLink(signature)}`);return {signature,usdcAmount,sharesIssued: sharesToMint,executionNAV: this.currentNAV,timestamp: new Date()};}/*** Executes atomic redemption: Fund Shares → USDC at current NAV*/async processRedemption(params: RedemptionParams): Promise<RedemptionResult> {const { fundMint, usdcMint, investor, shareAmount } = params;// Calculate USDC at current NAVconst usdcToPay = shareAmount * this.currentNAV;const shortAddr = `${investor.slice(0, 4)}...${investor.slice(-4)}`;console.log(`\n⚡ Processing redemption...`);console.log(` Investor: ${shortAddr}`);console.log(` Shares to redeem: ${shareAmount.toLocaleString()}`);console.log(` NAV: $${this.currentNAV.toFixed(6)}`);console.log(` USDC to pay: $${usdcToPay.toFixed(2)}`);// Get token accountsconst [investorShares] = await findAssociatedTokenPda({mint: fundMint,owner: investor,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});const [investorUSDC] = await findAssociatedTokenPda({mint: usdcMint,owner: investor,tokenProgram: TOKEN_PROGRAM_ADDRESS});const [fundUSDC] = await findAssociatedTokenPda({mint: usdcMint,owner: this.fundAdministrator.address,tokenProgram: TOKEN_PROGRAM_ADDRESS});const { value: latestBlockhash } = await this.client.rpc.getLatestBlockhash().send();// Build atomic transaction with both instructions// 1. Burn fund shares from investor (using delegated authority)const burnSharesIx = getBurnInstruction({account: investorShares,mint: fundMint,authority: this.fundAdministrator,amount: BigInt(Math.floor(shareAmount * 1e6))},{ programAddress: TOKEN_2022_PROGRAM_ADDRESS });// 2. Transfer USDC from fund to investorconst transferUSDCIx = getTransferCheckedInstruction({source: fundUSDC,mint: usdcMint,destination: investorUSDC,authority: this.fundAdministrator,amount: BigInt(Math.floor(usdcToPay * 1e6)),decimals: 6});const txMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(this.fundAdministrator, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) =>appendTransactionMessageInstructions([burnSharesIx, transferUSDCIx], tx));const signedTx = await signTransactionMessageWithSigners(txMessage);const signature = getSignatureFromTransaction(signedTx);await this.sendTransaction(signedTx);// Update fund statethis.totalAUM -= usdcToPay;this.totalSharesOutstanding -= shareAmount;console.log(`✅ REDEMPTION SETTLED ATOMICALLY`);console.log(` USDC paid: $${usdcToPay.toFixed(2)}`);console.log(` New AUM: $${this.totalAUM.toLocaleString()}`);console.log(` 🔗 Explorer: ${this.getTxExplorerLink(signature)}`);return {signature,sharesRedeemed: shareAmount,usdcPaid: usdcToPay,executionNAV: this.currentNAV,timestamp: new Date()};}/*** Queue an order for the next NAV strike*/queueOrder(investor: Address,orderType: "subscribe" | "redeem",amount: number): StrikeOrder {const order: StrikeOrder = {orderId: `ORD-${++this.orderCounter}`,investor,orderType,amount,strikeTime: this.getNextStrikeTime(),status: "pending"};this.pendingOrders.push(order);const shortAddr = `${investor.slice(0, 4)}...${investor.slice(-4)}`;console.log(`\n📝 Order queued: ${order.orderId}`);console.log(` Investor: ${shortAddr}`);console.log(` Type: ${orderType}`);console.log(` Amount: ${orderType === "subscribe"? `$${amount.toLocaleString()} USDC`: `${amount.toLocaleString()} shares`}`);return order;}/*** Execute NAV strike - update NAV and process all pending orders*/async executeStrike(fundMint: Address,usdcMint: Address,newNAV: number): Promise<StrikeResult> {const strikeId = `STRIKE-${Date.now()}`;const strikeTime = new Date();console.log("\n╔══════════════════════════════════════════════════════════════╗");console.log("║ NAV STRIKE EXECUTION ║");console.log("╚══════════════════════════════════════════════════════════════╝");console.log(` Strike ID: ${strikeId}`);console.log(` Strike Time: ${strikeTime.toISOString()}`);console.log(` New NAV: $${newNAV.toFixed(6)}`);console.log(` Pending Orders: ${this.pendingOrders.length}`);console.log("────────────────────────────────────────────────────────────────");// 1. Update NAVawait this.updateNAV(fundMint, newNAV);// 2. Process pending ordersconst signatures: string[] = [];let totalUSDCSubscribed = 0;let totalSharesMinted = 0;let totalSharesRedeemed = 0;let totalUSDCPaid = 0;let subscriptionsProcessed = 0;let redemptionsProcessed = 0;const subscriptions = this.pendingOrders.filter((o) => o.orderType === "subscribe" && o.status === "pending");const redemptions = this.pendingOrders.filter((o) => o.orderType === "redeem" && o.status === "pending");// Process subscriptionsconsole.log(`\n📥 Processing ${subscriptions.length} subscriptions...`);for (const order of subscriptions) {try {const result = await this.processSubscription({fundMint,usdcMint,investor: order.investor,usdcAmount: order.amount});order.status = "executed";signatures.push(result.signature);totalUSDCSubscribed += result.usdcAmount;totalSharesMinted += result.sharesIssued;subscriptionsProcessed++;} catch (error) {console.error(` ❌ Order ${order.orderId} failed:`, error);order.status = "failed";}}// Process redemptionsconsole.log(`\n📤 Processing ${redemptions.length} redemptions...`);for (const order of redemptions) {try {const result = await this.processRedemption({fundMint,usdcMint,investor: order.investor,shareAmount: order.amount});order.status = "executed";signatures.push(result.signature);totalSharesRedeemed += result.sharesRedeemed;totalUSDCPaid += result.usdcPaid;redemptionsProcessed++;} catch (error) {console.error(` ❌ Order ${order.orderId} failed:`, error);order.status = "failed";}}// Clear executed ordersthis.pendingOrders = this.pendingOrders.filter((o) => o.status === "pending");// Print summaryconsole.log("\n════════════════════════════════════════════════════════════════");console.log(" STRIKE SUMMARY ");console.log("════════════════════════════════════════════════════════════════");console.log(` Subscriptions: ${subscriptionsProcessed} processed`);console.log(` Total USDC In: $${totalUSDCSubscribed.toLocaleString()}`);console.log(` Shares Minted: ${totalSharesMinted.toFixed(2)}`);console.log(` Redemptions: ${redemptionsProcessed} processed`);console.log(` Shares Burned: ${totalSharesRedeemed.toFixed(2)}`);console.log(` Total USDC Out: $${totalUSDCPaid.toLocaleString()}`);console.log(` New AUM: $${this.totalAUM.toLocaleString()}`);console.log(` Shares Outstanding: ${this.totalSharesOutstanding.toFixed(2)}`);console.log("════════════════════════════════════════════════════════════════\n");return {strikeId,strikeTime,nav: this.currentNAV,subscriptionsProcessed,totalUSDCSubscribed,totalSharesMinted,redemptionsProcessed,totalSharesRedeemed,totalUSDCPaid,signatures};}/*** Get current fund state*/getFundState(fundMint: Address): FundState {return {fundMint,currentNAV: this.currentNAV,lastStrikeTime: this.lastStrikeTime,totalAUM: this.totalAUM,totalSharesOutstanding: this.totalSharesOutstanding};}/*** Get current NAV*/getCurrentNAV(): number {return this.currentNAV;}/*** Get pending orders*/getPendingOrders(): StrikeOrder[] {return [...this.pendingOrders];}/*** Calculate next strike time based on schedule*/getNextStrikeTime(): Date {const now = new Date();const currentTime = `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}`;for (const strikeTime of this.strikeSchedule) {if (strikeTime > currentTime) {const [hours, minutes] = strikeTime.split(":");const strikeDate = new Date(now);strikeDate.setHours(parseInt(hours), parseInt(minutes), 0, 0);return strikeDate;}}// Next strike is tomorrow's first strikeconst tomorrow = new Date(now);tomorrow.setDate(tomorrow.getDate() + 1);const [hours, minutes] = this.strikeSchedule[0].split(":");tomorrow.setHours(parseInt(hours), parseInt(minutes), 0, 0);return tomorrow;}/*** Airdrops SOL for testing*/async airdropSol(publicKey: Address, amount: number): Promise<void> {console.log(`\n💰 Airdropping ${amount} SOL to ${publicKey.slice(0, 20)}...`);const airdrop = airdropFactory({rpc: this.client.rpc,rpcSubscriptions: this.client.rpcSubscriptions});await airdrop({recipientAddress: publicKey,lamports: lamports(BigInt(amount * 1_000_000_000)),commitment: "confirmed"});console.log(`✅ Airdrop complete`);}}
完整使用示例
以下是演示完整一天 NAV 定价时点操作的完整示例:
/*** NAV Strikes Demo - Solana Kit Version** Demonstrates multiple daily NAV strikes for a money market fund on Solana.* Built with @solana/kit (web3.js 2.0)** Run with: npm run demo:kit* Requires: solana-test-validator running locally** ⚠️ EDUCATIONAL REFERENCE ONLY - NOT FOR PRODUCTION USE*/import {airdropFactory,generateKeyPairSigner,lamports,KeyPairSigner,Address} from "@solana/kit";import {NAVStrikeEngine,createSolanaClient,getExplorerLink} from "../nav-strike-engine";import {createTestUSDC,mintTestUSDC,getUSDCBalance,getFundShareBalance} from "../test-usdc";import type { SolanaClient } from "../types";/*** Helper to print section headers*/function printHeader(title: string): void {console.log("\n");console.log("╔══════════════════════════════════════════════════════════════╗");console.log(`║ ${title.padEnd(60)}║`);console.log("╚══════════════════════════════════════════════════════════════╝");}/*** Print final balances for all participants*/async function printBalances(client: SolanaClient,fundMint: Address,usdcMint: Address,participants: { name: string; address: Address }[]): Promise<void> {console.log("\n┌─────────────────────────────────────────────────────────────────┐");console.log("│ FINAL BALANCES │");console.log("├─────────────────────────────────────────────────────────────────┤");for (const { name, address } of participants) {const usdcBalance = await getUSDCBalance(client, usdcMint, address);const shareBalance = await getFundShareBalance(client, fundMint, address);console.log(`│ ${name.padEnd(15)} USDC: $${usdcBalance.toFixed(2).padStart(10)} │ Shares: ${shareBalance.toFixed(2).padStart(10)} │`);}console.log("└─────────────────────────────────────────────────────────────────┘");// Cost comparisonconsole.log("\n┌─────────────────────────────────────────────────────────────────┐");console.log("│ COST COMPARISON │");console.log("├─────────────────────────────────────────────────────────────────┤");console.log("│ Traditional Solana NAV Strikes │");console.log("│ ───────────────────────────────────────────────────────────── │");console.log("│ Strikes/Day: 1 (4PM) 4+ (configurable)│");console.log("│ Settlement: T+1/T+2 Instant │");console.log("│ Pricing: Unknown til 4PM Exact NAV at strike │");console.log("│ Compliance: Manual KYC/AML Onchain whitelist │");console.log("│ Settlement Risk: High None │");console.log("│ Audit Trail: Manual Blockchain │");console.log("└─────────────────────────────────────────────────────────────────┘");}/*** Main demo function*/async function main(): Promise<void> {console.log(`╔══════════════════════════════════════════════════════════════════╗║ ║║ ███╗ ██╗ █████╗ ██╗ ██╗ ███████╗████████╗██████╗ ║║ ████╗ ██║██╔══██╗██║ ██║ ██╔════╝╚══██╔══╝██╔══██╗ ║║ ██╔██╗ ██║███████║██║ ██║ ███████╗ ██║ ██████╔╝ ║║ ██║╚██╗██║██╔══██║╚██╗ ██╔╝ ╚════██║ ██║ ██╔══██╗ ║║ ██║ ╚████║██║ ██║ ╚████╔╝ ███████║ ██║ ██║ ██║ ║║ ╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═══╝ ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ║║ ║║ NAV STRIKES - Solana Kit Reference Implementation ║║ Built with @solana/kit (web3.js 2.0) ║║ ║╚══════════════════════════════════════════════════════════════════╝`);// ─────────────────────────────────────────────────────────────────// SETUP: Connect to local validator// ─────────────────────────────────────────────────────────────────printHeader("SETUP: Connecting to Local Validator");const client = await createSolanaClient();console.log("✅ Connected to local validator (http://127.0.0.1:8899)");// Create keypairsconst fundAdmin = await generateKeyPairSigner();const investorA = await generateKeyPairSigner();const investorB = await generateKeyPairSigner();console.log(`\n👤 Fund Administrator: ${fundAdmin.address}`);console.log(`👤 Investor A: ${investorA.address}`);console.log(`👤 Investor B: ${investorB.address}`);// Create airdrop functionconst airdrop = airdropFactory({rpc: client.rpc,rpcSubscriptions: client.rpcSubscriptions});// Fund accounts with SOLconsole.log("\n💰 Airdropping SOL to accounts...");await airdrop({recipientAddress: fundAdmin.address,lamports: lamports(10_000_000_000n),commitment: "confirmed"});console.log(" ✅ Fund Admin: 10 SOL");await airdrop({recipientAddress: investorA.address,lamports: lamports(2_000_000_000n),commitment: "confirmed"});console.log(" ✅ Investor A: 2 SOL");await airdrop({recipientAddress: investorB.address,lamports: lamports(2_000_000_000n),commitment: "confirmed"});console.log(" ✅ Investor B: 2 SOL");// ─────────────────────────────────────────────────────────────────// STEP 1: Create Test USDC// ─────────────────────────────────────────────────────────────────printHeader("STEP 1: Creating Test USDC");const usdcMint = await createTestUSDC(client, fundAdmin, fundAdmin);// Mint USDC to participantsawait mintTestUSDC(client,usdcMint,fundAdmin,investorA.address,500,"Investor A");await mintTestUSDC(client,usdcMint,fundAdmin,investorB.address,300,"Investor B");await mintTestUSDC(client,usdcMint,fundAdmin,fundAdmin.address,1000,"Fund Admin");// ─────────────────────────────────────────────────────────────────// STEP 2: Create NAV Strike Engine & Fund Token// ─────────────────────────────────────────────────────────────────printHeader("STEP 2: Creating Fund & NAV Strike Engine");const engine = new NAVStrikeEngine(client, fundAdmin, "localnet");const fundMint = await engine.createFundToken(fundAdmin, {name: "Example Money Market Fund",symbol: "EX-MMF",uri: "Link to of chain meta data",initialNAV: 1.0,strikeSchedule: ["09:30", "12:00", "14:30", "16:00"],decimals: 6});// ─────────────────────────────────────────────────────────────────// STEP 3: Whitelist Investors// ─────────────────────────────────────────────────────────────────printHeader("STEP 3: Whitelisting Investors (KYC/AML)");await engine.whitelistInvestor(fundMint, investorA.address, fundAdmin);await engine.whitelistInvestor(fundMint, investorB.address, fundAdmin);// ─────────────────────────────────────────────────────────────────// STRIKE 1: 9:30 AM - Initial Subscriptions// ─────────────────────────────────────────────────────────────────printHeader("STRIKE 1: 9:30 AM - Initial Subscriptions");// Investors delegate USDC and queue ordersawait engine.delegateUSDCForSubscription(investorA, usdcMint, 250);engine.queueOrder(investorA.address, "subscribe", 250);await engine.delegateUSDCForSubscription(investorB, usdcMint, 150);engine.queueOrder(investorB.address, "subscribe", 150);// Execute strike at $1.00 NAVawait engine.executeStrike(fundMint, usdcMint, 1.0);// ─────────────────────────────────────────────────────────────────// STRIKE 2: 12:00 PM - Additional Subscription// ─────────────────────────────────────────────────────────────────printHeader("STRIKE 2: 12:00 PM - Additional Subscription");// Investor A adds moreawait engine.delegateUSDCForSubscription(investorA, usdcMint, 100);engine.queueOrder(investorA.address, "subscribe", 100);// Execute strike at $1.01 NAV (slight gain from interest)await engine.executeStrike(fundMint, usdcMint, 1.01);// ─────────────────────────────────────────────────────────────────// STRIKE 3: 2:30 PM - Partial Redemption// ─────────────────────────────────────────────────────────────────printHeader("STRIKE 3: 2:30 PM - Partial Redemption");// Investor B redeems 50 sharesawait engine.delegateSharesForRedemption(investorB, fundMint, 50);engine.queueOrder(investorB.address, "redeem", 50);// Execute strike at $1.02 NAVawait engine.executeStrike(fundMint, usdcMint, 1.02);// ─────────────────────────────────────────────────────────────────// STRIKE 4: 4:00 PM - End of Day// ─────────────────────────────────────────────────────────────────printHeader("STRIKE 4: 4:00 PM - End of Day");// No new orders, just NAV updateawait engine.executeStrike(fundMint, usdcMint, 1.03);// ─────────────────────────────────────────────────────────────────// FINAL: Print Balances// ─────────────────────────────────────────────────────────────────printHeader("FINAL RESULTS");await printBalances(client, fundMint, usdcMint, [{ name: "Fund Admin", address: fundAdmin.address },{ name: "Investor A", address: investorA.address },{ name: "Investor B", address: investorB.address }]);// Print fund stateconst fundState = engine.getFundState(fundMint);console.log("\n📊 Fund State:");console.log(` Current NAV: $${fundState.currentNAV.toFixed(6)}`);console.log(` Total AUM: $${fundState.totalAUM.toLocaleString()}`);console.log(` Shares Outstanding: ${fundState.totalSharesOutstanding.toFixed(2)}`);console.log(` Last Strike: ${fundState.lastStrikeTime.toISOString()}`);console.log("\n✅ Demo complete! NAV Strikes with Solana Kit working correctly.");console.log(" View transactions on Solana Explorer (local validator - links shown above)\n");}// Run the demomain().catch((error) => {console.error("❌ Demo failed:", error);process.exit(1);});
生产环境注意事项
在部署至生产环境之前,请确保解决以下问题:
- 安全性:专业的密钥管理、多签控制,以及对代码库进行全面安全审计
- 合规监管:证券注册、KYC/AML 系统、转让限制及法定报告要求
- 密钥管理:具备完善备份与恢复流程的机构级托管解决方案
- 交易处理:优先费用设置、重试逻辑、确认处理及 RPC 冗余
- 基金运营:来自权威数据源的 NAV 计算、托管方集成及对账
- 监控:实时追踪、告警及自动化合规报告
后续步骤
深入了解 Solana 上的 NAV 定价时点后,您可以:
-
探索 Token Extensions:了解其他 Token 2022 扩展,例如永久委托等,以获取更多合规功能
-
实现 Token ACL:为增强合规性,考虑使用 Token ACL 管理投资者白名单,支持无许可解冻。这可实现自助式 KYC 验证,投资者通过合规审查后可自动激活账户,在维持监管控制的同时降低运营负担。
-
学习 DvP 结算:参阅我们的 券款对付指南,了解证券结算模式
-
生产架构:设计稳健的调度、监控和灾难恢复系统
总结
Solana 的原子化交易模型与 Token 2022 扩展为货币市场基金实现多个每日 NAV 定价时点提供了强大基础。其优势组合包括:
- 原生原子化执行(无智能合约风险)
- 亚秒级最终确认
- 近乎为零的交易成本
- 链上 NAV 透明度
- 内置合规功能(默认冻结、元数据)
使 Solana 成为现代化基金结算基础设施的理想平台,为投资者提供更频繁的交易机会。
然而,从本教育性参考实现迈向生产环境,还需要在安全性、合规性、托管和运营等方面开展大量额外工作。在涉及基金代币化时,请务必与具备资质的法律、监管和技术专家合作。
Is this page helpful?