Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

web3-eth-contract

Package Overview
Dependencies
Maintainers
4
Versions
477
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web3-eth-contract

Web3 module to interact with Ethereum smart contracts.

  • 4.3.1-dev.5a579fa.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
438K
increased by0.12%
Maintainers
4
Weekly downloads
 
Created

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

FAQs

Package last updated on 05 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc