ethers.js
ethers.js copied to clipboard
Document how to import types like BigNumber
Importing base types like Contract or Wallet is trivial:
import { Contract, Wallet } from "ethers";
const myContract: Contract = ...
But I had to dig up the source code to figure out how to import BigNumber:
import { utils } from "ethers";
const myBigNumber: utils.BigNumber = ...
It'd be nice to have this explained in the docs.
I've been mulling back-and-forth how to add this to the new docs, especially since many things are available from their sub-modules too.
I'll work on this on the next docs pass.
To add on to this, how do I import types like Block and Network. They seem to be internal to Ethers.
You can use:
import type { Network } from '@ethersproject/networks';
import type { Block } from '@ethersproject/abstract-provider';
FWIW I am now importing the BigNumber type like this:
import { BigNumber} from "@ethersproject/bignumber";
And I started using Sourcegraph to find where to import types from.
Any progress on this? As of 5.4.0, import { Provider } from "ethers" fails, when I'd expect that to be a top-level type given its importance to the API.
Also import { FallbackProvider, FeeData } from "ethers" would be nice, given that the documentation explicitly recommends relying on the ethers.getDefaultProvider method when in doubt.
Happy to be corrected!
You could use import { Provider } from "@ethersproject/providers"; or use import { providers } from "ethers"; from the umbrella package and use providers.Provider.
In v6 the root package will be busier, but in v5 there won’t be any changes in the organization.
You could use
import { Provider } from "@ethersproject/providers";or useimport { providers } from "ethers";from the umbrella package and useproviders.Provider.
Former seems cleaner, thanks!
I was about to report this. It seems quite surprising that something as basic as how to import Provider is not documented. The argument "but no one needs to instantiate abstract classes" doesn't hold because in the TypeScript world it's often necessary to reference types even when not instantiating them.
The package README.md files include this info, which in v6 will be linked to and will include a large import page that indicates where each class, function and interface comes from.
Importing base types like
ContractorWalletis trivial:import { Contract, Wallet } from "ethers"; const myContract: Contract = ...But I had to dig up the source code to figure out how to import
BigNumber:import { utils } from "ethers"; const myBigNumber: utils.BigNumber = ...It'd be nice to have this explained in the docs.
Thanks for this one. I was able to create a function to call this and make it less ugly.
import { utils } from 'ethers';
export const hexToNumber = (hex) => {const number = utils.BigNumber = hex; return number.toNumber();}
import { hexToNumber } from '../utils/hexToNumber';
const xyz = hexToNumber(await contract.getBalance));
@folego The pseudo code snippet you've provided seems to be intending to convert BigNumber into js number, which you shouldn't do. Please see this info.
Just as an update to this thread, on v5, BigNumber can be imported directly from the umbrella package.
import { BigNumber } from 'ethers';
While on v6 (beta), there's no BigNumber. It uses the js native bigint. :)
@folego The pseudo code snippet you've provided seems to be intending to convert BigNumber into js number, which you shouldn't do. Please see this info.
Just as an update to this thread, on v5, BigNumber can be imported directly from the umbrella package.
import { BigNumber } from 'ethers';While on v6 (beta), there's no BigNumber. It uses the js native bigint. :)
In my case, I am using the code only to convert small hex numbers. I was trying to avoid using other libraries. I am happy that will be a solution in v6. As soon we get it, I will update the code of my dapp.
And thanks for the import!
Would be nice to be able to import Block the same as BigNumber
In v6 everything is exported from the root. There is no longer any mystery to it, so hopefully that will make all our lives more comfy. :)
Thanks! :)