Skip to main content

Programmatic Setup

How dApp developers can use MetaMask to interact with the Celo network.


How to Add Celo to MetaMask

For dApp Developers interested in using MetaMask with Celo, we encourage doing the following 3 things, which are described in detail in the below sections.

  1. Add the Network
  2. Add Celo tokens (e.g. cUSD, cEUR)
  3. Handle cases where MetaMask is connected to a different network

Adding a Celo Network to MetaMask

To add a Celo Network to your dApp, you can use MetaMask's RPC API's wallet_addEthereumChain method. (See documentation).

Here is a JavaScript snippet you can use:

await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [<INSERT_NETWORK_PARAMS_HERE>],
});

Where it says INSERT_NETWORK_PARAMS_HERE, please replace with any of the following constants, depending on which network you'd like to connect to.

const CELO_PARAMS = {
chainId: '0xa4ec',
chainName: 'Celo',
nativeCurrency: { name: 'Celo', symbol: 'CELO', decimals: 18 }, rpcUrls: ['https://forno.celo.org'],
blockExplorerUrls: ['https://explorer.celo.org/'],
iconUrls: ['future'],
};
>

Adding Tokens (e.g. cUSD, cEUR)

To watch an asset on a Celo netowork (e.g. cUSD, cEUR) in your dApp, you can use MetaMask's RPC API's wallet_watchAsset method. (See documentation).

Here is a JavaScript snippet you can use:

await window.ethereum.request({
method: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address: '<INSERT_ADDRESS_HERE>',
'symbol': '<INSERT_SYMBOL_HERE>',
decimals: 18,
},
iconUrls: ['future'],
},
});

Where it says INSERT_ADDRESS_HERE, please replace with any of the following constants, depending on which network and which asset you'd like to connect to.

The following are examples for cUSD and cEUR.

  • Token Contract Address for cUSD: 0x765de816845861e75a25fca122bb6898b8b1282a
  • Token Contract Address for cEUR: 0xd8763cba276a3738e6de85b4b3bf5fded6d6ca73
tip

TODO: Replace this documentation link To double-check that the above values are up-to-date, please verify using the celocli network:contracts command (see documentation).

  • Where it says INSERT_SYMBOL_HERE, please replace with the correct symbol for the asset you'd like to watch. For Celo Dollars, it's cUSD and for Celo Euros, it's cEUR.

Handling cases where MetaMask is connected to a different network

We strongly suggest that you disable your dApp's functionality when MetaMask is connected to a non-Celo network.

MetaMask has an API for determining what network/chain you're connected to. See here for more documentation around that.