Create your first NEAR smart contract using Rust or JavaScript. Learn how to build, test, and deploy an auction contract.
Welcome! NEAR accounts can store small apps known as smart contracts. In this quick tutorial, we will guide you in creating your first contract on the NEAR testnet!Join us in creating a friendly auction contract, which allows users to place bids, track the highest bidder and claim tokens at the end of the auction.
Which language should I use?We recommend using Rust for production apps due to its mature tooling. However, if you are just prototyping or learning, feel free to use JavaScript!
Prefer an online IDE?Check out NEAR Playground for an easy-to-use online IDE with pre-configured templates.
Before starting, make sure to set up your development environment.
Rust
JavaScript
# Install Rust: https://www.rust-lang.org/tools/installcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh# Contracts will be compiled to wasm, so we need to add the wasm targetrustup target add wasm32-unknown-unknown# Install NEAR CLI-RS to deploy and interact with the contractcurl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/latest/download/near-cli-rs-installer.sh | sh# Install cargo near to help building the contractcurl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
# Install Node.js using nvm (more options in: https://nodejs.org/en/download)curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashnvm install latest# Install NEAR CLI to deploy and interact with the contractnpm install -g near-cli-rs@latest
For Mac Silicon users, Rosetta is needed to compile contracts:
The auction smart contract allows users to place bids, track the highest bidder and claim tokens at the end of the auction.Let’s explore the different components:
The contract stores the highest bid, auction end time, auctioneer address, and a flag to track if proceeds have been claimed. An init function is provided to initialize the contract state.
Users call the bid function while attaching a deposit representing their bid amount. The function validates that the auction is ongoing, and if the bid is higher than the current highest, it records the new bid and refunds the previous bidder.
Rust
JavaScript
#[payable]pub fn bid(&mut self) { let bidder = env::predecessor_account_id(); let bid = env::attached_deposit(); let now = env::block_timestamp(); assert!(now < self.auction_end_time, "Auction has ended"); assert!(bid > self.highest_bid.bid, "Bid is too low"); // Refund previous bidder if self.highest_bid.bidder != "none" { Promise::new(self.highest_bid.bidder.clone()) .transfer(self.highest_bid.bid); } // Store new highest bid self.highest_bid = Bid { bidder, bid };}
# Get a timestamp for 5 minutes from now (in nanoseconds)FIVE_MINUTES_FROM_NOW=$(( $(date +%s%N) + 5 * 60 * 1000000000 ))# Initialize the auctionnear call <contract-acc.testnet> init "{\"end_time\": \"$FIVE_MINUTES_FROM_NOW\", \"auctioneer\": \"influencer.testnet\"}" --useAccount <contract-acc.testnet>
# Create a new account to place the bidnear create-account <bidder-account.testnet> --useFaucet# Place a bid of 0.01 NEARnear call <contract-acc.testnet> bid '{}' --deposit 0.01 --useAccount <bidder-account.testnet>