Skip to content

Commit

Permalink
update cross canister call example
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 15, 2023
1 parent d9d5e7c commit 6a699d9
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
# "examples/run_time_errors",
# "examples/rust_type_conversions",
# "examples/complex_types",
# "examples/composite_queries",
# "examples/func_types",
# "examples/generics",
# "examples/guard_functions",
Expand Down Expand Up @@ -148,6 +147,7 @@ jobs:
"examples/candid_encoding",
"examples/counter",
"examples/complex_init",
"examples/composite_queries",
"examples/cross_canister_calls",
"examples/cycles",
"examples/ethereum_json_rpc",
Expand Down
25 changes: 11 additions & 14 deletions examples/composite_queries/canisters/canister1/canister1.did
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
type ManualReply = variant { Ok : text; Err : text };
type ManualReply_1 = variant { Ok : nat; Err : text };
service : () -> {
deepQuery : () -> (ManualReply) query;
incCanister1 : () -> (ManualReply_1) query;
incCanister2 : () -> (ManualReply_1) query;
incCounter : () -> (nat) query;
manualQuery : () -> (ManualReply) query;
simpleCompositeQuery : () -> (ManualReply) query;
simpleQuery : () -> (ManualReply) query;
simpleUpdate : () -> (ManualReply);
totallyManualQuery : () -> (ManualReply) query;
updateQuery : () -> (ManualReply) query;
}
service: () -> {
simpleCompositeQuery: () -> (text) query;
manualQuery: () -> (text) query;
totallyManualQuery: () -> (text) query;
deepQuery: () -> (text) query;
updateQuery: () -> (text) query;
simpleQuery: () -> (text) query;
simpleUpdate: () -> (text);
incCounter: () -> (nat) query;
incCanister2: () -> (nat) query;
}
220 changes: 83 additions & 137 deletions examples/composite_queries/canisters/canister1/canister1.ts
Original file line number Diff line number Diff line change
@@ -1,141 +1,87 @@
import {
ic,
Manual,
match,
nat,
Principal,
$query,
Result,
$update
} from 'azle';
import { Canister1 } from '../canister1/types';
import { Canister2 } from '../canister2/types';

const canister1 = new Canister1(
Principal.fromText(
process.env.CANISTER1_PRINCIPAL ??
ic.trap('process.env.CANISTER1_PRINCIPAL is undefined')
)
);

const canister2 = new Canister2(
Principal.fromText(
process.env.CANISTER2_PRINCIPAL ??
ic.trap('process.env.CANISTER2_PRINCIPAL is undefined')
)
);

let counter: nat = 0n;
import { ic, Manual, nat, Principal, query, Service, text, update } from 'azle';
import Canister2 from '../canister2/canister2';

