What is web3-eth-contract?
The web3-eth-contract package is part of the Web3.js library and is used to interact with Ethereum smart contracts. It allows you to deploy, call, and listen to smart contract events on the Ethereum blockchain.
What are web3-eth-contract's main functionalities?
Deploying a Contract
This feature allows you to deploy a new smart contract to the Ethereum blockchain. The code sample demonstrates how to use the web3-eth-contract package to deploy a contract using its ABI and bytecode.
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const contractABI = [ /* ABI array */ ];
const contractBytecode = '0x...';
const deployContract = async () => {
const accounts = await web3.eth.getAccounts();
const contract = new web3.eth.Contract(contractABI);
const deployedContract = await contract.deploy({ data: contractBytecode }).send({ from: accounts[0], gas: 1500000, gasPrice: '30000000000000' });
console.log('Contract deployed at address:', deployedContract.options.address);
};
deployContract();
Calling a Contract Method
This feature allows you to call a method on an already deployed smart contract. The code sample demonstrates how to call a method named 'myMethod' on a contract using its ABI and address.
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const contractABI = [ /* ABI array */ ];
const contractAddress = '0x...';
const contract = new web3.eth.Contract(contractABI, contractAddress);
const callMethod = async () => {
const result = await contract.methods.myMethod().call();
console.log('Method call result:', result);
};
callMethod();
Listening to Contract Events
This feature allows you to listen for events emitted by a smart contract. The code sample demonstrates how to listen for an event named 'MyEvent' from a contract using its ABI and address.
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const contractABI = [ /* ABI array */ ];
const contractAddress = '0x...';
const contract = new web3.eth.Contract(contractABI, contractAddress);
contract.events.MyEvent({
fromBlock: 0
}, (error, event) => {
if (error) {
console.error('Error:', error);
} else {
console.log('Event:', event);
}
});
Other packages similar to web3-eth-contract
ethers
The ethers.js library is a complete and compact library for interacting with the Ethereum blockchain and its ecosystem. It provides similar functionalities to web3-eth-contract, such as deploying contracts, calling contract methods, and listening to events. Ethers.js is known for its simplicity and ease of use.
truffle-contract
Truffle Contract is a part of the Truffle Suite and provides a better abstraction for interacting with Ethereum smart contracts. It offers similar functionalities to web3-eth-contract, including contract deployment, method calls, and event listening. Truffle Contract is tightly integrated with the Truffle framework, making it a good choice for developers using Truffle for their Ethereum development.
embark
Embark is a framework for developing and deploying decentralized applications (dApps) that integrates with Ethereum smart contracts. It provides functionalities similar to web3-eth-contract, such as contract deployment, method calls, and event listening. Embark is known for its powerful features and ease of use, especially for building complex dApps.
web3.js - Eth Contract Package
This is a sub-package of web3.js.
web3-eth-contract
contains the contract package used in web3-eth
.
Installation
You can install the package either using NPM or using Yarn
Using NPM
npm install web3-eth-contract
Using Yarn
yarn add web3-eth-contract
Getting Started
Prerequisites
Usage
You can initialize the typesafe Contract API instance with the following.
import { Contract } from 'web3-eth-contract';
const abi = [...] as const;
const contract = new Contract(abi);
- We prefer that you use
web3.eth.Contract
API in normal usage. - The use of
as const
is necessary to have fully type-safe interface for the contract. - As the ABIs are not extensive in size, we suggest declaring them
as const
in your TS project. - This approach is more flexible and seamless compared to other approaches of off-line compiling ABIs to TS interfaces (such as TypeChain.
Compatibility
We have tested the Typescript interface support for the ABIs compiled with solidity version v0.4.x
and above. If you face any issue regarding the contract typing, please create an issue to report to us.
The Typescript support for fixed length array types are supported up 30 elements. See more details here. This limitation is only to provide more performant developer experience in IDEs. In future we may come up with a workaround to avoid this limitation. If you have any idea feel free to share.
Package.json Scripts
Script | Description |
---|
clean | Uses rimraf to remove dist/ |
build | Uses tsc to build package and dependent packages |
lint | Uses eslint to lint package |
lint:fix | Uses eslint to check and fix any warnings |
format | Uses prettier to format the code |
test | Uses jest to run unit tests |
test:integration | Uses jest to run tests under /test/integration |
test:unit | Uses jest to run tests under /test/unit |