Skip to content

Commit

Permalink
Merge pull request #378 from matiasbenary/develop
Browse files Browse the repository at this point in the history
fix: PromiseIndex type
  • Loading branch information
fospring authored Feb 25, 2024
2 parents df7cd8f + 295ff7a commit c7be89a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 15 deletions.
81 changes: 81 additions & 0 deletions examples/__tests__/test-cross-contract-call-ts.ava.js
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);
});
2 changes: 2 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build:counter-lowlevel": "near-sdk-js build src/counter-lowlevel.js build/counter-lowlevel.wasm",
"build:counter-ts": "near-sdk-js build src/counter.ts build/counter-ts.wasm",
"build:cross-contract-call": "near-sdk-js build src/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-call.js build/cross-contract-call.wasm",
"build:cross-contract-call-ts": "near-sdk-js build src/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-call.ts build/cross-contract-call-ts.wasm",
"build:cross-contract-call-loop": "near-sdk-js build src/counter.js build/counter.wasm && near-sdk-js build src/cross-contract-call-loop.js build/cross-contract-call-loop.wasm",
"build:fungible-token-lockable": "near-sdk-js build src/fungible-token-lockable.js build/fungible-token-lockable.wasm",
"build:fungible-token": "near-sdk-js build src/fungible-token.ts build/fungible-token.wasm && near-sdk-js build src/fungible-token-helper.ts build/fungible-token-helper.wasm",
Expand Down Expand Up @@ -43,6 +44,7 @@
"test:counter-lowlevel": "COUNTER_LOWLEVEL=1 ava __tests__/test-counter.ava.js",
"test:counter-ts": "COUNTER_TS=1 ava __tests__/test-counter.ava.js",
"test:cross-contract-call": "ava __tests__/test-cross-contract-call.ava.js",
"test:cross-contract-call-ts": "ava __tests__/test-cross-contract-call-ts.ava.js",
"test:cross-contract-call-loop": "ava __tests__/test-cross-contract-call-loop.ava.js",
"test:fungible-token-lockable": "ava __tests__/test-fungible-token-lockable.ava.js",
"test:fungible-token": "ava __tests__/test-fungible-token.ava.js",
Expand Down
52 changes: 52 additions & 0 deletions examples/src/cross-contract-call.ts
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;
}
}
6 changes: 1 addition & 5 deletions packages/near-sdk-js/lib/utils.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/near-sdk-js/lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions packages/near-sdk-js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ export interface Env {

declare const env: Env;

// make PromiseIndex a nominal typing
enum PromiseIndexBrand {
_ = -1,
}
/**
* A PromiseIndex which represents the ID of a NEAR Promise.
*/
export type PromiseIndex = (number | bigint) & PromiseIndexBrand;
export type PromiseIndex = number | bigint;
/**
* A number that specifies the amount of NEAR in yoctoNEAR.
*/
Expand Down

0 comments on commit c7be89a

Please sign in to comment.