Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the call API (breaking changes) #2650

Merged
merged 23 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified canister_templates/experimental.wasm
Binary file not shown.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
113 changes: 62 additions & 51 deletions examples/experimental/demo/ckbtc/wallet/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,53 @@ export default class {

@update([], IDL.Nat64)
async getBalance(): Promise<bigint> {
return await call(this.ckBtcPrincipal, 'icrc1_balance_of', {
paramIdlTypes: [Account],
returnIdlType: IDL.Nat,
return await call<[Account], bigint>(
this.ckBtcPrincipal,
'icrc1_balance_of',
{
paramIdlTypes: [Account],
returnIdlType: IDL.Nat,
args: [
{
owner: canisterSelf(),
subaccount: [
padPrincipalWithZeros(msgCaller().toUint8Array())
]
}
]
}
);
}

@update([], UpdateBalanceResult)
async updateBalance(): Promise<UpdateBalanceResult> {
const updateBalanceResult: UpdateBalanceResult = await call<
[UpdateBalanceArgs],
UpdateBalanceResult
>(this.minterPrincipal, 'update_balance', {
paramIdlTypes: [UpdateBalanceArgs],
returnIdlType: UpdateBalanceResult,
args: [
{
owner: canisterSelf(),
owner: [canisterSelf()],
subaccount: [
padPrincipalWithZeros(msgCaller().toUint8Array())
]
}
]
});

return updateBalanceResult;
}

@update([], UpdateBalanceResult)
async updateBalance(): Promise<UpdateBalanceResult> {
const updateBalanceResult: UpdateBalanceResult = await call(
@update([], IDL.Text)
async getDepositAddress(): Promise<string> {
return await call<[GetBtcAddressArgs], string>(
this.minterPrincipal,
'update_balance',
'get_btc_address',
{
paramIdlTypes: [UpdateBalanceArgs],
returnIdlType: UpdateBalanceResult,
paramIdlTypes: [GetBtcAddressArgs],
returnIdlType: IDL.Text,
args: [
{
owner: [canisterSelf()],
Expand All @@ -49,52 +74,38 @@ export default class {
]
}
);

return updateBalanceResult;
}

@update([], IDL.Text)
async getDepositAddress(): Promise<string> {
return await call(this.minterPrincipal, 'get_btc_address', {
paramIdlTypes: [GetBtcAddressArgs],
returnIdlType: IDL.Text,
args: [
{
owner: [canisterSelf()],
subaccount: [
padPrincipalWithZeros(msgCaller().toUint8Array())
]
}
]
});
}

// TODO get rid of Result
@update([IDL.Text, IDL.Nat], TransferResult)
async transfer(to: string, amount: bigint): Promise<TransferResult> {
return await call(this.ckBtcPrincipal, 'icrc1_transfer', {
paramIdlTypes: [TransferArgs],
returnIdlType: TransferResult,
args: [
{
from_subaccount: [
padPrincipalWithZeros(msgCaller().toUint8Array())
],
to: {
owner: canisterSelf(),
subaccount: [
padPrincipalWithZeros(
Principal.fromText(to).toUint8Array()
)
]
},
amount,
fee: [],
memo: [],
created_at_time: []
}
]
});
return await call<[TransferArgs], TransferResult>(
this.ckBtcPrincipal,
'icrc1_transfer',
{
paramIdlTypes: [TransferArgs],
returnIdlType: TransferResult,
args: [
{
from_subaccount: [
padPrincipalWithZeros(msgCaller().toUint8Array())
],
to: {
owner: canisterSelf(),
subaccount: [
padPrincipalWithZeros(
Principal.fromText(to).toUint8Array()
)
]
},
amount,
fee: [],
memo: [],
created_at_time: []
}
]
}
);
}
}

Expand Down
10 changes: 9 additions & 1 deletion examples/experimental/demo/ckbtc/wallet/backend/minter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDL } from 'azle';
import { IDL, Principal } from 'azle';

import {
UpdateBalanceError,
Expand Down Expand Up @@ -59,11 +59,19 @@ export const GetBtcAddressArgs = IDL.Record({
owner: IDL.Opt(IDL.Principal),
subaccount: IDL.Opt(IDL.Vec(IDL.Nat8))
});
export type GetBtcAddressArgs = {
owner: [Principal] | [];
subaccount: [Uint8Array] | [];
};

export const UpdateBalanceArgs = IDL.Record({
owner: IDL.Opt(IDL.Principal),
subaccount: IDL.Opt(IDL.Vec(IDL.Nat8))
});
export type UpdateBalanceArgs = {
owner: [Principal] | [];
subaccount: [Uint8Array] | [];
};

export const Minter = IDL.Service({
get_btc_address: IDL.Func([GetBtcAddressArgs], [IDL.Text]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default Canister({

return responseJson;
} else {
return await call('aaaaa-aa', 'raw_rand', {
return await call<undefined, Uint8Array>('aaaaa-aa', 'raw_rand', {
returnIdlType: IDL.Vec(IDL.Nat8)
});
}
Expand All @@ -32,7 +32,7 @@ export default Canister({
if (process.env.AZLE_TEST_FETCH === 'true') {
await fetch(`icp://aaaaa-aa/raw_rand`);
} else {
await call('aaaaa-aa', 'raw_rand', {
await call<undefined, Uint8Array>('aaaaa-aa', 'raw_rand', {
returnIdlType: IDL.Vec(IDL.Nat8)
});
}
Expand All @@ -58,7 +58,7 @@ async function getRandomness(): Promise<blob> {

return responseJson;
} else {
return await call('aaaaa-aa', 'raw_rand', {
return await call<undefined, Uint8Array>('aaaaa-aa', 'raw_rand', {
returnIdlType: IDL.Vec(IDL.Nat8)
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ export default Canister({
[Principal, text, text, nat],
text,
async (canisterId, method, candidArgs, payment) => {
const result = await call(canisterId, method, {
raw: candidEncode(candidArgs),
cycles: payment
});
const result = await call<Uint8Array, Uint8Array>(
canisterId,
method,
{
args: candidEncode(candidArgs),
cycles: payment,
raw: true
}
);

return candidDecode(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ export default Canister({

return responseJson;
} else {
return await call(getSomeCanisterPrincipal(), 'update1', {
returnIdlType: IDL.Text
});
return await call<undefined, string>(
getSomeCanisterPrincipal(),
'update1',
{
returnIdlType: IDL.Text
}
);
}
})
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default Canister({

return responseJson;
} else {
return await call<[Account], nat>(
return await call<[Account], bigint>(
getCkBtcPrincipal(),
'icrc1_balance_of',
{
Expand Down Expand Up @@ -162,7 +162,7 @@ export default Canister({

return responseJson;
} else {
return await call<[AccountArg], text>(
return await call<[AccountArg], string>(
getMinterPrincipal(),
'get_btc_address',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CompQueryCanister = Canister({

return responseJson;
} else {
return await call<undefined, text>(
return await call<undefined, string>(
getCanister2Principal(),
'simpleQuery',
{
Expand All @@ -51,7 +51,7 @@ const CompQueryCanister = Canister({

return responseJson;
} else {
return await call<undefined, text>(
return await call<undefined, string>(
getCanister2Principal(),
'manualQuery',
{
Expand Down Expand Up @@ -82,7 +82,7 @@ const CompQueryCanister = Canister({

msgReply(encoded);
} else {
const result = await call<undefined, text>(
const result = await call<undefined, string>(
getCanister2Principal(),
'manualQuery',
{
Expand Down Expand Up @@ -114,7 +114,7 @@ const CompQueryCanister = Canister({

return responseJson;
} else {
return await call<undefined, text>(
return await call<undefined, string>(
getCanister2Principal(),
'deepQuery',
{
Expand All @@ -138,7 +138,7 @@ const CompQueryCanister = Canister({

return responseJson;
} else {
return await call<undefined, text>(
return await call<undefined, string>(
getCanister2Principal(),
'updateQuery',
{
Expand All @@ -162,7 +162,7 @@ const CompQueryCanister = Canister({

return responseJson;
} else {
return await call<undefined, text>(
return await call<undefined, string>(
getCanister2Principal(),
'simpleQuery',
{
Expand All @@ -186,7 +186,7 @@ const CompQueryCanister = Canister({

return responseJson;
} else {
return await call<undefined, text>(
return await call<undefined, string>(
getCanister2Principal(),
'deepQuery',
{
Expand Down Expand Up @@ -249,7 +249,7 @@ async function incCanister(canister: any, candidPath: string): Promise<nat> {

return responseJson;
} else {
return await call<undefined, nat>(
return await call<undefined, bigint>(
canister.principal.toText(),
'incCounter',
{
Expand All @@ -273,7 +273,7 @@ async function incCanister2(): Promise<nat> {

return responseJson;
} else {
return await call<undefined, nat>(
return await call<undefined, bigint>(
getCanister2Principal(),
'incCounter',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default Canister({

return await response.json();
} else {
return await call<undefined, text>(
return await call<undefined, string>(
canister3Principal,
'deepQuery',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function getTests(canister1: ActorSubclass<_SERVICE>): Test {
it('fails to perform a composite query attempting to call an update method', async () => {
const canisterId = getCanisterId('canister2');
const partialErrorMessage = new RegExp(
`Rejection code 5, Error from Canister ${canisterId}: Canister has no query method`
`reject code 5: Error from Canister ${canisterId}: Canister has no query method`
bdemann marked this conversation as resolved.
Show resolved Hide resolved
);

await expect(canister1.updateQuery()).rejects.toThrow(
Expand All @@ -44,7 +44,7 @@ export function getTests(canister1: ActorSubclass<_SERVICE>): Test {

it('fails to perform a composite query from a regular query', async () => {
const partialErrorMessage = new RegExp(
`Rejection code 5, IC0527: Composite query cannot be called in replicated mode`
`reject code 5: IC0527: Composite query cannot be called in replicated mode`
);

await expect(canister1.simpleUpdate()).rejects.toThrow(
Expand Down
Loading