Skip to content

Commit

Permalink
refactor ic_api
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 25, 2023
1 parent 830beb4 commit 5b4690e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# "examples/complex_types",
# "examples/func_types",
# "examples/generics",
# "examples/ic_api",
# "examples/imports",
# "examples/init",
# "examples/inline_types",
Expand Down Expand Up @@ -114,6 +113,7 @@ jobs:
"examples/ethereum_json_rpc",
"examples/guard_functions",
"examples/heartbeat",
"examples/ic_api",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
3 changes: 2 additions & 1 deletion examples/ic_api/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/ic_api/ic_api.wasm.gz",
"wasm": ".azle/ic_api/ic_api.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/ic_api",
"node_compatibility": true
Expand Down
124 changes: 52 additions & 72 deletions examples/ic_api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
// string: text;
// };

export default class extends Service {
export default Service({
// // returns the argument data as an array.
// argDataZeroParams(): Vec<null> {
// return ic.argData();
Expand Down Expand Up @@ -55,111 +55,91 @@ export default class extends Service {
// }

// returns the argument data as bytes.
@query([blob, int8, bool, text], blob)
argDataRaw(arg1: blob, arg2: int8, arg3: bool, arg4: text): blob {
return ic.argDataRaw();
}

argDataRaw: query(
[blob, int8, bool, text],
blob,
(arg1, arg2, arg3, arg4) => {
return ic.argDataRaw();
}
),
// returns the length of the argument data in bytes
@query([blob, int8, bool, text], nat32)
argDataRawSize(arg1: blob, arg2: int8, arg3: bool, arg4: text): nat32 {
return ic.argDataRawSize();
}

argDataRawSize: query(
[blob, int8, bool, text],
nat32,
(arg1, arg2, arg3, arg4) => {
return ic.argDataRawSize();
}
),
// returns the principal of the identity that called this function
@query([], principal)
caller(): Principal {
caller: query([], principal, () => {
return ic.caller();
}

}),
// returns the amount of cycles available in the canister
@query([], nat64)
canisterBalance(): nat64 {
canisterBalance: query([], nat64, () => {
return ic.canisterBalance();
}

}),
// returns the amount of cycles available in the canister
@query([], nat)
canisterBalance128(): nat {
canisterBalance128: query([], nat, () => {
return ic.canisterBalance128();
}

}),
// returns the canister's version number
@query([], nat64)
canisterVersion(): nat64 {
canisterVersion: query([], nat64, () => {
return ic.canisterVersion();
}

}),
// When called from a query call, returns the data certificate
// authenticating certified data set by this canister. Otherwise returns
// None.
@query([], Opt(blob))
dataCertificate(): Opt<blob> {
dataCertificate: query([], Opt(blob), () => {
return ic.dataCertificate();
}

}),
// When called from a query call, returns the data certificate
// authenticating certified data set by this canister. Otherwise returns
// None.
@update([], Opt(blob))
dataCertificateNull(): Opt<blob> {
dataCertificateNull: update([], Opt(blob), () => {
return ic.dataCertificate();
}

}),
// returns this canister's id
@query([], principal)
id(): Principal {
id: query([], principal, () => {
return ic.id();
}

}),
// Returns the number of instructions that the canister executed since the last
// entry point.
@query([], nat64)
instructionCounter(): nat64 {
instructionCounter: query([], nat64, () => {
return ic.instructionCounter();
}

}),
// determines whether the given principal is a controller of the canister
@query([principal], bool)
isController(principal: Principal): bool {
isController: query([principal], bool, (principal) => {
return ic.isController(principal);
}

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

}),
// prints a message through the local replica's output
@query([text], bool)
print(message: text): bool {
print: query([text], bool, (message) => {
ic.print(message);

return true;
}

@query([text], empty, { manual: true })
reject(message: text): Manual<empty> {
ic.reject(message);
}

}),
reject: query(
[text],
Manual(empty),
(message) => {
ic.reject(message);
},
{ manual: true }
),
// sets up to 32 bytes of certified data
@update([blob], Void)
setCertifiedData(data: blob): Void {
setCertifiedData: update([blob], Void, (data) => {
ic.setCertifiedData(data);
}

}),
// returns the current timestamp
@query([], nat64)
time(): nat64 {
time: query([], nat64, () => {
return ic.time();
}

}),
// traps with a message, stopping execution and discarding all state within the call
@query([text], bool)
trap(message: text): bool {
trap: query([text], bool, (message) => {
ic.trap(message);

return true;
}
}
})
});
4 changes: 2 additions & 2 deletions examples/ic_api/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { getCanisterId, runTests } from 'azle/test';
import { createActor } from './dfx_generated/ic_api';
import { getTests } from './tests';

const ic_api_canister = createActor(getCanisterId('ic_api'), {
const icApiCanister = createActor(getCanisterId('ic_api'), {
agentOptions: {
host: 'http://127.0.0.1:8000'
}
});

runTests(getTests(ic_api_canister as any));
runTests(getTests(icApiCanister as any));
2 changes: 1 addition & 1 deletion examples/ic_api/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function isNone<T>(option: [] | T[]): boolean {
return option.length === 0;
}

function candidDecode(bytes: Uint8Array): string {
function candidDecode(bytes: Uint8Array | number[]): string {
const hexString = [...bytes]
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
Expand Down

0 comments on commit 5b4690e

Please sign in to comment.