និមិត្តសញ្ញា Zephyrnet

តើកិច្ចសន្យាឆ្លាតវៃជាអ្វី? វិធីសាស្រ្តដោយដៃ

កាលបរិច្ឆេទ:

There are a couple of key players in the blockchain space that make it easier to create smart contracts and decentralized applications. We’ll be exploring how to create a smart contract using NEAR Studio.

Before we write any actual code, we need to choose where we’re going to write the code. The best option for developers looking to get their feet wet with developing smart contracts is the Near Studio IDE because it creates a ready to go environment from the start.

Keep in mind that you need to know a bit of JavaScript and possibly some TypeScript to go with it. If not, don’t worry as we’ll be breaking everything down into simple and easy-to-understand terms.

Now, if you head over to Near Studio, you’ll find that you can create a new project from a list of templates.

Select the “Counter Smart Contract” project. The counter template creates a basic TypeScript application that increments, decrements, and fetches a counter variable.

This example is perfect as it shows us how smart contracts are not always just agreements. They hold our back-end logic for use with the blockchain as a database.

Selecting the template creates the application, and now, if we head over to assembly/main.ts using the file explorer on the left, we can see the counter code for our smart contract.

You can take a couple of minutes to explore the functionality of this template by clicking the រត់ command in the top toolbar and navigating to the new window.

Then, in the new window, you can test out the commands by opening up the console and entering the commands shown on the screen.

ឥឡូវសូមមើលនៅ assembly/main.ts file, line-by-line.

import { context, storage, near } from “./near”;

The importing statement adds all the library classes we’ll need for the functionality of our app to work. If we want to see what the imported classes do, we can navigate to the “near” folder and find the correct class.

The first function we should see when we open main.ts is the increment function.

export function incrementCounter(): void { let newCounter = storage.get < i32 > “counter” + 1; storage.set < i32 > (“counter”, newCounter); near.log(“Counter is now: “ + newCounter.toString());}

The function executes three steps. First, we get an integer value with the key “counter” and add 1 to it. The counter variable looks like this in storage before the increment function:

{ counter: 0}

Once we increment, the value looks like this:

{ counter: 1}

The logical question now is: “How do we decrement by 1?”

export function decrementCounter(): void { let newCounter = storage.get < i32 > “counter” — 1; storage.set < i32 > (“counter”, newCounter); near.log(“Counter is now: “ + newCounter.toString());}

The first step is to get the current value for “counter” and reduce it by 1. Then we send the new value to the same counter variable in the blockchain.

នេះ​គឺជា​របៀប counter changes before we decrement:

{ counter: 1}

នេះ​គឺជា​របៀប counter changes after we decrement:

{ counter: 0}

The last function takes the crux of the previous functions, getting the counter variable, and returns that value.

export function getCounter(): i32 { return storage.get < i32 > “counter”;}

នេះ main.ts creates and exports the functionality we need, but for our users to use and see our feature, we’ll need to import those functions and frame our front end with our new functionality.

Source: https://medium.com/better-programm
ing/what-are-smart-contracts-a-hands-on-approach-9fc6e02abaa2?source=rss——-8—————–cryptocurrency

spot_img

បញ្ញាចុងក្រោយ

spot_img