How to Rank Top-Selling NFTs Using PHP
Tags: nft, php, ethereum, nfthistorylogs
Published: 2023-03-22 01:10:11

Well, we know that NFTs are transactions with special token IDs in the Ethereum logs.

But, what exactly does that mean

It means every token id includes a “Transfer” transaction, which includes special information in the smart contract. Every “Transfer” transaction includes a set of logs and special events. In fact, the whole of the Ethereum logs concept is a multidimensional array, which we can access to subset information.


One of these pieces of information is the “value” of a transaction. This value is in WEI format, which you could convert to Ether.


For more convenient access, we could use the NFT History Logs library.


Let’s get started!


First, install it via this command:

```
 composer require nfthistory/nfthistorylogs v0.0.1
```
Then import it like this:

```
<?php

require 'vendor/autoload.php'

use Nft\History\nftHistory;
```
Then, insert the contract address and set the provider:

```
$contractAddress = '0x00B3e138c6e4b233e5DDed8CfeD200f0c82B536c';
$provider = 'https://cloudflare-eth.com';
```
I used this contract address, 0x00B3e138c6e4b233e5DDed8CfeD200f0c82B536c, which belongs to The Boy who lost his mind Open Edition by Boss Logic. It is a collection of 82 NFTs at this smart contract address.


In this article, I want to list the top sales of NFTs using this contract address as a sample.

![The Boy who lost his mind Open Edition by Boss Logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0r3i8nx2q6059jej9e0w.jpg)
I used the free Cloudflare provider, but you could use your desired provider with an API key.

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

```
$nfthistory = new nftHistory($contractAdress, $provider);
```
Next, call the getAllTransferTrxHashAndIds() function:

```
$trxHash = $nfthistory->getAllTransferTrxHashAndIds();
```
The above function will give us a list of “Transfer” transaction hashes based on every token id.

The output of the function is something like this:

```
var_dump($trxHash);

// output:
array(82) {
   [19400010233]=>
       array(2) {
               [0]=>
              string(66) 
               '0x3982a65f77b8896c19722b8ee2314f2224c767fef25035ba968f57e0cb4a8c8e'

```
Now we have a set of token ids that includes “Transfer” hashes of specific NFTs. The number 19400010233 is the token id, and the string value with the length of 66 in hexadecimal format is the transaction hash. by using this transaction hash, we could access the value of that transaction.


So, let’s filter this array:

```
$filterData = array_map(function($log) use($nfthistory){

             return array_map(function($logs) use($nfthistory){

                    $result = $nfthistory->getTrxByHash($logs);

                     if($result['value'] !== '0x0'){

                                 $logs = $result['value'];
                                 return $logs;

                                 }
             },$log);

},$trxHash);
```
In the above code, we used array_map() to iterate over the array in order to filter it out. In this loop, we iterate again, using another array_map() to access every transaction hash. Then, using NFT History Logs , we call the getTrxHash() function to access the value of the transaction in trade.

The output of this function is a multidimensional array. The “value” index is important to us because it is the hexadecimal value of trade in WEI format. We put in a statement to remove the “0x0”, which means removing zero values.

In this step, you must have an array like this:

```
array(82){
[19400010074]=>
 array(4) {
    [0]=>
    NULL
    [1]=>
    NULL
    [2]=>
    NULL
    [3]=>
    string(17) '0x12dfb0cb5e88000'
  }
  [19400010323]=>
  array(2) {
    [0]=>
   NULL
    [1]=>
    NULL
  }
}

```
As you can see, the keys of the above array are the token ids that we preserved, and the values of the array are the prices of every transaction. Those whose values are NULL mean there was no sale or the transfer event was equal to zero Ether.


Now we filter this array out again:

```
$res = array_map(function($log) use($nfthistory){

        $sum = array_sum(array_map('hexdec',$log));

                $nftSumPrice = $nfthistory->weiToEther($sum);

                return $nftSumPrice;

},$filterData);
```
In the above code, we sum the prices of every token id that includes more than one price. Then, using the weiToEther() function, we convert the price in WEI format to Ether format and finally return the result.

The output must look like this:

```
// output:

array(82) {
  [19400010233]=>
  string(4) '0.00'
  [19400010322]=>
  string(4) '0.00'
  [19400010042]=>
  string(4) '0.00'
 [19400010113]=>
  string(4) '0.00'
  [19400010074]=>
  string(4) '0.08'
}

```
Now we have the sum prices as well as the other ones, which are NULL. We could remove NULL values, but I intentionally kept them to make the comparison better.

Sort the array in descending order like this:

```
arsort($res);
```
Then the output be like this:

```
array(82) {
  [19400010142]=>
  string(4) '0.40'
  [19400010060]=>
  string(4) '0.25'
  [19400010127]=>
  string(4) '0.21'
  [19400010159]=>
  string(4) '0.19'
  [19400010303]=>
  string(4) '0.15'
  [19400010055]=>
  string(4) '0.12'
  [19400010429]=>
  string(4) '0.11'
  [19400010236]=>
  string(4) '0.11'
  [19400010400]=>
  string(4) '0.10'
 [19400010136]=>
 string(4) '0.09'
}

```
The above output is the list of the top 10 selling NFTs. in order to be sure about these numbers, first go to this page to search for the first token id (19400010142). Put this number in the search section to access the transaction list of this token id.

![Transfer Trxs of a token id](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmxn33im8nzdl3aah7lp.jpg)

As you can see, this token ID is transferred twice. If you click on every transaction hash and go to the “value” section, you can see the price of the trade. The first one is 0.4 Ether, and the second one is 0.


I will try to add these functions as built-in functions to the library. At this moment, you can use it as is.


The rest of the functions are here.

If you have any questions, I will respond to you in the comment section. ^__^