chainstack-covalent-sdk
v1.0.8
Published
An SDK designed to streamline the process of developing using the integration of Chainstack with Covalent APIs, enhancing the ease of use and efficiency
Downloads
20
Maintainers
Readme
Chainstack Covalent API JavaScript SDK
A JavaScript SDK designed to streamline the process of developing using the integration of Chainstack with Covalent APIs, enhancing the ease of use and efficiency.
Quickstart
Install the Chainstack-Covalent integration from your Chainstack console.
Get a Chainstack API key.
In your project's directory, install the Chainstack SDK:
npm i chainstack-covalent-sdk
- In a new file, import the Chainstack SDK and use your Chainstack API key in the constructor:
const {ChainstackApi} = require("chainstack-covalent-sdk")
const CHAINSTACK_API_KEY = 'YOUR_CHAINSTACK_API_KEY'
const chainstack = new ChainstackApi(CHAINSTACK_API_KEY);
(Optional but recommended) — The Chainstack-Covalent SDK comes with the
dotenv
package included, so you can use a.env
file to import the Chainstack API key:- Create a
.env
file with your Chainstack API key:
CHAINSTACK_API_KEY="YOUR_CHAINSTACK_API_KEY"
- Import it in your project:
const {ChainstackApi} = require("chainstack-covalent-sdk") const CHAINSTACK_API_KEY = process.env.CHAINSTACK_API_KEY const chainstack = new ChainstackApi(CHAINSTACK_API_KEY);
- Create a
Usage
The Chainstack-Covalent SDK has many endpoints to get all sorts of data, from smart contract deployments to NFT data. Each endpoint takes an object as a parameter to configure the call to the Covalent API.
Fetch token balances
This endpoint fetches all the token balances from the specified address on the selected chain:
const {ChainstackApi} = require("chainstack-covalent-sdk")
const CHAINSTACK_API_KEY = process.env.CHAINSTACK_API_KEY
const chainstack = new ChainstackApi(CHAINSTACK_API_KEY);
// Config parameters
const parameters = {
chainName: 'eth-mainnet',
walletAddress: '0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13',
currency: 'USD',
nft: true,
noNftFetch: false,
noSpam: true,
};
chainstack.fetchTokenBalances(parameters)
.then(data => console.log(data))
.catch(error => console.error(error));
Explain the parameter object
The parameters object allows for fine tuning of your request, here is the explanation about the configuration parameters for fetchTokenBalances
:
chainName (string, required): This parameter represents the name of the blockchain network you're interested in. For example, 'eth-mainnet' for the Ethereum mainnet. Find a complete list on the Covalent docs.
const chainName = 'eth-mainnet';
walletAddress (string, required): This is the address of the wallet for which you want to fetch token balances. If you pass in an Ethereum Name Service (ENS) address or a RSK Name Service (RNS) address, it will be automatically resolved to the corresponding wallet address.
const walletAddress = '0xae2Fc483527B8EF99EB5D9B44875F005ba1FaE13';
quote-currency (string, optional): This is the currency in which you want the balances to be quoted, such as 'USD' for United States Dollars. If not provided, the balances may be returned in the native currency of the blockchain network.
const quoteCurrency = 'USD';
nft (boolean, optional): If set to
true
, Non-Fungible Tokens (NFTs) will be included in the response along with fungible tokens. Iffalse
or not provided, NFTs will not be included.const nft = true;
no-nft-fetch (boolean, optional): If set to
true
, the response will only include NFTs that have been cached. This can make the response faster, as it avoids having to fetch data about NFTs from the blockchain. Iffalse
or not provided, all NFTs will be included, regardless of whether they are cached.const noNftFetch = false;
no-spam (boolean, optional): If set to
true
, any tokens that are suspected to be spam will be excluded from the response. This currently supports 'eth-mainnet' and 'matic-mainnet'. Iffalse
or not provided, all tokens, including potential spam, will be included.const noSpam = true;
Remember, this function returns a promise. Handle this promise appropriately in your code, either by using .then()
and .catch()
methods, or by using the await
keyword inside an async function with a try-catch block.
Work in progress
This tool and its documentation is work in progress, we will be adding a complete list of endpoints available soon.