From c04638fe99f6dde864fe5043b281cf68bf3d0681 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Mon, 22 Aug 2022 17:21:58 +0800 Subject: [PATCH] add promise_batch_action_function_call_weight --- cli/builder/builder.c | 29 +++++++++++++++++++++++++ lib/api.d.ts | 1 + lib/api.js | 3 +++ src/api.ts | 18 +++++++++++++++ tests/__tests__/test_promise_api.ava.js | 8 ++++++- tests/src/promise_api.js | 4 ++++ tests/src/promise_batch_api.js | 6 +++++ 7 files changed, 68 insertions(+), 1 deletion(-) diff --git a/cli/builder/builder.c b/cli/builder/builder.c index 43d3e5715..4fc5153e0 100644 --- a/cli/builder/builder.c +++ b/cli/builder/builder.c @@ -117,6 +117,7 @@ extern void promise_batch_action_add_key_with_full_access(uint64_t promise_index extern void promise_batch_action_add_key_with_function_call(uint64_t promise_index, uint64_t public_key_len, uint64_t public_key_ptr, uint64_t nonce, uint64_t allowance_ptr, uint64_t receiver_id_len, uint64_t receiver_id_ptr, uint64_t method_names_len, uint64_t method_names_ptr); extern void promise_batch_action_delete_key(uint64_t promise_index, uint64_t public_key_len, uint64_t public_key_ptr); extern void promise_batch_action_delete_account(uint64_t promise_index, uint64_t beneficiary_id_len, uint64_t beneficiary_id_ptr); +extern void promise_batch_action_function_call_weight(uint64_t promise_index, uint64_t function_name_len, uint64_t function_name_ptr, uint64_t arguments_len, uint64_t arguments_ptr, uint64_t amount_ptr, uint64_t gas, uint64_t weight); // ####################### // # Promise API results # // ####################### @@ -761,6 +762,33 @@ static JSValue near_promise_batch_action_delete_key(JSContext *ctx, JSValueConst return JS_UNDEFINED; } +static JSValue near_promise_batch_action_function_call_weight(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) +{ + uint64_t promise_index; + const char *method_name_ptr, *arguments_ptr; + size_t method_name_len, arguments_len; + uint64_t amount_ptr[2]; // amount is u128 + uint64_t gas; + uint64_t weight; + + if (JS_ToUint64Ext(ctx, &promise_index, argv[0]) < 0) { + return JS_ThrowTypeError(ctx, "Expect Uint64 for promise_index"); + } + method_name_ptr = JS_ToCStringLen(ctx, &method_name_len, argv[1]); + arguments_ptr = JS_ToCStringLenRaw(ctx, &arguments_len, argv[2]); + if (quickjs_to_u128(ctx, argv[3], amount_ptr) != 0) { + return JS_ThrowTypeError(ctx, "Expect Uint128 for amount"); + } + if (JS_ToUint64Ext(ctx, &gas, argv[4]) < 0) { + return JS_ThrowTypeError(ctx, "Expect Uint64 for gas"); + } + if (JS_ToUint64Ext(ctx, &weight, argv[5]) < 0) { + return JS_ThrowTypeError(ctx, "Expect Uint64 for weight"); + } + promise_batch_action_function_call_weight(promise_index, method_name_len, (uint64_t)method_name_ptr, arguments_len, (uint64_t)arguments_ptr, (uint64_t)amount_ptr, gas, weight); + return JS_UNDEFINED; +} + static JSValue near_promise_batch_action_delete_account(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { uint64_t promise_index; @@ -979,6 +1007,7 @@ static void js_add_near_host_functions(JSContext* ctx) { JS_SetPropertyStr(ctx, env, "promise_batch_action_add_key_with_function_call", JS_NewCFunction(ctx, near_promise_batch_action_add_key_with_function_call, "promise_batch_action_add_key_with_function_call", 6)); JS_SetPropertyStr(ctx, env, "promise_batch_action_delete_key", JS_NewCFunction(ctx, near_promise_batch_action_delete_key, "promise_batch_action_delete_key", 2)); JS_SetPropertyStr(ctx, env, "promise_batch_action_delete_account", JS_NewCFunction(ctx, near_promise_batch_action_delete_account, "promise_batch_action_delete_account", 2)); + JS_SetPropertyStr(ctx, env, "promise_batch_action_function_call_weight", JS_NewCFunction(ctx, near_promise_batch_action_function_call_weight, "promise_batch_action_function_call_weight", 6)); JS_SetPropertyStr(ctx, env, "promise_results_count", JS_NewCFunction(ctx, near_promise_results_count, "promise_results_count", 0)); JS_SetPropertyStr(ctx, env, "promise_result", JS_NewCFunction(ctx, near_promise_result, "promise_result", 2)); JS_SetPropertyStr(ctx, env, "promise_return", JS_NewCFunction(ctx, near_promise_return, "promise_return", 1)); diff --git a/lib/api.d.ts b/lib/api.d.ts index 709d89c15..34dd10c38 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -58,6 +58,7 @@ export declare function promiseBatchActionAddKeyWithFullAccess(promiseIndex: num export declare function promiseBatchActionAddKeyWithFunctionCall(promiseIndex: number | BigInt, publicKey: Bytes, nonce: number | BigInt, allowance: number | BigInt, receiverId: string, methodNames: string): void; export declare function promiseBatchActionDeleteKey(promiseIndex: number | BigInt, publicKey: Bytes): void; export declare function promiseBatchActionDeleteAccount(promiseIndex: number | BigInt, beneficiaryId: string): void; +export declare function promiseBatchActionFunctionCallWeight(promiseIndex: number | BigInt, methodName: string, args: Bytes, amount: number | BigInt, gas: number | BigInt, weight: number | BigInt): void; export declare function promiseResultsCount(): BigInt; export declare enum PromiseResult { NotReady = 0, diff --git a/lib/api.js b/lib/api.js index 983cad0f0..278dc8c05 100644 --- a/lib/api.js +++ b/lib/api.js @@ -242,6 +242,9 @@ export function promiseBatchActionDeleteKey(promiseIndex, publicKey) { export function promiseBatchActionDeleteAccount(promiseIndex, beneficiaryId) { env.promise_batch_action_delete_account(promiseIndex, beneficiaryId); } +export function promiseBatchActionFunctionCallWeight(promiseIndex, methodName, args, amount, gas, weight) { + env.promise_batch_action_function_call_weight(promiseIndex, methodName, args, amount, gas, weight); +} export function promiseResultsCount() { return env.promise_results_count(); } diff --git a/src/api.ts b/src/api.ts index 3676aef14..13cae56d8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -395,6 +395,24 @@ export function promiseBatchActionDeleteAccount( env.promise_batch_action_delete_account(promiseIndex, beneficiaryId); } +export function promiseBatchActionFunctionCallWeight( + promiseIndex: number | BigInt, + methodName: string, + args: Bytes, + amount: number | BigInt, + gas: number | BigInt, + weight: number | BigInt, +) { + env.promise_batch_action_function_call_weight( + promiseIndex, + methodName, + args, + amount, + gas, + weight + ); +} + export function promiseResultsCount(): BigInt { return env.promise_results_count(); } diff --git a/tests/__tests__/test_promise_api.ava.js b/tests/__tests__/test_promise_api.ava.js index cbc366d0f..245d723c6 100644 --- a/tests/__tests__/test_promise_api.ava.js +++ b/tests/__tests__/test_promise_api.ava.js @@ -169,4 +169,10 @@ test('promise delete account', async t => { let r = await bob.callRaw(caller2Contract, 'test_delete_account', '', {gas: '100 Tgas'}); t.is(r.result.status.SuccessValue, ''); t.is(await caller2Contract.getSubAccount('e').exists(), false); -}); \ No newline at end of file +}); + +test('promise batch function call weight', async t => { + const { ali, caller2Contract, calleeContract } = t.context.accounts; + let r = await ali.callRaw(caller2Contract, 'test_promise_batch_call_weight', '', {gas: '100 Tgas'}); + t.assert(r.result.status.SuccessValue); +}) \ No newline at end of file diff --git a/tests/src/promise_api.js b/tests/src/promise_api.js index 01aebf66f..66634a8b4 100644 --- a/tests/src/promise_api.js +++ b/tests/src/promise_api.js @@ -17,6 +17,10 @@ export function cross_contract_callee() { near.valueReturn(bytes(JSON.stringify(callingData()))) } +export function cross_contract_call_gas() { + near.valueReturn(bytes(near.prepaidGas().toString())) +} + export function cross_contract_callback() { near.valueReturn(bytes(JSON.stringify({...callingData(), promiseResults: arrayN(near.promiseResultsCount()).map(i => near.promiseResult(i))}))) } diff --git a/tests/src/promise_batch_api.js b/tests/src/promise_batch_api.js index b59573364..8ccc02b2d 100644 --- a/tests/src/promise_batch_api.js +++ b/tests/src/promise_batch_api.js @@ -37,6 +37,12 @@ export function test_promise_batch_create_transfer() { near.promiseReturn(promiseId) } +export function test_promise_batch_call_weight() { + let promiseId = near.promiseBatchCreate('callee-contract.test.near') + near.promiseBatchActionFunctionCallWeight(promiseId, 'cross_contract_call_gas', bytes('abc'), 0, 0, 1) + near.promiseReturn(promiseId) +} + export function test_promise_batch_deploy_call() { let promiseId = near.promiseBatchCreate('b.caller2.test.near') near.promiseBatchActionCreateAccount(promiseId)