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

sol-parser

v1.0.2

Published

Parse any contract from any EVM blockchain using the API of their block explorer site

Downloads

8

Readme

sol-parser

version npm License

Parse any Solidity contract passing either the source code of the contract or the address of a verified deployed contract in a supported blockchain, which will retrieve the source code from its explorer using the API of their block explorer site.

Installation

npm install solidityparser

Usage

solidityparser provides a class that must be initialized with the network name or id of your choice.

const SolidityParser = require("sol-parser");

const parser = new SolidityParser();

let parseFromFile = parser.parseFile("./MyContract.sol");

let parseFromString = parser.parse(`
pragma solidity 0.8.4;
contract MyContract {
  function foo(){}
}
`);

A network can be specified alongside an API-key from the explorer.

const SolidityParser = require("sol-parser");

const parser = new SolidityParser({
  network: 1, // Ethereum mainnet
  api_keys: "EYUZIES8FZ7TFK581OERKZ0LFTN3FC0BOT" // API-key on etherscan.io
});

let parseFromAddress = parser.parseAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")
  .then(parsed => {
    // Parsed contract
  })
  .catch(error => {
    throw error;
  });
  • NOTE: parseAddress is an asynchronous function.

Methods

|Method|Description| |---|---| |SolidityParser.parse(code: String, options: Object)|Parses a solidity contract sent as a String.| |SolidityParser.parseFile(file: String, options: Object)|Parses a solidity contract from a file.| |SolidityParser.parseAddress(address: String, options: Object)|Parses a solidity contract sent as an address. This method needs an API-key from the explorer.|

Options

|Option|Default|Description| |---|---|---| |elementPosition|true|Returns the position start and end of any statement or declaration found in the code. Set to false if you want, for example, to compare a specific part of the code with the same part of another contract.|

Supported Networks

Any of the following networks can be initialized to the parser.

// Using the name of the network
const parser = new SolidityParser({
  network: "mainnet",
  api_keys: explorer_api
});

// Using the id of the network
const parser = new SolidityParser({
  network: 56,
  api_keys: explorer_api
});

|Network|Name|Id| |:---:|:---:|:---:| |Ethereum Mainnet| mainnet | 1 | |Ethereum Ropsten| ropsten | 3 | |Ethereum Rinkeby| rinkeby | 4 | |Ethereum Görli | görli or goerli | 5 | |Ethereum Kovan | kovan | 42 | |Optimistic | optimistic | 10 | |Optimistic Kovan| optimistic-testnet or optimistic-kovan | 69 | |Arbitrum | arbitrum | 42161 | |Arbitrum Rinkeby| arbitrum-testnet or arbitrum-rinkeby | 421611 | |Binance Smart Chain| bsc | 56 | |Binance Smart Chain Testnet| bsc-testnet | 97 | |Cronos| cronos | 25 | |Polygon| polygon or matic | 137 | |Polygon Mumbai| mumbai | 80001 | |Fantom| fantom | 250 | |Fantom Testnet| fantom-testnet | 4002 |

How to get an API key

In order to use the parseAddress feature, you must own an account in the desired blockchain explorer provided by Etherscan. Once you have an account, navigate to your profile, and create an API key under the API-KEYs section.

Keep in mind that free accounts have limited API usage:

Etherscan.io free plan features:

  • 5 calls/second limit
  • Up to 100,000 API calls per day

If you were to use more than the limit, consider upgrading your plan, or using an array of API keys which will rotate randomly on each request:

const SolidityParser = require("sol-parser");

const myKeys = ["DEHMWMJ3UZRIWDM2IFB7P1VVBGQ2FW290Z", "AWNAHFGIIJDDUUEUSPUEPG8HOU3AKHCL31", "CYVO7LDGQSHLW9RIORWA9VMBNZA687DKHZ"];
const parser = new SolidityParser({
  network: "mainnet",
  api_keys: myKeys
});