You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The NEAR platform has historically supported writing contracts in Rust and AssemblyScript. This document aims to introduce developers to a new way of writing smart contracts by using JavaScript.
8
8
9
-
JavaScript is a widelyused programming language that is most well known for its Webpage scripting usages. See the [official JavaScript docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript) for more details.
9
+
JavaScript is a widely-used programming language that is most well known for its Webpage scripting usages. See the [official JavaScript documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript) for more details.
10
10
11
-
<blockquoteclass="warning">
12
-
<strong>Heads up:</strong> JavaScript smart contract development is not recommended for financial use cases as it is still very new to the NEAR ecosystem.
13
-
14
-
</blockquote>
11
+
:::warning Heads up
12
+
JavaScript smart contract development is not recommended for financial use cases as it is still very new to the NEAR ecosystem.
13
+
:::
15
14
16
15
## Overview {#overview}
17
16
@@ -23,14 +22,14 @@ There are several pros and cons when comparing the enclave approach to the regul
| Can interact with any smart contract on NEAR |✅|❌|
25
24
| Synchronous Cross-Contract Calls |❌|✅|
26
-
| Standards Support |✅|Not in V1.0|
27
-
| Function Call Access Key Support |✅|Not in V1.0|
25
+
| Standards Support |✅|Not in v1.0|
26
+
| Function Call Access Key Support |✅|Not in v1.0|
28
27
29
-
The JavaScript Enclave is a very powerful tool to help you kickstart your smart contract programming journey. Writing contracts in javascript is much easier than learning Rust and the ability for cross-contract calls to be synchronous can greatly help with people's understanding of how contracts can work.
28
+
The JavaScript Enclave is a very powerful tool to help you kickstart your smart contract programming journey. Writing contracts in JavaScript is much easier than learning Rust, and thanks to the jsvm's ability to handle cross-contract calls synchronously can greatly help with people's understanding of how contracts can work.
30
29
31
30
## Quickstart {#quickstart}
32
31
33
-
In this quickstart guide, you'll learn the basics of setting up a new JavaScript smart contract on the enclave that stores and retrieves a greeting message. You'll then go through and create a simple web-based frontend that displays the greeting and allows you to change it.
32
+
In this quick-start guide, you'll learn the basics of setting up a new JavaScript smart contract on the enclave that stores and retrieves a greeting message. You'll then create a simple web-based frontend that displays the greeting and allows you to change it.
34
33
35
34
### Prerequisites
36
35
@@ -47,7 +46,7 @@ node -v
47
46
npm -v
48
47
```
49
48
50
-
It's important to have the **newest** version of the NEAR-CLI installed such that you can make use of the javascript features. To install or update, run:
49
+
It's important to have the **newest** version of the [NEAR-CLI](https://docs.near.org/docs/tools/near-cli) installed such that you can make use of the JavaScript features. To install or update, run:
51
50
52
51
```
53
52
npm install -g near-cli
@@ -113,13 +112,13 @@ Once the package has successfully been installed, you can create a convenient sc
113
112
114
113
> **Note:** This is optional and you can simply run `near-sdk build` instead.
115
114
116
-
You'll now want to create the `src` directory and initialize a new javascript file `index.js` where your contract logic will live.
115
+
You'll now want to create the `src` directory and initialize a new JS file `index.js` where your contract logic will live.
117
116
118
117
```bash
119
118
mkdir src &&cd src && touch index.js &&cd ..
120
119
```
121
120
122
-
The last step is to create a new file called `babel.config.json` which allows you to configure how the contract is built. In the project root, create a new file and add the following content.
121
+
The last step is to create a new file called `babel.config.json` which allows you to configure how the contract is built. In the project's root folder, create a new file and add the following content.
Now that you have the basic structure outlined for your project, it's time to start writing your first contract. You'll create a simple contract for setting and getting a greeting message on-chain.
157
156
158
-
The contract presents 2 methods: ```set_greeting``` and ```get_greeting```. ```set_greeting```stores a String in the contract's parameter message, while ```get_greeting``` retrieves it. By default, the contract returns the message "Hello".
157
+
The contract presents 2 methods: `set_greeting` and `get_greeting`:
158
+
-`set_greeting` stores a String in the contract's parameter message,
159
+
- while `get_greeting` retrieves it.
160
+
161
+
By default, the contract returns the message "Hello".
159
162
160
163
Start by opening the `src/index.js` file as this is where your logic will go. You'll then want to add some imports that will help when writing the contract:
Let's break down these imports to help you understand why they're necessary.
166
-
-`NearContract`: allows our contract to inherit functionalities for changing and reading the contract's state. State can be thought of as the data stored on chain.
170
+
171
+
-`NearContract`: allows the contract to inherit functionalities for changing and reading the contract's state. The state can be thought of as the data stored on-chain.
167
172
-`NearBindgen`: allows your contract to compile down to something that is NEAR compatible.
168
-
-`call, view`: allows your methods to be viewonly functions or mutable (change) functions.
169
-
-`near`: allows you to access important information within your functions such as the signer, predecessor, attached deposit etc..
173
+
-`call`, `view`: allows your methods to be view-only functions or mutable (change) functions.
174
+
-`near`: allows you to access important information within your functions such as the signer, predecessor, attached deposit, etc.
170
175
171
-
Now that you've imported everything from the sdk, create a new class that extends the `NearContract`. This class will contain the core logic of your smart contract. You can also use this opportunity to create a default message variable. Below the import add:
176
+
Now that you've imported everything from the SDK, create a new class that extends the `NearContract`. This class will contain the core logic of your smart contract. You can also use this opportunity to create a default message variable. Below the `import` add:
172
177
173
178
```js
174
179
// Define the default message
@@ -185,7 +190,7 @@ class StatusMessage extends NearContract {
185
190
}
186
191
}
187
192
```
188
-
Running the constructor will default the contract's `message` state variable with the `DEFAULT_MESSAGE`. There's no way to get the current greeting, however. Within the class, add the following function.
193
+
Running the constructor will default the contract's `message` state variable with the `DEFAULT_MESSAGE`. Since there's no way to get the current greeting, within the class you can add the following `view`function:
189
194
190
195
```js
191
196
// Public method - returns the greeting saved, defaulting to DEFAULT_MESSAGE
@@ -241,12 +246,14 @@ class StatusMessage extends NearContract {
241
246
}
242
247
}
243
248
```
244
-
> <strong>Heads up:</strong> You might see a warning from your JavaScript linter because the NEAR SDK uses an custom decorators which is experimentatl feature. This will be addressed in a future release of the JS SDK. It can be ignored for now.
249
+
:::note Heads up
250
+
You might see a warning from your JavaScript linter because the NEAR SDK uses a custom decorator which is an experimental feature. This will be addressed in a future release of the JS SDK. It can be ignored for now.
251
+
:::
245
252
246
253
247
254
### Building
248
255
249
-
Now that your contract is finished, it's time to build and deploy it. Run the following command to build your JS code and get the `build/contact.base64` contract file.
256
+
Now that your contract is finished, it's time to build and deploy it. Run the following command to build your JS code and get the `build/contract.base64` contract file.
250
257
251
258
```
252
259
yarn build
@@ -284,13 +291,13 @@ Now that your contract is deployed, you can start interacting with it. The first
You'll now initialize the contract such that the default greeting is set. If you try to interact with the contract before it's initialized, you'll be thrown an error saying "Contract state is empty".
294
+
You'll now initialize the contract such that the default greeting is set. If you try to interact with the contract before it's initialized, you'll get an error saying "Contract state is empty".
288
295
289
296
```bash
290
297
near js call $JS_CONTRACT init --accountId $JS_CONTRACT --deposit 0.1
291
298
```
292
299
293
-
Once the contract is initialized, you can view the current greeting by performing a view call:
300
+
Once the contract is initialized, you can view the current greeting by performing a `view` call:
0 commit comments