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
TypeScriptspecially 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-projectdirectorycd ~/dev/my-first-near-project
- Execute the command below to initialize a new
package.jsonto configure yournpmpackages.npm init
Configuration Initialization
- Execute the command below to create a new directory named
assemblymkdir assembly
- Execute the command below to populate the project directory with an
AssemblyScriptconfig file.echo "{ \"extends\" : \"near-sdk-as/asconfig.json\" }" > ./asconfig.json
- Execute the command below to populate the project directory with an
TypeScriptconfig file.echo "{ \"extends\" : \"assemblyscript/std/assembly.json\" }" > ./tsconfig.json
- Execute the command below to populate the
assemblydirectory with aTypeScriptindex fileecho "export function helloWorld(): string { return \"hello world\"; }" > ./assembly/index.ts- Upon execution, this file will return
hello worldas a string
- Upon execution, this file will return
Yarn Initialization
- Execute the command below to add the
yarndependency fornear-sdkyarn add -O near-sdk-as
- Modify the
package.jsonfile by replacingdependenciesblock with thedevDependenciesblock below
{
// ...
"devDependencies":{
"near-sdk-as": "^3.0.0"
}
// ...
}
- Modify the
package.jsonto include ascripts.buildkey with a value ofasb"build":"asb"
- Execute the command below to build the project in
AssemblyScriptyarn asb- Upon execution, a new directory named
buildshould generate with contents reflecting the compiledAssemblyScriptbytecode with extension.wasm
- Upon execution, a new directory named
- Execute the command below to build the project in
AssemblyScriptin human-readable codeyarn asb --wat- Upon execution, a new directory named
buildshould generate with contents reflecting the compiledAssemblyScripthuman-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.tsexample.spec.ts
- Upon execution, a new directory named
- Modify the file
example.spec.tsto 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.jsto reflect the code below
module.exports = require("near-sdk-as/imports")
- Execute the command below to run tests.
yarn asp- ensure the test passes





