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
Copy file name to clipboardExpand all lines: docs/develop/contracts/js/quickstart.md
+25-17Lines changed: 25 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,38 +1,36 @@
1
1
---
2
2
id: enclave-quickstart
3
-
title: Javascript Enclave
3
+
title: JavaScript Enclave
4
4
sidebar_label: Enclave Quickstart
5
5
---
6
6
7
-
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.
7
+
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 widely used 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 docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript) for more details.
10
10
11
11
<blockquoteclass="warning">
12
-
<strong>heads up</strong><br /><br />
13
-
14
-
Javascript smart contract development is not recommended for financial use cases as it is still very new to the NEAR ecosystem.
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.
15
13
16
14
</blockquote>
17
15
18
16
## Overview {#overview}
19
17
20
-
The Javascript Enclave, or jsvm for short, provides an isolated environment where users can learn the basics of how to write smart contracts on NEAR. This isolated environment is run on a virtual machine, similar to how [aurora](https://doc.aurora.dev/getting-started/aurora-engine) operates. Standard smart contracts that are built using Rust or AssemblyScript compile to [WebAssembly](https://webassembly.org/) or simply WASM. With the jsvm, contracts are encoded to base64 and are deployed to a virtual machine.
18
+
The JavaScript Enclave, or JavaScript Virtual Machine (jsvm) provides an isolated environment where users can learn the basics of how to write smart contracts on NEAR. This isolated environment is run on a virtual machine, similar to how [Aurora](https://doc.aurora.dev/getting-started/aurora-engine) operates. Standard smart contracts that are built using Rust or AssemblyScript compile to [WebAssembly](https://webassembly.org/) or simply WASM. With the jsvm, contracts are encoded to base64 and are deployed to a virtual machine.
21
19
22
20
There are several pros and cons when comparing the enclave approach to the regular WASM approach. The key differences are outlined below.
| Can interact with any smart contract on NEAR |✅|❌|
27
25
| Synchronous Cross-Contract Calls |❌|✅|
28
-
| Standards Support |✅|❌|
29
-
| Function Call Access Key Support |✅|❌|
26
+
| Standards Support |✅|Not in V1.0|
27
+
| Function Call Access Key Support |✅|Not in V1.0|
30
28
31
-
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.
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.
32
30
33
31
## Quickstart {#quickstart}
34
32
35
-
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.
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.
36
34
37
35
### Prerequisites
38
36
@@ -57,7 +55,7 @@ npm install -g near-cli
57
55
58
56
### Creating a project
59
57
60
-
Now that you have Nodeand npm installed, create a new directory to initialize the project in.
58
+
Now that you have Node, npm, and the NEAR-CLI installed, create a new directory to initialize the project in.
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.
159
157
160
-
The contract presents 2 methods: set_greeting and get_greeting. The first one stores a String in the contract's parameter message, while the second one retrieves it. By default, the contract returns the message "Hello".
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".
161
159
162
160
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.
168
-
-`NearContract`: allows our contract to inherit important functionalities for changing and reading the contract's state.
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.
169
167
-`NearBindgen`: allows your contract to compile down to something that is NEAR compatible.
170
-
-`call, view`: allows your methods to be view functions or change functions.
168
+
-`call, view`: allows your methods to be view only functions or mutable (change) functions.
171
169
-`near`: allows you to access important information within your functions such as the signer, predecessor, attached deposit etc..
172
170
173
-
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:
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:
174
172
175
173
```js
176
174
// Define the default message
@@ -243,6 +241,8 @@ class StatusMessage extends NearContract {
243
241
}
244
242
}
245
243
```
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.
Now that your contract is deployed, you can start interacting with it. The first thing to do is initialize the contract. For simplicity, export the account ID that the contract is deployed to into an environment variable.
274
+
The return from the deploy should include an account address in the first line after Account id:
Now that your contract is deployed, you can start interacting with it. The first thing to do is initialize the contract. For simplicity, export the account ID that the contract is deployed to into an environment variable.
0 commit comments