Skip to content

Commit

Permalink
add cycles methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jan 17, 2024
1 parent b94b194 commit 889e588
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 162 deletions.
1 change: 1 addition & 0 deletions src/compiler/generate_candid_and_canister_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function generateCandidAndCanisterMethods(wasmFilePath: string): {
msg_cycles_accept: () => {},
msg_cycles_accept128: () => {},
msg_cycles_available: () => {},
msg_cycles_available128: () => {},
msg_cycles_refunded: () => {},
msg_cycles_refunded128: () => {},
msg_method_name_copy: () => {},
Expand Down
95 changes: 47 additions & 48 deletions src/compiler/rust/canister/src/ic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ mod id;
mod instruction_counter;
mod is_controller;
mod method_name;
// mod msg_cycles_accept;
// mod msg_cycles_accept128;
// mod msg_cycles_available;
// mod msg_cycles_available128;
// mod msg_cycles_refunded;
// mod msg_cycles_refunded128;
mod msg_cycles_accept;
mod msg_cycles_accept128;
mod msg_cycles_available;
mod msg_cycles_available128;
mod msg_cycles_refunded;
mod msg_cycles_refunded128;
mod notify_raw;
mod performance_counter;
mod print;
Expand Down Expand Up @@ -169,48 +169,47 @@ pub fn register(context: &mut wasmedge_quickjs::Context) {
.into(),
);

// ic.set_property(
// "msgCyclesAccept",
// context
// .wrap_callback2(msg_cycles_accept::native_function)
// .unwrap(),
// )
// .unwrap();
// ic.set_property(
// "msgCyclesAccept128",
// context
// .wrap_callback2(msg_cycles_accept128::native_function)
// .unwrap(),
// )
// .unwrap();
// ic.set_property(
// "msgCyclesAvailable",
// context
// .wrap_callback2(msg_cycles_available::native_function)
// .unwrap(),
// )
// .unwrap();
// ic.set_property(
// "msgCyclesAvailable128",
// context
// .wrap_callback2(msg_cycles_available128::native_function)
// .unwrap(),
// )
// .unwrap();
// ic.set_property(
// "msgCyclesRefunded",
// context
// .wrap_callback2(msg_cycles_refunded::native_function)
// .unwrap(),
// )
// .unwrap();
// ic.set_property(
// "msgCyclesRefunded128",
// context
// .wrap_callback2(msg_cycles_refunded128::native_function)
// .unwrap(),
// )
// .unwrap();
ic.set(
"msgCyclesAccept",
context
.new_function::<msg_cycles_accept::NativeFunction>("")
.into(),
);

ic.set(
"msgCyclesAccept128",
context
.new_function::<msg_cycles_accept128::NativeFunction>("")
.into(),
);

ic.set(
"msgCyclesAvailable",
context
.new_function::<msg_cycles_available::NativeFunction>("")
.into(),
);

ic.set(
"msgCyclesAvailable128",
context
.new_function::<msg_cycles_available128::NativeFunction>("")
.into(),
);

ic.set(
"msgCyclesRefunded",
context
.new_function::<msg_cycles_refunded::NativeFunction>("")
.into(),
);

ic.set(
"msgCyclesRefunded128",
context
.new_function::<msg_cycles_refunded128::NativeFunction>("")
.into(),
);

ic.set(
"notifyRaw",
Expand Down
32 changes: 14 additions & 18 deletions src/compiler/rust/canister/src/ic/msg_cycles_accept.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use std::convert::TryInto;
use wasmedge_quickjs::{Context, JsFn, JsValue};

use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef};
pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
let max_amount_string = if let JsValue::String(js_string) = argv.get(0).unwrap() {
js_string.to_string()
} else {
panic!("conversion from JsValue to JsString failed")
};
let max_amount: u64 = max_amount_string.parse().unwrap();

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let max_amount_vec_u8: Vec<u8> = args
.get(0)
.expect("msgCyclesAccept must have one argument")
.to_js_value()?
.try_into()?;

let max_amount: u64 = candid::decode_one(&max_amount_vec_u8)?;

let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::msg_cycles_accept(max_amount))?.into();
to_qjs_value(&context, &return_js_value)
context
.new_string(&ic_cdk::api::call::msg_cycles_accept(max_amount).to_string())
.into()
}
}
32 changes: 14 additions & 18 deletions src/compiler/rust/canister/src/ic/msg_cycles_accept128.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use std::convert::TryInto;
use wasmedge_quickjs::{Context, JsFn, JsValue};

