Skip to content

Commit

Permalink
update cross canister calls and async_await, add canister example fet…
Browse files Browse the repository at this point in the history
…ch tests
  • Loading branch information
lastmjs committed Jan 30, 2024
1 parent 6049ed3 commit fa9cc9d
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 491 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ jobs:
with:
node-version: 18
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
run: DFX_VERSION=0.15.3-largewasm.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
run: DFX_VERSION=0.17.0-beta.0 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
uses: actions/cache@v3
with:
Expand Down
5 changes: 3 additions & 2 deletions examples/async_await/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "ts-node --transpile-only --ignore=false test/test.ts"
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"tests": "npm run pre_tests && ts-node --transpile-only --ignore=false test/test.ts",
"test": "AZLE_TEST_FETCH=false npm run tests && AZLE_TEST_FETCH=true npm run tests"
},
"dependencies": {
"azle": "0.19.0"
Expand Down
3 changes: 2 additions & 1 deletion examples/canister/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"output": "test/dfx_generated/canister",
"node_compatibility": true
},
"env": ["SOME_CANISTER_PRINCIPAL"]
"env": ["SOME_CANISTER_PRINCIPAL", "AZLE_TEST_FETCH"],
"assets": ["src"]
},
"some_canister": {
"type": "custom",
Expand Down
493 changes: 98 additions & 395 deletions examples/canister/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions examples/canister/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "ts-node --transpile-only --ignore=false test/test.ts"
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"tests": "npm run pre_tests && ts-node --transpile-only --ignore=false test/test.ts",
"test": "AZLE_TEST_FETCH=false npm run tests && AZLE_TEST_FETCH=true npm run tests"
},
"dependencies": {
"azle": "0.19.0"
},
"devDependencies": {
"@dfinity/agent": "0.15.4",
"@dfinity/agent": "^0.21.4",
"ts-node": "10.7.0",
"typescript": "4.6.3"
}
Expand Down
39 changes: 26 additions & 13 deletions examples/canister/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Principal,
query,
Record,
serialize,
text,
update,
Vec
Expand All @@ -19,22 +20,12 @@ export default Canister({
return someCanister;
}),
canisterReturnType: query([], SomeCanister, () => {
return SomeCanister(
Principal.fromText(
process.env.SOME_CANISTER_PRINCIPAL ??
ic.trap('process.env.SOME_CANISTER_PRINCIPAL is undefined')
)
);
return SomeCanister(Principal.fromText(getSomeCanisterPrincipal()));
}),
canisterNestedReturnType: update([], Wrapper, () => {
return {
someCanister: SomeCanister(
Principal.fromText(
process.env.SOME_CANISTER_PRINCIPAL ??
ic.trap(
'process.env.SOME_CANISTER_PRINCIPAL is undefined'
)
)
Principal.fromText(getSomeCanisterPrincipal())
)
};
}),
Expand All @@ -49,7 +40,29 @@ export default Canister({
[SomeCanister],
text,
async (someCanister) => {
return await ic.call(someCanister.update1);
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(
`icp://${getSomeCanisterPrincipal()}/update1`,
{
body: serialize({
candidPath: `/src/some_canister.did`
})
}
);
const responseJson = await response.json();

return responseJson;
} else {
return await ic.call(someCanister.update1);
}
}
)
});

function getSomeCanisterPrincipal(): string {
if (process.env.SOME_CANISTER_PRINCIPAL !== undefined) {
return process.env.SOME_CANISTER_PRINCIPAL;
}

throw new Error(`process.env.SOME_CANISTER_PRINCIPAL is not defined`);
}
65 changes: 21 additions & 44 deletions examples/canister/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCanisterId, Test } from 'azle/test';
import { _SERVICE } from './dfx_generated/canister/canister.did';
import { ActorSubclass } from '@dfinity/agent';
import { execSync } from 'child_process';
import { Principal } from '@dfinity/principal';

