简体   繁体   中英

Getting ERC20 token prices using Moralis

After calling:


async function getPrice() {
    fetch(
      "https://deep-index.moralis.io/api/token/ERC20/0xe53ec727dbdeb9e2d5456c3be40cff031ab40a55/price?chain=eth&chain_name=mainnet",
      {
        method: "GET",
        headers: {
          "X-API-KEY":
            "apiKey",
        },
      }
    )
      .then((res) => res)
      .then((data) => console.log(data));
  }
  getPrice();

I'm getting a success Response object like this

Response {type: "cors", url: "https://deep-index.moralis.io/api/token/ERC20/0xe5…0cff031ab40a55/price?chain=eth&chain_name=mainnet", redirected: false, status: 200, ok: true, …} 

how can i now retrieve the token price then?

I have modified your code a bit.

async function getPrice() {
            return fetch(
              "https://deep-index.moralis.io/api/token/ERC20/0xe53ec727dbdeb9e2d5456c3be40cff031ab40a55/price?chain=eth&chain_name=mainnet",
              {
                method: "GET",
                headers: {
                  accept: "*/*",
                  "X-API-KEY":
                    "xxxxxxxxxxxxxxxxxxxxx"
                }
              }
            ).then((response) => {
              return response.json();
            });
          }

Then you can call this function: const tokenPriceInfo = getPrice();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM