17

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 ...


17

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.


15

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.


12

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 ...


11

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 ...


11

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) ...


9

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


9

The filter method doesn't return the events but the filter object. To get the events you have to pass the filter to the contract.queryFilter method: const contract = new ethers.Contract( address, abi, provider.getSigner(0) ) let eventFilter = contract.filters.ContractEvent() let events = await contract.queryFilter(eventFilter)


9

As far as I can tell it isn't possible to actually use the arguments which are emitted, however you can test for them being emitted using the following chai code example: await expect(token.transfer(walletTo.address, 7)) .to.emit(token, 'Transfer') .withArgs(wallet.address, walletTo.address, 7); Here is the reference


8

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(...


8

This worked for me: const { chainId } = await provider.getNetwork() console.log(chainId) // 42


8

While I did not find a solution on why the .on() method is not working, I found a workaround using the transaction's receipt. The following code describes this workaround: let tx: ContractTransaction = await myToken.connect(accounts[0]).transfer(accounts[1].address, 1); let receipt: ContractReceipt = await tx.wait(); console.log(receipt.events?.filter((x) =&...


7

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 ...


7

There is a method called estimateGas in the Contract class. Example for how to use it: const erc20Abi = [ /* ... */ ]; const address = "TOKEN_ADDRESS_HERE"; const provider = ethers.getDefaultProvider(); const erc20 = new ethers.Contract(address, abi, provider); const recipient = "SOME_ADDRESS_HERE"; const estimation = await erc20....


6

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


6

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


6

I have been having the same issue. First make sure your hardhat.config.js file has this line: require("@nomiclabs/hardhat-waffle"); I fixed it by adding this to the top of the test file: const { ethers } = require("hardhat"); But before you try that, see if your tests still run as expected. Someone told me its a linting error which made ...


6

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


6

actually in solidity docs it's pointing that for each public member variable there will be a getter is defined automatically. In short you'll be able to get basic type public member like this: let x = await contract.totalAllocPoint(); you've been missing parenthesis here. While you can get array like: let y = await contract.poolInfo(2); if the array length ...


5

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(); ...


5

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....


5

The Wallet.fromMnemonic function has a second argument to specify the BIP-32 derivation path. By default it will use m/44'/60'/0'/0/0, but if you want to get the second account, you can use m/44'/60'/0'/0/1 for example: const { Wallet } = require('ethers'); const wallet = Wallet.fromMnemonic('one two three four ...', `m/44'/60'/0'/0/1`); Alternatively, you ...


5

While the return value of a function call executed on-chain cannot be returned off-chain, you can however simulate a function call on-chain to see what that function call would return. In ethers, you can use callStatic. From ethers' documentation: Rather than executing the state-change of a transaction, it is possible to ask a node to pretend that a call is ...


5

@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 = ...


5

The API for doing this is defined in EIP-747. You'll need to use the wallet_watchAsset method, something along the lines of the following example code: const tokenAddress = '0xd00981105e61274c8a5cd5a88fe7e037d935b513'; const tokenSymbol = 'TUT'; const tokenDecimals = 18; const tokenImage = 'http://placekitten.com/200/300'; try { // wasAdded is a boolean. ...


4

On top of all the good answers written here, I'd like to add Adrian Li's recently published comparison of web3 vs ethers.js: Web3.js vs Ethers.js It's a GitHub repo with a comprehensible README and code examples.


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

Yes, ethers.js has an Infura provider. Since Infura doesn't provide ethereum accounts you have to manage it your side. Ethers.js has wallets that can be used with any of their providers Wallet and signers.


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 ...


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