Skip to content

Commit

Permalink
feat: support to configure module settings (#130)
Browse files Browse the repository at this point in the history
* feat: support for config settings

* chore: lint
  • Loading branch information
peterpeterparker authored Jul 28, 2024
1 parent 307341d commit bcf306f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
54 changes: 46 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"@babel/preset-typescript": "^7.24.1",
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/ic-management": "^5.1.0",
"@dfinity/identity": "^1.4.0",
"@dfinity/principal": "^1.4.0",
"@junobuild/admin": "^0.0.52",
"@junobuild/cli-tools": "^0.0.14",
"@junobuild/config": "^0.0.11",
"@junobuild/config-loader": "^0.0.5",
"@junobuild/core-peer": "^0.0.21",
"@junobuild/utils": "^0.0.24",
Expand All @@ -45,6 +45,7 @@
"terminal-link": "^3.0.0"
},
"devDependencies": {
"@junobuild/config": "^0.0.13",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^6.20.0",
Expand Down
53 changes: 50 additions & 3 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {setAuthConfig, setConfig} from '@junobuild/admin';
import {ICManagementCanister, LogVisibility} from '@dfinity/ic-management';
import {Principal} from '@dfinity/principal';
import {type SatelliteParameters, setAuthConfig, setConfig} from '@junobuild/admin';
import type {ModuleSettings} from '@junobuild/config';
import {isNullish} from '@junobuild/utils';
import ora from 'ora';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {initAgent} from '../utils/actor.utils';
import {configEnv} from '../utils/config.utils';
import {satelliteParameters} from '../utils/satellite.utils';
import {init} from './init';
Expand All @@ -13,7 +17,7 @@ export const config = async (args?: string[]) => {

const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);
const {storage, authentication} = satelliteConfig;
const {storage, authentication, settings} = satelliteConfig;

const satellite = satelliteParameters({satellite: satelliteConfig, env});

Expand Down Expand Up @@ -42,9 +46,52 @@ export const config = async (args?: string[]) => {
},
satellite
})
])
]),
...(isNullish(settings) ? [] : [setSettings({settings, satellite})])
]);
} finally {
spinner.stop();
}
};

const setSettings = async ({
settings,
satellite
}: {
settings: ModuleSettings;
satellite: Omit<SatelliteParameters, 'satelliteId'> &
Required<Pick<SatelliteParameters, 'satelliteId'>>;
}) => {
const {
freezingThreshold,
reservedCyclesLimit,
logVisibility,
heapMemoryLimit,
memoryAllocation,
computeAllocation
} = settings;

const {satelliteId} = satellite;

const agent = await initAgent();

const {updateSettings} = ICManagementCanister.create({
agent
});

await updateSettings({
canisterId: Principal.fromText(satelliteId),
settings: {
freezingThreshold,
reservedCyclesLimit,
logVisibility: isNullish(logVisibility)
? undefined
: logVisibility === 'public'
? LogVisibility.Public
: LogVisibility.Controllers,
wasmMemoryLimit: heapMemoryLimit,
memoryAllocation,
computeAllocation
}
});
};
22 changes: 22 additions & 0 deletions src/utils/actor.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {HttpAgent} from '@dfinity/agent';
import {Ed25519KeyIdentity} from '@dfinity/identity/lib/cjs/identity/ed25519';
import {type ActorParameters} from '@junobuild/admin';
import {isNullish, nonNullish} from '@junobuild/utils';
Expand Down Expand Up @@ -26,3 +27,24 @@ export const actorParameters = (): ActorParameters => {
...(nonNullish(process.env.CONTAINER_URL) && {container: process.env.CONTAINER_URL})
};
};

export const initAgent = async (): Promise<HttpAgent> => {
const {identity, container, fetch} = actorParameters();

const localActor = nonNullish(container) && container !== false;

const host = localActor
? container === true
? 'http://127.0.0.1:5987'
: container
: 'https://icp-api.io';

const agent = new HttpAgent({identity, host, retryTimes: 10, fetch});

if (localActor) {
// Fetch root key for certificate validation during development
await agent.fetchRootKey();
}

return agent;
};

0 comments on commit bcf306f

Please sign in to comment.