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

chore: update management canister interface #844

Merged
merged 5 commits into from
Feb 13, 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
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li>chore: update Management Canister interface</li>
<li>feat: new CustomPath class, better docs, and deprecating metadata path type for CanisterStatus</li>
<li>chore: adding new controller to snapshot for e2e canister status</li>
</ul>
Expand Down
14 changes: 12 additions & 2 deletions e2e/node/basic/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ test('createCanister', async () => {
// Make sure this doesn't fail.
await getManagementCanister({
agent: await agent,
}).provisional_create_canister_with_cycles({ amount: [BigInt(1e12)], settings: [] });
}).provisional_create_canister_with_cycles({
amount: [BigInt(1e12)],
settings: [],
specified_id: [],
sender_canister_version: [],
});
});

test('withOptions', async () => {
Expand All @@ -93,5 +98,10 @@ test('withOptions', async () => {
// Make sure this doesn't fail.
await getManagementCanister({
agent: await agent,
}).provisional_create_canister_with_cycles({ amount: [BigInt(1e12)], settings: [] });
}).provisional_create_canister_with_cycles({
amount: [BigInt(1e12)],
settings: [],
specified_id: [],
sender_canister_version: [],
});
});
67 changes: 54 additions & 13 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { RequestId } from './request_id';
import { toHex } from './utils/buffer';
import { CreateCertificateOptions } from './certificate';
import managementCanisterIdl from './canisters/management_idl';
import _SERVICE from './canisters/management_service';
import _SERVICE, { canister_settings } from './canisters/management_service';

export class ActorCallError extends AgentError {
constructor(
Expand Down Expand Up @@ -173,11 +173,22 @@ export type ActorMethodMappedWithHttpDetails<T> = {
/**
* The mode used when installing a canister.
*/
export enum CanisterInstallMode {
Install = 'install',
Reinstall = 'reinstall',
Upgrade = 'upgrade',
}
export type CanisterInstallMode =
| {
reinstall: null;
}
| {
upgrade:
| []
| [
{
skip_pre_upgrade: [] | [boolean];
},
];
}
| {
install: null;
};

/**
* Internal metadata for actors. It's an enhanced version of ActorConfig with
Expand All @@ -196,6 +207,13 @@ export interface CreateActorClassOpts {
httpDetails?: boolean;
}

interface CreateCanisterSettings {
freezing_threshold?: bigint;
controllers?: Array<Principal>;
memory_allocation?: bigint;
compute_allocation?: bigint;
}

/**
* An actor base class. An actor is an object containing only functions that will
* return a promise. These functions are derived from the IDL definition.
Expand Down Expand Up @@ -230,7 +248,7 @@ export class Actor {
},
config: ActorConfig,
): Promise<void> {
const mode = fields.mode === undefined ? CanisterInstallMode.Install : fields.mode;
const mode = fields.mode === undefined ? { install: null } : fields.mode;
// Need to transform the arg into a number array.
const arg = fields.arg ? [...new Uint8Array(fields.arg)] : [];
// Same for module.
Expand All @@ -241,17 +259,37 @@ export class Actor {
: config.canisterId;

await getManagementCanister(config).install_code({
mode: { [mode]: null } as any,
mode,
arg,
wasm_module: wasmModule,
canister_id: canisterId,
sender_canister_version: [],
});
}

public static async createCanister(config?: CallConfig): Promise<Principal> {
public static async createCanister(
config?: CallConfig,
settings?: CreateCanisterSettings,
): Promise<Principal> {
function settingsToCanisterSettings(settings: CreateCanisterSettings): [canister_settings] {
return [
{
controllers: settings.controllers ? [settings.controllers] : [],
compute_allocation: settings.compute_allocation ? [settings.compute_allocation] : [],
freezing_threshold: settings.freezing_threshold ? [settings.freezing_threshold] : [],
memory_allocation: settings.memory_allocation ? [settings.memory_allocation] : [],
},
];
}

const { canister_id: canisterId } = await getManagementCanister(
config || {},
).provisional_create_canister_with_cycles({ amount: [], settings: [] });
).provisional_create_canister_with_cycles({
amount: [],
settings: settingsToCanisterSettings(settings || {}),
specified_id: [],
sender_canister_version: [],
});

return canisterId;
}
Expand Down Expand Up @@ -472,11 +510,14 @@ export type ManagementCanisterRecord = _SERVICE;

/**
* Create a management canister actor
* @param config
* @param config - a CallConfig
*/
export function getManagementCanister(config: CallConfig): ActorSubclass<ManagementCanisterRecord> {
function transform(_methodName: string, args: unknown[], _callConfig: CallConfig) {
const first = args[0] as any;
function transform(
_methodName: string,
args: Record<string, unknown> & { canister_id: string }[],
) {
const first = args[0];
let effectiveCanisterId = Principal.fromHex('');
if (first && typeof first === 'object' && first.canister_id) {
effectiveCanisterId = Principal.from(first.canister_id as unknown);
Expand Down
Loading
Loading