Kaustubh Rai
Kaustubh Rai

Reputation: 21

Error: Error happened during instantiation ServerTransactionError: Error happened during instantiation

Been working on a small project to and whenever I call any function of assemblyscript contract, I get "instantiation error". Heres the log:

Log Image

Scheduling a call: dev-1643739174146-18174234067579.init({"owner":"$OWNER"})

Doing account.functionCall() Receipt: 5CnMQ2RB2hHbDYTW6BecYfcYznLVqWuz3icVSXuXJcCj

Failure [dev-1643739174146-18174234067579]: Error: Error happened during instantiation

ServerTransactionError: Error happened during instantiation

at Object.parseResultError (/mnt/c/Users/d4r18/AppData/Roaming/npm/node_modules/near-cli/node_modules/near-api-js/lib/utils/rpc_errors.js:31:29)
at Account.signAndSendTransactionV2 (/mnt/c/Users/d4r18/AppData/Roaming/npm/node_modules/near-cli/node_modules/near-api-js/lib/account.js:160:36)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async scheduleFunctionCall (/mnt/c/Users/d4r18/AppData/Roaming/npm/node_modules/near-cli/commands/call.js:57:38)
at async Object.handler (/mnt/c/Users/d4r18/AppData/Roaming/npm/node_modules/near-cli/utils/exit-on-error.js:52:9)

Heres my modules.ts:

import { Context, u128, PersistentVector } from 'near-sdk-as';

@nearBindgen
export class Note {

    private content: string;
    constructor(public text: string) {
        this.content = text;
    }

    delete(): void {
        this.content = "";
    }

    edit(note: string, append: bool = false): string {
        if (append)
            this.content.concat(note);
        else
            this.content = note;

        return this.content;
    }

    get(): string {
        return this.content;
    }
}

@nearBindgen
export class Vector<T> extends PersistentVector<T>{
    get_all(): Array<T> {
        const result = new Array<T>();
        for (let i = 0; i < this.length; i++) {
            const entry = this[i];
            result.push(entry);
        }
        return result;
    }
}

and heres the index.ts

import { Context, logging, storage } from 'near-sdk-as'
import { Note, Vector } from './modules'

@nearBindgen
export class Contract {
  private owner: string;
  private notes: Vector<Note> = new Vector<Note>('m');
  private message: Note;

  constructor(master: string) {
    this.owner = master;
  }

  @mutateState()
  create(note: string): bool {
    // this.message = new Note(note);
    console.log("this.message.get()");
    return true;

  }

  list(): Array<Note> {
    return this.notes.get_all();
  }

  private assert_owner(): void {
    const caller = Context.predecessor;
    assert(
      this.owner == caller,
      'Only the owner of this contract may call this method'
    );
  }
}

Would be great help if anyone could help me with this.

Upvotes: 1

Views: 221

Answers (1)

Kaustubh Rai
Kaustubh Rai

Reputation: 21

The "console.log" statement was causing the problem. Removing it fixed the issue. I instead used logger.log() method for it. It can be imported from "near-sdk-as"

import { Context, logging, storage } from 'near-sdk-as'
import { Note, Vector } from './modules'

@nearBindgen
export class Contract {
  private owner: string;
  private notes: Vector<Note> = new Vector<Note>('m');
  private message: Note;
   
  constructor(master: string) {
    this.owner = master;
  }

  @mutateState()
  create(note: string): bool {
   // this.message = new Note(note);

   // Remove the console.log line
   // console.log("this.message.get()");
   return true;
  }

  list(): Array<Note> {
    return this.notes.get_all();
  }

  private assert_owner(): void {
    const caller = Context.predecessor;
    assert(
      this.owner == caller,
      'Only the owner of this contract may call this method'
    );
  }
}

Upvotes: 1

Related Questions