Method Signature
align.blockchain.providers.getProvider(
network: Network
): JsonRpcProvider
Parameters
Network: ethereum, polygon, base, arbitrum, optimism
Returns
Returns an ethers.js JsonRpcProvider instance.
Examples
import Align from "@tolbel/align";
const align = new Align({
apiKey: process.env.ALIGN_API_KEY!,
environment: "sandbox",
});
const provider = align.blockchain.providers.getProvider("polygon");
// Get current block number
const blockNumber = await provider.getBlockNumber();
console.log(`Current block: ${blockNumber}`);
// Get gas price
const feeData = await provider.getFeeData();
console.log(`Gas price: ${feeData.gasPrice?.toString()}`);
const provider = align.blockchain.providers.getProvider("polygon");
const block = await provider.getBlockNumber();
console.log("Block:", block);
Direct Contract Interaction
import { Contract } from "ethers";
const provider = align.blockchain.providers.getProvider("polygon");
const contract = new Contract(
"0xContractAddress...",
["function totalSupply() view returns (uint256)"],
provider
);
const supply = await contract.totalSupply();
console.log(`Total supply: ${supply}`);
Providers are cached internally. Multiple calls to getProvider() with the
same network return the same instance.