dfinity / agent-js

A collection of libraries and tools for building software around the Internet Computer, in JavaScript.
https://agent-js.icp.xyz
Apache License 2.0
155 stars 96 forks source link

services can only contain functions #702

Closed lastmjs closed 11 months ago

lastmjs commented 1 year ago

You can find more information here: https://forum.dfinity.org/t/illegal-service-definition-services-can-only-contain-functions/17609

I have an Azle canister:

import { CanisterResult, $query, query, ExternalCanister } from 'azle';

class MyService extends ExternalCanister {
    @query
    method1: () => CanisterResult<string>;
}

$query;
export function test(myService: MyService): MyService {
    return myService;
}

It produces this Candid file:

service : () -> {
  test : (service { method1 : () -> (text) query }) -> (
      service { method1 : () -> (text) query },
    ) query;
}

I've run dfx generate and I'm trying to do some tests using the agent:

import { Test } from 'azle/test';
import { _SERVICE } from './dfx_generated/service/service.did';
import { ActorSubclass } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';

export function getTests(serviceCanister: ActorSubclass<_SERVICE>): Test[] {
    return [
        {
            name: 'test',
            test: async () => {
                const result = await serviceCanister.test(
                    Principal.fromText('aaaaa-aa')
                );
                return {
                    Ok: result.toString() === 'aaaaa-aa'
                };
            }
        }
    ];
}

When I run this code I get: Illegal service definition: services can only contain functions. When I run the canister manually from the command line with dfx everything works fine.

alejandrade commented 1 year ago

Also getting this issue

chenyan-dfinity commented 1 year ago

Fixed in https://github.com/dfinity/agent-js/pull/731