Deploy and Test Your Contract
Now that we have a complete donation contract, let's deploy it to NEAR testnet and test all functionality through the command line.
Create a NEAR Account
First, you need a NEAR testnet account to deploy the contract:
- Near CLI (legacy)
- Near CLI (new)
# Create account with funding from faucet
near create-account your-contract.testnet --useFaucet
# Login to your account
near login
# Create a new account pre-funded by faucet
near account create-account sponsor-by-faucet-service \
your-contract.testnet autogenerate-new-keypair \
save-to-keychain network-config testnet create
Deploy the Contract
- Rust
- TypeScript
# Build the contract
cargo near build
# Deploy with initialization
near deploy your-contract.testnet \
--wasmFile target/near/contract.wasm \
--initFunction init \
--initArgs '{"beneficiary": "beneficiary.testnet"}'
# Build the contract
npm run build
# Deploy the WASM file
near deploy your-contract.testnet ./build/donation.wasm
# Initialize the contract
near call your-contract.testnet init \
'{"beneficiary": "beneficiary.testnet"}' \
--accountId your-contract.testnet
Test View Methods
Start by testing the read-only methods to verify deployment:
- Near CLI (legacy)
- Near CLI (new)
# Check the beneficiary
near view your-contract.testnet get_beneficiary
# Check number of donors (should be 0)
near view your-contract.testnet number_of_donors
# Check donations list (should be empty)
near view your-contract.testnet get_donations \
'{"from_index": 0, "limit": 10}'
# Check beneficiary
near contract call-function as-read-only \
your-contract.testnet get_beneficiary json-args '{}' \
network-config testnet now
# Check number of donors
near contract call-function as-read-only \
your-contract.testnet number_of_donors json-args '{}' \
network-config testnet now
Test Donations
Now let's test the donation functionality:
- Near CLI (legacy)
- Near CLI (new)
# Make a donation
near call your-contract.testnet donate \
--accountId donor.testnet \
--deposit 1
# Check your donation was recorded
near view your-contract.testnet get_donation_for_account \
'{"account_id": "donor.testnet"}'
# Verify donor count increased
near view your-contract.testnet number_of_donors
# Make a donation
near contract call-function as-transaction \
your-contract.testnet donate json-args '{}' \
prepaid-gas '30.0 Tgas' attached-deposit '1 NEAR' \
sign-as donor.testnet network-config testnet \
sign-with-keychain send
# Check donation was recorded
near contract call-function as-read-only \
your-contract.testnet get_donation_for_account \
json-args '{"account_id": "donor.testnet"}' \
network-config testnet now
Test Multiple Donations
Test the storage cost logic with multiple donations:
# First donation (pays storage cost)
near call your-contract.testnet donate \
--accountId alice.testnet --deposit 0.1
# Second donation from same account (no storage cost)
near call your-contract.testnet donate \
--accountId alice.testnet --deposit 0.2
# Different donor
near call your-contract.testnet donate \
--accountId bob.testnet --deposit 0.5
# Check total donors
near view your-contract.testnet number_of_donors
# Get paginated donations
near view your-contract.testnet get_donations \
'{"from_index": 0, "limit": 10}'
Verify Fund Transfer
Check that donations were forwarded to the beneficiary:
# Get beneficiary balance before
near state beneficiary.testnet
# Make donation
near call your-contract.testnet donate \
--accountId test.testnet --deposit 1
# Check beneficiary balance after (should increase by ~0.999 NEAR)
near state beneficiary.testnet
The beneficiary receives slightly less than the donation amount due to the storage cost deduction for first-time donors.
Test Error Cases
Verify error handling works correctly:
# Try donating too little to cover storage
near call your-contract.testnet donate \
--accountId new-donor.testnet --deposit 0.0001
# Should fail with storage cost error
# Try unauthorized beneficiary change
near call your-contract.testnet change_beneficiary \
'{"new_beneficiary": "hacker.testnet"}' \
--accountId not-owner.testnet
# Should fail with access error
Monitor Contract Activity
Track your contract's activity through NEAR explorers:
- Testnet Explorer: https://testnet.nearblocks.io/address/your-contract.testnet
- NEAR Explorer: https://explorer.testnet.near.org/accounts/your-contract.testnet
Look for:
- Donation transactions
- Transfer calls to beneficiary
- Contract method calls
- Token flow
Performance Testing
For production contracts, test with larger datasets:
# Create many donations to test pagination
for i in {1..20}; do
near call your-contract.testnet donate \
--accountId "test-$i.testnet" --deposit 0.1
done
# Test pagination limits
near view your-contract.testnet get_donations \
'{"from_index": 0, "limit": 5}'
near view your-contract.testnet get_donations \
'{"from_index": 5, "limit": 5}'
Debugging Tips
If transactions fail:
- Check gas limits: Complex operations may need more gas
- Verify deposits: Ensure sufficient NEAR for storage costs
- Review logs: Use
near tx-status TRANSACTION_HASHto see detailed logs - Test locally first: Use unit tests before testnet deployment
Contract Upgrade
When you need to update your contract:
# Deploy new version (keeps state)
near deploy your-contract.testnet new-contract.wasm
# Or redeploy with state reset
near delete your-contract.testnet beneficiary.testnet
# Then redeploy fresh
Continue to Build Frontend to learn how to create a web interface for your donation contract.