Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
numairawan committed Oct 3, 2023
1 parent fee6853 commit 1a44d8e
Show file tree
Hide file tree
Showing 6 changed files with 485 additions and 2 deletions.
59 changes: 57 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
# ethplorer-api-node

<p align="center">
<img src="https://raw.githubusercontent.com/NumairAwan/ethplorer-api-node/main/art/ethplorer-api-node.png" width="600" alt="ethplorer-api-node">
<p align="center">
<a href="https://github.com/NumairAwan/ethplorer-api-node"><img alt="Total Hits" src="https://hits.dwyl.com/NumairAwan/ethplorer-api-node.svg?style=flat-square"></a>
<a href="https://github.com/NumairAwan/ethplorer-api-node"><img alt="Downloads" src="https://img.shields.io/npm/dt/ethplorer-api-node"></a>
<a href="https://www.npmjs.com/package/ethplorer-api-node"><img alt="Version" src="https://img.shields.io/npm/v/ethplorer-api-node?logo=npm&style=flat-square"></a>
<a href="https://github.com/NumairAwan/ethplorer-api-node"><img alt="License" src="https://img.shields.io/github/license/numairawan/ethplorer-api-node"></a>
</p>
</p>

------
**ethplorer-api-node** is a powerful nodejs module for Ethplorer.io API. This feature-rich library not only provides multi-key support to effortlessly bypass rate limits but also offers blazing-fast performance, two key usage modes (sequential and random), and extensive promise support for smooth integration into your NodeJs projects.

🌟 Key Features:
- 🔄 Multi-Key Support: Seamlessly rotate API keys to bypass rate limits.
- ⚡️ High Performance: Enjoy super-fast access to ethereum blockchain data.
- 🎯 Two Key Usage Modes: Choose between sequential and random key usage.
- ✨ Promise Support: Simplify asynchronous operations with built-in promises.

## 📦 Install

```sh
npm i ethplorer-api-node
```

### Usage
For more sample please check test.js

```js
const EthplorerApi = require('ethplorer-api-node');

// Create an instance of EthplorerApi with your API keys
const apiKeys = ['your-api-key-1', 'your-api-key-2'];
const ethplorer = new EthplorerApi(apiKeys, true); // Use 'true' for random key mode or 'false' for sequential key mode

// Example: Get information about the last block
ethplorer.getLastBlock().then((lastBlock) => {

console.log('Last Block Information:');
console.log(lastBlock);
})
```

### Contributing
Contributions are welcome! Feel free to fork the repository and submit pull requests as well.

### License
This project is licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.


## Connect with Me

Feel free to reach out to me for any project-related queries or collaborations. I'm always happy to connect and discuss ideas!

