DEV Community

Cover image for How to Rank Top-Selling NFTs Using PHP
xmrrabbitx
xmrrabbitx

Posted on • Updated on

How to Rank Top-Selling NFTs Using PHP

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
Enter fullscreen mode Exit fullscreen mode

Then import it like this:

<?php

require 'vendor/autoload.php'

use Nft\History\nftHistory;
Enter fullscreen mode Exit fullscreen mode

Then, insert the contract address and set the provider:

$contractAddress = '0x00B3e138c6e4b233e5DDed8CfeD200f0c82B536c';
$provider = 'https://cloudflare-eth.com';
Enter fullscreen mode Exit fullscreen mode

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
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);
Enter fullscreen mode Exit fullscreen mode

Next, call the getAllTransferTrxHashAndIds() function:

$trxHash = $nfthistory->getAllTransferTrxHashAndIds();
Enter fullscreen mode Exit fullscreen mode

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'

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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
  }
}

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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'
}

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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'
}

Enter fullscreen mode Exit fullscreen mode

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

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. ^__^

Top comments (0)