> ## Documentation Index
> Fetch the complete documentation index at: https://docs.initia.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Token Denom Conversion

[Tutorial GitHub Repository](https://github.com/initia-labs/examples/tree/main/evm/cosmos-coin-to-erc20)

This tutorial demonstrates how to convert between a hex-based ERC20 token
address and a Cosmos coin denom.

## Prerequisites

* [NodeJS](https://nodejs.org)

## Project Setup

First, create a new directory for your project.

```sh theme={null}
mkdir cosmos-coin-to-erc20
cd cosmos-coin-to-erc20
```

Next, initialize the project and create an `index.js` file in a `src` directory.

```sh theme={null}
npm init
touch src/index.js
```

## Development

For this tutorial, assume that you have an ERC20 token address
`0x5E5f1a92eECA58053E8364630b66763aa6265Ab0` that you want to convert to a
Cosmos coin denom, and then convert it back to an ERC20 token address.

First, initialize the variable for the chain's REST endpoint and the ERC20 token
address.

```js src/index.js theme={null}
const restUrl = `https://rest-evm-1.anvil.asia-southeast.initia.xyz`
const erc20Address = '0x5E5f1a92eECA58053E8364630b66763aa6265Ab0'
```

### ERC20 to Cosmos Denom

To actually do the conversion, use the REST's
`/minievm/evm/v1/denoms/{contractAddress}` endpoint.

```js src/index.js theme={null}
// previous code
async function erc20ToCoinDenom(contractAddress) {
  const response = await fetch(
    `${restUrl}/minievm/evm/v1/denoms/${contractAddress}`,
  )
  const data = await response.json()
  return data
}
```

If the address is a valid ERC20 token address, the response just returns the
token's Cosmos coin denom.

```sh theme={null}
evm/5E5f1a92eECA58053E8364630b66763aa6265Ab0
```

### Cosmos Denom to ERC20 Address

To convert a Cosmos coin denom back to an ERC20 token address, use the REST's
`/minievm/evm/v1/contracts/by_denom?denom={denom}` endpoint.

```js src/index.js theme={null}
// previous code
async function coinDenomToErc20(denom) {
  const response = await fetch(
    `${restUrl}/minievm/evm/v1/contracts/by_denom?denom=${denom}`,
  )
  const data = await response.json()
  return data
}
```

If the denom is a valid Cosmos coin denom, the response just returns the token's
ERC20 token address.

```sh theme={null}
0x5E5f1a92eECA58053E8364630b66763aa6265Ab0
```

The full code for the script, including the section to actually call the
function is as follows:

```js src/index.js theme={null}
const restUrl = `https://rest-evm-1.anvil.asia-southeast.initia.xyz`
const erc20Address = '0x5E5f1a92eECA58053E8364630b66763aa6265Ab0'

async function erc20ToCoinDenom(contractAddress) {
  const response = await fetch(
    `${restUrl}/minievm/evm/v1/denoms/${contractAddress}`,
  )
  const data = await response.json()
  return data
}
async function coinDenomToErc20(denom) {
  const response = await fetch(
    `${restUrl}/minievm/evm/v1/contracts/by_denom?denom=${denom}`,
  )
  const data = await response.json()
  return data
}

;(async () => {
  const coinDenomResponse = await erc20ToCoinDenom(erc20Address)
  console.log(coinDenomResponse.denom)
  const erc20Response = await coinDenomToErc20(coinDenomResponse.denom)
  console.log(erc20Response.address)
})()
```

If you run this script, you should see the following output:

```sh theme={null}
evm/5E5f1a92eECA58053E8364630b66763aa6265Ab0
0x5E5f1a92eECA58053E8364630b66763aa6265Ab0
```
