-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from near/nested-example
Add nested and nested nested collection example
- Loading branch information
Showing
3 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |