Hello Contract

To Share and +4 nLEARNs

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 contract that stores a greeting, and exposes functions to interact with it.


Prerequisites

Before starting, make sure to set up your development environment.

Working on Windows?

See our blog post [getting started on NEAR using Windows](/blog/getting-started-on-windows) for a step-by-step guide on how to set up WSL and your environment


“`bash
# 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 | bash
nvm install latest

# âš ï¸ For Mac Silicon users only, Rosetta is needed to compile contracts
# /usr/sbin/softwareupdate –install-rosetta –agree-to-license

# Install NEAR CLI to deploy and interact with the contract
npm install -g near-cli-rs@latest
“`

“`bash
# Install Rust: https://www.rust-lang.org/tools/install
curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh

# Contracts will be compiled to wasm, so we need to add the wasm target
rustup target add wasm32-unknown-unknown

# Install NEAR CLI-RS to deploy and interact with the contract
curl –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 contract
curl –proto ‘=https’ –tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
“`


Creating the Contract

Create a smart contract by using one of the scaffolding tools and following their instructions:


“`bash
npx create-near-app@latest
“`

![img](@site/statichttps://raw.githubusercontent.com/Techbridge3/docs/master/website/static//docs/hello-near-ts.gif)
_Creating a project using `create-near-app`_

This will generate a project with the following structure:

“`bash
├── sandbox-test # sanbox testing
│ └── main.ava.js
├── src # contract’s code
│ └── contract.ts
├── README.md
├── package.json # package manager
└── tsconfig.json
“`

“`bash
cargo near new “`

![img](@site/statichttps://raw.githubusercontent.com/Techbridge3/docs/master/website/static//docs/hello-near-rs.gif)
_Creating a project using `cargo near new`_

This will generate a project with the following structure:

“`bash
├── src # contract’s code
│ └── lib.rs
├── tests # sandbox testing
│ └── test_basics.rs
├── Cargo.toml # package manager
├── README.md
└── rust-toolchain.toml
“`


The Contract

The Hello World smart contract stores a greeting in its state, and exposes two functions to interact with it:

  1. set_greeting: to change the greeting
  2. get_greeting: to fetch the greeting



Test the Contract

Building and testing the contract is as simple as running the test command. The contract will be compiled and the tests will be executed.


“`bash
npm run test
““

Failing tests?

Make sure that you are using `node v18`, `v20` or `v22` – you can manage multiple versions using `nvm` – and that you have `Rosetta` installed on MacOS if you have an Apple Silicon processor.


“`bash
cargo test
“`

In the background, these commands are calling the build tools for each language and using a Sandbox to test the contract.


Create a Testnet Account

Now that you know the contract is passing the tests, let’s create a testnet account in which to deploy the contract. near-cli supports two versions of some commands – full and short one. It’s up to you which format you prefer, but full version provides more features.


“`bash
# Replace with a custom name
near create-account –useFaucet
“`

Example Result

“`bash
$> near create-account lovely-event.testnet –useFaucet
# New account “lovely-event.testnet” created successfully
“`

“`bash
# Replace with a custom name
near account create-account sponsor-by-faucet-service autogenerate-new-keypair save-to-keychain network-config testnet create
““

Example Result

“`bash
$> near account create-account sponsor-by-faucet-service lovely-event.testnet autogenerate-new-keypair save-to-keychain network-config testnet create

# New account “lovely-event.testnet” created successfully
“`


Build the Contract

When you are ready to create a build of the contract run a one-line command depending on your environment.


“`bash
npm run build
“`

“`bash
cargo near build
“`


Deploy the Contract

Having our account created, we can now deploy the contract:



“`bash
near deploy build/hello_near.wasm
“`

“`bash
near contract deploy use-file ./target/wasm32-unknown-unknown/release/hello.wasm without-init-call network-config testnet sign-with-keychain send
“`


“`bash
near deploy ./target/wasm32-unknown-unknown/release/hello.wasm
“`

“`bash
near contract deploy use-file ./target/wasm32-unknown-unknown/release/hello.wasm without-init-call network-config testnet sign-with-keychain send
“`

Congrats! Your contract now lives in the NEAR testnet network.


Interacting with the Contract

To interact with your deployed smart contract, you can call its functions through the command line.


Get Greeting

The get_greeting function only reads from the contract’s state, and can thus be called for free.


“`bash
> near view get_greeting
# “Hello”
“`

“`bash
> near contract call-function as-read-only get_greeting json-args {} network-config testnet now
# “Hello”
“`


Set Greeting

The set_greeting method writes on the contract’s storage, and thus requires a user to sign a transaction in order to be executed.


“`bash
> near call set_greeting ‘{“greeting”: “Hola”}’ –accountId
# Log: Saving greeting “Hola”
“`

“`bash
> near contract call-function as-transaction set_greeting json-args ‘{“greeting”: “Hola”}’ prepaid-gas ‘100.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as network-config testnet sign-with-keychain send
#Log: Saving greeting “Hola”
“`


Moving Forward

That’s it for the quickstart tutorial. You have now seen a fully functional contract with a minimal user interface and testing.

To better understand the contract’s structure, check our contract’s anatomy page.

If you prefer to see more examples, check our examples page.

Generate comment with AI 2 nL
Scroll to Top