Skip to content

Commit

Permalink
docs: update examples to use DFX v0.16.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed Feb 10, 2024
1 parent 408a65f commit 3f5b8e5
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 64 deletions.
17 changes: 13 additions & 4 deletions dfx.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"version": 1,
"dfx": "0.16.1",
"output_env_file": ".env",
"networks": {
"local": {
"bind": "127.0.0.1:8080",
"type": "ephemeral",
"replica": {
"subnet_type": "system"
}
}
},
"canisters": {
"counter": {
"main": "examples/counter/src/main.mo",
Expand Down Expand Up @@ -28,8 +40,5 @@
"output": "examples/todo/declarations"
}
}
},
"output_env_file": ".env",
"dfx": "0.15.2",
"version": 1
}
}
9 changes: 0 additions & 9 deletions examples/clock/tests/clock/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion examples/clock/tests/clock/index.js

This file was deleted.

12 changes: 6 additions & 6 deletions examples/clock/tests/src/clock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'node:path';
import { Principal } from '@dfinity/principal';
import { PocketIc } from '@hadronous/pic';
import { ClockActor, idlFactory, ClockService } from '../clock';
import { Actor, PocketIc } from '@hadronous/pic';
import { _SERVICE, idlFactory } from '../../declarations/clock.did';

