Count on NEAR
Our counter example is a friendly decentralized app that stores a number and exposes methods to increment
,
decrement
, and reset
it.
Obtaining the Counter Example
You have two options to start the Counter Example.
- You can use the app through
GitHub Codespaces
, which will open a web-based interactive environment. - Clone the repository locally and use it from your computer.
Codespaces | Clone locally |
---|---|
🌐 https://github.com/near-examples/counters |
Structure of the Example
The example is divided in two main components:
- The smart contract, available in two flavors: Rust and JavaScript
- The frontend, that interacts with an already deployed contract.
- 🌐 JavaScript
- 🦀 Rust
┌── 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
┌── 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!
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.
- _app.js
- index.js
Loading...
Loading...
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.
- 🌐 Javascript
- 🦀 Rust
Loading...
Loading...