The Mento protocol is a decentralized and transparent protocol designed to create stablecoins, digital assets pegged to stable values like fiat currencies. A diversified portfolio of crypto assets fully backs each stablecoin issued on Mento, ensuring robustness and reliability.

Mento is accessible to anyone, allowing users to create stablecoins or exchange them for collateral assets freely. This cheatsheet shows how you can use the SDK to build apps that integrate with Mento. For more information about Mento you can check out the documentation.

Install

  1. You can add the Mento SDK to your dApp by running one of the following commands:
# Install with npm
npm install @mento-protocol/mento-sdk

# Or install with yarn
yarn add @mento-protocol/mento-sdk

# Or install with pnpm
pnpm add @mento-protocol/mento-sdk
  1. The Mento SDK currently depends on ethersV5, so you must add that to your dApp to use it. You can run any of the following commands to install ethers. The recommended version is 5.7.
# Install with npm
npm install [email protected]

# Or install with yarn
yarn add [email protected]

# Or install with pnpm
pnpm add [email protected]
  1. Now that the dependencies are set up, you can create a new instance of the Mento interface and an ethers provider to interact with the protocol. The code snippet below shows how this is done.
import { Mento } from '@mento-protocol/mento-sdk';
import { providers } from 'ethers';

async function main(): Promise<void> {
  
  // Create a new provider for the Celo Alfajores testnet
  const provider = new providers.JsonRpcProvider(
    '<https://alfajores-forno.celo-testnet.org>',
  );

  // Create a new Mento instance
  const mento = await Mento.create(provider);
}

Now we are set up with the SDK, we can make use of it to do various things.

Getting all asset pools

The Mento protocol has a built-in decentralized exchange mechanism that facilitates asset exchanges through liquidity pools, adjusting prices based on reserve levels. In order to determine which assets can be exchanged, you can use the following snippet:

import { Mento } from '@mento-protocol/mento-sdk';
import { providers } from 'ethers';

async function main(): Promise<void> {
  // Create a new provider for the Celo Alfajores testnet
  const provider = new providers.JsonRpcProvider(
    '<https://alfajores-forno.celo-testnet.org>',
  );

  // Create a new Mento instance
  const mento = await Mento.create(provider);

  // Get a list of all pairs that can be traded on Mento
  const pairs = await mento.getTradeablePairs();
  console.log(`Found ${pairs.length} tradeable pairs:`);
  console.log(pairs);
}

main()
  .then(() => console.log('Done! 🚀'))
  .catch((e) => console.log('Error: ', e));

Getting an exchange quote

Once you have decided which assets you want to exchange, you can use the following snippet to get a quote.

import { Mento } from '@mento-protocol/mento-sdk';
import { providers, utils } from 'ethers';

async function main(): Promise<void> {
  // Create a new provider for the Celo Alfajores testnet
  const provider = new providers.JsonRpcProvider(
    '<https://alfajores-forno.celo-testnet.org>',
  );

  // Create a new Mento instance
  const mento = await Mento.create(provider);

  const celoTokenAddr = '0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9';
  const cUSDTokenAddr = '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1';
  const tokenUnits = 18; // both CELO and cUSD have 18 decimal places

  // how much cUSD can I get for 1 CELO?
  const amountIn = utils.parseUnits('1', tokenUnits);
  const quoteAmountOut = await mento.getAmountOut(
    celoTokenAddr,
    cUSDTokenAddr,
    amountIn,
  );

  console.log(
    `~${utils.formatUnits(
      quoteAmountOut,
      tokenUnits,
    )} cUSD in exchange for 1 CELO`,
  );
}

main()
  .then(() => console.log('Done! 🚀'))
  .catch((e) => console.log('Error: ', e));