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 10241af84..951d64150 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -48,6 +48,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 function promiseResult(resultIdx: number | BigInt): Bytes | PromiseResult.NotReady | PromiseResult.Failed; export declare function promiseReturn(promiseIdx: number | BigInt): void; diff --git a/lib/api.js b/lib/api.js index daa81062f..a1ff7bb61 100644 --- a/lib/api.js +++ b/lib/api.js @@ -184,6 +184,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 f3dbdc95d..ce020b3e9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -319,6 +319,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 740041793..7c836dc63 100644 --- a/tests/__tests__/test_promise_api.ava.js +++ b/tests/__tests__/test_promise_api.ava.js @@ -171,6 +171,12 @@ test('promise delete account', async t => { t.is(await caller2Contract.getSubAccount('e').exists(), false); }); +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); +}); + test('promise batch transfer overflow', async t => { const { bob, caller2Contract } = t.context.accounts; let r = await bob.callRaw(caller2Contract, 'test_transfer_overflow', '', {gas: '100 Tgas'}); @@ -181,4 +187,4 @@ test('promise create gas overflow', async t => { const { ali, callerContract } = t.context.accounts; let r = await ali.callRaw(callerContract, 'test_promise_create_gas_overflow', '', {gas: '100 Tgas'}); t.assert(r.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.startsWith('Smart contract panicked: Expect Uint64 for gas')); -}); \ No newline at end of file +}); diff --git a/tests/src/promise_api.js b/tests/src/promise_api.js index 09fb6621b..cb976942c 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 3145d95c5..c42ebf24f 100644 --- a/tests/src/promise_batch_api.js +++ b/tests/src/promise_batch_api.js @@ -44,6 +44,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)