solana-anchor-claude-skill by quiknode-labs/solana-anchor-claude-skill
npx skills add https://github.com/quiknode-labs/solana-anchor-claude-skill --skill solana-anchor-claude-skill应用这些规则以确保代码质量、可维护性并遵守项目标准。
npm test。如果测试失败,说明还有工作要做。直到 npm test 在你编写的代码上通过之前,都不要停止。关键:占位符测试不计为成功。
仅仅执行 assert.ok(true) 或类似操作的测试不是真正的测试
在测试实际调用程序指令之前,不要将"编写测试"标记为完成
不要问"我现在应该编写真正的测试吗?"——如果测试是占位符,请立即编写真正的测试
真正的测试必须:初始化账户、发送交易、验证状态变化、检查余额
如果你发现自己正在编写占位符测试,请停止并转而编写真正的集成测试
总结工作时,使用符号 '✅' 展示你已完成的工作项,对于每个剩余的工作项,添加一个 '❌'。
使用这些官方文档来源:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
不要使用 Solana Labs 的文档。该公司已被 Anza 取代。
不要使用来自 Project Serum 的任何文档或工具,该项目多年前就已崩溃。
不要使用 yarn。请使用 npm。Yarn 没有存在的理由,只会增加不必要的依赖。在你看到的地方都用 npm 替换 Yarn。
不要使用 Switchboard Functions —— 该产品已失效且不再维护。(注意:Switchboard 预言机仍然活跃且可用。)
不要使用 Clockwork —— 该产品已失效。对于计划的指令处理程序调用,请改用 TukTuk。
尽可能使用最新的稳定版 Anchor、Rust、TypeScript、Solana Kit 和 Kite。如果出现错误,优先考虑更新而不是回滚。
每个项目必须在项目根目录下有一个 README.md 文件,其中包含:
npm test)保持 README 的专注性和实用性。避免通用的样板文件——编写真正能帮助他人理解和使用这个特定项目的文档。
你的黄金法则是"完美不在于无可增加,而在于无可删减"。
删除:
关键 - 使用前验证:
TODO,让下一个程序员知道何时可以删除变通方法)标记为临时变通方法确保良好的变量命名。与其添加注释来解释事物是什么,不如给它们起有用的名字。
不要这样做:
// Foo
const shlerg = getFoo();
应该这样做:
const foo = getFoo();
命名约定:
shoes),数组中的项应为单数形式 (shoes.forEach((shoe) => {...}))calculateFoo 或 getBarcontext 而不是 ctx)e 来表示抛出的东西transaction 的某种变体。将指令命名为 instruction 的某种变体。将签名命名为 signature 的某种变体。不要混淆它们——例如,如果类型看起来像指令,你不应该称它为 'transaction',因为那是欺骗性的。你仍然可以添加注释以提供额外上下文,但要注意避免那些通过良好的变量命名能更好传达信息的注释。
这是一个魔法数字。不要这样做:
const FINALIZE_EVENT_DISCRIMINATOR = new Uint8Array([
27, 75, 117, 221, 191, 213, 253, 249,
]);
应该这样做:
const FINALIZE_EVENT_DISCRIMINATOR = getEventDiscriminator(
arciumIdl,
"FinalizeComputationEvent",
);
// 在生产环境中我们会以不同方式处理 或 **实现不完整** - 需要程序配置处理和正确的 PDA 派生 或 **进行中** 这样的注释,也不应该有返回占位数据的函数。相反:把该做的工作做完。这些规范适用于 TypeScript 单元测试、浏览器代码以及项目中任何使用 TypeScript 的地方。
避免使用 tsconfig.json,除非确实需要,因为我们使用 tsx 来运行大多数 TypeScript,而它通常不需要。如果你确实需要一个 tsconfig.json,请在文件顶部说明原因,并且你可以使用你想要的 ECMAScript/JavaScript 的最新版本——最高到 2023 年。
优先使用 async/await 和 try/catch,而不是 .then() 或 .catch() 或使用回调进行流程控制。tsx 支持顶级 await,因此你不需要将顶级 await 包装在 IIFE 中。
Array<item>,为了与其他泛型语法(如 Promise<T>、Map<K, V> 和 Set<T>)保持一致,永远不要使用 item[]any// 并位于代码上方(而不是旁边)/* */ 语法@solana/web3.js 版本 1 代码。不要使用 @coral-xyz/anchor 包创建新代码。不要用 web3.js 版本 1 代码替换 Solana Kit。web3.js 版本 1 是遗留的,最终应该被移除。Solana Kit 以前被称为 web3.js 版本 2。请使用 Solana Kit,最好通过 Solana Kite 使用。connection.getPDAAndBump() 将种子转换为 PDA 和 bumpnpx create-codama-clients
bs58 npm 包。不要这样做:
import bs58 from "bs58";
const signature = bs58.encode(signatureBytes);
应该这样做:
import { getBase58Decoder } from "@solana/codecs";
const signature = getBase58Decoder().decode(signatureBytes);
是的,这些不同的包对 'encode' 和 'decode' 有不同的概念。
tests 目录中创建 TypeScript 单元测试tsx 而不是 ts-mocha 启动测试)单元测试导入:
import { before, describe, test } from "node:test";
import assert from "node:assert";
test 而不是 itJavaScript 允许任意项——字符串、数组、数字等被'抛出'。但是你可以假设任何抛出的非 Error 项都是程序员的错误。像这样处理它(包括注释,因为大多数 TypeScript 开发人员不知道这一点):
// 在 JS 中,可以抛出任何东西。理智的程序员
// 只会抛出 Errors,但我们仍然必须检查以满足
// TypeScript(并标记任何疯狂行为)
const ensureError = function (thrownObject: unknown): Error {
if (thrownObject instanceof Error) {
return thrownObject;
}
return new Error(Non-Error thrown: ${String(thrownObject)});
};
和
try {
// 一些可能抛出异常的代码
} catch (thrownObject) {
const error = ensureError(thrownObject);
throw error;
}
记住这是 Solana 而不是 Ethereum。
Token 程序术语:
链上
每个项目都需要一个 IDL。
[features]
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]
如果它使用 SPL Tokens(像几乎每个 Anchor 项目一样),它将需要这个依赖项(插入适用的版本):
[dependencies]
anchor-spl = "0.32.1"
lib.rs 或 Anchor.toml 中的程序 IDstate 文件夹内创建所需状态的文件instructions 或 handlers 文件夹(无论哪个存在)内创建所需指令处理程序的文件AccountConstraints 结尾,而不仅仅是命名为与函数相同的东西admin,位于存在的父文件夹内 (instructions/admin/ 或 handlers/admin/)context.bumps.foo 而不是 context.bumps.get("foo").unwrap() —— 后者已经过时max_len 属性max_len 数字:第一个是向量的最大长度,第二个是向量中项的最大长度8 + 32 之类的space,使用类似这样的语法:space = SomeStruct::DISCRIMINATOR.len() + SomeStruct::INIT_SPACE,#[derive(InitSpace)],以获得 INIT_SPACE 特性示例:
#[derive(InitSpace)]
#[account]
pub struct UserProfile {
pub authority: Pubkey,
#[max_len(50)]
pub username: String,
pub bump: u8,
}
#[derive(Accounts)]
pub struct InitializeProfile<'info> {
#[account(
init,
payer = authority,
space = UserProfile::DISCRIMINATOR.len() + UserProfile::INIT_SPACE,
seeds = [b"profile", authority.key().as_ref()],
bump
)]
pub profile: Account<'info, UserProfile>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
pub bump: u8Clock::get()?; 而不是 anchor_lang::solana_program::clock创建 git 提交时,不要添加 "Co-Authored-By: Claude" 或类似的署名。
每周安装量
91
仓库
GitHub 星标数
76
首次出现
2026年2月7日
安全审计
安装于
gemini-cli87
opencode87
codex86
github-copilot85
kimi-cli84
amp84
Apply these rules to ensure code quality, maintainability, and adherence to project standards.
npm test. If the tests fail, there is more work to do. Don't stop until npm test passes on the code you have made.CRITICAL: Placeholder tests don't count as success.
Tests that just do assert.ok(true) or similar are NOT real tests
DO NOT mark "Write tests" as complete until tests actually call the program instructions
DO NOT ask "should I write real tests now?" - if the tests are placeholders, write real ones immediately
Real tests must: initialize accounts, send transactions, verify state changes, check balances
If you find yourself writing placeholder tests, stop and write real integration tests instead
When summarizing your work, show the work items you have achieved with this symbol '✅' and there is more work to do, add a '❌' for each remaining work item.
Use these official documentation sources:
Do not use Solana Labs documentation. The company has been replaced by Anza.
Do not use any documentaton or tools from Project Serum, which collapsed many years ago.
Do not use yarn. Use npm. Yarn has no reason to exist and only adds unnecessary dependencies. Replace Yarn with npm everywhere you see it.
Do not use Switchboard Functions - this product is dead and no longer maintained. (Note: Switchboard oracles are still active and usable.)
Do not use Clockwork - this product is dead. For scheduled instruction handler invocation, use TukTuk instead.
Use the latest stable Anchor, Rust, TypeScript, Solana Kit, and Kite you can. If a bug occurs, favor updating rather than rolling back.
Every project must have a README.md file in the project root that includes:
npm test)Keep the README focused and practical. Avoid generic boilerplate - write documentation that would actually help someone understand and work with this specific project.
Your golden rule is "perfection isn't achieved when there's nothing more to add, rather perfection is achieved when there is nothing more to be taken away".
Remove:
CRITICAL - Verify Before Use:
TODO letting the next programmer know when they can delete the workaround)Ensure good variable naming. Rather than add comments to explain what things are, give them useful names.
Don't do this:
// Foo
const shlerg = getFoo();
Do this instead:
const foo = getFoo();
Naming conventions:
shoes), items within arrays should be the singular (shoes.forEach((shoe) => {...}))calculateFoo or getBarcontext rather than ctx)e for something throwntransaction. Name instructions some variant of instruction. Name signatures some variant of signature. Do not confuse them - eg if the type looks like an instruction, you should not call it a 'transaction' because that is deceptive.You can still add comments for additional context, just be careful to avoid comments that are explaining things that would be better conveyed by good variable naming.
This is a magic number. Don't do this:
const FINALIZE_EVENT_DISCRIMINATOR = new Uint8Array([
27, 75, 117, 221, 191, 213, 253, 249,
]);
Instead do this:
const FINALIZE_EVENT_DISCRIMINATOR = getEventDiscriminator(
arciumIdl,
"FinalizeComputationEvent",
);
// In production we'd do this differently or **Implementation incomplete** - Needs program config handling and proper PDA derivations or **WORK IN PROGRESS** in the final code you produce, or functions that return placeholder data. Instead: do the fucking work.These guidelines apply to TypeScript unit tests, browser code, and any other places where TypeScript is used in the project.
Avoid using a tsconfig.json unless it's needed, as we use tsx to run most typescript and it doesn't usually need one. If you do need a tsconfig.json, state why at the top of the file, and you can use the most modern version of ECMAScript/JavaScript you want - up to say 2023.
Favor async/await and try/catch over .then() or .catch() or using callbacks for flow control. tsx has top level await so you don't need to wrap top level await in IIFEs.
Array<item>, never use item[] for consistency with other generic syntax like Promise<T>, Map<K, V>, and Set<T>any// and be above (not beside) the code/* */ syntax@solana/web3.js version 1 code. Do not make new code using @coral-xyz/anchor package. Don't replace Solana Kit with web3.js version 1 code. web3.js version 1 is legacy and should be eventually removed. Solana Kit used to be called web3.js version 2. Use Solana Kit, preferably via Solana Kite.connection.getPDAAndBump() to turn seeds into PDAs and bumpsnpx create-codama-clients
bs58 npm package.Don't do this:
import bs58 from "bs58";
const signature = bs58.encode(signatureBytes);
Do this instead:
import { getBase58Decoder } from "@solana/codecs";
const signature = getBase58Decoder().decode(signatureBytes);
Yes, these difference packages have difference concepts of 'encode' and 'decode'.
tests directorytsx instead of ts-mocha)Unit testing imports:
import { before, describe, test } from "node:test";
import assert from "node:assert";
test rather than itJavaScript allows arbitrary items - strings, array, numbers etc to be 'thrown'. However you can assume that any non-Error item that is thrown is an programmer error. Handle it like this (including the comment since most TypeScript developers don't know this):
// In JS it's possible to throw anything. A sensible programmer
// will only throw Errors but we must still check to satisfy
// TypeScript (and flag any craziness)
const ensureError = function (thrownObject: unknown): Error {
if (thrownObject instanceof Error) {
return thrownObject;
}
return new Error(Non-Error thrown: ${String(thrownObject)});
};
and
try {
// some code that might throw
} catch (thrownObject) {
const error = ensureError(thrownObject);
throw error;
}
Remember this is Solana not Ethereum.
Token program terminology:
Onchain
Every project will need an IDL.
[features]
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]
and if it uses SPL Tokens (like almost every Anchor project) it will need this dependency (insert whatever version is applicable):
[dependencies]
anchor-spl = "0.32.1"
lib.rs or Anchor.toml when making changesstate folder for whatever state is neededinstructions or handlers folders (whichever exists) for whatever instruction handlers are neededAccountConstraints rather than just naming them the same thing as the functionadmin inside whichever parent folder exists (instructions/admin/ or handlers/admin/)context.bumps.foo not context.bumps.get("foo").unwrap() - the latter is outdatedmax_len attributemax_len: the first is the max length of the vector, the second is the max length of the items in the vector8 + 32 or whateverspace, use syntax like: space = SomeStruct::DISCRIMINATOR.len() + SomeStruct::INIT_SPACE,#[derive(InitSpace)] added to them, to get the INIT_SPACE traitExample:
#[derive(InitSpace)]
#[account]
pub struct UserProfile {
pub authority: Pubkey,
#[max_len(50)]
pub username: String,
pub bump: u8,
}
#[derive(Accounts)]
pub struct InitializeProfile<'info> {
#[account(
init,
payer = authority,
space = UserProfile::DISCRIMINATOR.len() + UserProfile::INIT_SPACE,
seeds = [b"profile", authority.key().as_ref()],
bump
)]
pub profile: Account<'info, UserProfile>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
pub bump: u8 to every struct stored in PDAClock::get()?; rather than anchor_lang::solana_program::clockDo not add "Co-Authored-By: Claude" or similar attribution when creating git commits.
Weekly Installs
91
Repository
GitHub Stars
76
First Seen
Feb 7, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
gemini-cli87
opencode87
codex86
github-copilot85
kimi-cli84
amp84
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
120,000 周安装