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 build method in promise api to allow execute promise without return it #397

Merged
merged 1 commit into from
May 23, 2024
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
6 changes: 6 additions & 0 deletions packages/near-sdk-js/lib/promise.d.ts

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

8 changes: 8 additions & 0 deletions packages/near-sdk-js/lib/promise.js

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

9 changes: 9 additions & 0 deletions packages/near-sdk-js/src/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,15 @@ export class NearPromise {
onReturn() {
this.asReturn().constructRecursively();
}

/**
* Attach the promise to transaction but does not return it. The promise will be executed, but
* whether it success or not will not affect the transaction result. If you want the promise fail
* also makes the transaction fail, you can simply return the promise from a @call method.
*/
build(): PromiseIndex {
return this.constructRecursively();
}
}

export type PromiseOrValue<T> = NearPromise | T;
30 changes: 30 additions & 0 deletions tests/__tests__/test_highlevel_promise.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,33 @@ test("highlevel promise and", async (t) => {
}
);
});

test("highlevel promise not build and not return", async (t) => {
const { bob, highlevelPromise } = t.context.accounts;

let r = await bob.callRaw(highlevelPromise, "not_return_not_build", "", {
gas: "100 Tgas",
});

try {
let balance = await highlevelPromise.getSubAccount("b").balance();
} catch (e) {
t.is(e.type, "AccountDoesNotExist");
}
});

test("highlevel promise build and not return", async (t) => {
const { bob, highlevelPromise } = t.context.accounts;

let r = await bob.callRaw(highlevelPromise, "build_not_return", "", {
gas: "100 Tgas",
});
t.is(
r.result.receipts_outcome[1].outcome.executor_id,
highlevelPromise.getSubAccount("b").accountId
);
t.is(r.result.receipts_outcome[1].outcome.status.SuccessValue, "");

let balance = await highlevelPromise.getSubAccount("b").balance();
t.is(balance.total.toString(), "10000000000000000000000000");
});
17 changes: 17 additions & 0 deletions tests/src/highlevel-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,21 @@ export class HighlevelPromiseContract {
);
return promise;
}

@call({})
not_return_not_build() {
// let promise = NearPromise.new("b.highlevel-promise.test.near")
// .createAccount()
// .transfer(10000000000000000000000000n);
// nothing happens
}

@call({})
build_not_return() {
let promise = NearPromise.new("b.highlevel-promise.test.near")
.createAccount()
.transfer(10000000000000000000000000n);
promise.build();
// doesn't return the promise, but promise should be created and executed
}
}
Loading