Skip to content

Latest commit

 

History

History
108 lines (76 loc) · 5.44 KB

L4.1_Get_Price.md

File metadata and controls

108 lines (76 loc) · 5.44 KB

Get Price

Now for the fun part! Let's fetch a live price using the 0x Swap's /price endpoints 🙌

In our app, we want to dynamically surface the amount of buy token a user can get whenever they input a sell token amount.

Screenshot 2024-04-16 at 4 42 10 PM

Price vs Quote

Before we make a call, let's discuss the difference between price vs quote.

The /price endpoint helps us get an indicative price.

An indicative price is used when users is just browsing and wants to check the price they could receive on a trade. They are not ready for a firm quote yet.

Later, when the user is actually ready to make a trade, we will ping /quote which returns an order that is ready to submitted on-chain.

/price is nearly identical to /quote, but with a few key differences:

  • /price does not return a order that can be submitted on-chain; it simply provides us the same information
  • Think of it as the "read-only" version of /quote"

It is important to ping /quote only when the user is ready to submit the order because Market Makers must commit their assets to settle that trade when they provide the quote. So if we ping /quote too much when we really are just asking for a price and not ready to submit an order, then this can clog up the system!

Fetch price

Fetching a price with the /swap/v1/price endpoint is a straight-forward HTTP GET request.

Let's walk-through how to fetch a price.

Step 0. Get a 0x API key

Every call to a 0x API must include a 0x API secret key. Create a 0x account and get a live API key. See the guide here to get setup.

Step 1. Set up a price request

A simple price API request will look like this

https://api.0x.org/swap/v1/quote?sellToken=0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270&buyToken=0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174&sellAmount=1000000000000000000&excludedSources=Kyber&takerAddress=0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045  --header '0x-api-key: REPLACE_WITH_API_KEY'

Let's break it down to the required parameters (full list of parameters here):

https://polygon.api.0x.org/swap/v1/price                     // Request an indicative price on Polygon
?sellToken=0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270        // Sell WMATIC
&sellAmount=1000000000000000000                              // Sell amount 1 WMATIC (18 decimals)
&buyToken=0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174         // Buy USDC
&takerAddress=0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045     // Address that will make the trade (include if available)
--header '0x-api-key: [API_KEY]'                             // Replace with your own API key

Plug the API request into your favorite API testing platform, I'll use HTTPie here.

httpie

Step 2. Review response

You will receive a response that looks like this (some fields omitted):

{
  "chainId": 137,
  "price": "0.8769",
  "grossPrice": "0.878204",
  "estimatedPriceImpact": "0",
  "value": "0",
  "gasPrice": "122739220000",
  "gas": "300000",
  "estimatedGas": "300000",
  "protocolFee": "0",
  "minimumProtocolFee": "0",
  "buyTokenAddress": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
  "buyAmount": "876900",
  "grossBuyAmount": "878204",
  "sellTokenAddress": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
  ...
      {
      "name": "Uniswap_V3",
      "proportion": "0.8"
    },
    {
      "name": "MeshSwap",
      "proportion": "0.2"
    },
  ...
  "allowanceTarget": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
  ...

Let's take a look at some notable fields:

  • price - If buyAmount was specified in the request it provides the price of buyToken in sellToken and vice versa.
  • buyAmount - The amount of buyToken (in buyToken units) that would be bought in this swap
  • sources - The percentage distribution of buyAmount or sellAmount split between each liquidity source. Ex: [{ name: '0x', proportion: "0.8" }, { name: 'Kyber', proportion: "0.2"}, ...]
  • See a full list of the response params here

Troubleshooting

If you encounter an error when making an API request, the following are common reasons why:

  • Ensure that the sell or buy amounts are not too small. Check decimal value of the sellToken (100 may actually mean 0.00000000001 of the token)! If the inputted value is too small, you may get an code: 100 validation failed error because there's not enough liquidity for an order that small.

100_error

Now that we've been familiarized with how to make an API request, let's plug this API price request into our app and display the price to the end user.