JS SDK
Overview:
JavaScript SDKs provide easy-to-use interfaces for interacting with EVM (Ethereum Virtual Machine) compatible blockchains like X1EcoChain, Ethereum, Polygon, BSC, Avalanche, and others.
If you want to use JavaScript to connect with a blockchain node, it's possible to use vanilla JavaScript but several convenience libraries exist within the ecosystem that make this much easier. With these libraries, developers can write intuitive, one-line methods to initialize JSON-RPC requests (under the hood) that interact with blockchain.
Note: This guide contains basic information about libraries and it's not a full guide. More info about libraries could be found in their official documentation.
Main Libraries:
Ethers.js (Recommended)
Modern, lightweight library with TypeScript support
Promise-based API with async/await support
Built-in wallet management and contract interaction
Web3.js
Original Ethereum JavaScript library
Extensive ecosystem and community support
Callback and promise-based APIs
Quick Example:
Here's a simple example using Ethers.js to check an account balance:
import { ethers } from 'ethers';
// Connect to network (X1EcoChain Nubica Testnet)
const provider = new ethers.JsonRpcProvider('https://nubica-rpc.x1eco.com');
// Check X1 balance
async function getBalance() {
const address = '<some address>';
const balance = await provider.getBalance(address);
// Convert from wei to X1
const ethBalance = ethers.formatEther(balance);
console.log(`Balance: ${ethBalance} X1`);
}
getBalance();
What's next?
Authorize wallet and try to interact with smart contract
Handle transactions and events
Implement error handling
Additional Links:
Last updated