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

feat-issues-242: borsh support #361

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
61 changes: 61 additions & 0 deletions examples/__tests__/test-status-message-borsh.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Worker } from "near-workspaces";
import test from "ava";

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the contract.
const statusMessage = await root.devDeploy("./build/status-message-borsh.wasm");

// Create test users
const ali = await root.createSubAccount("ali");
const bob = await root.createSubAccount("bob");
const carl = await root.createSubAccount("carl");

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, statusMessage, ali, bob, carl };
});

test.after.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("Root gets null status", async (t) => {
const { statusMessage, root } = t.context.accounts;
const result = await statusMessage.view("get_status", {
account_id: root.accountId,
});
t.is(result, null);
});

test("Ali sets then gets status", async (t) => {
const { ali, statusMessage } = t.context.accounts;
await ali.call(statusMessage, "set_status", { message: "hello" });

t.is(
await statusMessage.view("get_status", { account_id: ali.accountId }),
"hello"
);
});

test("Bob and Carl have different statuses", async (t) => {
const { statusMessage, bob, carl } = t.context.accounts;
await bob.call(statusMessage, "set_status", { message: "hello" });
await carl.call(statusMessage, "set_status", { message: "world" });

const bobStatus = await statusMessage.view("get_status", {
account_id: bob.accountId,
});
const carlStatus = await statusMessage.view("get_status", {
account_id: carl.accountId,
});
t.is(bobStatus, "hello");
t.is(carlStatus, "world");
});
50 changes: 50 additions & 0 deletions examples/__tests__/test-status-message-deserialize-err.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Worker } from "near-workspaces";
import test from "ava";

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the contract.
const statusMessage = await root.devDeploy("./build/status-message-deserialize-err.wasm");

// Create test users
const ali = await root.createSubAccount("ali");
const bob = await root.createSubAccount("bob");
const carl = await root.createSubAccount("carl");

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, statusMessage, ali, bob, carl };
});

test.after.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("Root gets null status", async (t) => {
const { statusMessage, root } = t.context.accounts;
const result = await statusMessage.view("get_status", {
account_id: root.accountId,
});
t.is(result, null);
});

test("Ali sets then gets status", async (t) => {
const { ali, statusMessage } = t.context.accounts;
await ali.call(statusMessage, "set_status", { message: "hello" });

let res = await ali.callRaw(statusMessage, "get_status", { account_id: ali.accountId });

t.assert(
res.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes(
"Smart contract panicked: deserialize err"
)
);
});

47 changes: 47 additions & 0 deletions examples/__tests__/test-status-message-serialize-err.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Worker } from "near-workspaces";
import test from "ava";

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the contract.
const statusMessage = await root.devDeploy("./build/status-message-serialize-err.wasm");

// Create test users
const ali = await root.createSubAccount("ali");
const bob = await root.createSubAccount("bob");
const carl = await root.createSubAccount("carl");

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, statusMessage, ali, bob, carl };
});

test.after.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("Root gets null status", async (t) => {
const { statusMessage, root } = t.context.accounts;
const result = await statusMessage.view("get_status", {
account_id: root.accountId,
});
t.is(result, null);
});

