Near Learn

What we’re building + project setup

Start a NEAR crowdfunding dApp in Rust: the rules of the contract, installing cargo-near and near-cli-rs, cargo near new, and your first wasm build.

Beginner7 min read3-question check

In this course you build one project from an empty folder to mainnet: a Kickstarter-style crowdfunding contract. Anyone can start a campaign with a goal and a deadline, anyone can back it with NEAR or a fungible token, and when the deadline passes the money goes one of two ways: to the creator if the goal was met, back to every backer if it was not.

It is a small contract, but it touches almost everything that is different on NEAR: who pays for storage, payable methods, timestamps, asynchronous transfers that can fail, callbacks, the NEP-141 token receiver, events, paginated views, sandbox tests, deployment and upgrades. Each lesson is one milestone and ends with a checklist of what should work.

The rules of the contract#

WhoMethodWhenEffect
Anyonecreate_campaign(title, goal, duration_ms, token)Any timeOpens a campaign; the creator pays its storage
Backerpledge(campaign_id) (attach NEAR)Before the deadlineAdds the deposit (minus storage) to the backer’s pledge
Backerft_transfer_call on the token, msg = campaign idBefore the deadlinePledges NEP-141 tokens instead of NEAR
Creatorclaim(campaign_id)After the deadline, goal metSends everything raised to the creator, once
Backerrefund(campaign_id)After the deadline, goal missedSends that backer’s pledge back
Anyoneget_campaign, get_campaigns, get_backers, get_pledgeAny time (free)Read-only views for the frontend
What the finished contract does

Install the tools#

Rust, cargo-near (build/deploy) and near-cli-rs (accounts and calls)
Shell
# Rust (skip if you already have rustup)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# cargo-near: project template, wasm builds, deploys
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh

# near-cli-rs: the `near` command
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/latest/download/near-cli-rs-installer.sh | sh

Create the project#

Scaffold from the official template
Shell
cargo near new crowdfund
cd crowdfund

# what you get:
# Cargo.toml            crate config, near-sdk + near-workspaces
# rust-toolchain.toml   pins the Rust version and the wasm32 target
# src/lib.rs            a sample contract (we replace it next lesson)
# tests/test_basics.rs  a sandbox test (we replace it in lesson 9)
# .github/workflows/    CI that builds, tests and deploys

The template’s Cargo.toml is already right for a contract. The parts worth understanding: crate-type = ["cdylib", "rlib"] (cdylib produces the wasm, rlib lets tests link the crate), near-sdk as the only runtime dependency, near-workspaces and tokio as dev-dependencies for sandbox tests, and a release profile tuned for small, safe wasm.

Cargo.toml (trimmed) — rename the package and keep the rest
TOML
[package]
name = "crowdfund"
version = "0.1.0"
edition = "2024"
# cargo-near publishes this link through the NEP-330 contract_source_metadata view
repository = "https://github.com/you/crowdfund"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "5.24"

[dev-dependencies]
near-sdk = { version = "5.24", features = ["unit-testing"] }
near-workspaces = { version = "0.22", features = ["unstable"] }
tokio = { version = "1.12.0", features = ["full"] }
serde_json = "1"

[profile.release]
codegen-units = 1
opt-level = "z"         # optimise for size: you pay storage for the wasm too
lto = true
debug = false
panic = "abort"
overflow-checks = true  # arithmetic overflow panics instead of wrapping

Build the wasm#

Build, then run the template tests
Shell
cargo near build non-reproducible-wasm
# -> target/near/crowdfund.wasm (plus an ABI file next to it)

cargo test   # unit tests + the sandbox test in tests/

non-reproducible-wasm builds with your local toolchain — the right choice while developing. For mainnet you will use reproducible-wasm, which builds inside a pinned Docker image so anyone can verify the deployed code matches the source (lesson 12). Plain cargo build is not the way to build a contract: recent near-sdk versions stop the build with a message telling you to use cargo near build.

Check yourself

3 questions · progress saved in this browser

  1. 1.Why does the template’s Cargo.toml set crate-type = ["cdylib", "rlib"]?
  2. 2.Which build command should you use while developing locally?
  3. 3.What does overflow-checks = true in the release profile change?