Count on NEAR

Our counter example is a friendly decentralized app that stores a number and exposes methods to increment,
decrement, and reset it.

img


Obtaining the Counter Example

You have two options to start the Counter Example.

  1. You can use the app through GitHub Codespaces, which will open a web-based interactive environment.
  2. Clone the repository locally and use it from your computer.
Codespaces Clone locally
Open in GitHub Codespaces 🌠https://github.com/near-examples/counters

Structure of the Example

The example is divided in two main components:

  1. The smart contract, available in two flavors: Rust and JavaScript
  2. The frontend, that interacts with an already deployed contract.

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

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


Frontend

The counter example includes a frontend interface designed to interact seamlessly with an existing smart contract that has been deployed. This interface allows users to increase or decrease the counter as needed.


Running the Frontend

To start the frontend you will need to install the dependencies and start the server.

cd frontend
yarn
yarn dev

Go ahead and login with your NEAR account. If you don’t have one, you will be able to create one in the moment. Once logged in, use the + and - buttons to increase and decrease the counter. Then, use the Gameboy buttons to reset it and make the counter blink an eye!

img
Frontend of the Counter


Understanding the Frontend

The frontend is a Next.JS project generated by create-near-app. Check _app.js and index.js to understand how components are displayed and interacting with the contract.





Smart Contract

The contract presents 4 methods: get_num, increment, decrement, and reset. The method get_num retrieves the current value, and the rest modify it.









Testing the Contract

The contract readily includes a set of unit and sandbox testing to validate its functionality. To execute the tests, run the following commands:


“`bash
cd contract-ts
yarn
yarn test
“`


“`bash
cd contract-rs
cargo test
“`


Deploying the Contract to the NEAR network

In order to deploy the contract you will need to create a NEAR account.


“`bash
# Create a new account pre-funded by a faucet
near create-account –useFaucet
“`

“`bash
# Create a new account pre-funded by a faucet
near account create-account sponsor-by-faucet-service .testnet autogenerate-new-keypair save-to-keychain network-config testnet create
“`

Go into the directory containing the smart contract (cd contract-ts or cd contract-rs), build and deploy it:

cargo near build

cargo near deploy <accountId>

CLI: Interacting with the Contract

To interact with the contract through the console, you can use the following commands.

Get the current number of the counter


“`bash
near view counter.near-examples.testnet get_num
“`

“`bash
near contract call-function as-read-only counter.near-examples.testnet get_num json-args {} network-config testnet now
“`


Increment the counter


“`bash
# Replace with your account ID
near call counter.near-examples.testnet increment –accountId
“`

“`bash
# Replace with your account ID
near contract call-function as-transaction counter.near-examples.testnet increment json-args {} prepaid-gas ‘30.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as aha_6.testnet network-config testnet sign-with-keychain send
“`


Decrement the counter


“`bash
# Replace with your account ID
near call counter.near-examples.testnet decrement –accountId
“`

“`bash
# Replace with your account ID
near contract call-function as-transaction counter.near-examples.testnet decrement json-args {} prepaid-gas ‘30.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as aha_6.testnet network-config testnet sign-with-keychain send
“`


Reset the counter to zero


“`bash
# Replace with your account ID
near call counter.near-examples.testnet reset –accountId
“`

“`bash
# Replace with your account ID
near contract call-function as-transaction counter.near-examples.testnet reset json-args {} prepaid-gas ‘30.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as aha_6.testnet network-config testnet sign-with-keychain send
“`


Moving Forward

A nice way to learn is by trying to expand the contract. Modify it by adding a parameter to increment and decrement,
so the user can choose by how much to change the value. For this, you will need to use knowledge from the anatomy
and storage sections.

Scroll to Top
March Redeem to NEAR
This is default text for notification bar