Skip to content

Commit

Permalink
composite_queries exampled implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 24, 2023
1 parent 75f1e2e commit ca966d8
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 500 deletions.
141 changes: 72 additions & 69 deletions examples/composite_queries/canisters/canister1/index.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,92 @@
import { ic, Manual, nat, Principal, query, Service, text, update } from 'azle';
import {
ic,
init,
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')
)
);
let canister2: typeof Canister2;
let counter: nat = 0n;

counter: nat = 0n;
const incCounter = query([], nat, async () => {
counter += 1n;

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

const service = Service({
init: init([], () => {
canister2 = Canister2(
Principal.fromText(
process.env.CANISTER2_PRINCIPAL ??
ic.trap('process.env.CANISTER2_PRINCIPAL is undefined')
)
);
}),
// Composite query calling a query
simpleCompositeQuery: query([], text, async () => {
return await ic.call(canister2.simpleQuery);
}),
// Composite query calling a manual query
@query([], text)
async manualQuery(): Promise<text> {
return await ic.call(this.canister2.manualQuery);
}

manualQuery: query([], text, async () => {
return (await ic.call(canister2.manualQuery)) as unknown as string; // TODO is this the best we can do for the types in this situation?
}),
// 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);
}

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

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

updateQuery: query([], text, async () => {
return await ic.call(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);
}

simpleQuery: query([], text, async () => {
return await ic.call(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);
}

simpleUpdate: update([], text, async () => {
return await ic.call(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;
}

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

const canister1AResult = await ic.call(this.incCounter);
incCanister1: query([], nat, async () => {
// TODO This is not an ideal solution but will work for now
const self = Service({
incCounter
})(ic.id());

const canister1BResult = await ic.call(this.incCounter);
counter += 1n;

return this.counter + canister1AResult + canister1BResult;
}
const canister1AResult = await ic.call(self.incCounter);
const canister1BResult = await ic.call(self.incCounter);

return 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);
incCanister2: query([], nat, async () => {
counter += 1n;

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

return this.counter + canister2AResult + canister2BResult;
}
}
return counter + canister2AResult + canister2BResult;
})
});

export default Canister1;
export default service;
76 changes: 42 additions & 34 deletions examples/composite_queries/canisters/canister2/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import { ic, Manual, nat, Principal, query, Service, text, update } from 'azle';
import {
ic,
init,
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;
let canister3: typeof Canister3;
let counter: nat = 0n;

export default Service({
init: init([], () => {
canister3 = Canister3(
Principal.fromText(
process.env.CANISTER3_PRINCIPAL ??
ic.trap('process.env.CANISTER3_PRINCIPAL is undefined')
)
);
}),
// TODO is this supposed to be a query?
@query([], nat)
async incCounter(): Promise<nat> {
this.counter += 1n;
return this.counter;
}

@query([], text)
simpleQuery(): text {
incCounter: query([], nat, () => {
counter += 1n;
return counter;
}),
simpleQuery: query([], text, () => {
return 'Hello from Canister 2';
}

@update([], text)
updateQuery(): text {
}),
updateQuery: update([], 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);
}
}
}),
manualQuery: query(
[],
Manual(text),
() => {
ic.reply('Hello from Canister 2 manual query', text);
},
{ manual: true }
),
deepQuery: query([], text, async () => {
return await ic.call(canister3.deepQuery);
})
});
11 changes: 5 additions & 6 deletions examples/composite_queries/canisters/canister3/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Service, query, text } from 'azle';
import { query, Service, text } from 'azle';

export default class extends Service {
@query([], text)
deepQuery(): text {
export default Service({
deepQuery: query([], text, () => {
return 'Hello from Canister 3';
}
}
})
});
9 changes: 6 additions & 3 deletions examples/composite_queries/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "canisters/canister1",
"ts": "canisters/canister1/index.ts",
"candid": "canisters/canister1/index.did",
"wasm": ".azle/canister1/canister1.wasm.gz",
"wasm": ".azle/canister1/canister1.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister1",
"node_compatibility": true
Expand All @@ -23,7 +24,8 @@
"root": "canisters/canister2",
"ts": "canisters/canister2/index.ts",
"candid": "canisters/canister2/index.did",
"wasm": ".azle/canister2/canister2.wasm.gz",
"wasm": ".azle/canister2/canister2.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister2",
"node_compatibility": true
Expand All @@ -36,7 +38,8 @@
"root": "canisters/canister3",
"ts": "canisters/canister3/index.ts",
"candid": "canisters/canister3/index.did",
"wasm": ".azle/canister3/canister3.wasm.gz",
"wasm": ".azle/canister3/canister3.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/canister3",
"node_compatibility": true
Expand Down
Loading

0 comments on commit ca966d8

Please sign in to comment.