Mento Protocol
  • Overview
    • Getting Started
      • What Is Mento?
      • Quick Start Guides
      • Analytics & Dashboards
    • Core Concepts
      • Stability Mechanisms
      • The Reserve
      • Oracles & Price Feeds
      • Trading Limits & Circuit Breakers
      • The Broker & Virtual AMMs
      • Fixed-Price Market Makers (FPMMs)
      • Research & Economics
    • Governance & the MENTO Token
      • Understanding Mento Governance
      • Participating in Governance
        • veMENTO & Voting Power
        • Creating Proposals
        • Voting Process
      • MENTO Tokenomics
      • Watchdogs & Safety
    • Security & Risk
      • Overview
      • Audit Reports
  • Build On Mento
    • Integration Overview
      • Integrate Stables
      • Integrate the Broker
      • Integrate Oracles
    • Mento SDK
      • Installation
      • Guides
        • Getting Exchange Pairs
        • Getting a Quote
        • Initiating a Swap
    • Smart Contracts
      • Broker
      • TradingLimits
      • BiPoolManager
      • Pricing Modules
      • SortedOracles
      • BreakerBox
      • Reserve
      • StableToken
      • Audits
    • Deployments
      • Addresses
      • Verification
  • Use Mento
    • Getting Mento Stables
      • On Celo
      • On Mobile
      • From Other Chains
      • Via Centralized Exchanges
Powered by GitBook
On this page
Edit on GitHub
  1. Build On Mento
  2. Mento SDK
  3. Guides

Getting a Quote

PreviousGetting Exchange PairsNextInitiating a Swap

Last updated 1 year ago

CtrlK

After you have already fetched all tradeable pairs, 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 mento-sdk-examples repo:

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