Skip to content

Commit

Permalink
Merge pull request #1227 from demergent-labs/manual_examples
Browse files Browse the repository at this point in the history
Manual examples
  • Loading branch information
lastmjs authored Sep 15, 2023
2 parents ad486ed + 2177d4f commit 264c512
Show file tree
Hide file tree
Showing 25 changed files with 337 additions and 374 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,10 @@
# These are the ones that don't work
# "examples/tuple_types",
# "examples/service",
# "examples/outgoing_http_requests",
# "examples/robust_imports",
# "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 @@ -149,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 Expand Up @@ -180,6 +179,7 @@ jobs:
"examples/null_example",
"examples/optional_types",
"examples/pre_and_post_upgrade",
"examples/outgoing_http_requests",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
14 changes: 0 additions & 14 deletions examples/composite_queries/canisters/canister1/canister1.did

This file was deleted.

141 changes: 0 additions & 141 deletions examples/composite_queries/canisters/canister1/canister1.ts

This file was deleted.

12 changes: 12 additions & 0 deletions examples/composite_queries/canisters/canister1/index.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
service: () -> {
simpleCompositeQuery: () -> (text) query;
manualQuery: () -> (text) query;
totallyManualQuery: () -> (text) query;
deepQuery: () -> (text) query;
updateQuery: () -> (text) query;
simpleQuery: () -> (text) query;
simpleUpdate: () -> (text);
incCounter: () -> (nat) query;
incCanister1: () -> (nat) query;
incCanister2: () -> (nat) query;
}
89 changes: 89 additions & 0 deletions examples/composite_queries/canisters/canister1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ic, Manual, nat, Principal, query, Service, text, update } from 'azle';
import Canister2 from '../canister2';

class Canister1 extends Service {
canister2 = new Canister2(
Principal.fromText(
process.env.CANISTER2_PRINCIPAL ??
ic.trap('process.env.CANISTER2_PRINCIPAL is undefined')
)
);

counter: nat = 0n;

// Composite query calling a query
@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, { manual: true })
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
@query([], nat)
async incCanister1(): Promise<nat> {
this.counter += 1n;

const canister1AResult = await ic.call(this.incCounter);

const canister1BResult = await ic.call(this.incCounter);

return this.counter + canister1AResult + canister1BResult;
}

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

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

const canister2BResult = await ic.call(this.canister2.incCounter);

return this.counter + canister2AResult + canister2BResult;
}
}

export default Canister1;
6 changes: 0 additions & 6 deletions examples/composite_queries/canisters/canister1/types.ts

This file was deleted.

8 changes: 0 additions & 8 deletions examples/composite_queries/canisters/canister2/canister2.did

This file was deleted.

38 changes: 0 additions & 38 deletions examples/composite_queries/canisters/canister2/canister2.ts

This file was deleted.

7 changes: 7 additions & 0 deletions examples/composite_queries/canisters/canister2/index.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service: () -> {
incCounter: () -> (nat) query;
simpleQuery: () -> (text) query;
updateQuery: () -> (text);
manualQuery: () -> (text) query;
deepQuery: () -> (text) query;
}
40 changes: 40 additions & 0 deletions examples/composite_queries/canisters/canister2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ic, Manual, nat, Principal, query, Service, text, update } from 'azle';
import Canister3 from '../canister3';

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

counter: nat = 0n;

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

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

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

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

@query([], text)
async deepQuery(): Promise<text> {
return await ic.call(this.canister3.deepQuery);
}
}
Loading

0 comments on commit 264c512

Please sign in to comment.