Skip to content

Commit

Permalink
fix for counter
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 30, 2023
1 parent 70cf27d commit 85af490
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
4 changes: 4 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"docs": {
"type": "assets",
"source": ["docs/generated"]
},
"counter": {
"type": "motoko",
"main": "e2e/node/canisters/counter.mo"
}
}
}
11 changes: 8 additions & 3 deletions e2e/node/basic/counter.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ActorSubclass } from '@dfinity/agent';
import type { _SERVICE } from '../canisters/declarations/counter/index';
import counterCanister, { noncelessCanister, createActor } from '../canisters/counter';
import { it, expect, describe, vi } from 'vitest';

Expand Down Expand Up @@ -31,13 +33,16 @@ describe('counter', () => {
expect(set1.size < values.length || set2.size < values2.length).toBe(true);
}, 40000);
it('should increment', async () => {
const { actor: counter } = await noncelessCanister();
const { actor } = await noncelessCanister();
const counter = actor as ActorSubclass<_SERVICE>;

await counter.write(BigInt(0));
expect(Number(await counter.read())).toEqual(0);

await counter.inc();
expect(Number(await counter.read())).toEqual(1);
await counter.inc();
expect(Number(await counter.read())).toEqual(2);

expect(await counter.inc_read()).toEqual(BigInt(2));
}, 40000);
});
describe('retrytimes', () => {
Expand Down
42 changes: 13 additions & 29 deletions e2e/node/canisters/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ let cache: {
actor: any;
} | null = null;

const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
write: IDL.Func([IDL.Nat], [], []),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};

/**
* Create a counter Actor + canisterId
*/
Expand All @@ -24,15 +35,6 @@ export default async function (): Promise<{

const canisterId = await Actor.createCanister({ agent: await agent });
await Actor.install({ module }, { canisterId, agent: await agent });
const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};

cache = {
canisterId,
Expand All @@ -59,20 +61,11 @@ export async function noncelessCanister(): Promise<{

const canisterId = await Actor.createCanister({ agent: disableNonceAgent });
await Actor.install({ module }, { canisterId, agent: disableNonceAgent });
const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};

const actor = Actor.createActor(idl, { canisterId, agent: await disableNonceAgent }) as any;
return {
canisterId,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
actor: Actor.createActor(idl, { canisterId, agent: await disableNonceAgent }) as any,
actor,
};
}

Expand All @@ -91,14 +84,5 @@ export const createActor = async (options?: HttpAgentOptions) => {

const canisterId = await Actor.createCanister({ agent });
await Actor.install({ module }, { canisterId, agent });
const idl: IDL.InterfaceFactory = ({ IDL }) => {
return IDL.Service({
inc: IDL.Func([], [], []),
inc_read: IDL.Func([], [IDL.Nat], []),
read: IDL.Func([], [IDL.Nat], ['query']),
greet: IDL.Func([IDL.Text], [IDL.Text], []),
queryGreet: IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};
return Actor.createActor(idl, { canisterId, agent }) as any;
};
Binary file modified e2e/node/canisters/counter.wasm
Binary file not shown.
11 changes: 10 additions & 1 deletion e2e/node/canisters/declarations/counter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Actor, HttpAgent } from '@dfinity/agent';
import { Actor, ActorMethod, HttpAgent } from '@dfinity/agent';

// Imports and re-exports candid interface
import { idlFactory } from './counter.did.js';
Expand Down Expand Up @@ -35,3 +35,12 @@ export const createActor = (canisterId, options = {}) => {
...options.actorOptions,
});
};

export interface _SERVICE {
greet: ActorMethod<[string], string>;
inc: ActorMethod<[], undefined>;
inc_read: ActorMethod<[], bigint>;
queryGreet: ActorMethod<[string], string>;
read: ActorMethod<[], bigint>;
write: ActorMethod<[bigint], undefined>;
}

0 comments on commit 85af490

Please sign in to comment.