# How do i programmatically convert ICP to cycles?

**URL:** <https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035>\
**Category:** Developers\
**Created:** [June 25, 2022, 3:03pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035 "2022-06-25T15:03:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [June 25, 2022, 3:03pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/1 "2022-06-25T15:03:53Z")

</div>

I’m trying to program my main canister to convert ICP to cycles to be consumed. How does one go about doing this? Any references to code samples would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [June 25, 2022, 4:14pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/2 "2022-06-25T16:14:46Z")

</div>

In JavaScript? If yes, I might have a code snippet somewhere…

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [June 25, 2022, 6:09pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/3 "2022-06-25T18:09:19Z")

</div>

> [@peterparker](#):
>
> In JavaScript?

Yep. That would be perfect.

---

<div class="post-metadata">

**Author:** ![dpdp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/dpdp/32/1143_2.png) [@dpdp](https://forum.dfinity.org/u/dpdp)\
**Post date:** [June 25, 2022, 8:32pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/4 "2022-06-25T20:32:42Z")

</div>

You should be able to reverse engineer it by reading the nns-dapp: [nns-dapp/periodic\_tasks\_runner.rs at a05bac7533fa9bef048bc6660ccb33bac750f993 · dfinity/nns-dapp · GitHub](https://github.com/dfinity/nns-dapp/blob/a05bac7533fa9bef048bc6660ccb33bac750f993/rs/src/periodic_tasks_runner.rs#L199)

I suppose you’ll have to do two transactions, an ICP transfer, then a notify\_top\_up to the CMC (cycle minting canister); where you’d specify the canister who will receive the cycles;

---

<div class="post-metadata">

**Author:** ![coin\_master](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/coin_master/32/15963_2.png) [@coin\_master](https://forum.dfinity.org/u/coin_master)\
**Post date:** [June 25, 2022, 9:04pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/5 "2022-06-25T21:04:59Z")

</div>

Here’s also an example with XTC (Which is currently way cheaper).

> <https://github.com/tarek-eg/ic-nodejs-examples/blob/79ba50c3ea090641391b3797989e9228a4ed6e40/src/xtc.ts#L11>

And you can combine it with `mint_by_icp`

> **[Getting Started - Documentation | Dank](https://docs.dank.ooo/xtc/getting-started/#deposit-icp-to-mint-an-xtc-balance-mint_by_icp)**
>
> Dank is a collection of Open Internet Service on the Internet Computer.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [June 26, 2022, 6:39am UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/6 "2022-06-26T06:39:20Z")

</div>

To convert ICP in cycles in JavaScript you need:

- a function that does the conversion
- the exchange rate that can be used in that function for the conversion

The exchange rate can be queried in the CMC canister, so you need its `.did` files. You can copy these from my project for example [ic/ic/cycles at main · papyrs/ic · GitHub](https://github.com/papyrs/ic/tree/main/ic/cycles)

Note: I named these files `cycles.xxx` but it’s a bit misleading, `cmc.xxx` would be more accurate, I still need to rename these.

In NodeJS you can query the exchange rate as following (source in my project [here](https://github.com/papyrs/ic/blob/main/scripts/services/cycles.services.mjs)).

Note that I add the conversion to trillion ratio within that function for conveniance reason for my scripts.

```auto
import pkgAgent from '@dfinity/agent';
import fetch from 'node-fetch';
import {idlFactory as nnsIdlFactory} from '../../ic/cycles/cycles.utils.did.mjs';
import {E8S_PER_ICP, icpToE8s} from '../utils/icp.utils.mjs';

const {HttpAgent, Actor} = pkgAgent;

const icpXdrConversionRate = async () => {
  const agent = new HttpAgent({fetch, host: 'https://ic0.app'});

  const actor = Actor.createActor(nnsIdlFactory, {
    agent,
    canisterId: 'rkp4c-7iaaa-aaaaa-aaaca-cai'
  });

  const {data} = await actor.get_icp_xdr_conversion_rate();
  const {xdr_permyriad_per_icp} = data;

  const CYCLES_PER_XDR = BigInt(1_000_000_000_000);

  // trillionRatio
  return (xdr_permyriad_per_icp * CYCLES_PER_XDR) / BigInt(10_000);
};

```

Once you got the function to get the exchange rate you can for example convert icp to cycles as following:

```auto
export const icpToCycles = async (amount) => {
  const trillionRatio = await icpXdrConversionRate();

  const e8ToCycleRatio = trillionRatio / E8S_PER_ICP;
  const cyclesAmount = icpToE8s(amount) * e8ToCycleRatio;

  const oneTrillion = BigInt(1000000) * BigInt(1000000);

  console.log(
    `${amount} ICP equals ${Number(cyclesAmount) / Number(oneTrillion)} (${cyclesAmount}) cycles`
  );

  return cyclesAmount;
};

```

Note that in above example the amount of cycle is a `string` (because I call the function from the command line). That’s why it uses a function `icpToE8s` that parse it to `bigint`. You can find its source code [here](https://github.com/papyrs/ic/blob/main/scripts/utils/icp.utils.mjs).

Let me know if that helps?

* * *

Side notes:

- this is a NodeJS example. if you need something that works in the browser let me know, we’ve got that in NNS dapp too
- I also go cycles to ICP if you need that, let me know too
- we actually literally discussed last week with my colleague the idea of providing such utilities in a JavaScript library too. as we do with [nns-js](https://github.com/dfinity/nns-js/) the idea would be to provide a “cmc-js” or something. not our top priority but we think it could be cool and handy, so might happen some day

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [July 15, 2022, 8:20am UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/7 "2022-07-15T08:20:20Z")

</div>

I have cleaned my above solution and just shared it in a brief blog post:

> **[ICP to cycles in JavaScript](https://6zvwc-sqaaa-aaaal-aalma-cai.raw.ic0.app/d/icp-to-cycles-in-javascript)**
>
> How to programmatically convert ICP to cycles in NodeJS?

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [October 21, 2022, 2:25pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/8 "2022-10-21T14:25:39Z")

</div>

I’m finally at the part of my project where I’m ready to implement this solution. I’m reading you blog and just wanted to say, well done! on both the article and application where the article is written.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [October 21, 2022, 2:46pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/9 "2022-10-21T14:46:01Z")

</div>

Thanks for the flowers Jesse 🙏. Hearing that someone found one of my small blog posts useful make my day!

Since I published the article it is worth to note that we managed to take some time and create that [`cmc-js`](https://github.com/dfinity/ic-js/tree/main/packages/cmc) I mention in the opening paragraph.

Using this library should also be able to get the ICP to cycles conversion rate quite easily:

```auto
import { CMCCanister } from "@dfinity/cmc";
import { createAgent } from "@dfinity/utils";

const agent = await createAgent({
  identity,
  host: HOST,
});

const { getIcpToCyclesConversionRate } = CMCCanister.create({
  agent,
  canisterId: CYCLES_MINTING_CANISTER_ID,
});

const rate = await getIcpToCyclesConversionRate();

```

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [October 24, 2022, 2:01pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/11 "2022-10-24T14:01:55Z")

</div>

I cannot help here, never used the Plug Wallet. You might want to start a new thread as it’s not really related to this post or ask Plug directly for more visibility.

---

<div class="post-metadata">

**Author:** ![Safik](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/safik/32/953_2.png) [@Safik](https://forum.dfinity.org/u/Safik)\
**Post date:** [October 24, 2022, 7:31pm UTC](https://forum.dfinity.org/t/how-do-i-programmatically-convert-icp-to-cycles/14035/12 "2022-10-24T19:31:42Z")

</div>

Ok. There is nothing more superfluous in this branch. It is important to me how the Identity class object is generated (from where it comes from). Right now I’m busy creating a module for converting ICP into Cycles.
