Вова Буйна
Вова Буйна

Reputation: 11

NEAR smart-contract error: "Contract is not initialized"

I created NEAR smart-contract using Assembly Script and deployed to testnet. When I call any function I receiving error that contract is not initialised (contract was added to workspaces in asconfig.json):

Error: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: contract is not initialized, filename: \"src/token/assembly/index.ts\" line: 123 col: 3"}}
ServerTransactionError: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: contract is not initialized, filename: \"src/token/assembly/index.ts\" line: 123 col: 3"}}

First lines of my contract

Upvotes: 1

Views: 541

Answers (2)

John
John

Reputation: 11429

This error can occur when you have a Singleton style contract, and a constructor(){} in it. If you don't use the constructor, just remove it. Or, you can call it with zero arguments when you deploy the contract:

near deploy --accountId example-contract.testnet --wasmFile out/singleton.wasm --initFunction new --initArgs '{}'

or using dev-deploy, without passing an accountId to generate a new dev-account

near dev-deploy --wasmFile out/singleton.wasm --initFunction new --initArgs '{}'

If you have a constructor with some arguments, you need to pass the arguments when you deploy it as well

near deploy --accountId example-contract.testnet --wasmFile out/singleton.wasm --initFunction new --initArgs '{"name":"someName"}'
@nearBindgen
export class Contract {
 name: string;
 // If the constructor is empty without any argumanets, just remove it, and you don't have to think about init.
 // constructor(){}
 
 // When you have some arguments in the constructor, you need to call init after it's deployed. 
 constructor(name:string){
   this.name = name;
 }

}

If the contract is already deployed, which I think is the case when you get this error in the first place, you can also call init afterwards

near call example-contract.testnet init '{"name":"someName"}' --accountId example-contract.testnet

More information can be found in NEAR's documentation

Upvotes: 2

Вова Буйна
Вова Буйна

Reputation: 11

I was using Singleton style for my code and in this way it is required to init my contract before using. Simplest way: use just functions (without Singleton).

Upvotes: 0

Related Questions