From a690e33dfaa2b906292f73b9548c4f7a7b64446d Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Thu, 23 May 2024 16:02:21 +0800 Subject: [PATCH] Add build method in promise api to allow execute promise without return it --- packages/near-sdk-js/lib/promise.d.ts | 6 ++++ packages/near-sdk-js/lib/promise.js | 8 +++++ packages/near-sdk-js/src/promise.ts | 9 ++++++ tests/__tests__/test_highlevel_promise.ava.js | 30 +++++++++++++++++++ tests/src/highlevel-promise.js | 17 +++++++++++ 5 files changed, 70 insertions(+) diff --git a/packages/near-sdk-js/lib/promise.d.ts b/packages/near-sdk-js/lib/promise.d.ts index 691200a1f..d5f4186ff 100644 --- a/packages/near-sdk-js/lib/promise.d.ts +++ b/packages/near-sdk-js/lib/promise.d.ts @@ -370,6 +370,12 @@ export declare class NearPromise { * Called by NearBindgen, when return object is a NearPromise instance. */ onReturn(): void; + /** + * 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; } export declare type PromiseOrValue = NearPromise | T; export {}; diff --git a/packages/near-sdk-js/lib/promise.js b/packages/near-sdk-js/lib/promise.js index f40080105..b29c840df 100644 --- a/packages/near-sdk-js/lib/promise.js +++ b/packages/near-sdk-js/lib/promise.js @@ -484,4 +484,12 @@ 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() { + return this.constructRecursively(); + } } diff --git a/packages/near-sdk-js/src/promise.ts b/packages/near-sdk-js/src/promise.ts index 79359887a..5e63cb3b3 100644 --- a/packages/near-sdk-js/src/promise.ts +++ b/packages/near-sdk-js/src/promise.ts @@ -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 = NearPromise | T; diff --git a/tests/__tests__/test_highlevel_promise.ava.js b/tests/__tests__/test_highlevel_promise.ava.js index f33abc988..bff79efdf 100644 --- a/tests/__tests__/test_highlevel_promise.ava.js +++ b/tests/__tests__/test_highlevel_promise.ava.js @@ -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"); +}); diff --git a/tests/src/highlevel-promise.js b/tests/src/highlevel-promise.js index c4a6783a6..adb49883a 100644 --- a/tests/src/highlevel-promise.js +++ b/tests/src/highlevel-promise.js @@ -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 + } }