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

Rename payment to cycles #2254

Merged
merged 4 commits into from
Nov 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn get_function(ctx: Ctx) -> QuickJsResult<Function> {
canister_id_bytes: TypedArray<u8>,
method: String,
args_raw: TypedArray<u8>,
payment_string: String|
cycles_string: String|
-> QuickJsResult<()> {
let canister_id = Principal::from_slice(canister_id_bytes.as_ref());
let args_raw = args_raw
Expand All @@ -28,7 +28,7 @@ pub fn get_function(ctx: Ctx) -> QuickJsResult<Function> {
"args_raw could not be converted into bytes",
))?
.to_vec();
let payment: u128 = payment_string
let payment: u128 = cycles_string
.parse()
.map_err(|e| throw_error(ctx.clone(), e))?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn get_function(ctx: Ctx) -> Result<Function> {
move |canister_id_bytes: TypedArray<u8>,
method: String,
args_raw: TypedArray<u8>,
payment_string: String|
cycles_string: String|
-> Result<Value> {
let canister_id = Principal::from_slice(canister_id_bytes.as_ref());
let args_raw = args_raw
Expand All @@ -20,7 +20,7 @@ pub fn get_function(ctx: Ctx) -> Result<Function> {
"args_raw could not be converted into bytes",
))?
.to_vec();
let payment: u128 = payment_string
let payment: u128 = cycles_string
.parse()
.map_err(|e| throw_error(ctx.clone(), e))?;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/stable/ic_apis/azle_ic_stable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type AzleIcStable = {
canisterIdBytes: Uint8Array,
method: string,
argsRaw: Uint8Array,
paymentString: string
cyclesString: string
) => void;
caller: () => Uint8Array;
candidCompiler: (candidPath: string) => string;
Expand All @@ -30,7 +30,7 @@ export type AzleIcStable = {
canisterIdBytes: Uint8Array,
method: string,
argsRawBuffer: Uint8Array,
paymentString: string
cyclesString: string
) => void;
performanceCounter: (counterType: number) => bigint;
rejectCode: () => number;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/stable/ic_apis/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function call<Args extends any[] | undefined, Return = any>(
paramIdlTypes?: IDL.Type[];
returnIdlType?: IDL.Type;
args?: Args;
payment?: bigint; // TODO this should be called cycles: https://github.com/demergent-labs/azle/issues/2104
cycles?: bigint;
raw?: Uint8Array;
}
): Promise<Return> {
Expand Down Expand Up @@ -58,7 +58,7 @@ export async function call<Args extends any[] | undefined, Return = any>(

const paramIdlTypes = options?.paramIdlTypes ?? [];
const args = options?.args ?? [];
const payment = options?.payment ?? 0n;
const cycles = options?.cycles ?? 0n;

const canisterIdPrincipal =
typeof canisterId === 'string'
Expand All @@ -67,7 +67,7 @@ export async function call<Args extends any[] | undefined, Return = any>(
const canisterIdBytes = canisterIdPrincipal.toUint8Array();
const argsRaw =
raw === undefined ? idlEncode(paramIdlTypes, args) : raw;
const paymentString = payment.toString();
const cyclesString = cycles.toString();

// TODO consider finally, what if deletion goes wrong
try {
Expand All @@ -77,15 +77,15 @@ export async function call<Args extends any[] | undefined, Return = any>(
canisterIdBytes.buffer,
method,
argsRaw.buffer,
paymentString
cyclesString
);
} else {
globalThis._azleIcStable.callRaw(
promiseId,
canisterIdBytes,
method,
argsRaw,
paymentString
cyclesString
);
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stable/ic_apis/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export async function chunk(): Promise<void> {
return undefined;
}

await call(id(), '_azle_chunk', { raw: candidEncode('()'), payment: 0n });
await call(id(), '_azle_chunk', { raw: candidEncode('()'), cycles: 0n });
}
10 changes: 5 additions & 5 deletions src/lib/stable/ic_apis/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function notify(
options?: {
paramIdlTypes?: IDL.Type[];
args?: any[];
payment?: bigint;
cycles?: bigint;
raw?: Uint8Array;
}
): void {
Expand All @@ -29,7 +29,7 @@ export function notify(

const paramIdlTypes = options?.paramIdlTypes ?? [];
const args = options?.args ?? [];
const payment = options?.payment ?? 0n;
const cycles = options?.cycles ?? 0n;
const raw = options?.raw;

const canisterIdPrincipal =
Expand All @@ -38,21 +38,21 @@ export function notify(
: canisterId;
const canisterIdBytes = canisterIdPrincipal.toUint8Array();
const argsRaw = raw === undefined ? idlEncode(paramIdlTypes, args) : raw;
const paymentString = payment.toString();
const cyclesString = cycles.toString();

if (globalThis._azleIcExperimental !== undefined) {
return globalThis._azleIcExperimental.notifyRaw(
canisterIdBytes.buffer,
method,
argsRaw.buffer,
paymentString
cyclesString
);
}

return globalThis._azleIcStable.notifyRaw(
canisterIdBytes,
method,
argsRaw,
paymentString
cyclesString
);
}
13 changes: 9 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

import { ActorSubclass } from '@dfinity/agent';
import { ActorSubclass, HttpAgent } from '@dfinity/agent';
import { describe, expect, test } from '@jest/globals';
import { join } from 'path';

Expand Down Expand Up @@ -116,10 +116,15 @@ export async function getCanisterActor<T>(
const { createActor } = await import(
join(process.cwd(), 'test', 'dfx_generated', canisterName)
);

const agent = new HttpAgent({
host: 'http://127.0.0.1:8000'
});

await agent.fetchRootKey();

const actor = createActor(getCanisterId(canisterName), {
agentOptions: {
host: 'http://127.0.0.1:8000'
}
agent
});

return actor;
Expand Down
8 changes: 4 additions & 4 deletions tests/end_to_end/candid_rpc/class_syntax/bitcoin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class {
network: { regtest: null }
}
],
payment: BITCOIN_API_CYCLE_COST
cycles: BITCOIN_API_CYCLE_COST
});
}

Expand All @@ -46,7 +46,7 @@ export default class {
network: { regtest: null }
}
],
payment: BITCOIN_API_CYCLE_COST
cycles: BITCOIN_API_CYCLE_COST
});
}

