Getting Started With NEAR Protocol: A Beginners Guide to Building Decentralized Apps.
Introduction
The NEAR protocol is one of the most prominent platforms for developing decentralized apps (dApps) in today's quickly changing blockchain landscape.
NEAR is a well-liked option for a wide range of applications because of its reputation for being user-friendly, extremely scalable, and particularly friendly to developers.
In this chapter, we will examine what distinguishes NEAR from other blockchain platforms.
NEAR was developed with an emphasis on simplicity and excellent performance to facilitate and expedite blockchain development.
One of its primary characteristics is a unique architecture known as "sharding," which divides work among various network segments to enable NEAR to process thousands of transactions per second.
What is Sharding In NEAR? A Quick Overview
In NEAR, sharding is a technique used to split the blockchain network into smaller parts, called shards, which can handle tasks and process data independently.
This approach helps NEAR manage more transactions at once by spreading out the workload, making it scalable and able to support many users without slowing down.
The network can manage thousands of transactions per second since each NEAR shard may process transactions in parallel, or simultaneously.
Compared to traditional blockchains, where each node must process every transaction, which can cause lag as the network expands, NEAR is significantly faster and more efficient thanks to this configuration.
To put it briefly, NEAR sharding divides tasks and processes them in discrete parts throughout the network, increasing network speed and scalability.
In this chapter, we’ll explore the following key topics:
An introduction to NEAR
Understanding advanced concepts in NEAR
But before that, let's dive into the technical requirements needed to kick start with the NEAR blockchain.
Technical Requirements
Prerequisites
To develop a smart contract on NEAR, you must install Node.js.
If you want to use Rust as your main programming language, you must install Rustup also.
You will also need the following:
• Node.js: Download and install Node.js (https://nodejs.org/en/download/)
After successful download, open your terminal and type the code below. If successful, you will get the node version as a response like the one below.
Rust and Wasm:
I. Follow these instructions for setting up Rust: https://doc.rust-lang.org/beta/book/ch01-01-installation.html
II. Then, add the wasm32-unknown-unknown toolchain, which enables compiling
Rust to WebAssembly (Wasm) – https://webassembly.org/– the low-level language used by the NEAR platform:
Next, the environment must then be configured with the necessary installations.
Installation
To kick off your journey with NEAR Protocol and start building dApps, you will need to set up the required tools and environment.
This guide will walk you through installing the NEAR SDK and NEAR Command-Line Interface (CLI) on Ubuntu, Windows, and macOS.
Follow these steps to prepare your development environment.
Start by ensuring you have all prerequisites and then select the setup instructions for your operating system to get everything ready for NEAR development. Let’s get started.
NEAR CLI Installation
To install the NEAR CLI, follow these instructions for your preferred operating system (OS):
For Ubuntu
i. Install Rust by running the following command:
Ii. Add the Cargo bin directory to your PATH:
Iii. Install the NEAR CLI by running the following command, or navigate to NEAR docs
Iv. Kindly verify the installation by running the following command:
The above command signifies that NEAR CLI has been successfully installed on my PC (ubuntu)
We can also verify again by running near from the command line like the one below.
NEAR CLI successfully installed. Great! 💪
On Windows
i. Ensure that you have WSL activated on Windows, then open up your WSL terminal.
ii. Install Rust by running the following command:
III. Add the Cargo bin directory to your PATH:
IV. Install the NEAR CLI by running the following command:
V. Verify the installation by running the following command:
- MacOS
I. Install Homebrew by running the following command:
II. Install Rust by running the following command:
iii. Add the Cargo bin directory to your PATH:
iv. Install the NEAR CLI by running the following command:
v. Verify the installation by running the following command:
Now that NEAR is installed, let us examine its fundamental ideas and see why it is a well-liked option for developing dApps.
Introducing NEAR
We will examine what makes NEAR the best option for developers creating dApps in this part.
The foundational ideas of the NEAR environment will be discussed, beginning with a thorough comprehension of accounts, addresses, and access keys.
After that, we will go through the specifics of smart contracts and the blockchain state in NEAR, giving you the knowledge you need to fully utilize this innovative platform.
Why Choose NEAR?
Rust developers are strongly encouraged to choose the NEAR blockchain as their preferred platform for building and implementing smart contracts.
NEAR's seamless integration of Rust into its development stack makes it an ideal choice for Rust lovers.
Rust is a programming language that is well-known for its memory safety and high efficiency.
NEAR is a great option for Rust developers for the following reasons:
Native Rust Support:
The main language for creating smart contracts in NEAR is Rust.
This eliminates the requirement for learning a new language or set of tools by enabling Rust developers to construct smart contracts on NEAR using their current abilities and experience.
In addition to utilizing NEAR's special characteristics, developers can benefit from Rust's robust library ecosystem and safety features.
Memory Safety and Performance: Rust’s memory safety features, like ownership and borrowing, help prevent common programming errors, such as null pointer dereferences and data races.
This makes Rust ideal for writing secure smart contracts. NEAR’s support for Rust allows developers to leverage these safety benefits, creating smart contracts that are both highly reliable and performant.
Scalability and High Throughput: NEAR offers exceptional scalability and high transaction throughput thanks to its sophisticated consensus process and sharded design.
NEAR is perfect for high-demand use cases with big user populations or intricate interactions since it enables Rust developers to create apps that can process thousands of transactions per second.
Developer-Friendly Environment: With robust tooling, thorough documentation, and a vibrant community, NEAR offers a very encouraging environment.
To speed up learning and development, developers have access to a variety of tools, such as NEAR SDKs, code samples, and tutorials.
By promoting cooperation and information exchange, the NEAR ecosystem enables developers to network and get access to a wealth of collective knowledge.
Opportunities for the Ecosystem: NEAR is expanding quickly in the blockchain
industry, drawing a variety of projects and partnerships.
By selecting NEAR, Rust developers can access a vibrant network of dApps, tools, and services that facilitate cooperation, partnerships, and the development of decentralized solutions with a large user base.
NEAR is the perfect platform for Rust developers wishing to create and implement smart contracts because of its native support for Rust, scalability, high performance, developer-friendly environment, and growing ecosystem.
Developers may build safe, effective, and scalable dApps on the NEAR blockchain by applying their knowledge of Rust.
Understanding the core foundational elements of NEAR
Let's examine the fundamental elements of NEAR's architecture: state, smart contracts, access keys, accounts, and addresses.
- Accounts
Accounts are necessary in the NEAR ecosystem for users to communicate with decentralized apps (dApps).
To create safe and intuitive applications on the NEAR blockchain, developers must have a solid understanding of accounts.
A summary of named accounts, implicit accounts, and account IDs is provided below, along with useful code examples:
Account ID: Each account on NEAR is uniquely identified by its account ID. It is usually shown as strings, such alice.near or my_dapp.near, and is legible by humans.
Account IDs provide access to the account's data and smart contract features and act as addresses for money transfers.
Implicit Accounts: When a non-existent account ID is used in a transaction, an implicit account is immediately generated.
For brief exchanges or keeping information for a single transaction, these accounts are helpful.
Implicit accounts, on the other hand, are unable to accept money, hold long-term data, or have private keys.
Here's an illustration of how to create an implicit account in Rust:
Named Accounts: Named accounts are those that may accept money, store information, and communicate with other smart contracts since they have a persistent state and private keys.
Within the NEAR ecosystem, these accounts give users or dApps a more permanent identification. NEAR's SDKs allow developers to programmatically create and manage named accounts.
Knowing the distinctions between named accounts, implicit accounts, and account IDs allows developers to design unique solutions for a range of NEAR ecosystem use cases.
Developers may create reliable dApps on the NEAR blockchain and guarantee safe, smooth user interactions by utilizing the power of accounts.
We will then go into greater detail on NEAR addresses, looking at their composition and function within the NEAR blockchain ecosystem.
- Addresses
As distinct account IDs that facilitate safe network transactions and interactions, addresses are an essential component of the NEAR blockchain.
Address comprehension is essential for developers creating dApps on NEAR. Let's examine the structure, function, and significance of addresses.
Account IDs, which are human-readable representations of accounts, are the source of addresses in the NEAR ecosystem.
Since these addresses are cryptographic hashes generated from account IDs, they provide a safe and impenetrable method of account identification.
The integrity and legitimacy of transactions and interactions on the blockchain are ensured by the cryptographic nature of NEAR addresses.
NEAR addresses have two primary purposes:
Transaction Routing: To route transactions to the appropriate recipients, NEAR addresses are necessary.
To guarantee that the transaction is delivered to the correct account, the sender provides the recipient's address when initiating the transaction.
To accomplish effective and scalable transaction handling, the NEAR network uses these addresses to determine which shard, a group of nodes, is in charge of processing the transaction.
Safe Transactions and Interactions: An additional cryptographic layer of security is offered by NEAR addresses.
Transactions are signed using the private key associated with an address, verifying the sender's integrity and authenticity.
This guarantees that transactions can only be started by the account owner or another authorized party.
The cryptographic features and address structure of NEAR improve the security of funds, data, and smart contract interactions within the ecosystem.
Addresses can be used by NEAR developers to build seamless and safe user experiences.
Developers can enable users to send and receive money, access particular features inside their applications, and engage with the blockchain securely by including addresses in their dApps.
The importance of access keys in the NEAR Protocol will next be discussed, with an emphasis on account administration, security, and increasing ecosystem adaptability.
- Access Keys
A vital component of the NEAR Protocol, access keys are essential for account administration, ecosystem adaptability, and security.
Access keys offer a practical means of controlling access to accounts and their resources as well as managing account permissions. These are the main advantages they provide:
Enhanced Security: Access keys give precise control over the actions that various entities engaging with an account are permitted to take. This reduces the possibility of unwanted activity by allowing users to limit access for third-party apps or smart contracts.
Granular Permissions: Using access keys, developers can grant various entities particular permissions.
By guaranteeing that only permitted operations are performed by approved entities, this guarantees secure and modular account administration.
Flexibility and Delegation: Access keys facilitate seamless interactions and integrations between dApps by providing the ability to assign specific tasks to other accounts or smart contracts.
For effective account management, this supports intricate configurations including multi-signature accounts and authority delegation.
There are two kinds of access keys available in the NEAR Protocol, each with varying degrees of security and control:
Full Access Keys: These keys give the bearer total authority over an account and enable them to take any action on its behalf.
Account owners or other trustworthy parties who need unfettered access to the account usually utilize full access keys.
Function Call Keys: Also referred to as limited access keys, function call keys allow a smart contract to do particular operations or make function calls.
Granular control is made possible by them, allowing the account to be used for only specific operations. These keys are frequently used to enable particular actions without giving complete access to the account or to assign responsibilities to contracts with reliable third parties.
Since a specific access key is needed to complete any transaction or function, access keys are especially important for managing closed accounts.
An additional degree of security is provided by locked accounts, which guarantee that an attacker cannot carry out any actions without the assigned access key, even if they manage to obtain other keys.
Here's an example of how to make a function call key and a full access key in Rust:
Access keys give NEAR account holders the flexibility to manage their accounts, enhance security, and assign permissions.
Developers can design dynamic and safe dApps within the NEAR Protocol by carefully applying access key techniques and using various key kinds.
After discussing the importance of access keys, we go on to smart contracts, which are the fundamental components of decentralized applications (dApps) on the NEAR blockchain.
- Smart Contract
Decentralized apps (dApps) on the NEAR blockchain are built on smart contracts, which automate business logic and enable trustless interactions.
The idea of smart contracts and their crucial function in the NEAR ecosystem will be discussed in this part, along with code samples that illustrate how they are implemented.
Mutability, which establishes whether a contract can be modified after deployment, is a crucial component of smart contracts.
By locking the code and maintaining the original rules while blocking revisions, immutable contracts provide increased security and trust.
But it also means that once released, defects cannot be fixed. Upgradeable contracts, on the other hand, permit changes to address issues, enhance functionality, or accommodate evolving needs.
Although this flexibility is advantageous, it also adds complexity and poses security issues because changes need to be carefully controlled to avoid jeopardizing the integrity of the contract or the interests of users.
To help developers determine the ideal degree of mutability to satisfy the requirements of their application as well as the relevant security standards, NEAR offers them tools and governance models that enable them to strike a balance between security and flexibility.
The rules and logic controlling interactions in a decentralized system are contained in smart contracts, which are self-executing agreements.
Smart contracts on the NEAR blockchain are kept directly on the blockchain and are written in languages like TypeScript, AssemblyScript, or Rust, guaranteeing transparency and immutability.
Here’s an example of a simple smart contract written in Rust using the NEAR SDK:
We define a smart contract named MyContract in this example.
The NEAR blockchain's smart contracts have several advantages. They guarantee transparency and immutability, which means that the data and logic of the contract cannot be changed once it has been implemented.
Contracts operate as planned thanks to the security and fault tolerance offered by the NEAR blockchain's execution environment.
Additionally, by automating business logic, smart contracts lessen the need for middlemen and boost productivity.
We will then explore the idea of state in the context of the NEAR ecosystem, with a particular emphasis on contract state and account metadata, which are crucial for blockchain data management.
- State Management
To store and retrieve data related to accounts and smart contracts, state management is essential.
With an emphasis on account information and contract state, we will examine how state functions inside the NEAR ecosystem in this section.
Accounts on the NEAR blockchain are capable of storing more metadata than just their basic data.
Profile information, user preferences, and any other unique data that enhances the user experience are examples of account metadata. Because of this adaptability, developers can customize dApps and improve ecosystem relationships.
The NEAR SDKs allow developers to store, modify, and retrieve this metadata, which makes it a crucial component for creating dynamic applications.
An illustration of how to store account info (metadata) is provided here:
On the NEAR blockchain, smart contracts control their own state, which is made up of the variables and data required to carry out the logic of the contract and keep it operational.
Essential data including user balances, customized user information, and any other information relevant to the contract's functioning may be included in this contract state.
Through particular contract methods, the contract state can be read or changed, and it is safely saved on the blockchain, guaranteeing permanence.
Here is an illustration of how the contract state is handled in a Rust-based smart contract on the NEAR blockchain:
The smart contract in this example is defined by the MyContract struct, and its state variable is counter.
While increment increments the counter and get_counter obtains its current value, the new function initializes the counter to 0 by default.
To guarantee smooth serialization and deserialization of contract state on the NEAR blockchain, this Rust implementation makes use of the features of the NEAR SDK and derives serialization traits using the Borsh crate.
We are prepared to investigate more complex subjects that will be necessary for creating strong projects on the NEAR platform now that we have a firm grasp of the fundamental ideas of NEAR.
Conclusion
To sum up, NEAR Protocol provides a strong, developer-friendly ecosystem with adaptable state and account management, scalable infrastructure, and reliable tools.
These fundamental and sophisticated ideas enable developers to produce dApps that are safe, effective, and scalable while satisfying a variety of use cases.
Developers are in a strong position to create and shape the future of decentralized applications thanks to NEAR's encouraging environment.
Subscribe to my newsletter
Read articles from ajayi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
ajayi
ajayi
I, Ajayi Damola Ramon am a passionate and detail-oriented professional with a strong foundation in Statistics and expertise in Rust development. As a Blockchain Developer, I specialize in leveraging Rust to build decentralized applications and smart contracts, particularly in the ICP ecosystem. My technical journey combines deep analytical skills as a Data Analyst with hands-on coding experience, allowing me to solve complex problems through data-driven insights and efficient Rust solutions. With a background in statistics, I excel at breaking down technical concepts and translating them into actionable insights, both in the world of data science and blockchain development. I am constantly exploring new innovations in the tech world, especially Rust, Go, and blockchain technologies, and I am committed to contributing to the open-source community through projects and technical writing. Beyond coding, I am passionate about sharing knowledge through technical articles and tutorials. I regularly publish in-depth guides on Rust, blockchain, and data analytics on my Hashnode blog, helping developers of all levels deepen their understanding of emerging technologies. Connect with me to collaborate on cutting-edge blockchain projects or to explore the fascinating intersection of data, statistics, and decentralized systems.