Skip to content

Commit

Permalink
add promise_batch_action_function_call_weight (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: near-bulldozer[bot] <73298989+near-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
ailisp and near-bulldozer[bot] authored Aug 25, 2022
1 parent 6b7624c commit 2e6bfad
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
29 changes: 29 additions & 0 deletions cli/builder/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
// #######################
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions lib/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 18 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 7 additions & 1 deletion tests/__tests__/test_promise_api.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand All @@ -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'));
});
});
4 changes: 4 additions & 0 deletions tests/src/promise_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))})))
}
Expand Down
6 changes: 6 additions & 0 deletions tests/src/promise_batch_api.js

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

0 comments on commit 2e6bfad

Please sign in to comment.