22 votes
Accepted

Trouble with web3.eth.Contract, ABI usage with Typescript

you can also do import { AbiItem } from 'web3-utils' import Abi from './abi.json' new web3.eth.Contract(Abi as AbiItem[], contractAddress)
yangli-io's user avatar
  • 423
21 votes
Accepted

How to get chainId / network name / network id etc in hardhat?

There are a few ways to get the network/chain name/id. We are going to assume you are in a script that you'd like to run, or a test. When running these, you must have a hardhat.config.ts or hardhat....
Patrick Collins's user avatar
15 votes

String to Hex in Ethers.js

Good news! Ethers can natively read strings like "0x123beef" as a datatype it calls a HexString (source) Once you have a HexString, the docs for the available functions/methods are here. ...
Linum Labs's user avatar
  • 1,448
11 votes

Trying to use window.ethereum.request in Typescript errors out with Property 'request' does not exist on type 'EthereumProvider'

If you're doing this in React with CRA or ViteJS or even Remix-Run: File: src/vite-env.d.ts /// <reference types="vite/client" /> interface Window { ethereum: any; } File: src/...
codingwithmanny's user avatar
9 votes
Accepted

Getting Metamask account's balance using fromWei

Apparently, this is not the same inside the anonymous callback function you're passing to getBalance. Try replacing it with a fat arrow function, that binds the outer this value to the inner scope. ...
martriay's user avatar
  • 427
8 votes

Trying to use window.ethereum.request in Typescript errors out with Property 'request' does not exist on type 'EthereumProvider'

