What's the equivalent of ic_cdk::api::call_with_payment in agent-js

I am trying to use XTC from agent-js and I wanted to use the mint function.

From the documentation the candid interface for mint is (principal, nat) -> (MintResult);

And this is how you should call the mint function from dfx:

$ dfx canister --network=ic --wallet=$(dfx identity --network=ic get-wallet) call --with-cycles AMOUNT aanaa-xaaaa-aaaah-aaeiq-cai mint "(principal \"$(dfx identity get-principal)\",0:nat)"

The interesting part here is the --with-cycles AMOUNT

As per documentation the AMOUNT is the cycles amount that you need to mint and the second arg to mint 0:nat is just a fake argument that gets ignored by mint logic.

This is also mentioned in this post

One can spot an interesting expression inside the mint() method - (to, Nat::from(0)) . Unfortunately, this is how XTC’s API works - you have to pass zero as a second argument (which in classic ERC-20 mint() is the amount of tokens to mint). This value does no affect the algorithm - you will get the same amount of XTC as the amount of cycles you send to this method. So, basically, this second argument is a fake.

The problem here is in the generated javascript binding to this function is looking like this
mint: (arg_0: Principal, arg_1: bigint) => Promise<MintResult>;

and there’s no api for passing cycles equivalent to --with-cycles.

The question is how one could pass cycles to this function or how one could use the mint function from agent-js.

1 Like

Well for people coming later I figured it out.
You can call the cycles wallet itself to get the same effect.

  let args = IDL.encode(
    [IDL.Text, IDL.Opt(IDL.Nat), IDL.Opt(IDL.Vec(IDL.Nat8))],
    [to_principal, [], []],
  );
  let swapCyclesToICP = await walletActor.wallet_call({
    canister: Principal.fromText(canister_id_you_want_to_call),
    method_name: "mint",
    args: [...Buffer.from(args)],
    cycles: BigInt(100_000),
  });
3 Likes