// TODO these tests should be rewritten to use @dfinity/agent once this issue is resolved: https://github.com/dfinity/agent-js/issues/702
// TODO this issue also needs to be resolved: https://forum.dfinity.org/t/services-wont-deserialize-properly-if-functions-arent-in-alphabetical-order/20885
Expand All @@ -10,85 +10,62 @@ export function getTests(canister: ActorSubclass<_SERVICE>): Test[] {
{
name: 'canisterParam',
test: async () => {
const result = execSync(
`dfx canister call canister canisterParam '(service "aaaaa-aa")'`
)
.toString()
.trim();
const result = await canister.canisterParam(
Principal.fromText('aaaaa-aa')
);

return {
Ok: result === '(service "aaaaa-aa")'
Ok: result.toText() === 'aaaaa-aa'
};
}
},
{
name: 'canisterReturnType',
test: async () => {
const result = execSync(
`dfx canister call canister canisterReturnType`
)
.toString()
.trim();
const result = await canister.canisterReturnType();

return {
Ok:
result ===
`(service "${getCanisterId('some_canister')}")`
Ok: result.toText() === getCanisterId('some_canister')
};
}
},
{
name: 'canisterNestedReturnType',
test: async () => {
const result = execSync(
`dfx canister call canister canisterNestedReturnType`
)
.toString()
.trim();
const result = await canister.canisterNestedReturnType();

return {
Ok:
result ===
`(record { someCanister = service "${getCanisterId(
'some_canister'
)}" })`
result.someCanister.toText() ===
getCanisterId('some_canister')
};
}
},
{
name: 'canisterList',
test: async () => {
const result = execSync(
`dfx canister call canister canisterList '(vec { service "r7inp-6aaaa-aaaaa-aaabq-cai"; service "rrkah-fqaaa-aaaaa-aaaaq-cai" })'`
)
.toString()
.trim();
const result = await canister.canisterList([
Principal.fromText('r7inp-6aaaa-aaaaa-aaabq-cai'),
Principal.fromText('rrkah-fqaaa-aaaaa-aaaaq-cai')
]);

return {
Ok:
result ===
`(
vec {
service "r7inp-6aaaa-aaaaa-aaabq-cai";
service "rrkah-fqaaa-aaaaa-aaaaq-cai";
},
)`
result.length === 2 &&
result[0].toText() === 'r7inp-6aaaa-aaaaa-aaabq-cai' &&
result[1].toText() === 'rrkah-fqaaa-aaaaa-aaaaq-cai'
};
}
},
{
name: 'canisterCrossCanisterCall',
test: async () => {
const result = execSync(
`dfx canister call canister canisterCrossCanisterCall '(service "${getCanisterId(
'some_canister'
)}")'`
)
.toString()
.trim();
const result = await canister.canisterCrossCanisterCall(
Principal.fromText(getCanisterId('some_canister'))
);

return {
Ok: result === '("SomeCanister update1")'
Ok: result === 'SomeCanister update1'
};
}
}
Expand Down
79 changes: 47 additions & 32 deletions examples/cross_canister_calls/src/canister1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ let canister2: typeof Canister2;

export default Canister({
init: init([], () => {
canister2 = Canister2(Principal.fromText(getCanister2Id()));
canister2 = Canister2(Principal.fromText(getCanister2Principal()));
}),
transfer: update([text, text, nat64], nat64, async (from, to, amount) => {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://${getCanister2Id()}/transfer`, {
body: serialize({
candidPath: 'canister2/index.did',
args: [from, to, amount]
})
});
const response = await fetch(
`icp://${getCanister2Principal()}/transfer`,
{
body: serialize({
candidPath: 'canister2/index.did',
args: [from, to, amount]
})
}
);
const responseJson = await response.json();

return responseJson;
Expand All @@ -41,12 +44,15 @@ export default Canister({
}),
balance: update([text], nat64, async (id) => {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://${getCanister2Id()}/balance`, {
body: serialize({
candidPath: 'canister2/index.did',
args: [id]
})
});
const response = await fetch(
`icp://${getCanister2Principal()}/balance`,
{
body: serialize({
candidPath: 'canister2/index.did',
args: [id]
})
}
);
const responseJson = response.json();

return responseJson;
Expand All @@ -58,12 +64,15 @@ export default Canister({
}),
account: update([AccountArgs], Opt(Account), async (args) => {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://${getCanister2Id()}/account`, {
body: serialize({
candidPath: 'canister2/index.did',
args: [args]
})
});
const response = await fetch(
`icp://${getCanister2Principal()}/account`,
{
body: serialize({
candidPath: 'canister2/index.did',
args: [args]
})
}
);
const responseJson = await response.json();

if (responseJson.length === 1) {
Expand All @@ -79,12 +88,15 @@ export default Canister({
}),
accounts: update([], Vec(Account), async () => {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://${getCanister2Id()}/accounts`, {
body: serialize({
candidPath: 'canister2/index.did',
args: []
})
});
const response = await fetch(
`icp://${getCanister2Principal()}/accounts`,
{
body: serialize({
candidPath: 'canister2/index.did',
args: []
})
}
);
const responseJson = await response.json();

return responseJson;
Expand All @@ -96,12 +108,15 @@ export default Canister({
}),
trap: update([], text, async () => {
if (process.env.AZLE_TEST_FETCH === 'true') {
const response = await fetch(`icp://${getCanister2Id()}/trap`, {
body: serialize({
candidPath: 'canister2/index.did',
args: []
})
});
const response = await fetch(
`icp://${getCanister2Principal()}/trap`,
{
body: serialize({
candidPath: 'canister2/index.did',
args: []
})
}
);
const responseJson = await response.json();

return responseJson;
Expand All @@ -121,7 +136,7 @@ export default Canister({
})
});

function getCanister2Id(): string {
function getCanister2Principal(): string {
if (process.env.CANISTER2_PRINCIPAL !== undefined) {
return process.env.CANISTER2_PRINCIPAL;
}
Expand Down

0 comments on commit fa9cc9d

Please sign in to comment.