-
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.
- Loading branch information
1 parent
01cab6d
commit ea2632f
Showing
11 changed files
with
858 additions
and
665 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,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"); | ||
}); |
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,40 @@ | ||
import {NearBindgen, call, view, near, borshSerialize, borshDeserialize} from "near-sdk-js"; | ||
|
||
const schema = { | ||
struct: { records: {map: { key: 'string', value: 'string' }} } | ||
}; | ||
|
||
function borshSerializeStatusMessage(statusMessage) { | ||
return borshSerialize(schema, statusMessage); | ||
} | ||
|
||
function borshDeserializeStatusMessage(value) { | ||
return borshDeserialize(schema, value) | ||
} | ||
|
||
@NearBindgen({ | ||
serializer(value) { | ||
return borshSerializeStatusMessage(value); | ||
}, | ||
deserializer(value) { | ||
return borshDeserializeStatusMessage(value); | ||
} | ||
}) | ||
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 | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
import * as borsh from 'borsh'; | ||
import {Schema} from "borsh"; | ||
import {DecodeTypes} from "borsh/lib/types/types"; | ||
export function borshSerialize(schema: Schema, value: unknown, checkSchema?: boolean): Uint8Array { | ||
return borsh.serialize(schema, value, checkSchema); | ||
} | ||
export function borshDeserialize(schema: Schema, buffer: Uint8Array, checkSchema?: boolean): DecodeTypes { | ||
return borsh.deserialize(schema, buffer, checkSchema); | ||
} |
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
Oops, something went wrong.