Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
How To Fetch OpenSea API

I am trying to fetch different collection stats and iterate through them using a for loop to designate which collection I want to retrieve. I want to push each collection into another array and return that array of 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;
  }
}

The newNFTArray.push is never called - I must be missing a step through one of the iterations?

The Response object array:

{
  "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
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Map the collection returning promises for Promise.all to execute.

export async function getNFTSales() {
  const collections = [
    'cryptopunks',
    'boredapeyachtclub',
    'mutant-ape-yacht-club',
  ];
  try {

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

    results = await = Promise.all(promises);
    // if each promise results in an array, results will be an array of arrays
    // flat() will put all the elements of all the arrays into one (I guessed that the OP wants this)
    return results.flat();
  }
  // note - removed catch. A catch that just throws doesn't do anything
}

The caller:

async function theCallingFunction() {
  const newNFTArray = await getNFTSales();
  console.log(newNFTArray);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda