中文 / English
With example codes shown below, developers can learn the difference between web3.js
and ethers.js
This project mainly focus on tasks including connect to ethereum
, get accounts
, deploy the contract
, call methods of the contract
.
- Install dependencies
yarn install
- Run compile.js script
yarn compile
- Start local test network
yarn chain
- Run the test script
yarn test
// web3.js
const web3 = new Web3('http://localhost:8545');
// or
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// ethers
const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545');
const signer = provider.getSigner();
// ethers
const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545');
const accounts1 = await provider.listAccounts();
// web3.js
const web3 = new Web3('http://127.0.0.1:8545');
const accounts2 = await web3.eth.getAccounts();
// using ethers
// deploying contract with ethers needs abi, bytecode, signer.
const factory = new ethers.ContractFactory(abi, bytecode, signer);
const contractInstance = await factory.deploy(0);
const tx = await contractInstance.deployTransaction.wait();
console.log(tx);
console.log('Contract deployed at address:', contractInstance.address);
// using web3
const accounts = await web3.eth.getAccounts();
const contract = new web3.eth.Contract(abi);
const tx = contract.deploy({
data: bytecode,
arguments: [0],
});
// const deployReceipt = tx.send({
// from: accounts[0],
// gas: gasLimit,
// gasPrice,
// });
// deployReceipt.on('receipt', function (receipt) {
// console.log(`Contract deployed at address: ${receipt.contractAddress}`);
// });
const contractInstance = await tx.send({
from: accounts[0],
gas: gasLimit,
gasPrice,
});
console.log('Contract deployed at address:', contractInstance.options.address);
// Web3.js
const contractInstance = new web3.eth.Contract(abi, contractAddress);
let currentValue = await contractInstance.methods.currentValue().call();
console.log('Incrementer Contract currentValue:', currentValue);
// ethers
const readContract = new ethers.Contract(contractAddress, abi, provider);
let currentValue = await readContract.currentValue();
console.log('Incrementer Contract currentValue:', currentValue.toString());
// Web3.js
const accounts = await web3.eth.getAccounts();
const contractInstance = new web3.eth.Contract(abi, contractAddress);
const tx = contractInstance.methods.descrement(1);
await tx
.send({
from: accounts[0],
gas: gasLimit,
gasPrice,
})
.on('receipt', async (recepit) => {
currentValue = await contractInstance.methods.currentValue().call();
console.log('Incrementer Contract currentValue:', currentValue);
});
// ethers
const writeContract = new ethers.Contract(contractAddress, abi, signer);
const tx = await writeContract.increment(ethers.BigNumber.from(5));
await tx.wait();
currentValue = await readContract.currentValue();
console.log('Incrementer Contract currentValue:', currentValue.toString());
// written in etherjs
const readContract = new ethers.Contract(contractAddress, abi, provider);
// The parameters passed to filters can only be indexed parameters.
const filter = readContract.filters.Bid(null, utils.hexlify(BigNumber.from(auction.recordId)));
// Filter the blocks
const logsFrom = await readContract.queryFilter(filter, 0, "latest");
logsFrom.forEach(item => console.log(items.args));
// written in web3
const web3 = new Web3(provider);
const contractInstance = new web3.eth.Contract(abi, contractAddress);
const logs = await contractInstance.getPastEvents('Descrement', {
filter: {},
fromBlock: 0,
});
logs.forEach((item) => {
console.log('Descrement Event:', item); // same results as the optional callback above
});
const provider = new ethers.providers.JsonRpcProvider('http://localhost:8545');
// http provider does not support subscription
const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8545'));
// using ethers
async function subByEthers() {
const readContract = new ethers.Contract(contractAddress, abi, provider);
let filterForm = readContract.filters.Increment();
readContract.on(filterForm, (amount, event) => {
console.log('Increment events:', event);
});
}
subByEthers();
// using web3
async function subByWeb3() {
const contractInstance = new web3.eth.Contract(abi, contractAddress);
contractInstance.events
.Descrement({
fromBlock: 0,
})
.on('data', (event) => {
console.log('Descrement Event:', event); // same results as the optional callback above
})
.on('error', function (error, receipt) {
console.error('Descrement Event error:', error);
});
}
subByWeb3();
}
const server = http.createServer(() => {});
server.listen(8002);