// Composite query calling a query
$query;
export async function simpleCompositeQuery(): Promise<Result<string, string>> {
return await canister2.simpleQuery().call();
class Canister1 extends Service {
// canister1 = new Canister1(
// Principal.fromText(
// process.env.CANISTER1_PRINCIPAL ??
// ic.trap('process.env.CANISTER1_PRINCIPAL is undefined')
// )
// );

canister2 = new Canister2(
Principal.fromText(
process.env.CANISTER2_PRINCIPAL ??
ic.trap('process.env.CANISTER2_PRINCIPAL is undefined')
)
);

counter: nat = 0n;

@query([], text)
async simpleCompositeQuery(): Promise<text> {
return await ic.call(this.canister2.simpleQuery);
}

// Composite query calling a manual query
@query([], text)
async manualQuery(): Promise<text> {
return await ic.call(this.canister2.manualQuery);
}

// Manual composite query calling a manual query
@query([], text)
async totallyManualQuery(): Promise<Manual<text>> {
ic.reply(await ic.call(this.canister2.manualQuery), text);
}

// Composite query calling another composite query
@query([], text)
async deepQuery(): Promise<text> {
return await ic.call(this.canister2.deepQuery);
}

// Composite query calling an update method. SHOULDN'T WORK
@query([], text)
async updateQuery(): Promise<text> {
return await ic.call(this.canister2.updateQuery);
}

// Composite query being called by a query method. SHOULDN'T WORK
@query([], text)
async simpleQuery(): Promise<text> {
return await ic.call(this.canister2.simpleQuery);
}

// Composite query being called by an update method. SHOULDN'T WORK
@update([], text)
async simpleUpdate(): Promise<text> {
return await ic.call(this.canister2.deepQuery);
}

// Composite query that modifies the state. Should revert after the call is done
@query([], nat)
async incCounter(): Promise<nat> {
this.counter += 1n;
return this.counter;
}

// Composite query calling queries on the same canister. SHOULDN'T WORK
// @query([], nat)
// async incCanister1(): Promise<nat> {
// this.counter += 1n;

// return await ic.call(this.canister1.incCounter);
// }

// Composite query calling queries that modify the state
@query([], nat)
async incCanister2(): Promise<nat> {
this.counter += 1n;

return await ic.call(this.canister2.incCounter);
}
}

// Composite query calling a manual query
$query;
export async function manualQuery(): Promise<Result<string, string>> {
return await canister2.manualQuery().call();
}

// Manual composite query calling a manual query
$query;
export async function totallyManualQuery(): Promise<
Manual<Result<string, string>>
> {
ic.reply(await canister2.manualQuery().call());
}

// Composite query calling another composite query
$query;
export async function deepQuery(): Promise<Result<string, string>> {
const callResult = await canister2.deepQuery().call();

return match(callResult, {
Ok: (stringQueryResult) =>
match(stringQueryResult, {
Ok: (stringQuery) => ({ Ok: stringQuery }),
Err: (err) => ({ Err: err })
}),
Err: (err) => ({ Err: err })
});
}

// Composite query calling an update method. SHOULDN'T WORK
$query;
export async function updateQuery(): Promise<Result<string, string>> {
return await canister2.updateQuery().call();
}

// Composite query being called by a query method. SHOULDN'T WORK
$query;
export async function simpleQuery(): Promise<Result<string, string>> {
return await canister2.simpleQuery().call();
}

// Composite query being called by an update method. SHOULDN'T WORK
$update;
export async function simpleUpdate(): Promise<Result<string, string>> {
const callResult = await canister2.deepQuery().call();

return match(callResult, {
Ok: (stringQueryResult) =>
match(stringQueryResult, {
Ok: (stringQuery) => ({ Ok: stringQuery }),
Err: (err) => ({ Err: err })
}),
Err: (err) => ({ Err: err })
});
}

// Composite query that modifies the state. Should revert after the call is done
$query;
export async function incCounter(): Promise<nat> {
counter += 1n;
return counter;
}

// Composite query calling queries on the same canister. SHOULDN'T WORK
$query;
export async function incCanister1(): Promise<Result<nat, string>> {
counter += 1n;

const canister1AResult = await canister1.incCounter().call();

return match(canister1AResult, {
Ok: async (canister1AOk) => {
const canister1BResult = await canister1.incCounter().call();

return match(canister1BResult, {
Ok: (canister1BOk) => ({
Ok: counter + canister1AOk + canister1BOk
}),
Err: (err) => ({ Err: err })
});
},
Err: (err) => ({ Err: err })
});
}

// Composite query calling queries that modify the state
$query;
export async function incCanister2(): Promise<Result<nat, string>> {
counter += 1n;

const canister2AResult = await canister2.incCounter().call();

return match(canister2AResult, {
Ok: async (canister2AOk) => {
const canister2BResult = await canister2.incCounter().call();

return match(canister2BResult, {
Ok: (canister2BOk) => ({
Ok: counter + canister2AOk + canister2BOk
}),
Err: (err) => ({ Err: err })
});
},
Err: (err) => ({ Err: err })
});
}
export default Canister1;
6 changes: 0 additions & 6 deletions examples/composite_queries/canisters/canister1/types.ts

This file was deleted.

15 changes: 7 additions & 8 deletions examples/composite_queries/canisters/canister2/canister2.did
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
type ManualReply = variant { Ok : text; Err : text };
service : () -> {
deepQuery : () -> (ManualReply) query;
incCounter : () -> (nat) query;
manualQuery : () -> (text) query;
simpleQuery : () -> (text) query;
updateQuery : () -> (text);
}
service: () -> {
incCounter: () -> (nat) query;
simpleQuery: () -> (text) query;
updateQuery: () -> (text);
manualQuery: () -> (text) query;
deepQuery: () -> (text) query;
}
62 changes: 32 additions & 30 deletions examples/composite_queries/canisters/canister2/canister2.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
import { ic, Manual, nat, Principal, $query, Result, $update } from 'azle';
import { Canister3 } from '../canister3/types';
import { ic, Manual, nat, Principal, query, Service, text, update } from 'azle';
import Canister3 from '../canister3/canister3';

const canister3 = new Canister3(
Principal.fromText(
process.env.CANISTER3_PRINCIPAL ??
ic.trap('process.env.CANISTER3_PRINCIPAL is undefined')
)
);
export default class extends Service {
canister3 = new Canister3(
Principal.fromText(
process.env.CANISTER3_PRINCIPAL ??
ic.trap('process.env.CANISTER3_PRINCIPAL is undefined')
)
);

let counter: nat = 0n;
counter: nat = 0n;

// TODO is this supposed to be a query?
$query;
export async function incCounter(): Promise<nat> {
counter += 1n;
return counter;
}
// TODO is this supposed to be a query?
@query([], nat)
async incCounter(): Promise<nat> {
this.counter += 1n;
return this.counter;
}

$query;
export function simpleQuery(): string {
return 'Hello from Canister 2';
}
@query([], text)
simpleQuery(): text {
return 'Hello from Canister 2';
}

$update;
export function updateQuery(): string {
return 'Hello from a Canister 2 update';
}
@update([], text)
updateQuery(): text {
return 'Hello from a Canister 2 update';
}

$query;
export function manualQuery(): Manual<string> {
ic.reply('Hello from Canister 2 manual query');
}
@query([], text)
manualQuery(): Manual<text> {
ic.reply('Hello from Canister 2 manual query', text);
}

$query;
export async function deepQuery(): Promise<Result<string, string>> {
return await canister3.deepQuery().call();
@query([], text)
async deepQuery(): Promise<text> {
return await ic.call(this.canister3.deepQuery);
}
}
25 changes: 0 additions & 25 deletions examples/composite_queries/canisters/canister2/types.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/composite_queries/canisters/canister3/canister3.did
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
service : () -> { deepQuery : () -> (text) query }
service: () -> {
deepQuery: () -> (text) query;
}
Loading

0 comments on commit 6a699d9

Please sign in to comment.