test("Ali sets status", async (t) => {
const { ali, statusMessage } = t.context.accounts;
let res = await ali.callRaw(statusMessage, "set_status", { message: "hello" });

t.assert(
res.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes(
"Smart contract panicked: serialize err"
)
);
});
11 changes: 9 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"scripts": {
"build": "run-s build:*",
"build:status-message": "near-sdk-js build src/status-message.js build/status-message.wasm",
"build:status-message-borsh": "near-sdk-js build src/status-message-borsh.js build/status-message-borsh.wasm",
"build:status-message-serialize-err": "near-sdk-js build src/status-message-serialize-err.js build/status-message-serialize-err.wasm",
"build:status-message-deserialize-err": "near-sdk-js build src/status-message-deserialize-err.js build/status-message-deserialize-err.wasm",
"build:clean-state": "near-sdk-js build src/clean-state.js build/clean-state.wasm",
"build:counter": "near-sdk-js build src/counter.js build/counter.wasm",
"build:counter-lowlevel": "near-sdk-js build src/counter-lowlevel.js build/counter-lowlevel.wasm",
Expand Down Expand Up @@ -45,15 +48,19 @@
"test:parking-lot": "ava __tests__/test-parking-lot.ava.js",
"test:programmatic-update": "ava __tests__/test-programmatic-update.ava.js",
"test:state-migration": "ava __tests__/test-state-migration.ava.js",
"test:nested-collections": "ava __tests__/test-nested-collections.ava.js"
"test:nested-collections": "ava __tests__/test-nested-collections.ava.js",
"test:status-message-borsh": "ava __tests__/test-status-message-borsh.ava.js",
"test:status-message-serialize-err": "ava __tests__/test-status-message-serialize-err.ava.js",
"test:status-message-deserialize-err": "ava __tests__/test-status-message-deserialize-err.ava.js"
},
"author": "Near Inc <hello@nearprotocol.com>",
"license": "Apache-2.0",
"dependencies": {
"lodash-es": "^4.17.21",
"near-contract-standards": "workspace:*",
"near-sdk-js": "workspace:*",
"typescript": "^4.7.4"
"typescript": "^4.7.4",
"borsh": "^v1.0.0"
},
"devDependencies": {
"@types/lodash-es": "^4.17.6",
Expand Down
41 changes: 41 additions & 0 deletions examples/src/status-message-borsh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {NearBindgen, call, view, near} from "near-sdk-js";
import * as borsh from 'borsh';

const schema = {
struct: { records: {map: { key: 'string', value: 'string' }} }
};

function borshSerializeStatusMessage(statusMessage) {
return borsh.serialize(schema, statusMessage);
}

function borshDeserializeStatusMessage(value) {
return borsh.deserialize(schema, value)
}

@NearBindgen({
serializer(value) {
return borshSerializeStatusMessage(value);
gagdiez marked this conversation as resolved.
Show resolved Hide resolved
},
deserializer(value) {
return borshDeserializeStatusMessage(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Not related of this PR, but to ensure @NearBindgen(... serializer) API does work, can you try if intentionally throw error here, the test will fail due to the error?

Copy link
Contributor Author

@fospring fospring Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add throw Error test after new commits, test steps,
serialize throw error as expected:

pnpm run build:status-message-serialize-err
pnpm run test:status-message-serialize-err

outputs:

pnpm run test:status-message-serialize-err

> examples@1.0.0 test:status-message-serialize-err /Users/qiuyongchun/workspace/near/near-sdk-js/examples
> ava __tests__/test-status-message-serialize-err.ava.js


Contract logs from dev-13382.test.near.get_status({"account_id":"test.near"}) view call: [ 'get_status for account_id test.near' ]
  ✔ Root gets null status (683ms)
  ✔ Ali sets status (2.5s)
pnpm run build:status-message-deserialize-err
pnpm run test:status-message-deserialize-err

deserialize error outputs:

pnpm run test:status-message-deserialize-err

> examples@1.0.0 test:status-message-deserialize-err /Users/qiuyongchun/workspace/near/near-sdk-js/examples
> ava __tests__/test-status-message-deserialize-err.ava.js


Contract logs from dev-15825.test.near.get_status({"account_id":"test.near"}) view call: [ 'get_status for account_id test.near' ]
  ✔ Root gets null status (677ms)
Contract logs from dev-15825.test.near.set_status({"message":"hello"}) call: [ 'ali.test.near set_status with message hello' ]
  ✔ Ali sets then gets status (5s)
  ─

  2 tests passed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

}
})
export class StatusMessage {
constructor() {
this.records = new Map()
}

@call({})
set_status({ message }) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
}

@view({})
get_status({ account_id }) {
env.log(`get_status for account_id ${account_id}`)
return this.records.get(account_id) || null
}
}
25 changes: 25 additions & 0 deletions examples/src/status-message-deserialize-err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {NearBindgen, call, view, near} from "near-sdk-js";

@NearBindgen({
deserializer(_value) {
throw new Error('deserialize err')
},
})
export class StatusMessage {
constructor() {
this.records = new Map()
}

@call({})
set_status({ message }) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
}

@view({})
get_status({ account_id }) {
env.log(`get_status for account_id ${account_id}`)
return this.records.get(account_id) || null
}
}
25 changes: 25 additions & 0 deletions examples/src/status-message-serialize-err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {NearBindgen, call, view, near} from "near-sdk-js";

@NearBindgen({
serializer(_value) {
throw new Error('serialize err')
},
})
export class StatusMessage {
constructor() {
this.records = new Map()
}

@call({})
set_status({ message }) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
}

@view({})
get_status({ account_id }) {
env.log(`get_status for account_id ${account_id}`)
return this.records.get(account_id) || null
}
}
Loading