nadcab-labs-crypto-api
v1.0.6
Published
A unified JavaScript library to interact with Ethereum-compatible blockchains and Tron using a simplified API.
Downloads
30
Maintainers
Readme
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.
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
API Key Creation
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, Alchemy, or QuickNode.
- 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 initializingNC3
for Ethereum, BSC, or Polygon:
const nc3Ethereum = new NC3('ethereum', 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
Tron:
Use TronGrid 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 initializingNC3
for Tron: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:
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:
# 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:
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:
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:
# 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
Nadcab Labs Crypto Api exposes four public methods:
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 likefrom
,to
,value
,gas
, andgasPrice
for Ethereum-compatible chains. - For Tron,
txData
includes fields likefrom
,to
, andamount
.
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 acallback
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 aproviderUrl
.
getTransactionReceipt(txHash)
:- Fetches the transaction receipt for a specific transaction hash (
txHash
). - Useful for checking the status of a transaction.
- Fetches the transaction receipt for a specific transaction hash (
createAccount()
:- Generates a new account with a private key and address.
- Primarily used for generating new wallets on the supported blockchains.
License
This package is MIT licensed. (c) Nadcab Labs 2023.