Why am I seeing error: Cannot find name 'decode' and 'encode' when building a NEAR smart contract (AssemblyScript)?

Viewed 192

I've started a new NEAR AssemblyScript smart contract project using near-sdk-as: 3.2.3, and I'm experiencing some errors when I try to build it.

I get these errors when I try to build my AssemblyScript code: asb --target debug.

 asb --target debug

ERROR TS2304: Cannot find name 'decode'.

     return decode<T>(bytes);
            ~~~~~~
 in ~lib/near-sdk-core/util.ts(46,12)

ERROR TS2304: Cannot find name 'encode'.

       this.setBytes(key, encode<T>(value));
                          ~~~~~~
 in ~lib/near-sdk-core/storage.ts(160,26)

I'm using a PersistentMap in my smart contract, and I have defined a custom class, User. The example below only includes the minimal code necessary to reproduce the same error.

index.ts

import { PersistentMap} from 'near-sdk-as';
import { User } from './user';
    
@nearBindgen
export class Contract {
  private userMap: PersistentMap<string, User> = new PersistentMap<string, User>('users');

  getUser(username: string): User {
    return this.userMap.getSome(username); 
  }
}

user.ts

@nearBindgen
export class User {
  private _username: string = '';

  constructor(username: string) {
    this._username = username;
  }
}
1 Answers

I'm answering my own question because I spent some time trying to figure it out.

After taking a look at the GitHub project: starter--near-sdk, I noticed that I needed to extend "near-sdk-as/asconfig.json" in my asconfig.ts file:

{
  "extends": "near-sdk-as/asconfig.json"
}

I could build my contract successfully after adding this line in my own asconfig.json file

Related