Rule
0 installs · 0 said it worked
Rust and Solana Anchor smart contract best-practices rules
Cursor rules guiding development of Solana smart contracts in Rust with the Anchor framework, covering program structure, account validation, serialization, security, testing, and deployment.
People use this for
- solana anchor rust rules
- anchor program account validation
- rust solana smart contract guidelines
- write anchor program best practices
- how to structure solana programs
- Name
rust-and-solana-anchor-smart-contract-best-practices-rules- Works with
- Cursor
- License
- CC0(as observed at the source)
- Version
- v1 · updated Nov 29, 2026
Imported on Nov 29, 2026 from github.com . License observed at the source on Nov 29, 2026: CC0.
Are you the author?Claim this profileRequest removal
Use this rule
$ curl -fsSL "https://promptcommunity.dev/api/v1/posts/rust-and-solana-anchor-smart-contract-best-practices-rules-vpg2/export/mdc" -o .cursor/rules/rust-and-solana-anchor-smart-contract-best-practices-rules.mdcThen open a new session; Cursor picks it up from .cursor/rules/.
Manual install
Save rust-and-solana-anchor-smart-contract-best-practices-rules.mdc as .cursor/rules/rust-and-solana-anchor-smart-contract-best-practices-rules.mdc for this project or ~/.cursor/rules/rust-and-solana-anchor-smart-contract-best-practices-rules.mdc for every project, then start a new session.
Rule
description: "Rust best practices for Solana smart contract development using Anchor framework and Solana SDK" globs: programs//*.rs, src//.rs, tests/**/.ts alwaysApply: false
Rust + Solana (Anchor) Best Practices
Program Structure
- Structure Solana programs using
Anchorframework standards - Place program entrypoint logic in
lib.rs, notmain.rs - Organize handlers into modules (e.g.,
initialize,update,close) - Separate state definitions, errors, instructions, and utils
- Group reusable logic under a
utilsmodule (e.g., account validation) - Use
declare_id!()to define program ID
Anchor Framework
- Use
#[derive(Accounts)]for all instruction contexts - Validate accounts strictly using constraint macros (e.g.,
#[account(mut)],seeds,bump]) - Define all state structs with
#[account]and#[derive(AnchorSerialize, AnchorDeserialize)] - Prefer
Init,Close,Realloc,Mut, and constraint macros to avoid manual deserialization - Use
ctx.accountsto access validated context accounts - Handle CPI (Cross-Program Invocation) calls via Anchor’s CPI helpers
Serialization
- Use Borsh or Anchor's custom serializer (not Serde) for on-chain data
- Always include
#[account(zero_copy)]or#[repr(C)]for packed structures - Avoid floating point types — use
u64,u128, or fixed-point math - Zero out or close unused accounts to reduce rent costs
Testing
- Write tests in TypeScript using Anchor’s Mocha + Chai setup (
tests/*.ts) - Use
anchor.workspace.MyProgramto load deployed contracts - Use
provider.simulate()to inspect failed txs - Spin up a local validator (
anchor test) and reset between tests - Airdrop SOL to wallets with
provider.connection.requestAirdrop(...) - Validate program logs using
tx.confirmation.logMessages
Solana SDK (Manual)
- Use
solana_programcrate when not using Anchor (bare-metal programs) - Carefully deserialize accounts using
AccountInfo,try_from_slice_unchecked - Use
solana_program::msg!for lightweight debugging logs - Verify accounts via
is_signer,is_writable,key == expected - Never panic! Use
ProgramError::Custom(u32)orErrorCodeenums
Security Patterns
- Always validate
msg.sender/signer withaccount_info.is_signer - Prevent replay attacks via
seeds,bump, and unique PDAs - Use strict size checks before reallocating or deserializing
- Avoid unsafe unchecked casting; prefer Anchor deserialization
- For CPIs, validate
target_programagainst expected program ID - When using randomness, never rely on timestamps — use oracles or off-chain VRFs
Performance
- Prefer zero-copy deserialization when accounts are large
- Minimize compute usage; avoid loops and recursion
- Avoid memory reallocations mid-instruction
- Use
#[account(zero_copy)]and#[repr(packed)]for tight layout - Profile compute units with
solana logsandanchor run
Dev Workflow
- Use
anchor initto scaffold projects - Add Anchor IDL support for front-end usage (JSON ABI)
- Use
anchor build,anchor deploy,anchor testconsistently - Use separate
Anchor.tomlenvironments for devnet/mainnet/localnet - Format all Rust code with
cargo fmt, lint withcargo clippy - Keep
Cargo.lockchecked intoprograms/but not root
Documentation
- Use
///Rust doc comments for all instructions and accounts - Include doc examples for each instruction
- Document PDA derivation logic and bump seed expectations
- Maintain up-to-date
README.mdwith test commands and deployment steps
Wallet & Network Handling
- Use
anchorProvider.wallet.publicKeyfor signer verification in tests - Do not hardcode keypairs — use env-based loading (
process.env.ANCHOR_WALLET) - Deploy with clear
clustertargets (localnet,devnet,mainnet) - Use
anchor keys syncto propagate program ID changes - Commit
target/idl/andtarget/types/to share with front end
CI/CD & Deploy
- Use GitHub Actions with
solana-cli,anchor-cli, andnodeinstalled - Run
anchor testin CI for every PR - Use
solana program deploywith explicit--program-idon production deploys - Upload IDLs to a central registry (e.g., GitHub, IPFS, or
anchor.cloud)
No result recorded yet. Added it? File a Tried-it report so the next person knows how it went in your editor.
Tried it
Reports from people who added this rule to their editor. They roll up into the badges above.
No one has filed a report yet. Try the prompt and be the first.
Related rules
- Cursor rules for writing and testing Xian smart contracts in Python
A Cursor rules file that guides the assistant through writing Xian blockchain smart contracts in Python, covering naming, state, context variables, events, error handling, and unittest-based testing with security checks.
Coding · @patrickjs
- Data validation architect role for multi-layer input validation and sanitization
A role prompt that turns the model into a senior data integrity expert who designs multi-layer input validation, sanitization, injection prevention, and error handling, and writes the plan to a TODO_data-validator.md checklist file.
Coding · @wkaandemir
- Quality engineering agent role for risk-based test strategy and CI quality gates
A senior quality engineering role prompt that guides an assistant through risk-based test strategy, test automation architecture, CI/CD quality gates, edge-case analysis, and defect management, writing results to a TODO checklist file.
Coding · @wkaandemir
- Agent that writes, runs, and repairs tests after code changes
A system prompt defining a test-writer-fixer agent that writes unit, integration, and end-to-end tests, runs suites, analyzes failures, and repairs tests without weakening them.
Coding · @ersinyilmaz
- Solidity and Foundry smart contract security expert rules for Cursor
Cursor rules that make the assistant act as a Solidity and smart contract security expert, covering best practices, Foundry testing, deployment workflow, and dependency setup.
Coding · @patrickjs
License: CC0v1 · published Nov 29, 2026
Comments
No comments yet. If you tried this prompt, say how it went.