DEV Community

Cover image for Retrieve Top 10 selling Nfts Using PHP and Infura
xmrrabbitx
xmrrabbitx

Posted on

Retrieve Top 10 selling Nfts Using PHP and Infura

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

infura signup

then create a new API key:

infura API key

Choose Web3 API and name your project:

create api key infura

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

infura endpoint

Now it's time to use the real magic. Install the latest version of the NFT history logs library:

composer require nfthistory/nfthistorylogs dev-master
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 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>';
Enter fullscreen mode Exit fullscreen mode

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

$nfthistory = new nftHistory($contractAdress, $provider);
Enter fullscreen mode Exit fullscreen mode

Now call the topSellNfts() function to retrieve as many top-selling nft tokens as you want:

$NftHistory->topSellNfts("multiThread", 10);
Enter fullscreen mode Exit fullscreen mode

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

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

I hope this is helpful to you. ^__^

Top comments (0)