Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default will be used if call a contract without explicit init #169

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/__tests__/test-status-message.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions lib/near-contract.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/near-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down