Expand All @@ -65,7 +65,7 @@ export default class {
network: { regtest: null }
}
],
payment: BITCOIN_API_CYCLE_COST
cycles: BITCOIN_API_CYCLE_COST
}
);
}
Expand All @@ -88,7 +88,7 @@ export default class {
network: { regtest: null }
}
],
payment: transactionFee
cycles: transactionFee
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export default class {
canisterId: Principal,
method: string,
candidArgs: string,
payment: bigint
cycles: bigint
): Promise<string> {
const result = await call<undefined>(canisterId, method, {
raw: candidEncode(candidArgs),
payment
cycles
});

return candidDecode(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class {
return notify(this.canister2Id, 'receiveNotification', {
paramIdlTypes: [IDL.Text],
args: ['This is the notification'],
payment: 10n
cycles: 10n
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export default class {
async sendCycles(): Promise<bigint> {
return await call(getCyclesPrincipal(), 'receiveCycles', {
returnIdlType: IDL.Nat64,
payment: 1_000_000n
cycles: 1_000_000n
});
}

@update([])
sendCyclesNotify(): void {
return notify(getCyclesPrincipal(), 'receiveCycles', {
payment: 1_000_000n
cycles: 1_000_000n
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function getBalance(
]
}
],
payment: 50_000_000n
cycles: 50_000_000n
}
);

Expand Down Expand Up @@ -136,7 +136,7 @@ async function getBlockByNumber(url: string, number: number): Promise<string> {
]
}
],
payment: 50_000_000n
cycles: 50_000_000n
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class {
sender_canister_version: []
}
],
payment: 100_000_000_000n
cycles: 100_000_000_000n
});

return true;
Expand Down Expand Up @@ -178,7 +178,7 @@ export default class {
sender_canister_version: []
}
],
payment: 100_000_000_000n
cycles: 100_000_000_000n
}
);

Expand Down Expand Up @@ -285,7 +285,7 @@ export default class {
canister_id: canisterId
}
],
payment: 10_000_000n
cycles: 10_000_000n
});

return true;
Expand Down Expand Up @@ -355,7 +355,7 @@ async function createCanister(): Promise<create_canister_result> {
paramIdlTypes: [create_canister_args],
returnIdlType: create_canister_result,
args: [{ settings: [], sender_canister_version: [] }],
payment: 50_000_000_000_000n
cycles: 50_000_000_000_000n
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function getSignatureResult(
}
}
],
payment: 10_000_000_000n
cycles: 10_000_000_000n
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class {
'receiveNotification',
{
raw: Uint8Array.from(candidEncode('()')),
payment: 0n
cycles: 0n
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class {
]
}
],
payment: 50_000_000n
cycles: 50_000_000n
});

return new TextDecoder().decode(Uint8Array.from(httpResponse.body));
Expand All @@ -67,7 +67,7 @@ export default class {
}
)
`),
payment: 50_000_000n
cycles: 50_000_000n
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getTests(

const keysResult =
await stableBTreeMapInstructionThresholdCanister.keysSmallRecord(
3_800
3_500
);

const valuesResult =
Expand All @@ -28,7 +28,7 @@ export function getTests(
1_000
);

expect(keysResult).toHaveLength(3_800);
expect(keysResult).toHaveLength(3_500);
expect(valuesResult).toHaveLength(4_000);
expect(itemsResult).toHaveLength(1_000);
}, 100_000);
Expand Down
17 changes: 9 additions & 8 deletions tests/property/ic_api/cycles_burn/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { getCanisterId } from 'azle/dfx';
import { execSyncPretty } from 'azle/src/build/stable/utils/exec_sync_pretty';
import { defaultPropTestParams, expect, it, Test } from 'azle/test';
import {
defaultPropTestParams,
expect,
getCanisterActor,
it,
Test
} from 'azle/test';
import fc from 'fast-check';

import { createActor } from './dfx_generated/canister';
import { _SERVICE as Actor } from './dfx_generated/canister/canister.did';

export function getTests(): Test {
return () => {
Expand All @@ -19,11 +24,7 @@ export function getTests(): Test {
'inherit'
);

const actor = createActor(getCanisterId('canister'), {
agentOptions: {
host: 'http://127.0.0.1:8000'
}
});
const actor = await getCanisterActor<Actor>('canister');

const cycleBalanceBefore =
await actor.getCycleBalance();
Expand Down
Loading