1.0.2 • Published 2 years ago

sol-parser v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

MethodDescription
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

OptionDefaultDescription
elementPositiontrueReturns 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
});
NetworkNameId
Ethereum Mainnetmainnet1
Ethereum Ropstenropsten3
Ethereum Rinkebyrinkeby4
Ethereum Görligörli or goerli5
Ethereum Kovankovan42
Optimisticoptimistic10
Optimistic Kovanoptimistic-testnet or optimistic-kovan69
Arbitrumarbitrum42161
Arbitrum Rinkebyarbitrum-testnet or arbitrum-rinkeby421611
Binance Smart Chainbsc56
Binance Smart Chain Testnetbsc-testnet97
Cronoscronos25
Polygonpolygon or matic137
Polygon Mumbaimumbai80001
Fantomfantom250
Fantom Testnetfantom-testnet4002

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
});