Skip to content

Commit

Permalink
Merge pull request #1261 from demergent-labs/functional_syntax_cycles
Browse files Browse the repository at this point in the history
Functional syntax cycles
  • Loading branch information
lastmjs authored Sep 25, 2023
2 parents 3e46a2e + ffbf1e8 commit 0ad9072
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

# All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved
# "examples/complex_types",
# "examples/cycles",
# "examples/date",
# "examples/ethereum_json_rpc",
# "examples/func_types",
Expand Down Expand Up @@ -114,6 +113,7 @@ jobs:
"examples/composite_queries",
"examples/counter",
"examples/cross_canister_calls",
"examples/cycles",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
6 changes: 0 additions & 6 deletions examples/cycles/canisters/cycles/cycles.did

This file was deleted.

27 changes: 10 additions & 17 deletions examples/cycles/canisters/cycles/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { ic, nat, nat64, query, Service, update } from 'azle';

export default class extends Service {
@update([], nat64)
receiveCycles(): nat64 {
export default Service({
receiveCycles: update([], nat64, () => {
return ic.msgCyclesAccept(ic.msgCyclesAvailable() / 2n);
}

}),
// Moves all transferred cycles to the canister
@update([], nat)
receiveCycles128(): nat {
receiveCycles128: update([], nat, () => {
return ic.msgCyclesAccept128(ic.msgCyclesAvailable128() / 2n);
}

@query([], nat64)
getCanisterBalance(): nat64 {
}),
getCanisterBalance: query([], nat64, () => {
return ic.canisterBalance();
}

@query([], nat)
getCanisterBalance128(): nat {
}),
getCanisterBalance128: query([], nat, () => {
return ic.canisterBalance128();
}
}
})
});
76 changes: 39 additions & 37 deletions examples/cycles/canisters/intermediary/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
import { ic, nat, nat64, Principal, query, Service, update, Void } from 'azle';
import {
ic,
init,
nat,
nat64,
Principal,
query,
Service,
update,
Void
} from 'azle';
import Cycles from '../cycles';

export default class extends Service {
cyclesCanister = new Cycles(
Principal.fromText(
process.env.CYCLES_PRINCIPAL ??
ic.trap('process.env.CYCLES_PRINCIPAL is undefined')
)
);

let cyclesCanister: typeof Cycles;

export default Service({
init: init([], () => {
cyclesCanister = Cycles(
Principal.fromText(
process.env.CYCLES_PRINCIPAL ??
ic.trap('process.env.CYCLES_PRINCIPAL is undefined')
)
);
}),
// Reports the number of cycles returned from the Cycles canister
@update([], nat64)
async sendCycles(): Promise<nat64> {
return await ic.call(this.cyclesCanister.receiveCycles, {
sendCycles: update([], nat64, async () => {
return await ic.call(cyclesCanister.receiveCycles, {
cycles: 1_000_000n
});
}

@update([], Void)
sendCyclesNotify(): Void {
return ic.notify(this.cyclesCanister.receiveCycles, {
}),
sendCyclesNotify: update([], Void, () => {
return ic.notify(cyclesCanister.receiveCycles, {
cycles: 1_000_000n
});
}

}),
// Reports the number of cycles returned from the Cycles canister
@update([], nat)
async sendCycles128(): Promise<nat> {
await ic.call(this.cyclesCanister.receiveCycles128, {
sendCycles128: update([], nat, async () => {
await ic.call(cyclesCanister.receiveCycles128, {
cycles: 1_000_000n
});

return ic.msgCyclesRefunded128();
}

@update([], Void)
sendCycles128Notify(): Void {
return ic.notify(this.cyclesCanister.receiveCycles128, {
}),
sendCycles128Notify: update([], Void, () => {
return ic.notify(cyclesCanister.receiveCycles128, {
cycles: 1_000_000n
});
}

@query([], nat64)
getCanisterBalance(): nat64 {
}),
getCanisterBalance: query([], nat64, () => {
return ic.canisterBalance();
}

@query([], nat)
getCanisterBalance128(): nat {
}),
getCanisterBalance128: query([], nat, () => {
return ic.canisterBalance128();
}
}
})
});
20 changes: 0 additions & 20 deletions examples/cycles/canisters/intermediary/intermediary.did

This file was deleted.

6 changes: 4 additions & 2 deletions examples/cycles/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "canisters/cycles",
"ts": "canisters/cycles/index.ts",
"candid": "canisters/cycles/index.did",
"wasm": ".azle/cycles/cycles.wasm.gz",
"wasm": ".azle/cycles/cycles.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/cycles",
"node_compatibility": true
Expand All @@ -18,7 +19,8 @@
"root": "canisters/intermediary",
"ts": "canisters/intermediary/index.ts",
"candid": "canisters/intermediary/index.did",
"wasm": ".azle/intermediary/intermediary.wasm.gz",
"wasm": ".azle/intermediary/intermediary.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/intermediary",
"node_compatibility": true
Expand Down
Loading

0 comments on commit 0ad9072

Please sign in to comment.