Nadcab Labs Crypto API
Nadcab Labs Crypto API is a unified JavaScript library designed to simplify the interaction with multiple blockchain networks, including Ethereum-compatible blockchains (Ethereum, Binance Smart Chain, Polygon) and Tron. By combining the capabilities of web3
and tronweb
into a single, cohesive API—called nc3.js
—this library provides a streamlined and consistent interface for developers to work with these diverse blockchain ecosystems.
What is Crypto API?
A Crypto API is a set of programming interfaces and tools that allow developers to interact with blockchain networks and cryptocurrencies programmatically. It provides the necessary functions to perform blockchain operations such as generating wallet addresses, sending and receiving transactions, checking balances, and interacting with smart contracts.
Nadcab Labs Crypto API is an example of such a library that merges the functionality of different blockchain APIs—like web3
for Ethereum-compatible blockchains and tronweb
for Tron—into a single, unified interface (nc3.js
). This unified API simplifies the development process by providing a consistent and simplified way to interact with multiple blockchains, reducing the need for developers to learn and manage multiple libraries and interfaces.
Installation and Usage
To use the Nadcab Labs Crypto API, you need to install it via npm. Run the following command in your project directory:
npm install nadcab-labs-crypto-api
If the **Nadcab Labs Crypto API** requires an API key to interact with blockchain networks or third-party services, follow these steps to create and use an API key:
- **Ethereum, BSC, and Polygon (EVM-Compatible Chains):**
- Sign up for an account with a blockchain node provider like [Infura](https://infura.io/), [Alchemy](https://www.alchemy.com/), or [QuickNode](https://www.quicknode.com/).
- Create a new project in the provider's dashboard.
- Generate an API key for your project. This key will be used to interact with the blockchain network.
- Copy the generated API key and use it as the `providerUrl` when initializing `NC3` for Ethereum, BSC, or Polygon:
```bash
const nc3Ethereum = new NC3('ethereum', 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
```
- **Tron:**
- Use [TronGrid](https://www.trongrid.io/) or any other Tron-compatible provider.
- Sign up and create a new project to generate an API key.
- Use the API key in the `providerUrl` when initializing `NC3` for Tron:
```bash
const nc3Tron = new NC3('tron', 'https://api.trongrid.io/YOUR_TRONGRID_API_KEY');
```
### Create Address
To generate a blockchain address for different supported networks using the **Nadcab Labs Crypto API**:
- **Ethereum, BSC, and Polygon (EVM Chains):**
- Provide a private key to generate an address:
```bash
const NC3 = require('nadcab-labs-crypto-api');
# Initialize for Ethereum
const nc3Ethereum = new NC3('ethereum', 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
# Generate an Ethereum address using a private key
const ethAddress = nc3Ethereum.generateAddress('your-private-key');
console.log('Ethereum Address:', ethAddress);
```
- **Tron:**
- Simply call the method to generate a new address:
```bash
# Initialize for Tron
const nc3Tron = new NC3('tron', 'https://api.trongrid.io');
# Generate a Tron address
const tronAddress = nc3Tron.generateAddress();
console.log('Tron Address:', tronAddress);
```
### Send Transaction
To send a transaction on different supported blockchains:
- **Ethereum, BSC, and Polygon (EVM Chains):**
- Provide the necessary transaction details:
```bash
nc3Ethereum.sendTransaction({
from: 'your-eth-address',
to: 'recipient-eth-address',
value: nc3Ethereum.web3.utils.toWei('0.01', 'ether'), # Send 0.01 ETH
gas: 21000,
gasPrice: nc3Ethereum.web3.utils.toWei('20', 'gwei'), # Gas price in Gwei
}).then((receipt) => {
console.log('Ethereum Transaction Receipt:', receipt);
}).catch(console.error);
```
- **Tron:**
- Use similar transaction details for Tron:
```bash
nc3Tron.sendTransaction({
from: 'your-tron-address',
to: 'recipient-tron-address',
amount: 100, # Amount in TRX
}).then((receipt) => {
console.log('Tron Transaction Receipt:', receipt);
}).catch(console.error);
```
### Receive Transactions via Webhook
To set up a webhook server for receiving transaction notifications in real-time:
- **Ethereum, BSC, Polygon, or Tron:**
- Start a webhook server by specifying a port and a callback function to handle the data:
```bash
# Start a webhook server for Ethereum
nc3Ethereum.startWebhookServer(3000, (data) => {
console.log('Received Ethereum Transaction Data:', data);
});
# Start a webhook server for Tron
nc3Tron.startWebhookServer(3001, (data) => {
console.log('Received Tron Transaction Data:', data);
});
```
- The webhook server will listen for incoming transactions on the specified port and execute the provided callback function whenever a transaction is detected.
### Functions
- **`generateAddress(privateKey)`**:
- Generates a new blockchain address.
- For Ethereum-compatible chains (Ethereum, BSC, Polygon), requires a `privateKey`.
- For Tron, the `privateKey` is not required.
- **`sendTransaction(txData)`**:
- Sends a transaction to the specified blockchain network.
- Accepts `txData` object with fields like `from`, `to`, `value`, `gas`, and `gasPrice` for Ethereum-compatible chains.
- For Tron, `txData` includes fields like `from`, `to`, and `amount`.
- **`getBalance(address)`**:
- Retrieves the balance of a specific address.
- Accepts the `address` parameter and returns the balance in the smallest unit (Wei for Ethereum, SUN for Tron).
- **`startWebhookServer(port, callback)`**:
- Sets up a webhook server to listen for incoming transactions.
- Accepts a `port` number and a `callback` function to handle incoming transaction data.
- **`setProviderUrl(providerUrl)`**:
- Sets or updates the provider URL for connecting to the blockchain network.
- Accepts a `providerUrl` string (e.g., Infura, Alchemy, TronGrid).
- **`nc3.js` Initialization**:
- Initializes the library for a specific blockchain network (Ethereum, BSC, Polygon, or Tron).
- Requires a `chain` parameter (e.g., 'ethereum', 'tron') and a `providerUrl`.
- **`getTransactionReceipt(txHash)`**:
- Fetches the transaction receipt for a specific transaction hash (`txHash`).
- Useful for checking the status of a transaction.
- **`createAccount()`**:
- Generates a new account with a private key and address.
- Primarily used for generating new wallets on the supported blockchains.
### Licensed
This package is MIT licensed. (c) Nadcab Labs 2024.
### Authors
- [@amanvaths](https://www.linkedin.com/in/aman-vaths) - Aman Vaths
- [@nadcablabs](https://www.nadcab.com) - Nadcab Labs
### Keywords
- nadcab
- nadcablabs
- cryptoapi
- tronapi
- eth-address
- get-txn
- crypto-txn-api