Buidler plugin that integrates ethers.js into the Buidler development environment
npx @tessl/cli install tessl/npm-nomiclabs--buidler-ethers@1.0.0The @nomiclabs/buidler-ethers plugin integrates ethers.js into the Buidler development environment, providing convenient access to blockchain interaction functionality through the Buidler Runtime Environment.
npm install --save-dev @nomiclabs/buidler-ethers ethers@^4.0.23// Plugin activation in buidler.config.js
usePlugin("@nomiclabs/buidler-ethers");The plugin extends the Buidler Runtime Environment and does not export functions directly. Access is through the BRE:
// In tasks, tests, or scripts
const { ethers } = require("@nomiclabs/buidler");
// or in TypeScript with proper types
import { BuidlerRuntimeEnvironment } from "@nomiclabs/buidler/types";usePlugin("@nomiclabs/buidler-ethers");
// Use in a task
task("example", "Example task", async (_, { ethers }) => {
// Get current block number
const blockNumber = await ethers.provider.getBlockNumber();
console.log("Current block:", blockNumber);
// Get signers
const signers = await ethers.signers();
console.log("First signer address:", await signers[0].getAddress());
// Get a contract factory
const Greeter = await ethers.getContract("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
});The plugin uses a lazy initialization pattern to extend the Buidler Runtime Environment with three key components:
EthersProviderWrapper adapts Buidler's IEthereumProvider to ethers' JsonRpcProvider interfaceSigner instancesThe integration is seamless and requires no additional setup beyond plugin activation.
Access to an ethers-compatible JSON-RPC provider that wraps Buidler's network provider.
ethers.provider: EthersProviderWrapperThe provider is an instance of EthersProviderWrapper that extends JsonRpcProvider and provides all standard ethers provider functionality including:
send(method: string, params: any): Promise<any> - Send JSON-RPC requestsgetBlockNumber(): Promise<number> - Get current block numberlistAccounts(): Promise<string[]> - Get available accountsgetSigner(account: string): Signer - Get signer for specific accountJsonRpcProvider methodsCreate ethers ContractFactory instances from compiled contract artifacts.
ethers.getContract: (name: string) => Promise<ContractFactory>Parameters:
name (string): Name of the contract artifact (without .sol extension)Returns:
Promise<ContractFactory>: ContractFactory instance connected to the first available signerUsage Example:
// Assuming you have a Greeter.sol contract compiled in artifacts/
const Greeter = await ethers.getContract("Greeter");
// Deploy the contract
const greeter = await Greeter.deploy("Hello, Buidler!");
await greeter.deployed();
// Call contract methods
const greeting = await greeter.greet();
console.log(greeting);Get array of ethers Signer instances for all available accounts.
ethers.signers: () => Promise<Signer[]>Returns:
Promise<Signer[]>: Array of ethers Signer instances for each account returned by the providerUsage Example:
const signers = await ethers.signers();
// Get the first signer's address
const address = await signers[0].getAddress();
console.log("Deployer address:", address);
// Get balance
const balance = await signers[0].getBalance();
console.log("Balance:", ethers.utils.formatEther(balance), "ETH");class EthersProviderWrapper extends JsonRpcProvider {
constructor(buidlerProvider: IEthereumProvider);
send(method: string, params: any): Promise<any>;
}Internal wrapper class that adapts Buidler's provider to ethers' JsonRpcProvider interface. Emits debug events following ethers conventions.
interface BuidlerRuntimeEnvironment {
ethers: {
provider: JsonRpcProvider;
getContract: (name: string) => Promise<ContractFactory>;
signers: () => Promise<Signer[]>;
};
}The plugin extends the Buidler Runtime Environment with the ethers property containing the three core functions.
For TypeScript projects, include the type extensions in your tsconfig.json:
{
"files": [
"node_modules/@nomiclabs/buidler-ethers/src/type-extensions.d.ts"
]
}This provides full type safety for the extended BRE interface, including autocompletion and type checking for the ethers property.
The plugin follows ethers.js conventions for error handling:
Debug information is emitted for all JSON-RPC requests through the provider's debug events.