const WASM_PATH = resolve(
__dirname,
Expand All @@ -17,14 +17,14 @@ const WASM_PATH = resolve(
);

describe('Clock', () => {
let actor: ClockActor;
let actor: Actor<_SERVICE>;
let pic: PocketIc;
let canisterId: Principal;

beforeEach(async () => {
pic = await PocketIc.create();
const fixture = await pic.setupCanister<ClockService>({
idlFactory,
const fixture = await pic.setupCanister<_SERVICE>({
idlFactory: idlFactory,
wasm: WASM_PATH,
});
actor = fixture.actor;
Expand All @@ -51,7 +51,7 @@ describe('Clock', () => {
it('should set and get canister cycles', async () => {
const cycles = await pic.getCyclesBalance(canisterId);

const cyclesToAdd = 1_000_000;
const cyclesToAdd = 1_000_000_000;
const updatedCyclesBalance = await pic.addCycles(canisterId, cyclesToAdd);
const fetchUpdatedCyclesBalance = await pic.getCyclesBalance(canisterId);

Expand Down
9 changes: 0 additions & 9 deletions examples/counter/tests/counter/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion examples/counter/tests/counter/index.js

This file was deleted.

8 changes: 4 additions & 4 deletions examples/counter/tests/src/counter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolve } from 'node:path';
import { Principal } from '@dfinity/principal';
import { PocketIc } from '@hadronous/pic';
import { CounterActor, idlFactory, CounterService, init } from '../counter';
import { Actor, PocketIc } from '@hadronous/pic';
import { IDL } from '@dfinity/candid';
import { _SERVICE, idlFactory, init } from '../../declarations/counter.did';

const WASM_PATH = resolve(
__dirname,
Expand All @@ -19,14 +19,14 @@ const WASM_PATH = resolve(

describe('Counter', () => {
let pic: PocketIc;
let actor: CounterActor;
let actor: Actor<_SERVICE>;
let canisterId: Principal;

const countInitArg = 1n;

beforeEach(async () => {
pic = await PocketIc.create();
const fixture = await pic.setupCanister<CounterService>({
const fixture = await pic.setupCanister<_SERVICE>({
idlFactory,
wasm: WASM_PATH,
arg: IDL.encode(init({ IDL }), [countInitArg]),
Expand Down
8 changes: 4 additions & 4 deletions examples/todo/tests/src/todo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolve } from 'node:path';
import { Principal } from '@dfinity/principal';
import { AnonymousIdentity } from '@dfinity/agent';
import { PocketIc, createIdentity } from '@hadronous/pic';
import { TodoActor, idlFactory, TodoService } from '../todo';
import { Actor, PocketIc, createIdentity } from '@hadronous/pic';
import { _SERVICE, idlFactory } from '../../declarations/todo.did';

const WASM_PATH = resolve(
__dirname,
Expand All @@ -19,15 +19,15 @@ const WASM_PATH = resolve(

describe('Todo', () => {
let pic: PocketIc;
let actor: TodoActor;
let actor: Actor<_SERVICE>;
let canisterId: Principal;

const alice = createIdentity('superSecretAlicePassword');
const bob = createIdentity('superSecretBobPassword');

beforeEach(async () => {
pic = await PocketIc.create();
const fixture = await pic.setupCanister<TodoService>({
const fixture = await pic.setupCanister<_SERVICE>({
idlFactory,
wasm: WASM_PATH,
});
Expand Down
9 changes: 0 additions & 9 deletions examples/todo/tests/todo/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion examples/todo/tests/todo/index.js

This file was deleted.

32 changes: 16 additions & 16 deletions packages/pic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,24 @@ PicJS leverages a [`postinstall`](https://docs.npmjs.com/cli/v9/using-npm/script
}
```

## API Docs

More detailed documentation is available in the [API docs](https://hadronous.github.io/pic-js/). The best place to start is with the [PocketIc](https://hadronous.github.io/pic-js/classes/PocketIc.html) class and then move on to the [Actor](https://hadronous.github.io/pic-js/interfaces/Actor.html) class.

## Examples

All examples are written in [TypeScript](https://www.typescriptlang.org/) with [Jest](https://jestjs.io/) as the test runner,
but `@hadronous/pic` can be used with JavaScript and any other testing runner, such as [NodeJS](https://nodejs.org/dist/latest-v20.x/docs/api/test.html), [bun](https://bun.sh/docs/cli/test) or [Mocha](https://mochajs.org/).

- The [Counter](https://github.com/hadronous/pic-js/tree/main/examples/counter/README.md) example demonstrates how to work with a simple canister as well as init arguments, canister upgrades and WASM reinstallation.
- The [Clock](https://github.com/hadronous/pic-js/tree/main/examples/clock/README.md) example demonstrates how to work with the replica's system time, canister timers as well as checking for canister existence and cycle management.
- The [Todo](https://github.com/hadronous/pic-js/tree/main/examples/todo/README.md) example demonstrates how to work with more complex canisters, identities, canister upgrades, and stable memory management.

## Canister Declarations

The DFX auto-generated bindings for canisters have some issues that make the developer experience with PicJS less than ideal. There are four files generated by DFX for a canister. To use a `counter` canister as an example:
The DFX auto-generated bindings for canisters had some issues in versions of DFX lower than `0.16.0` that made the developer experience with PicJS less than ideal. If you are using DFX v`0.16.0` or higher then you can ignore this section.

There are four files generated by DFX for a canister. To use a `counter` canister as an example:

- `counter.did.js` - Exports the canister's Candid as a JavaScript object. The `idlFactory` and `init` exports in this file are not included in the `counter.did.d.ts` file.

Expand Down Expand Up @@ -171,18 +186,3 @@ Fixes for these issues are already merged into Candid, but they need to be relea
export declare const idlFactory: IDL.InterfaceFactory;
export declare const init: ({ IDL }: { IDL: IDL }) => IDL.Type[];
```

Examples of this fix can be found for each of the examples in the [examples](#examples) section.

## API Docs

More detailed documentation is available in the [API docs](https://hadronous.github.io/pic-js/). The best place to start is with the [PocketIc](https://hadronous.github.io/pic-js/classes/PocketIc.html) class and then move on to the [Actor](https://hadronous.github.io/pic-js/interfaces/Actor.html) class.

## Examples

All examples are written in [TypeScript](https://www.typescriptlang.org/) with [Jest](https://jestjs.io/) as the test runner,
but `@hadronous/pic` can be used with JavaScript and any other testing runner, such as [NodeJS](https://nodejs.org/dist/latest-v20.x/docs/api/test.html), [bun](https://bun.sh/docs/cli/test) or [Mocha](https://mochajs.org/).

- The [Counter](https://github.com/hadronous/pic-js/tree/main/examples/counter/README.md) example demonstrates how to work with a simple canister as well as init arguments, canister upgrades and WASM reinstallation.
- The [Clock](https://github.com/hadronous/pic-js/tree/main/examples/clock/README.md) example demonstrates how to work with the replica's system time, canister timers as well as checking for canister existence and cycle management.
- The [Todo](https://github.com/hadronous/pic-js/tree/main/examples/todo/README.md) example demonstrates how to work with more complex canisters, identities, canister upgrades, and stable memory management.

0 comments on commit 3f5b8e5

Please sign in to comment.