How to Programmatically Convert ICP Into Cycles in NodeJS

A tutorial for using JavaScript to convert Internet Computer utility tokens into cycles to pay for computation and storage.

David Dal Busco
The Internet Computer Review
3 min readJul 22, 2022

--

Until there is a library (e.g. cmc-js 👀) that facilitates the conversion of ICP utility tokens into cycles in JavaScript, the following tutorial may help you implement such a transformation.

Want to start building decentralized apps without learning a new programming language? Check out Juno, the open-source Blockchain-as-a-Service platform that makes it faster and easier than ever before to build dapps with frontend code only! ⚡️🚀🤯

​Introduction

Long story short, to convert ICP to cycles you need two things:

  1. ​The exchange rate to transform the values which is returned by the cycles-minting canister (CMC) through the function get_icp_xdr_conversion_rate.
  2. A function that, well, does the conversion.

Dependencies

To query the CMC in JavaScript easily, you need agent-js (and its peer dependencies). In addition, because following solution runs in a NodeJS context and not in a browser, node-fetch is required as well to provide previous library a way to perform XMLHttpRequest​.

Candid

​The description of the interface of the cycles-minting canister has to be used to initialize the communication channel.

The CMC candid file can be downloaded on the Internet Computer repo. Its binding files ( .idl.js​, .d.ts​ etc. ) can be generated with the didc command line tool or Canlista (a GUI version of the tool) but, if you want to spare yourself such operations, you can download all these files directly from the backend repo of my blogging platform project Papyrs.

In the following code snippets I use these particular bindings.

XDR conversion rate

On mainnet, ​the canister ID of the CMC is rkp4c-7iaaa-aaaaa-aaaca-caid​​. This ID should be used to instantiate the actor in order to query get_icp_xdr_conversion_rate​which returns xdr_permyriad_per_icp​​, the actual conversion rate in XDR we are looking for.

With 1 XDR being equal to 1 trillion cycles, I convert the result to trillion ratio before returning it.

Conversion ICP to cycles

We aim to convert readable numbers. That is why we first need a small utility that maps number to BigInt​ (because the conversion function will process such types).

A few samples of ​above function result (number -> bigint):

​Finally, to effectively convert the ICP to cycles, we can implement a cross-multiplication with the conversion rate.

Demo

​Put together in a small demo, e.g., above functions can be chained to log the result of the conversion to the console.

If run in a command line, the sample should output such a result:

Conclusion

​I hope this small tutorial will be useful. If you have any idea of improvements, let me know!

​To infinity and beyond
David

For more adventures, follow me on Twitter.
____

Start building at internetcomputer.org and join the developer community at forum.dfinity.org.

--

--