Mento Protocol
  • Mento Protocol
    • Home
    • What, why, who Mento?
    • Quick Links
  • HOW TO SOURCE MENTO STABLES
    • Overview
    • CEXs, DEXs, Pools
    • From other Chains
    • On-ramp Providers
    • Automation via MATE
  • Protocol Concepts
    • Stability
    • Reserve
    • Asset exchanges
      • Broker
      • Trading Limits
      • Exchange Providers
      • BiPoolManager
    • Oracles
    • On-Chain Circuit Breaker
    • Governance
      • Verification
  • Developers
    • Repository Overview
    • Integrate Mento Stables
    • Smart Contracts
      • Broker
      • TradingLimits
      • BiPoolManager
      • Pricing Modules
      • SortedOracles
      • BreakerBox
      • Reserve
      • StableToken
      • Audits
    • Deployments
      • Addresses
      • Verification
      • Parameters
    • Mento SDK
      • Installation
      • Guides
        • Getting Exchange Pairs
        • Getting a Quote
        • Initiating a Swap
    • Oracles
      • Oracle Client
        • Price Sources
      • Becoming an Oracle Provider
  • Economics
    • Stability
    • Risks
    • Research
  • Governance & Token
    • Overview
    • Governance Components
    • Governance Scope
    • MENTO Token
      • Listing information
    • Airdrop
    • Governance Watchdogs
Powered by GitBook
On this page
Edit on GitHub
  1. Developers
  2. Mento SDK
  3. Guides

Getting a Quote

PreviousGetting Exchange PairsNextInitiating a Swap

Last updated 1 year ago

After you have already , you are ready to get price quotes directly from the Broker contract. In the example below we will do so for the CELO/cUSD exchange.

We start by importing the SDK and instantiating it on the Alfajores Celo testnet:

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

const provider = new providers.JsonRpcProvider(
  "https://alfajores-forno.celo-testnet.org"
);
const mento = await Mento.create(provider);

In order to get a quote you will need the address of the token you will provide (tokenIn) as well as the address of the token you intend to get out (tokenOut). In this example we will get a quote for CELO -> cUSD using the addresses from the previous step.

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

We can now get a quote for how much cUSD we can expect to receive in exchange 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`
);

Alternatively, you can also get a quote for the amount of tokens that you would need to provide in order to buy an exact amount of another desired token. For example, the amount of cUSD needed to buy 1 CELO:

const amountOut = utils.parseUnits("1", tokenUnits);
const quoteAmountIn = await mento.getAmountIn(
  cUSDTokenAddr,
  celoTokenAddr,
  amountOut
);

console.log(
  `~${utils.formatUnits(quoteAmountIn, tokenUnits)} cUSD needed to buy 1 CELO`
);

You can find the full runnable code for this section within the repo:

fetched all tradeable pairs
mento-sdk-examples
https://github.com/mento-protocol/mento-sdk-examples/blob/main/src/quotes.tsgithub.com