Common mistakes you might make using the near-sdk-as and assemblyscript
Tags: blockchain, near, typescript, beginners
Published: 2022-01-30 16:15:00

In this article, we will be exploring a few mistakes that you might make when getting started with the near-sdk-as. Before we dive into these mistakes, let us look at the near-sdk-as.


# Introduction

The near-sdk-as package is used to write smart contracts to interact with the NEAR Blockchain. These smart contracts are written using assemblyscript, a typescript-based programming language optimized to compile to WebAssembly. Now I know what you might be thinking, "assemblyscript is typescript based, then it must be similar to typescript"… well, you are not wrong, but you are also not correct. If you are coming from a javascript/typescript environment, this thinking already sets you up to make inevitable mistakes when working with assemblyscript and the near-sdk-as.

Let's look at these common mistakes:


# Common Mistakes

## Javascript/Typescript packages

You need to note that you cannot use Typescript/Javascript libraries with assemblyscript… I tried. It would be best to stick to strictly assemblyscript packages to avoid breaking your code.


## Generating Random Numbers 

Generating random numbers with assemblyscript is not exactly as easy as you get to do it in Javascript, but do not worry, near-sdk-as have an easier way to do this using the RNG class. It would be best to always browse through the documentation for packages before using them. Here is a sample use case for the RNG package:


```typescript
import { RNG } from "near-sdk-as";
...
const roll = new RNG<u32>(1, u32.MAX_VALUE);
const randomNumber = roll.next();
//roll.next() will return a random number that falls withing the max value for a u32(32 bit unsigned integer) data type
...
```

For more information on how this works, you can read the documentation here.


## Date.now() ? 

The blockchain provides you with a timestamp(literally). The Context class has a property `blockTimestamp` that returns the current time in nanoseconds. It would be best not to use the native date constructor to get the current date and time. 


```typescript
import { Context } from "near-sdk-core";
...
export function currentTime(): u64{
  const time = Context.blockTimestamp;
  return time;
}
...
```


## Using Signer the right way

The signer property from the near-sdk-as Context class has a special use case: it can only be used in “change” functions/methods. There exist two types of methods/functions on the NEAR blockchain; change and view methods.


- **View Methods**: These are methods that fetch or retrieve information from the storage.
- **Change Methods**: These are methods that mutate/update the storage. 

The change methods are transactions; therefore, it requires an account to sign these transactions. The property signer on the Context class can be used to access the account that signed the change method called. View methods do not require to be signed; therefore, signer property does not return an account for these methods. Some might find themselves trying to access a signing account from Context class in a view method, resulting in your smart contract panicking/failing.


Summary and Resources

The mistakes above and a few that is not listed can be avoided or easily navigated by exploring the official documentation and reviewing a few open-source smart contracts provided by the NEAR Edu and NEAR community(https://github.com/Learn-NEAR).