NEAR AssemblyScript Project Set Up
Overview
- Prerequisites
- Establishing Vocabulary
- Initializing Project Environment
Prerequisites
Establishing Vocabulary
TypeScript
- strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
WebAssembly
- low-level language with a compact binary format that runs with nearly-native-performance in the browser.
AssemblyScript
- strict variant of
TypeScript
specially created forWebAssembly
.
- strict variant of
Initializing Project Environment
Npm initialization
- Execute the command below to create a new directory as workspace of project.
mkdir -p ~/dev/my-first-near-project
- Execute the command below to change directory to the newly created
my-first-near-project
directorycd ~/dev/my-first-near-project
- Execute the command below to initialize a new
package.json
to configure yournpm
packages.npm init
Configuration Initialization
- Execute the command below to create a new directory named
assembly
mkdir assembly
- Execute the command below to populate the project directory with an
AssemblyScript
config file.echo "{ \"extends\" : \"near-sdk-as/asconfig.json\" }" > ./asconfig.json
- Execute the command below to populate the project directory with an
TypeScript
config file.echo "{ \"extends\" : \"assemblyscript/std/assembly.json\" }" > ./tsconfig.json
- Execute the command below to populate the
assembly
directory with aTypeScript
index fileecho "export function helloWorld(): string { return \"hello world\"; }" > ./assembly/index.ts
- Upon execution, this file will return
hello world
as a string
- Upon execution, this file will return
Yarn Initialization
- Execute the command below to add the
yarn
dependency fornear-sdk
yarn add -O near-sdk-as
- Modify the
package.json
file by replacingdependencies
block with thedevDependencies
block below
{
// ...
"devDependencies":{
"near-sdk-as": "^3.0.0"
}
// ...
}
- Modify the
package.json
to include ascripts.build
key with a value ofasb
"build":"asb"
- Execute the command below to build the project in
AssemblyScript
yarn asb
- Upon execution, a new directory named
build
should generate with contents reflecting the compiledAssemblyScript
bytecode with extension.wasm
- Upon execution, a new directory named
- Execute the command below to build the project in
AssemblyScript
in human-readable codeyarn asb --wat
- Upon execution, a new directory named
build
should generate with contents reflecting the compiledAssemblyScript
human-readable code with extension.wat
- Upon execution, a new directory named
Test Initialization
- Execute the command below to initialize tests
yarn asp --init
- Upon execution, a new directory named
./assembly/__tests__/
should generate with boilerplate testing contentas-pect.d.ts
example.spec.ts
- Upon execution, a new directory named
- Modify the file
example.spec.ts
to reflect the code below
import {helloWorld} from '..';
describe("example", ()=>{
it("should return 'hello world'", ()=> {
expect(helloWorld()).toStrictEqual("hello world");
})
})
- Modify the file
as-pect.config.js
to reflect the code below
module.exports = require("near-sdk-as/imports")
- Execute the command below to run tests.
yarn asp
- ensure the test passes