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

Add nested and nested nested collection example #251

Merged
merged 4 commits into from
Oct 11, 2022
Merged
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
122 changes: 122 additions & 0 deletions examples/__tests__/test-nested-collections.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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 nestedCollections = await root.devDeploy(
"./build/nested-collections.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, nestedCollections, 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("Ali sets then gets text", async (t) => {
const { ali, nestedCollections } = t.context.accounts;
await ali.call(nestedCollections, "add", { id: "1", text: "hello" });
await ali.call(nestedCollections, "add", { id: "2", text: "world" });

t.is(
await nestedCollections.view("get", { id: "1", accountId: ali.accountId }),
"hello"
);

t.is(
await nestedCollections.view("get", { id: "2", accountId: ali.accountId }),
"world"
);
});

test("Bob and Carl have different statuses", async (t) => {
const { nestedCollections, bob, carl } = t.context.accounts;
await bob.call(nestedCollections, "add", { id: "1", text: "hello" });
await carl.call(nestedCollections, "add", { id: "1", text: "world" });

t.is(
await nestedCollections.view("get", { id: "1", accountId: bob.accountId }),
"hello"
);

t.is(
await nestedCollections.view("get", { id: "1", accountId: carl.accountId }),
"world"
);
});

test("sets then gets nested nested collection", async (t) => {
const { ali, bob, nestedCollections } = t.context.accounts;
await ali.call(nestedCollections, "add_to_group", {
group: "x",
id: "1",
text: "hello",
});
await ali.call(nestedCollections, "add_to_group", {
group: "x",
id: "2",
text: "world",
});
await ali.call(nestedCollections, "add_to_group", {
group: "y",
id: "2",
text: "cat",
});
await bob.call(nestedCollections, "add_to_group", {
group: "y",
id: "2",
text: "dog",
});

t.is(
await nestedCollections.view("get_from_group", {
group: "x",
id: "1",
accountId: ali.accountId,
}),
"hello"
);

t.is(
await nestedCollections.view("get_from_group", {
group: "x",
id: "2",
accountId: ali.accountId,
}),
"world"
);

t.is(
await nestedCollections.view("get_from_group", {
group: "y",
id: "2",
accountId: ali.accountId,
}),
"cat"
);

t.is(
await nestedCollections.view("get_from_group", {
group: "y",
id: "2",
accountId: bob.accountId,
}),
"dog"
);
});
6 changes: 4 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"postinstall": "cd .. && yarn link && cd examples && yarn link near-sdk-js",
"build": "yarn build:clean-state && yarn build:counter && yarn build:counter-lowlevel && yarn build:counter-ts && yarn build:cross-contract-call && yarn build:fungible-token-lockable && yarn build:fungible-token && yarn build:non-fungible-token && yarn build:status-message-collections && yarn build:status-message && yarn build:parking-lot",
"build": "yarn build:clean-state && yarn build:counter && yarn build:counter-lowlevel && yarn build:counter-ts && yarn build:cross-contract-call && yarn build:fungible-token-lockable && yarn build:fungible-token && yarn build:non-fungible-token && yarn build:status-message-collections && yarn build:status-message && yarn build:parking-lot && yarn build:nested-collections",
"build:status-message": "near-sdk-js build src/status-message.js build/status-message.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",
Expand All @@ -18,6 +18,7 @@
"build:non-fungible-token": "near-sdk-js build src/non-fungible-token-receiver.js build/non-fungible-token-receiver.wasm && near-sdk-js build src/non-fungible-token.js build/non-fungible-token.wasm",
"build:status-message-collections": "near-sdk-js build src/status-message-collections.js build/status-message-collections.wasm",
"build:parking-lot": "near-sdk-js build src/parking-lot.ts build/parking-lot.wasm",
"build:nested-collections": "near-sdk-js build src/nested-collections.ts build/nested-collections.wasm",
"test": "ava && yarn test:counter-lowlevel && yarn test:counter-ts",
"test:status-message": "ava __tests__/test-status-message.ava.js",
"test:clean-state": "ava __tests__/test-clean-state.ava.js",
Expand All @@ -29,7 +30,8 @@
"test:fungible-token": "ava __tests__/test-fungible-token.ava.js",
"test:non-fungible-token": "ava __tests__/test-non-fungible-token.ava.js",
"test:status-message-collections": "ava __tests__/test-status-message-collections.ava.js",
"test:parking-lot": "ava __tests__/test-parking-lot.ava.js"
"test:parking-lot": "ava __tests__/test-parking-lot.ava.js",
"test:nested-collections": "ava __tests__/test-nested-collections.ava.js"
},
"author": "Near Inc <hello@nearprotocol.com>",
"license": "Apache-2.0",
Expand Down
66 changes: 66 additions & 0 deletions examples/src/nested-collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { NearBindgen, near, call, view, UnorderedMap } from "near-sdk-js";
import { log } from "./log";

@NearBindgen({})
class Contract {
outerMap: UnorderedMap<UnorderedMap<string>>;
groups: UnorderedMap<UnorderedMap<UnorderedMap<string>>>;

constructor() {
this.outerMap = new UnorderedMap("o");
this.groups = new UnorderedMap("gs");
}

@call({})
add({ id, text }) {
const innerMap = this.outerMap.get(id, {
reconstructor: UnorderedMap.reconstruct,
defaultValue: new UnorderedMap<string>("i_" + id + "_"),
});
innerMap.set(near.signerAccountId(), text);
this.outerMap.set(id, innerMap);
}

@view({})
get({ id, accountId }) {
const innerMap = this.outerMap.get(id, {
reconstructor: UnorderedMap.reconstruct,
});
if (innerMap === null) {
return null;
}
return innerMap.get(accountId);
}

@call({})
add_to_group({ group, id, text }) {
const groupMap = this.groups.get(group, {
reconstructor: UnorderedMap.reconstruct,
defaultValue: new UnorderedMap<UnorderedMap<string>>("g_" + group + "_"),
});
const innerMap = groupMap.get(id, {
reconstructor: UnorderedMap.reconstruct,
defaultValue: new UnorderedMap<string>("gi_" + group + "_" + id + "_"),
});
innerMap.set(near.signerAccountId(), text);
groupMap.set(id, innerMap);
this.groups.set(group, groupMap);
}

@view({})
get_from_group({ group, id, accountId }) {
const groupMap = this.groups.get(group, {
reconstructor: UnorderedMap.reconstruct,
});
if (groupMap === null) {
return null;
}
const innerMap = groupMap.get(id, {
reconstructor: UnorderedMap.reconstruct,
});
if (innerMap === null) {
return null;
}
return innerMap.get(accountId);
}
}