使用 Moralis 获取 ERC20 代币价格

时间:2021-07-26 15:15:48

标签: javascript api ethereum web3 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, …} 

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

1 个答案:

答案 0 :(得分:2)

我稍微修改了你的代码。

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