-
Notifications
You must be signed in to change notification settings - Fork 71
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 #378 from matiasbenary/develop
fix: PromiseIndex type
- Loading branch information
Showing
6 changed files
with
137 additions
and
15 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,81 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(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 status-message the contract. | ||
const statusMessage = await root.devDeploy("./build/status-message.wasm"); | ||
|
||
// Deploy the onCall contract. | ||
const onCall = await root.devDeploy("./build/cross-contract-call-ts.wasm"); | ||
|
||
// Init the contract | ||
await onCall.call(onCall, "init", { | ||
statusMessageContract: statusMessage.accountId, | ||
}); | ||
|
||
// Create test accounts | ||
const ali = await root.createSubAccount("ali"); | ||
const bob = await root.createSubAccount("bob"); | ||
|
||
// Save state for test runs, it is unique for each test | ||
t.context.worker = worker; | ||
t.context.accounts = { | ||
root, | ||
statusMessage, | ||
onCall, | ||
ali, | ||
bob, | ||
}; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("Nobody is on-call in the beginning", async (t) => { | ||
const { onCall } = t.context.accounts; | ||
const result = await onCall.view("person_on_call", {}); | ||
t.is(result, ""); | ||
}); | ||
|
||
test("Person can be set on-call if AVAILABLE", async (t) => { | ||
const { ali, bob, onCall, statusMessage } = t.context.accounts; | ||
|
||
// Ali set her status as AVAILABLE | ||
await ali.call(statusMessage, "set_status", { message: "AVAILABLE" }); | ||
// Bob sets Ali on-call | ||
await bob.call( | ||
onCall, | ||
"set_person_on_call", | ||
{ accountId: ali.accountId }, | ||
{ gas: 120000000000000 } | ||
); | ||
|
||
// Check that Ali is on-call | ||
t.is(await onCall.view("person_on_call", {}), ali.accountId); | ||
}); | ||
|
||
test("Person can NOT be set on-call if UNAVAILABLE", async (t) => { | ||
const { ali, bob, onCall, statusMessage } = t.context.accounts; | ||
|
||
// Ali set her status as AVAILABLE | ||
await ali.call(statusMessage, "set_status", { message: "UNAVAILABLE" }); | ||
// Bob tries to sets Ali on-call | ||
await bob.call( | ||
onCall, | ||
"set_person_on_call", | ||
{ accountId: ali.accountId }, | ||
{ gas: 120000000000000 } | ||
); | ||
|
||
// Check that Ali is NOT on-call | ||
t.not(await onCall.view("person_on_call", {}), ali.accountId); | ||
}); |
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,52 @@ | ||
import { NearBindgen, call, view, initialize, near, bytes } from "near-sdk-js"; | ||
|
||
@NearBindgen({ requireInit: true }) | ||
export class OnCall { | ||
personOnCall:string =""; | ||
statusMessageContract:string =""; | ||
|
||
@initialize({}) | ||
init({ statusMessageContract }) { | ||
this.statusMessageContract = statusMessageContract; | ||
} | ||
|
||
@call({}) | ||
set_person_on_call({ accountId }) { | ||
near.log(`Trying to set ${accountId} on-call`); | ||
const promise = near.promiseBatchCreate(this.statusMessageContract); | ||
near.promiseBatchActionFunctionCall( | ||
promise, | ||
"get_status", | ||
JSON.stringify({ account_id: accountId }), | ||
0, | ||
30000000000000 | ||
); | ||
near.promiseThen( | ||
promise, | ||
near.currentAccountId(), | ||
"_set_person_on_call_private", | ||
JSON.stringify({ accountId: accountId }), | ||
0, | ||
30000000000000 | ||
); | ||
} | ||
|
||
@call({ privateFunction: true }) | ||
_set_person_on_call_private({ accountId }) { | ||
near.log(`_set_person_on_call_private called, accountId ${accountId}`); | ||
const status = JSON.parse(near.promiseResult(0)); | ||
near.log(`${accountId} status is ${status}`); | ||
if (status === "AVAILABLE") { | ||
this.personOnCall = accountId; | ||
near.log(`${accountId} set on-call`); | ||
} else { | ||
near.log(`${accountId} can not be set on-call`); | ||
} | ||
} | ||
|
||
@view({}) | ||
person_on_call() { | ||
near.log(`Returning person on-call: ${this.personOnCall}`); | ||
return this.personOnCall; | ||
} | ||
} |
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