use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef};
pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
let max_amount_string = if let JsValue::String(js_string) = argv.get(0).unwrap() {
js_string.to_string()
} else {
panic!("conversion from JsValue to JsString failed")
};
let max_amount: u128 = max_amount_string.parse().unwrap();

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let max_amount_vec_u8: Vec<u8> = args
.get(0)
.expect("msgCyclesAccept128 must have one argument")
.to_js_value()?
.try_into()?;

let max_amount: u128 = candid::decode_one(&max_amount_vec_u8)?;

let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::msg_cycles_accept128(max_amount))?.into();
to_qjs_value(&context, &return_js_value)
context
.new_string(&ic_cdk::api::call::msg_cycles_accept128(max_amount).to_string())
.into()
}
}
17 changes: 8 additions & 9 deletions src/compiler/rust/canister/src/ic/msg_cycles_available.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef};
use wasmedge_quickjs::{Context, JsFn, JsValue};

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::msg_cycles_available())?.into();
to_qjs_value(&context, &return_js_value)
pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
context
.new_string(&ic_cdk::api::call::msg_cycles_available().to_string())
.into()
}
}
17 changes: 8 additions & 9 deletions src/compiler/rust/canister/src/ic/msg_cycles_available128.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef};
use wasmedge_quickjs::{Context, JsFn, JsValue};

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::msg_cycles_available128())?.into();
to_qjs_value(&context, &return_js_value)
pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
context
.new_string(&ic_cdk::api::call::msg_cycles_available128().to_string())
.into()
}
}
17 changes: 8 additions & 9 deletions src/compiler/rust/canister/src/ic/msg_cycles_refunded.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef};
use wasmedge_quickjs::{Context, JsFn, JsValue};

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::msg_cycles_refunded())?.into();
to_qjs_value(&context, &return_js_value)
pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
context
.new_string(&ic_cdk::api::call::msg_cycles_refunded().to_string())
.into()
}
}
17 changes: 8 additions & 9 deletions src/compiler/rust/canister/src/ic/msg_cycles_refunded128.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use quickjs_wasm_rs::{to_qjs_value, CallbackArg, JSContextRef, JSValue, JSValueRef};
use wasmedge_quickjs::{Context, JsFn, JsValue};

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::msg_cycles_refunded128())?.into();
to_qjs_value(&context, &return_js_value)
pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
context
.new_string(&ic_cdk::api::call::msg_cycles_refunded128().to_string())
.into()
}
}
18 changes: 10 additions & 8 deletions src/compiler/rust/canister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ fn execute_js(function_name: &str, pass_arg_data: bool) {
let method_callback_function = method_callback.to_function().unwrap();

// TODO this returns a value so I think we need to check it to get an error
method_callback_function.call(&[candid_args_js_value]);
let result = method_callback_function.call(&[candid_args_js_value]);

match &result {
wasmedge_quickjs::JsValue::Exception(js_exception) => {
// TODO maybe we can just call toString() on the error object
js_exception.dump_error();
panic!("TODO needs error info");
}
_ => {}
};

// TODO might we need to do this in init and post_upgrade?
context.event_loop().unwrap().run_tick_task();

// TODO I am not sure what the first parameter to call is supposed to be
// method_callback
// .call(&method_callback, &[candid_args_js_value_ref])
// .unwrap();

// context.execute_pending().unwrap();
});
});
}
Expand Down
11 changes: 4 additions & 7 deletions src/lib/ic/msg_cycles_accept.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { decode } from '../candid/serde/decode';
import { encode } from '../candid/serde/encode';

