10

For me, it is less about features (however there are a couple cool features/design choices in ethers.js). Ethers.js is: smaller well tested well documented well maintained less buggy If you want to use ethers, you might want to check Waffle, a framework for creating smart contracts with ethers.


7

The 2 main advantages of ethers.js in my mind are: ENS names are first-class citizens key management and state - separation of concerns There are a number of other differentiating factors. https://medium.com/l4-media/announcing-ethers-js-a-web3-alternative-6f134fdd06f3


6

As of v4 at least, the answer is yes, the contract ABI must be provided. Creating a contract with no ABI and no provider or signer: let tokenContract = new ethers.Contract(tokenAddress); Yields this error: TypeError: Cannot read property 'forEach' of undefined Creating a contract with just no provider or signer: let tokenContract = new ethers.Contract(...


6

This answer assumes that you understand how to connect to a contract using Ethers. Your question specifies listening for an event to be emitted, and to do a task based on that. Here is the link to the Ethers v5 documentation, which we'll expand on a bit below: https://docs.ethers.io/v5/api/providers/provider/#Provider--events (Ethers v4 is here and here) ...


6

The return-value of a non-constant (neither pure nor view) function is available only when the function is called on-chain (i.e., from this contract or from another contract). When you call such function from the off-chain (e.g., from an ethers.js script), you need to execute it within a transaction, and the return-value is the hash of that transaction. This ...


5

getBalance is a function of the Ether.js blockchain provider object, it is used this way : const balance = await provider.getBalance("address"); Note that you can use contract.address to obtain the address of the contract instance.


4

Why I prefer ethers.js over web3.js List item less buggy Huge size difference. Web3 is very bulky for a frontend use concise documentation Easier for beginners New Projects are using ethers.js over web3 For web3, well it is a mist standard. You'll get a good idea using the sample apps here: https://github.com/adrianmcli/web3-vs-ethers


4

Truffle use Web3 by default and its injected inside as global dependency. You can't get rid of it, but you can install ether.js as dependency on your project. truffle init then you install your module npm install --save ethers and eventually you use it in your truffle tests var ethers = require('ethers'); you can use the ethers library into your tests ...


4

The ethers.js library uses BN.js internally for its maths, but the BigNumber class that is exposed serializes all values as immutable strings, and uses Object.defineProperty to ensure the resulting object is completely immutable. So, what you are seeing neither BN.js or bignumber.js. A custom implementation would probably be the best way to describe it, but ...


4

You can use an HDNode which is defined as: A Hierarchical Deterministic Wallet represents a large tree of private keys which can reliably be reproduced from an initial seed. Each node in the tree is represented by an HDNode which can be descended into. When you use this HDNode, you can change the path variable you give it in order to get different ...


4

to transfer some ETHs you could use the following code: const Tx = require('ethereumjs-tx') const ethers = require('ethers') const sendEths = async ({ to, from, fromPrivateKey, value, gasPrice, gasLimit = ethers.utils.hexlify(21000), }) => { const txCount = await provider.getTransactionCount(from) // build the transaction const tx = new ...


4

The equivalent in ethers.js involves first creating an "interface" object and calling a method on that: > let ABI = [ "function transfer(address to, uint amount)" ]; > let iface = new ethers.utils.Interface(ABI); > iface.encodeFunctionData("transfer", [ <function_params> ]) Taken from here


4

I used attach to call the implementation functions from my proxy contract: adder = Delegate.attach(proxy.address) as Delegate


3

I was running with this problem and you can inject it with ganache-cli const ethereumObject = require("ganache-cli").provider(); You can check out more here: https://michalzalecki.com/integration-tests-with-web3-ganache-cli-and-jest/ - I'm using ethers and it works! Kinda late but I hope this helps someone


3

I was able to do it with this code below: import { ethers, utils } from 'ethers'; export async function payWithMetamask(sender, receiver, strEther) { console.log(`payWithMetamask(receiver=${receiver}, sender=${sender}, strEther=${strEther})`) let ethereum = window.ethereum; // Request account access if needed await ethereum.enable(); ...


3

Here's is an example mainnet configuration in truffle-config.js using Infura: mainnet: { provider: function() { return new HDWalletProvider( process.env.MNEMONIC, `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}` ) }, gas: 2500000, gasPrice: ...


3

Posted the question to github and got the response You will need to include this code inside an async function block. I should update the documentation to include that, for example, you can change the code at the bottom to: (async function() { let factory = new ethers.ContractFactory(abi, bytecode, wallet); let contract = await factory.deploy(...


3

web3.js vs. ethers.js https://medium.com/l4-media/announcing-ethers-js-a-web3-alternative-6f134fdd06f3 One major difference between ethers.js and web3 is how they handle key management and interaction with the ethereum blockchain. Web3 assumes that there is a local node connected to the application. That node is assumed to store keys, sign transactions, and ...


3

You may notice in the comments, I was unable to work out what wasn't working for me, but not a fan of "it works, thanks" answers which don't help others. So I have put a working example up on gists for anyone who is looking for this. Just need to make some changes to be specific to your situation, but should be relatively straight forward. https://gist....


3

You can deploy a contract using Ethers.js' ContractFactory. import { ContractFactory } from 'ethers'; const factory = new ContractFactory(contractAbi, contractByteCode); // If your contract requires constructor args, you can specify them here const contract = await factory.deploy(deployArgs); console.log(contract.address); console.log(contract....


3

Two things happened: require("@nomiclabs/hardhat-waffle"); was missing in hardhat.config.js (as mentionned at the end of https://hardhat.org/tutorial/creating-a-new-hardhat-project.html) const { ethers } = require("ethers"); should be replaced by const { ethers } = require("hardhat"); or removed as it is available in the global ...


3

You can specify the transaction fields when calling a contract function like so: var options = { gasPrice: 1000000000}; // in wei const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [WETH_ADDRESS, CONTRACT], RECIPIENT, Date.now() + 1000 * 60 * 10, //10 minutes options );


3

Looking at the json-rpc-provider.ts file you can find the constructor declaration export class JsonRpcProvider extends BaseProvider { ... constructor(url?: ConnectionInfo | string, network?: Networkish) { ... url can be a string or a ConnectionInfo. ConnectionInfo is an object with properties: // Exported Types export type ConnectionInfo = { url: ...


3

Contract addresses are deterministic and need the deployer address and nonce to pre-compute it. You can use the following code to determine the contract address before deployment. const rlp = require('rlp'); const keccak = require('keccak'); const web3 = require('web3') const encodedData = rlp.encode([ '0x6c4465dc4dc3466c5736142ce8e12917a1e22c4', // ...


3

@ethersproject/address provides a getContractAddress() function to find future deployment address. const { ethers } = require('hardhat') const { getContractAddress } = require('@ethersproject/address') async function main() { const [owner] = await ethers.getSigners() const transactionCount = await owner.getTransactionCount() const futureAddress = ...


2

You can use etheres.js with Truffle, but you might want to consider using frameworks dedicated to ethers.js like Waffle Waffle is like Truffle, but simpler, faster and sweeter. Also uses ethers.js :)


2

This is something I was also wondering recently when I was getting 429 Too Many Requests responses from the default provider. Looking at the source code there are default API keys hardcoded in the source code (see etherscan-provider.ts#L87 and infura-provider.ts#17). It is possible that Richard has some additional arrangements with Etherscan/Infura to allow ...


2

I had the same issue and using once for events filtering also didn't work. I had to stop provider from polling events. You can do it like this: bytesContract.provider.polling = false This is what ethers documentation says about it prototype.polling mutable If the provider is currently polling because it is actively watching for events. This may be set to ...


2

For this to work, you have to call an approve function of the ERC20 token smart contract that you want to transfer. You have to pass the amount that you want to transfer and the spender address to the approve function. Only then you will be able to make the actual transfer.


2

const tokenWeiPrice = (answers.priceEur / ethEur) * Math.pow(10, 18); Problem #1: 10 ^ 18 > Number.MAX_SAFE_INTEGER Problem #2: tokenWeiPrice is not necessarily an integer. You should use BigNumber conclusively for the purpose of interacting with contracts. If ethers.js doesn't support this type of input, then before passing variable x of type ...


Only top voted, non community-wiki answers of a minimum length are eligible