簡體   English   中英

如何獲取 OpenSea API

[英]How To Fetch OpenSea API

我正在嘗試獲取不同的集合統計信息並使用 for 循環遍歷它們以指定我要檢索的集合。 我想將每個集合推入另一個數組並返回 collections 數組。

export async function getNFTSales() {
  const collections = [
    'cryptopunks',
    'boredapeyachtclub',
    'mutant-ape-yacht-club',
  ];
  const newNFTArray = [];
  try {
    Promise.all(
      collections.forEach(collection => {
        fetch(`https://api.opensea.io/api/v1/collection/${collection}/stats`)
          .then(response => response.json())
          .then(response => {
            response.forEach(nftArray => {
              newNFTArray.push(nftArray);
            });
          });
        return newNFTArray;
      }),
    );
  } catch (error) {
    throw error;
  }
}

從未調用 newNFTArray.push - 我一定是錯過了其中一個迭代的步驟?

響應 object 數組:

{
  "stats": {
    "one_day_volume": 560.27,
    "one_day_change": -0.2779002178143809,
    "one_day_sales": 8,
    "one_day_average_price": 70.03375,
    "seven_day_volume": 10322.029999999999,
    "seven_day_change": 0.45140486499035165,
    "seven_day_sales": 139,
    "seven_day_average_price": 74.25920863309352,
    "thirty_day_volume": 39963.36100000096,
    "thirty_day_change": 0.2829772043143385,
    "thirty_day_sales": 476,
    "thirty_day_average_price": 83.95664075630454,
    "total_volume": 767357.0922492892,
    "total_sales": 18341,
    "total_supply": 9999,
    "count": 9999,
    "num_owners": 3296,
    "average_price": 41.83834536008337,
    "num_reports": 7,
    "market_cap": 742517.827122302,
    "floor_price": null
  }
}

Map 集合返回承諾Promise.all執行。

  const promises = collections.map(collection => { // note the map
    // note the return
    return fetch(`https://api.opensea.io/api/v1/collection/${collection}/stats`)
      .then(response => response.json());
  });

  try {
    Promise.all(promises).then(results => {
      newNFTArray = results.flat() 
    });
  } catch (error) {
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM