使用 Moralis 获取 ERC20 代币价格

问题描述

调用后:


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();

我得到了一个像这样的成功响应对象

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,…} 

那我现在如何检索代币价格?

解决方法

我稍微修改了你的代码。

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();
            });
          }

然后就可以调用这个函数了: const tokenPriceInfo = getPrice();