Skip to content

Commit

Permalink
Merge pull request #2084 from demergent-labs/burn
Browse files Browse the repository at this point in the history
Burn
  • Loading branch information
lastmjs authored Sep 11, 2024
2 parents 7cbbf07 + 5173fdb commit 4f7a5d3
Show file tree
Hide file tree
Showing 19 changed files with 6,503 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ jobs:
"tests/end_to_end/http_server/web_assembly",
"tests/property/ic_api/caller",
"tests/property/ic_api/chunk",
"tests/property/ic_api/cycles_burn",
"tests/property/ic_api/id",
"tests/property/ic_api/instruction_counter",
"tests/property/ic_api/is_controller",
Expand Down
Binary file modified canister_templates/experimental.wasm
Binary file not shown.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
17 changes: 17 additions & 0 deletions src/build/rust/canister/src/ic/cycles_burn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use wasmedge_quickjs::{Context, JsFn, JsValue};

pub struct NativeFunction;
impl JsFn for NativeFunction {
fn call(context: &mut Context, _this_val: JsValue, argv: &[JsValue]) -> JsValue {
let 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 amount: u128 = amount_string.parse().unwrap();

context
.new_string(&ic_cdk::api::cycles_burn(amount).to_string())
.into()
}
}
8 changes: 8 additions & 0 deletions src/build/rust/canister/src/ic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod candid_encode;
mod canister_balance;
mod canister_version;
mod clear_timer;
mod cycles_burn;
mod data_certificate;
mod id;
mod instruction_counter;
Expand Down Expand Up @@ -111,6 +112,13 @@ pub fn register(context: &mut wasmedge_quickjs::Context) {
.into(),
);

ic.set(
"cyclesBurn",
context
.new_function::<cycles_burn::NativeFunction>("")
.into(),
);

ic.set(
"dataCertificate",
context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function execute(
canister_self_copy: (): void => {},
canister_self_size: (): void => {},
canister_version: (): void => {},
cycles_burn128: (): void => {},
certified_data_set: (): void => {},
data_certificate_copy: (): void => {},
data_certificate_present: (): void => {},
Expand Down
1 change: 1 addition & 0 deletions src/lib/stable/ic_apis/azle_ic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type AzleIc = {
canisterBalance: () => string;
canisterVersion: () => string;
clearTimer: (timerIdString: string) => void;
cyclesBurn: (amount: string) => string;
dataCertificate: () => ArrayBuffer | undefined;
id: () => string;
instructionCounter: () => string;
Expand Down
11 changes: 11 additions & 0 deletions src/lib/stable/ic_apis/cycles_burn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Burns cycles from the canister
* @returns the amount of cycles that were actually burned
*/
export function cyclesBurn(amount: bigint): bigint {
if (globalThis._azleIc === undefined) {
return 0n;
}

return BigInt(globalThis._azleIc.cyclesBurn(amount.toString()));
}
1 change: 1 addition & 0 deletions src/lib/stable/ic_apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { canisterBalance } from './canister_balance';
export { canisterVersion } from './canister_version';
export { chunk } from './chunk';
export { clearTimer } from './clear_timer';
export { cyclesBurn } from './cycles_burn';
export { dataCertificate } from './data_certificate';
export { id } from './id';
export { instructionCounter } from './instruction_counter';
Expand Down
4 changes: 4 additions & 0 deletions tests/property/ic_api/cycles_burn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
12 changes: 12 additions & 0 deletions tests/property/ic_api/cycles_burn/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"canisters": {
"canister": {
"type": "azle",
"main": "src/index.ts",
"declarations": {
"output": "test/dfx_generated/canister",
"node_compatibility": true
}
}
}
}
12 changes: 12 additions & 0 deletions tests/property/ic_api/cycles_burn/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
bail: true,
testTimeout: 100_000_000,
transform: {
'^.+\\.ts$': ['ts-jest', { isolatedModules: true }],
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading

0 comments on commit 4f7a5d3

Please sign in to comment.