NEAR Token Drops
Let's start with the simplest drop type: native NEAR tokens. This will teach you the core concepts before we move to more complex token types.
Project Setup
First, create a new Rust project:
cargo near new near-drop --contract
cd near-drop
Update Cargo.toml:
[dependencies]
near-sdk = { version = "5.1.0", features = ["unstable"] }
serde = { version = "1.0", features = ["derive"] }
Basic Contract Structure
Let's start with the main contract in src/lib.rs:
Loading...
Contract Initialization
Loading...
Creating NEAR Drops
The main function everyone will use:
Loading...
Claiming Tokens
Now for the claiming logic in src/claim.rs:
Loading...
Core claiming logic:
Loading...
Helper Functions
Add some useful view functions:
Loading...
Build and Test
# Build the contract
cargo near build
# Create test account
near create-account drop-test.testnet --useFaucet
# Deploy
near deploy drop-test.testnet target/near/near_drop.wasm
# Initialize
near call drop-test.testnet new '{"top_level_account": "testnet"}' --accountId drop-test.testnet
Create Your First Drop
# Create a drop with 2 NEAR per claim for 2 recipients
near call drop-test.testnet create_near_drop '{
"public_keys": [
"ed25519:HcwvxZXSCX341Pe4vo9FLTzoRab9N8MWGZ2isxZjk1b8",
"ed25519:5oN7Yk7FKQMKpuP4aroWgNoFfVDLnY3zmRnqYk9fuEvR"
],
"amount_per_drop": "2000000000000000000000000"
}' --accountId drop-test.testnet --deposit 5
Claim Tokens
Recipients can now claim using their private keys:
# Claim to existing account
near call drop-test.testnet claim_for '{"account_id": "alice.testnet"}' \
--accountId drop-test.testnet \
--keyPair <private-key-here>
# Or create new account and claim
near call drop-test.testnet create_account_and_claim '{"account_id": "bob-new.testnet"}' \
--accountId drop-test.testnet \
--keyPair <private-key-here>
What You've Built
Congratulations! You now have:
✅ NEAR token distribution system
✅ Gasless claiming with function-call keys
✅ Account creation for new users
✅ Automatic cleanup after claims
✅ Cost estimation for creating drops
The foundation is solid. Next, let's add support for fungible tokens, which involves cross-contract calls and is a bit more complex.
Continue to Fungible Token Drops →
Try creating a small drop and claiming it yourself to make sure everything works before moving on!