/**
* Moves cycles from the call to the canister balance
Expand All @@ -12,10 +10,9 @@ export function msgCyclesAccept(maxAmount: nat64): nat64 {
return undefined as any;
}

const maxAmountCandidBytes = encode(nat64, maxAmount).buffer;
const msgCyclesAcceptAmountMovedString = globalThis._azleIc.msgCyclesAccept(
maxAmount.toString()
);

const msgCyclesAcceptCandidBytes =
globalThis._azleIc.msgCyclesAccept(maxAmountCandidBytes);

return decode(nat64, msgCyclesAcceptCandidBytes);
return BigInt(msgCyclesAcceptAmountMovedString);
}
10 changes: 3 additions & 7 deletions src/lib/ic/msg_cycles_accept_128.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { nat } from '../candid/types/primitive/nats/nat';
import { decode } from '../candid/serde/decode';
import { encode } from '../candid/serde/encode';

/**
* Moves cycles from the call to the canister balance
Expand All @@ -12,10 +10,8 @@ export function msgCyclesAccept128(maxAmount: nat): nat {
return undefined as any;
}

const maxAmountCandidBytes = encode(nat, maxAmount).buffer;
const msgCyclesAccept128AmountMovedString =
globalThis._azleIc.msgCyclesAccept128(maxAmount.toString());

const msgCyclesAccept128CandidBytes =
globalThis._azleIc.msgCyclesAccept128(maxAmountCandidBytes);

return decode(nat, msgCyclesAccept128CandidBytes);
return BigInt(msgCyclesAccept128AmountMovedString);
}
5 changes: 2 additions & 3 deletions src/lib/ic/msg_cycles_available.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { decode } from '../candid/serde/decode';

/**
* Returns the amount of cycles that were transferred by the caller of the
Expand All @@ -11,8 +10,8 @@ export function msgCyclesAvailable(): nat64 {
return undefined as any;
}

const msgCyclesAvailableCandidBytes =
const msgCyclesAvailableAmountString =
globalThis._azleIc.msgCyclesAvailable();

return decode(nat64, msgCyclesAvailableCandidBytes);
return BigInt(msgCyclesAvailableAmountString);
}
5 changes: 2 additions & 3 deletions src/lib/ic/msg_cycles_available_128.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { nat } from '../candid/types/primitive/nats/nat';
import { decode } from '../candid/serde/decode';

/**
* Returns the amount of cycles that were transferred by the caller of the
Expand All @@ -11,8 +10,8 @@ export function msgCyclesAvailable128(): nat {
return undefined as any;
}

const msgCyclesAvailable128CandidBytes =
const msgCyclesAvailable128AmountString =
globalThis._azleIc.msgCyclesAvailable128();

return decode(nat, msgCyclesAvailable128CandidBytes);
return BigInt(msgCyclesAvailable128AmountString);
}
6 changes: 3 additions & 3 deletions src/lib/ic/msg_cycles_refunded.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { decode } from '../candid/serde/decode';

/**
* Returns the amount of cycles that came back with the response as a refund.
Expand All @@ -11,7 +10,8 @@ export function msgCyclesRefunded(): nat64 {
return undefined as any;
}

const msgCyclesRefundedCandidBytes = globalThis._azleIc.msgCyclesRefunded();
const msgCyclesRefundedAmountString =
globalThis._azleIc.msgCyclesRefunded();

return decode(nat64, msgCyclesRefundedCandidBytes);
return BigInt(msgCyclesRefundedAmountString);
}
Loading

0 comments on commit 889e588

Please sign in to comment.