[<img align="left" alt="Telegram" width="32px" src="https://upload.wikimedia.org/wikipedia/commons/8/82/Telegram_logo.svg" />](https://t.me/NumairAwan)
[<img align="left" alt="WhatsApp" width="32px" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/WhatsApp.svg/512px-WhatsApp.svg.png?20220228223904" />](https://wa.me/+923164700904)

Binary file added art/ethplorer-api-node.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
209 changes: 209 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
const axios = require("axios");

class EthplorerApi {
/**
* Initializes the Ethplorer API client.
* @param {string[] | string} apiKeys - An array of API keys or a single API key string.
* @param {boolean} randomKeys - Whether to use API keys randomly or sequentially.
*/
constructor(apiKeys, randomKeys = false) {
this.randomKeys = randomKeys;
this.apiAddress = "https://api.ethplorer.io/";

if (!apiKeys) {
throw new Error("API keys are required.");
}

if (typeof apiKeys === "string") {
this.apiKeys = [apiKeys];
} else if (Array.isArray(apiKeys)) {
this.apiKeys = apiKeys;
} else {
throw new Error("Invalid API keys format.");
}

this.apiKeyIndex = 0; // Index for sequential API key usage.
}

/**
* Makes an Axios request to the specified URL.
* @param {string} url - The URL to make the request to.
* @returns {Promise<any>} - A promise that resolves to the response data.
*/
async axiosRequest(url) {
try {
const response = await axios.get(url, {
headers: {
Accept: "application/json",
"User-Agent": "Googlebot/2.1 (+http://www.google.com/bot.html)",
},
timeout: 10000,
});
return response.data;
} catch (error) {
throw error;
}
}

/**
* Retrieves the next API key for sequential usage.
* @returns {string} - The next API key in sequence.
*/
getNextApiKey() {
if (this.randomKeys) {
return this.getRandomApiKey();
}

const apiKey = this.apiKeys[this.apiKeyIndex];
this.apiKeyIndex = (this.apiKeyIndex + 1) % this.apiKeys.length;
return apiKey;
}

/**
* Get random api keys.
* @returns {string} - Random key.
*/
getRandomApiKey() {
return this.apiKeys[Math.floor(Math.random() * this.apiKeys.length)];
}

/**
* Retrieves the last block information.
* @returns {Promise<any>} - A promise that resolves to the last block information.
*/
getLastBlock() {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getLastBlock?apiKey=${apiKey}`;
return this.axiosRequest(url);
}

/**
* Retrieves token information by its address.
* @param {string} token - The token address.
* @returns {Promise<any>} - A promise that resolves to token information.
*/
getTokenInfo(token) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTokenInfo/${token}?apiKey=${apiKey}`;
return this.axiosRequest(url);
}

/**
* Retrieves address information by its address.
* @param {string} address - The Ethereum address.
* @returns {Promise<any>} - A promise that resolves to address information.
*/
getAddressInfo(address) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getAddressInfo/${address}?apiKey=${apiKey}`;
return this.axiosRequest(url);
}

/**
* Retrieves transaction information by its hash.
* @param {string} tx - The transaction hash.
* @returns {Promise<any>} - A promise that resolves to transaction information.
*/
getTxInfo(tx) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTxInfo/${tx}?apiKey=${apiKey}`;
return this.axiosRequest(url);
}

/**
* Retrieves token history by its address.
* @param {string} token - The token address.
* @param {string} type - The type of history (default: 'transfer').
* @param {number} limit - The maximum number of records to retrieve (default: 10).
* @returns {Promise<any>} - A promise that resolves to token history.
*/
getTokenHistory(token, type = "transfer", limit = 10) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTokenHistory/${token}?apiKey=${apiKey}&type=${type}&limit=${limit}`;
return this.axiosRequest(url);
}

/**
* Retrieves address history by its address.
* @param {string} token - The Ethereum address.
* @param {string} type - The type of history (default: 'transfer').
* @param {number} limit - The maximum number of records to retrieve (default: 10).
* @returns {Promise<any>} - A promise that resolves to address history.
*/
getAddressHistory(token, type = "transfer", limit = 10) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getAddressHistory/${token}?apiKey=${apiKey}&type=${type}&limit=${limit}`;
return this.axiosRequest(url);
}

/**
* Retrieves address transactions by its address.
* @param {string} token - The Ethereum address.
* @param {number} limit - The maximum number of records to retrieve (default: 10).
* @returns {Promise<any>} - A promise that resolves to address transactions.
*/
getAddressTransactions(token, limit = 10) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getAddressTransactions/${token}?apiKey=${apiKey}&limit=${limit}`;
return this.axiosRequest(url);
}

/**
* Retrieves top tokens based on criteria.
* @param {string} criteria - The criteria for ranking (default: 'cap').
* @param {number} limit - The maximum number of records to retrieve (default: 50).
* @returns {Promise<any>} - A promise that resolves to top tokens.
*/
getTop(criteria = "cap", limit = 50) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTop?apiKey=${apiKey}&criteria=${criteria}&limit=${limit}`;
return this.axiosRequest(url);
}

/**
* Retrieves top tokens.
* @returns {Promise<any>} - A promise that resolves to top tokens.
*/
getTopTokens() {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTopTokens/?apiKey=${apiKey}`;
return this.axiosRequest(url);
}

/**
* Retrieves top token holders by token address.
* @param {string} token - The token address.
* @param {number} limit - The maximum number of records to retrieve (default: 100).
* @returns {Promise<any>} - A promise that resolves to top token holders.
*/
getTopTokenHolders(token, limit = 100) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTopTokenHolders/${token}?apiKey=${apiKey}&limit=${limit}`;
return this.axiosRequest(url);
}

/**
* Retrieves token history grouped by day by token address.
* @param {string} token - The token address.
* @returns {Promise<any>} - A promise that resolves to token history grouped by day.
*/
getTokenHistoryGrouped(token) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTokenHistoryGrouped/${token}?apiKey=${apiKey}`;
return this.axiosRequest(url);
}

/**
* Retrieves token price history grouped by day by token address.
* @param {string} token - The token address.
* @param {number} period - The period in days for price history (default: 365).
* @returns {Promise<any>} - A promise that resolves to token price history grouped by day.
*/
getTokenPriceHistoryGrouped(token, period = 365) {
const apiKey = this.getNextApiKey();
const url = `${this.apiAddress}getTokenPriceHistoryGrouped/${token}?apiKey=${apiKey}&period=${period}`;
return this.axiosRequest(url);
}
}

module.exports = EthplorerApi;
Loading

0 comments on commit 1a44d8e

Please sign in to comment.