Get assets canister cycles balance

Hello developers!

If there is a possibility to ask the asset canister ({“type”: “assets”} in dfx.json) for it’s cycles balance programmatically?

I would like to monitor cycles balance in Prometheus.
Currently all my Motoko canisters has a public query method to return cycles using ExperimentalCycles module.
Is there is a way to do it with asset canisters?

Only way I know is to get it using “dfx” command line.

I am thinking… I can fork “certified-assets” repo and add additional logic to it…

1 Like

For any canister you can add the blackhole canister as an additional controller and then anyone can query the cycle balance through that canister.

In fact, that should be considered best practice for anyone who don’t mind the public to see this data. And then pages like ic.rocks (pinging @wang) can display cycle balances, maybe even graph them over time…

2 Likes

Is there any experience on this topic?

How we can monitor an asset canister without dfx command e.g.

dfx wallet --network ic balance --precise

You need to make a call to the canister_status on the management canister with a caller that is listed as a controller of the respective canister.

Here @peterparker shared a snippet of how to do it using agent-js: How to make IC Management API canister_status call with @dfinity/agent?

I even build my self an open source monitoring dapp that supports NNS and SNS canisters :point_right: https://cycles.watch/

2 Likes

Thank you very much, these are great examples. I will study it and try to build a nodejs script to fetch the balance.

1 Like

If you are interested in automatically topping up your canisters, this is a really helpful tool

1 Like

I’m sorry I need more help on this topic. In your example, see link2, your actor is calling the method canister_status.

const response = await actor.canister_status({
canister_id: Principal.fromText(canisterId)
})

In my example I receive the following error:

myAgent.canister_status is not a function

I use the did.js file from the generated asset canister, but there is no service function canister_status in.

How can I add this function ?? What I’m missing ?

I would very grateful for some light.

Hey @rbole,

you need to call the canister_status method of the “management canister”.

It’s because in Papyrs I implement the canister status on the backend side while in Cycles.watch it’s on the frontend side :stuck_out_tongue_winking_eye:


If you want to do it on the backend side, and then expose it through a custom actor function, you can use canister_status basically summarized as following:

let ic = actor "aaaaa-aa";
await ic.canister_status({canister_id = canisterId});
// here expose the results you need through an actor function

Source: my utils.mo and types.mo


If you want to do it on the frontend side without exposing a custom function in your own canister you can also instantiate the IC and query directly the canister_status


const createICActor = (identity: Identity): Promise<ICActor> =>
  createActor<ICActor>({
    config: {
      canisterId: Principal.fromText('aaaaa-aa'),
      callTransform: transform,
      queryTransform: transform
    },
    idlFactory,
    identity
  });

const actor: ICActor = await createICActor(identity);

  const {cycles, status, memory_size} = await actor.canister_status({
    canister_id: Principal.fromText(canisterId)
  });

You just need a small transform function to convert the IC self canister id.

Source: function and actor in my cycles.watch ic.services.ts and you also gonna need the ic did files


In both the ID to use to access the IC is aaaaa-aa.

Let me know if it works out.


Side note: in the IC-js repo we do have not yet implemented these functions for the IC but it’s totally the plan to add some soon