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.
npm install solidityparsersolidityparser provides a class that must be initialized with the network name or id of your choice.
constSolidityParser=require("sol-parser");constparser=newSolidityParser();letparseFromFile=parser.parseFile("./MyContract.sol");letparseFromString=parser.parse(`pragma solidity 0.8.4;contract MyContract { function foo(){}}`);A network can be specified alongside an API-key from the explorer.
constSolidityParser=require("sol-parser");constparser=newSolidityParser({network: 1,// Ethereum mainnetapi_keys: "EYUZIES8FZ7TFK581OERKZ0LFTN3FC0BOT"// API-key on etherscan.io});letparseFromAddress=parser.parseAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").then(parsed=>{// Parsed contract}).catch(error=>{throwerror;});- NOTE:
parseAddressis an asynchronous function.
| 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. |
| 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. |
Any of the following networks can be initialized to the parser.
// Using the name of the networkconstparser=newSolidityParser({network: "mainnet",api_keys: explorer_api});// Using the id of the networkconstparser=newSolidityParser({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 |
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:
constSolidityParser=require("sol-parser");constmyKeys=["DEHMWMJ3UZRIWDM2IFB7P1VVBGQ2FW290Z","AWNAHFGIIJDDUUEUSPUEPG8HOU3AKHCL31","CYVO7LDGQSHLW9RIORWA9VMBNZA687DKHZ"];constparser=newSolidityParser({network: "mainnet",api_keys: myKeys});