Ic-cdk=0.9.2 how to calculate the cycles used in http_request

Version: ic-cdk=0.9.2
citation path:use ic_cdk::api::management_canister::http_request::http_request;
The http_request method also needs to carry cycles when initiating a request

What is the calculation logic of this cycles? Is there a corresponding example? I am in
examples

Checked the similar but hard-coded, how to perform dynamic calculation

My error message is

The http_request resulted into error. RejectionCode: CanisterReject, Error: http_request request sent with 60_106_800 cycles, but 60_148_400 cycles are required.
2 Likes

How to calculate the required cycles in the canister is currently an unsolved problem that we’re aware of. If you’re ok hard-coding some values, you can find the formula here. Some of the numbers in there are outdated. I’ll fix these. But this table contains the current live values

1 Like

@Severin is there an update on this? I wanna use a http_request as a workaround for the lack of support of the ic_cdk for my use case, but now I come across this issue. This doesnt really help my project and pretty much means I cant continue untill one of the two issues I am having is solved…

  1. using call_raw, fetching the Candid from an unknown canister (canister is defined by the user, not me) and mapping the interface/types to have a correct response instead of this
vec {\n    record {\n      23_515 = 1 : nat32;\n      272_465_847 = 1_697_306_847_757_880_997 : nat64;\n      999_220_391 = null;\n

Pretty much what GitHub - dfinity/node-ic0: An easy-to-use JavaScript API for the Internet Computer. does but in rust.

  1. calculating the cost of cycles depending on the args size and doing an http_request. The args could differ for each request.

You could try to fetch the candid:service metadata of the canister and then figure out how to map that to the responses. The IC does not support fetching that on-chain, so you would have to use an http_request for that as well.

IIUC this does something different under the hood, actually. Turning the numbers into words is not possible, but this does the opposite since it would know the field names somehow already.

Not really something I can help. You have to probably just assume the max cost and hope you get a refund

1 Like

Do you know we must pass an explicit cycles value to the http_request method?

Because the CDK does not have enough information to make that decision on its own

I meat the same issue 2024. maybe i didnt calcute right.

(
  "The http_request resulted into error. RejectionCode: 
CanisterReject, Error: http_request request sent with 
176_928_000 cycles,
 but 20_933_203_200 cycles are required.",
)

its about 120times bigger cycles than i expect.

//https://internetcomputer.org/docs/current/developer-docs/gas-cost
pub fn calculate_cost(
  node_num: u32,
  send_bytes: u64,
  receive_bytes: u64,
) -> u128 {
  let base_fee =
    (3_000_000 + 60_000 * node_num as u64) as u128 * node_num as u128;
  let request_cost = 400 * node_num as u64 as u128 * send_bytes as u128;
  let response_cost = 800 * node_num as u64 as u128 * receive_bytes as u128;
  base_fee + request_cost + response_cost
}

Can i just send with 1T cycles and after http_request it return rest of cycles for me ?