Loading...
Loading...
NEAR Protocol smart contract development in Rust. Use when writing, reviewing, or deploying NEAR smart contracts. Covers contract structure, state management, cross-contract calls, testing, security, and optimization patterns. Based on near-sdk v5.x with modern macro syntax.
npx skill4agent add near/agent-skills near-smart-contracts# Install Rust (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Add wasm32 target for compiling contracts
rustup target add wasm32-unknown-unknown
# Install cargo-near (build, deploy, and manage contracts)
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
# Install near-cli-rs (interact with NEAR network)
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/latest/download/near-cli-rs-installer.sh | shCRITICAL: ALWAYS runto create new projects. NEVER manually create Cargo.toml, lib.rs, or any project files. The command generates all required files with correct configurations.cargo near new
# REQUIRED: Create a new contract project using the official template
cargo near new my-contract
# Navigate to project directory
cd my-contract
# Build the contract
cargo near build
# Run tests
cargo testcargo near newCargo.tomlsrc/lib.rstests/overflow-checks = trueCargo.tomlsrc/lib.rsmy-contract/
├── Cargo.toml # Dependencies and project config
├── src/
│ └── lib.rs # Main contract code
├── tests/ # Integration tests
│ └── test_basics.rs
└── README.md# Create a testnet account (if needed)
near account create-account sponsor-by-faucet-service my-contract.testnet autogenerate-new-keypair save-to-keychain network-config testnet create
# Build in release mode
cargo near build --release
# Deploy to testnet
cargo near deploy my-contract.testnet without-init-call network-config testnet sign-with-keychain send| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Security & Safety | CRITICAL | |
| 2 | Contract Structure | HIGH | |
| 3 | State Management | HIGH | |
| 4 | Cross-Contract Calls | MEDIUM-HIGH | |
| 5 | Contract Upgrades | MEDIUM-HIGH | |
| 6 | Chain Signatures | MEDIUM-HIGH | |
| 7 | Gas Optimization | MEDIUM | |
| 8 | Yield & Resume | MEDIUM | |
| 9 | Testing | MEDIUM | |
| 10 | Best Practices | MEDIUM | |
security-storage-checkssecurity-access-controlpredecessor_account_idsecurity-reentrancysecurity-overflowoverflow-checks = truesecurity-callback-validationsecurity-private-callbacks#[private]security-yoctonear-validation#[payable]security-sybil-resistancestructure-near-bindgen#[near(contract_state)]#[near_bindgen]structure-initialization#[init]structure-versioningstructure-eventsenv::log_str()structure-standardsstructure-serializers#[near(serializers = [json, borsh])]structure-panic-default#[derive(PanicOnDefault)]state-collectionsnear_sdk::storeIterableMapIterableSetVectorLookupMapLookupSetUnorderedMapUnorderedSetTreeMapstate-serializationstate-lazy-loadingstate-pagination.skip().take()state-migrationstate-storage-coststate-unique-prefixesstate-native-vs-sdkxcc-promise-chainingxcc-callback-handlingxcc-gas-managementxcc-error-handlingxcc-result-unwrapupgrade-migrationmigrate#[init(ignore_state)]upgrade-self-updateupgrade-cleanup-old-stateupgrade-dao-controlledchain-signatureschain-callback-handlingchain-gas-allocationgas-batch-operationsgas-minimal-state-readsgas-efficient-collectionsgas-view-functions&selfgas-avoid-cloninggas-early-validationrequire!gas-prepaid-gasyield-resumeyield-gatekeepingtesting-integration-testsnear-sandboxnear-apitesting-unit-teststesting-sandboxtesting-edge-casestesting-gas-profilingtesting-cross-contracttesting-failure-scenariostesting-time-travelsandbox.fast_forward()best-contract-toolsnear-sdk-contract-toolsbest-panic-messagesbest-loggingenv::log_str()best-documentationbest-error-typesbest-constantsbest-require-macrorequire!assert!best-promise-returnbest-sdk-cratesbest-account-id-encodingrules/security-storage-checks.md
rules/structure-near-bindgen.md
rules/state-collections.md
rules/xcc-promise-chaining.md
rules/upgrade-migration.md
rules/chain-signatures.md
rules/yield-resume.md
rules/best-contract-tools.md
rules/testing-integration-tests.mdcargo near buildcargo near deploynear contract callnear contract view#[near(contract_state)]#[near_bindgen]#[near(serializers = [json, borsh])]near_sdk::store::IterableMapIterableSetLookupMapLookupSetVectorUnorderedMapUnorderedSetTreeMapPromise::new().then()#[handle_result]Result<T, E>| Storage | Cost | Notes |
|---|---|---|
| 1 byte | 0.00001 NEAR | ~10kb per 0.1 NEAR |
| 100 KB | ~1 NEAR | Approximate reference |
| AccountId | 64+ bytes | Can save 40% with base32 encoding |
| Contract code | Variable | Paid by contract account |
| Collection | Iterable | Clear | Ordered | Range | Use Case |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Ordered list with index access |
| No | No | No | No | Fast key-value, no iteration needed |
| No | No | No | No | Fast membership checks |
| Yes | Yes | Yes | No | Key-value with iteration |
| Yes | Yes | Yes | No | Set with iteration |
| Yes | Yes | No | No | Key-value, unordered iteration |
| Yes | Yes | No | No | Set, unordered iteration |
| Yes | Yes | Yes | Yes | Sorted key-value with range queries |