You can create a global.d.ts file at the root level of your project & type window.ethereum this way instead of using any. declare global { interface Window { ethereum?: import('ethers')....
Jay Puntham-Baker's user avatar
7 votes
Accepted

What is the equivilent to Solidity's abi.encode() function in Web3 JS?

You can use web3.eth.abi.encodeParameters: const web3 = new Web3; web3.eth.abi.encodeParameters( ['address', 'uint256[]'], ['0x6b175474e89094c44da98b954eedeac495271d0f', [1, 2, 3]] ); Results in: ...
Morten's user avatar
  • 6,017
7 votes
Accepted

Instantiate a contract using interface generated by TypeChain

There are two ways you can go about this: Method 1: (not using generated factory) import { IHintHelpers } from '../typechain-types'; const contract = new ethers.Contract(address, abi, signer) as ...
Soham Zemse's user avatar
  • 2,031
6 votes

TypeError: web3_1.default is not a constructor

If using a recent web3 (e.g. 1.0.0-beta.50), a possible remedy is to set esModuleInterop: { "compilerOptions": { "esModuleInterop": true } } See also: https://github.com/ethereum-ts/...
Eron Wright's user avatar
6 votes
Accepted

How to import the Contract type from the web3.js library?

I figured it out: import { Contract } from "web3-eth-contract"; It's recommended that you also list web3-eth-contract as a direct dependency so you avoid getting a warning from eslint.
Paul Razvan Berg's user avatar
6 votes
Accepted

Calculate The Gas Fee Of A Smart Contract Function Call And Convert The Result to USD Value

I Have Solved It, The First Thing I did was understand What is Gas? If you are unclear on what gas is, I recommend reading the answers to the StackOverflow question "what is gas?". ...
Kay Albertus's user avatar
6 votes

Invalid value undefined for HardhatConfig.networks.mainnet_fork.url - Expected a value of type string

If you are using .env install dotenv using npm install dotenv --save or as in my case, I have forgotten to add require("dotenv").config(); on top of the hardhat.config.js file
aakash verma's user avatar
6 votes

How to get chainId / network name / network id etc in hardhat?

Another way to do it that works for me is const { ethers } = require("hardhat"); const network = await ethers.getDefaultProvider().getNetwork(); console.log("Network name=", ...
Josh Davis's user avatar
5 votes

Trying to use window.ethereum.request in Typescript errors out with Property 'request' does not exist on type 'EthereumProvider'

Use (window as any).ethereum.request to get around type checking.
Mark Waser's user avatar
4 votes
Accepted

Alternatives for Truffle for running and writing tests

I found openzeppelin-test-environment solving my problem. It allows ephemeral Ethereum blockchain setup, contract deployment, etc. relatively easy. Below is my original Truffle + TypeScript test ...
Mikko Ohtamaa's user avatar
4 votes

How to mock an ethers provider while testing with jest?

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-...
Cesar's user avatar
  • 211
4 votes

Trying to use window.ethereum.request in Typescript errors out with Property 'request' does not exist on type 'EthereumProvider'

You can try adding the following line at the beginning: declare let window: any; Then, you can do stuff like this: import Web3 from 'web3'; //... let web3: Web3; if (window.ethereum) { ...
Sergi Juanati's user avatar
4 votes

Fetching InternalType data from another contract

The "internalType" is the representation used by solidity. For example the contract contract B { struct View { uint x; } function foo(View memory bad) public view { ...
Ismael's user avatar
  • 30.6k
4 votes

How do you check if a contract has a function using Ethers.js?

While other answers are correct, I think they can be improved as they only check for inclusion of a specific function selector in the bytecode which is not the same as checking if the jump table ...
hroussille's user avatar
  • 7,661
4 votes

Accessing storage of a map of a map

To do it that way, you must know what to look for, meaning that you should know beforehand some combinations of tokens addresses to compute the pair address for. Or, as @longhuynh suggested, you could ...
hroussille's user avatar
  • 7,661
4 votes

How can I batch execute multiple Gnosis Safe transactions via js/ts?

You can use the MultiSend contract to batch execute execTransaction calls. https://github.com/safe-global/safe-contracts/blob/main/contracts/libraries/MultiSend.sol
mikheevm's user avatar
  • 1,075
4 votes

How can I assign my custom contract type to a contract instance from Ethers?

Typechain's Example Typechain has a code example of this in their repo here (0xfbB26A55a5757326368Ea292fe9678EafAE9970a). I'll copy it over here first in case that's enough, and then talk through the ...
The Renaissance's user avatar
4 votes
Accepted

How to get deployed contract address with ethers?

The answer by @AlexanderHerranz is for ethers v5, for newer versions of hardhat that use ethers v6 you can get the deployed address using the async getAddress() method as shown below: const ...
MShakeG's user avatar
  • 1,603
3 votes

Correct Typescript typing for web3 ethereum contract

I couldn't know for sure if you only want to know the web3.eth.Contract type. But if it's what I imagine you can use this: import { Contract } from 'web3-eth-contract'; For example: import { Contract ...
n3n3's user avatar
  • 608
3 votes
Accepted

String to Hex in Ethers.js

I couldn't find such a function in ethers.js (hexlify doesn't accept strings as input), but maybe you'll find this utf8ToHex helper function useful: function utf8ToHex(str) { return '0x' + Array....
pwagner's user avatar
  • 900
3 votes

Trouble with web3.eth.Contract, ABI usage with Typescript

It seems that the issue comes from how TypeScript imports the JSON files when using REACT. Instead of importing with import use require Replace import * as ERC721ABI from "../ABIs/ERC721.json&...
Juanu's user avatar
  • 130
3 votes

Trying to use window.ethereum.request in Typescript errors out with Property 'request' does not exist on type 'EthereumProvider'

The answers thus far have been for ethers v5, here is how you do it for v6... import { ethers } from "ethers"; import { BrowserProvider, Eip1193Provider } from "ethers/types/providers&...
Dwayne Forde's user avatar
3 votes
Accepted

How to get Hardhat to log a returned variable rather than the entire transaction receipt? (Test written in TypeScript)

Unfortunately, it's not possible to get the return value of a state-changing function outside the off-chain. It's only possible to get it on-chain, in other contracts which call your contract. What is ...
Lauri Peltonen's user avatar
3 votes

where to get typings for ethereum object on window

Ethers has a definition for this called ExternalProvider. The interface is defined in EIP-1193. You can also find a good answer for this on StackOverflow: https://stackoverflow.com/questions/65504958/...
Richard's user avatar
  • 4,944
3 votes

How can I check if an Ethereum address is valid by using zod parser?

From zod docs: Zod lets you provide custom validation logic via refinements. In order to achieve this you may want first to define a zod schema by using the refine() function in combination with ...
Vsevolod's user avatar

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