npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wiiib/check-evm-address

v1.1.0

Published

Utils for getting web3 contracts' data and checking whether they belong to certain standards.

Downloads

4

Readme

@wiiib/check-evm-address

Utils for getting web3 contracts' data and checking whether they belong to certain standards.

Cover


Contracts and interfaces

checkCode

Check if there's a contract on the provided address

await checkCode(address, yourJsonRpcProvider)
// > { code: '0x...', isContract: true }

Returns

  • code: string - code that belongs to the address
  • isContract: boolean - if a contract belongs to the address (i.e. if it has any code)

checkInterfaces

Check if the contract on the provided address implements certain interfaces.

await checkInterfaces(addresses, yourJsonRpcProvider)
// > { isIERC165: true, isIERC721: false, ... }

Returns

  • isIERC165, isIERC721, isIERC721Metadata, isIERC1155, isIERC1155MetadataURI, isIERC20 - boolean flags if the contract implements the corresponding interface (false as a fallback if no contract belongs to the address)
  • isContract: boolean - if a contract belongs to the address
  • type: ERC1155 | ERC721 | ERC20 | null - summarized type / standard of the contract determined by the returns above

Metadata utils

Contracts

getErc20Metadata

Returns metadata of the erc20 contract.

await getErc20Metadata(address, yourJsonRpcProvider)
// > { metadata: { name: string, symbol: string, decimals: string } }

Throws an error if there's no erc20 contract on the address.

getErc721Metadata

Returns metadata of the erc721 contract.

await getErc721Metadata(address, yourJsonRpcProvider)
// > { metadata: { name: string, symbol: string } }

Throws an error if there's no erc721 contract on the address.

getMetadata

Returns metadata of the contract, if applicable.

await getMetadata(address, yourJsonRpcProvider)
// > { metadata: { name: string, symbol: string } }

Returns null if there's no suitable contract on the address.

Tokens

getErc721TokenMetadata

Returns metadata of the erc721 token by the contract's tokenURI method.

await getErc721TokenMetadata(address, yourJsonRpcProvider, tokenId)
// > { metadata: { ... } }

Throws an error if there's no erc721 contract on the address. Returns null if tokenId is invalid / doesn't exist or there's no uri for this tokenID.

getErc1155TokenMetadata

Returns metadata of the erc1155 token by the contract's uri method.

await getErc1155TokenMetadata(address, yourJsonRpcProvider, tokenId)
// > { metadata: { ... } }

Throws an error if there's no erc1155 contract on the address. Returns null if tokenId is invalid / doesn't exist or there's no uri for this tokenID.

getTokenMetadata

Returns metadata of the token, if applicable.

await getTokenMetadata(address, yourJsonRpcProvider, tokenId)
// > { metadata: { ... } }

Returns null if there's no suitable contract on the address, tokenId is invalid / doesn't exist or there's no uri for this tokenID.

Note You can pass abortSignal as the additional 4th argument in each of the token metadata methods above, and call it whenever you want to abort the pending metadata request.

const abortController = new AbortController()
getTokenMetadata(address, provider, tokenId, abortController.signal).then(/* ... */)
// ...
abortController.abort()

IPFS resolvers

resolveIpfsString

Resolves ipfs:// link into https URL with provided gateway.

resolveIpfsString('ipfs://...SOME_HASH...')
// > https://gateway.pinata.cloud/ipfs/...SOME_HASH...

Default resolver uses pinata gateway, but you can easily provide your own:

const customResolver = (link) => `https://your-gateway.io/${link.replace(/^ipfs:\/\//, '')}`
resolveIpfsString('ipfs://...SOME_HASH...', customResolver)
// > https://your-gateway.io/...SOME_HASH...

resolveIpfs

This method accepts any value and tries to resolve all the IPFS links recursively.

resolveIpfs([{ uri: 'ipfs://HASH' }, 'nothing', 42])
// > [{ uri: 'https://gateway.pinata.cloud/ipfs/HASH' }, 'nothing', 42]

For string inputs, it works just like resolveIpfsString. For inputs that aren't strings nor objects nor arrays, the input value will be returned.

Custom resolvers are allowed as well:

resolveIpfs([{ uri: 'ipfs://HASH' }, 'nothing', 42])
// > [{ uri: 'https://gateway.pinata.cloud/ipfs/HASH' }, 'nothing', 42]

Additional helpers

formatChainId

Format ChainID to hex and int formats.

formatChainId('0x5') // a hex string
// > { hexId: '0x5', intId: 5 }

formatChainId(137) // a number
// > { hexId: '0x89', intId: 137 }

formatChainId(0x38) // a number
// > { hexId: '0x38', intId: 56 }

formatChainId('1') // a stringified number
// > { hexId: '0x1', intId: 1 }

Returns

  • hexId: string - hex-string formatted ID
  • intId: number - decimal-int formatted ID

Throws an error if NaN or a not-positive number is provided.