ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

Document how to import types like BigNumber

Open PaulRBerg opened this issue 5 years ago • 13 comments
trafficstars

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.

PaulRBerg avatar Jun 03 '20 13:06 PaulRBerg

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.

ricmoo avatar Jun 03 '20 23:06 ricmoo

To add on to this, how do I import types like Block and Network. They seem to be internal to Ethers.

adrianmcli avatar Jun 14 '20 18:06 adrianmcli

You can use:

import type { Network } from '@ethersproject/networks';
import type { Block } from '@ethersproject/abstract-provider';

andrevmatos avatar Oct 27 '20 23:10 andrevmatos

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.

PaulRBerg avatar Oct 28 '20 12:10 PaulRBerg

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!

jmcph4 avatar Sep 06 '21 00:09 jmcph4

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.

ricmoo avatar Sep 06 '21 00:09 ricmoo

You could use import { Provider } from "@ethersproject/providers"; or use import { providers } from "ethers"; from the umbrella package and use providers.Provider.

Former seems cleaner, thanks!

jmcph4 avatar Sep 06 '21 00:09 jmcph4

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.

aspiers avatar Jan 07 '22 21:01 aspiers

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.

ricmoo avatar Feb 03 '22 21:02 ricmoo

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.

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 avatar Apr 27 '22 01:04 folego

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

zemse avatar Apr 28 '22 13:04 zemse

@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!

folego avatar Apr 28 '22 13:04 folego

Would be nice to be able to import Block the same as BigNumber

cesarvarela avatar Jun 02 '22 03:06 cesarvarela

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! :)

ricmoo avatar Jan 18 '23 13:01 ricmoo