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:
- sign up on the infura website
- setup the provider for the nft history logs library
- 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:
then create a new API key:
Choose Web3 API and name your project:
Copy the url of Ethereum's mainnet. Do not share the API key with anyone.
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
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
I hope this is helpful to you. ^__^
Top comments (0)