diff --git a/examples/__tests__/test-status-message.ava.js b/examples/__tests__/test-status-message.ava.js index a89828191..de58690d1 100644 --- a/examples/__tests__/test-status-message.ava.js +++ b/examples/__tests__/test-status-message.ava.js @@ -13,8 +13,8 @@ test.before(async t => { './build/status-message.wasm', ); - // Init the contract - await statusMessage.call(statusMessage, 'init', {}); + // When we skip init the status message, default() will be used + // await statusMessage.call(statusMessage, 'init', {}); // Create test users const ali = await root.createSubAccount('ali'); diff --git a/lib/near-contract.js b/lib/near-contract.js index 13a117830..f7b62f2c8 100644 --- a/lib/near-contract.js +++ b/lib/near-contract.js @@ -2,11 +2,11 @@ import * as near from "./api"; export class NearContract { deserialize() { const rawState = near.storageRead("STATE"); + let c = this.default(); if (rawState) { const state = JSON.parse(rawState); - // reconstruction of the contract class object from plain object - let c = this.default(); Object.assign(this, state); + // reconstruction of the contract class object from plain object for (const item in c) { if (c[item].constructor?.deserialize !== undefined) { this[item] = c[item].constructor.deserialize(this[item]); @@ -14,7 +14,7 @@ export class NearContract { } } else { - throw new Error("Contract state is empty"); + Object.assign(this, c); } } serialize() { diff --git a/src/near-contract.ts b/src/near-contract.ts index 76afcb31c..17a8f9e6b 100644 --- a/src/near-contract.ts +++ b/src/near-contract.ts @@ -3,18 +3,18 @@ import * as near from "./api"; export abstract class NearContract { deserialize() { const rawState = near.storageRead("STATE"); + let c = this.default() if (rawState) { const state = JSON.parse(rawState) - // reconstruction of the contract class object from plain object - let c = this.default() Object.assign(this, state); + // reconstruction of the contract class object from plain object for (const item in c) { if (c[item].constructor?.deserialize !== undefined) { this[item] = c[item].constructor.deserialize(this[item]) } } } else { - throw new Error("Contract state is empty"); + Object.assign(this, c) } }