Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
web3-eth-contract
Advanced tools
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.
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);
}
});
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 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 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.
This is a sub-package of web3.js.
This is the contract package used in the web3-eth
package.
Please read the documentation for more.
npm install web3-eth-contract
const Web3EthContract = require('web3-eth-contract');
// Set provider for all later instances to use
Web3EthContract.setProvider('ws://localhost:8546');
const contract = new Web3EthContract(jsonInterface, address);
contract.methods.somFunc().send({from: ....})
.on('receipt', function(){
...
});
All the TypeScript typings are placed in the types
folder.
FAQs
Web3 module to interact with Ethereum smart contracts.
The npm package web3-eth-contract receives a total of 167,653 weekly downloads. As such, web3-eth-contract popularity was classified as popular.
We found that web3-eth-contract demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.