Retrieve Top 10 selling Nfts Using PHP and Infura
Tags: infura, php, nft, ethereum
Published: 2023-04-14 10:58:17

If you have read my previous articles, you will notice that the previous version of the NFT history logs library didnt support the top-selling built-in function to retrieve these kinds of logs. but in the latest update, we can now retrieve as many top-selling NFT tokens from a specific contract address as we want in just one line of code.

You will retrieve the top 10 selling NFTs in five stages:
1. sign up on the infura website
2. setup the provider for the nft history logs library
3. retrieve the top 10 selling NFT in one line of code.

So, why Infura? [Infura ](https://www.infura.io/)is a good option to start with that supports 100K free requests per day. And yes, it has premium plans, but its free plan is enough for us.

First of all, sign up for free in [Infura](https://www.infura.io/):

![infura signup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jxcn060sfeyounaeyim.jpeg)

then create a new API key:

![infura API key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76u55n8bfe222ujoshw0.jpeg)

Choose Web3 API and name your project:

![create api key infura](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1mczprwtoyeinefprhy.jpeg)

Copy the url of Ethereum's mainnet. Do not share the API key with anyone.

![infura endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5az1iukitkw9h927d4g.jpeg)

Now it's time to use the real magic. Install the latest version of the [NFT history logs library](https://github.com/xmrrabbitx/NftHistoryLogs#installation):

```
composer require nfthistory/nfthistorylogs dev-master
```

Then import it like this:

```
<?php

require 'vendor/autoload.php'

use Nft\History\nftHistory;
```
Then, insert the contract address you want and paste the provider URL that you copied from the Infura website:

```
$contractAddress = '0x00B3e138c6e4b233e5DDed8CfeD200f0c82B536c';
$provider = 'https://mainnet.infura.io/v3/<YOUR API KEY>';
```

In this step, create an instance of the nftHistory object:

```
$nfthistory = new nftHistory($contractAdress, $provider);
```
Now call the topSellNfts() function to retrieve as many top-selling nft tokens as you want:

```
$NftHistory->topSellNfts("multiThread", 10);
```

In the code above, we retrieve the top-selling NFT tokens of a contract address in one single code. We set the mode on multiTread to decrease the time for retrieving. Also, we set 10, which means retrieve the top 10 selling NFT tokens of a contract address. This option can be different based on your needs.

The output is:

```
array(10) {
  [0]=>
  string(4) "0.40"
  [1]=>
  string(4) "0.25"
  [2]=>
  string(4) "0.21"
  [3]=>
  string(4) "0.19"
  [4]=>
  string(4) "0.15"
  [5]=>
  string(4) "0.12"
  [6]=>
  string(4) "0.11"
  [7]=>
  string(4) "0.11"
  [8]=>
  string(4) "0.10"
  [9]=>
  string(4) "0.09"
}
```

As you can see, we successfully retrieved the top 10 out of 83 NFT token sale logs with a single code. Just remember, the bulk request (multiThread) may lead to a rate limit with some providers. Fortunately, Infura is more resilient than that. use carefully. 

The rest of the functions are [here](https://github.com/xmrrabbitx/NftHistoryLogs#usage)

I hope this is helpful to you. ^__^