How to Interact with Smart Contracts
This guide provides examples of how to interact with the Noderr Protocol's smart contracts using JavaScript and Ethers.js v6.
Note: The Noderr dApp uses ethers.js v6. The v5 API (
ethers.providers.*,ethers.utils.*) is not compatible. Use the v6 API shown below.
Setup
import { ethers } from'ethers'; // ethers ^6.x// Read-only providerconst provider = new ethers.JsonRpcProvider('https://sepolia.base.org');
const vaultAbi = [...]; // Vault ABIconst vaultAddress = '0x...'; // Vault addressconst vaultContract = new ethers.Contract(vaultAddress, vaultAbi, provider);
Reading Data
asyncfunctiongetVaultBalance(userAddress) {
const balance = await vaultContract.balanceOf(userAddress);
// ethers v6: formatUnits is a top-level function, not ethers.utils.formatUnitsconsole.log('Vault Balance:', ethers.formatUnits(balance, 18));
}
Sending Transactions
asyncfunctiondepositToVault(amount) {
// ethers v6: BrowserProvider replaces Web3Provider; getSigner() is asyncconst browserProvider = new ethers.BrowserProvider(window.ethereum);
const signer = await browserProvider.getSigner();
const vaultWithSigner = vaultContract.connect(signer);
// ethers v6: parseUnits is a top-level function, not ethers.utils.parseUnitsconst tx = await vaultWithSigner.deposit(ethers.parseUnits(amount, 18));
await tx.wait();
console.log('Deposit successful!');
}