|
| 1 | +# polygonscan-python |
| 2 | + |
| 3 | +A minimal, yet complete, Python API for [polygonscan.com](https://polygonscan.com/). |
| 4 | + |
| 5 | +This package was cloned from [bscscan-python](https://github.com/pcko1/bscscan-python) and |
| 6 | +readapted to polygon network. A special thanks to the [creator](https://github.com/pcko1). |
| 7 | + |
| 8 | +All standard and pro endpoints are provided. |
| 9 | + |
| 10 | +Available on [PyPI](https://pypi.org/project/polygonscan-python/). Powered by [polygonscan.com APIs](https://polygonscan.com/apis#misc). |
| 11 | + |
| 12 | +___ |
| 13 | + |
| 14 | +## Endpoints |
| 15 | + |
| 16 | +The following endpoints are provided: |
| 17 | + |
| 18 | +<details><summary>Accounts <a href="https://polygonscan.com/apis#accounts">(source)</a></summary> |
| 19 | +<p> |
| 20 | + |
| 21 | +* `get_matic_balance` |
| 22 | +* `get_matic_balance_multiple` |
| 23 | +* `get_normal_txs_by_address` |
| 24 | +* `get_normal_txs_by_address_paginated` |
| 25 | +* `get_internal_txs_by_address` |
| 26 | +* `get_internal_txs_by_address_paginated` |
| 27 | +* `get_internal_txs_by_txhash` |
| 28 | +* `get_internal_txs_by_block_range_paginated` |
| 29 | +* `get_erc20_token_transfer_events_by_address` |
| 30 | +* `get_erc20_token_transfer_events_by_contract_address_paginated` |
| 31 | +* `get_erc20_token_transfer_events_by_address_and_contract_paginated` |
| 32 | +* `get_erc721_token_transfer_events_by_address` |
| 33 | +* `get_erc721_token_transfer_events_by_contract_address_paginated` |
| 34 | +* `get_erc721_token_transfer_events_by_address_and_contract_paginated` |
| 35 | +* `get_mined_blocks_by_address` |
| 36 | +* `get_mined_blocks_by_address_paginated` |
| 37 | + |
| 38 | +</details> |
| 39 | + |
| 40 | +<details><summary>Contracts <a href="https://polygonscan.com/apis#contracts">(source)</a></summary> |
| 41 | +<p> |
| 42 | + |
| 43 | +* `get_contract_abi` |
| 44 | +* `get_contract_source_code` |
| 45 | + |
| 46 | +</details> |
| 47 | + |
| 48 | +</details> |
| 49 | + |
| 50 | +<details><summary>Transactions <a href="https://polygonscan.com/apis#transactions">(source)</a></summary> |
| 51 | +<p> |
| 52 | + |
| 53 | +* `get_contract_execution_status` |
| 54 | +* `get_tx_receipt_status` |
| 55 | + |
| 56 | +</details> |
| 57 | + |
| 58 | +<details><summary>Blocks <a href="https://polygonscan.com/apis#blocks">(source)</a></summary> |
| 59 | +<p> |
| 60 | + |
| 61 | +* `get_block_reward_by_block_number` |
| 62 | +* `get_est_block_countdown_time_by_block_number` |
| 63 | +* `get_block_number_by_timestamp` |
| 64 | + |
| 65 | +</details> |
| 66 | + |
| 67 | +<details><summary>GETH/Parity Proxy <a href="https://polygonscan.com/apis#proxy">(source)</a></summary> |
| 68 | +<p> |
| 69 | + |
| 70 | +* `get_proxy_block_number` |
| 71 | +* `get_proxy_block_by_number` |
| 72 | +* `get_proxy_uncle_by_block_number_and_index` |
| 73 | +* `get_proxy_block_transaction_count_by_number` |
| 74 | +* `get_proxy_transaction_by_hash` |
| 75 | +* `get_proxy_transaction_by_block_number_and_index` |
| 76 | +* `get_proxy_transaction_count` |
| 77 | +* `get_proxy_transaction_receipt` |
| 78 | +* `get_proxy_call` |
| 79 | +* `get_proxy_code_at` |
| 80 | +* `get_proxy_storage_position_at` |
| 81 | +* `get_proxy_gas_price` |
| 82 | +* `get_proxy_est_gas` |
| 83 | + |
| 84 | +</details> |
| 85 | + |
| 86 | +<details><summary>Tokens <a href="https://polygonscan.com/apis#tokens">(source)</a></summary> |
| 87 | +<p> |
| 88 | + |
| 89 | +* `get_total_supply_by_contract_address` |
| 90 | +* `get_acc_balance_by_token_and_contract_address` |
| 91 | + |
| 92 | +</details> |
| 93 | + |
| 94 | +<details><summary>Gas Tracker <a href="https://polygonscan.com/apis#gastracker">(source)</a></summary> |
| 95 | +<p> |
| 96 | + |
| 97 | +* `get_est_confirmation_time` |
| 98 | +* `get_gas_oracle` |
| 99 | + |
| 100 | +</details> |
| 101 | + |
| 102 | +<details><summary>Stats <a href="https://polygonscan.com/apis#stats">(source)</a></summary> |
| 103 | +<p> |
| 104 | + |
| 105 | +* `get_total_matic_supply` |
| 106 | +* `get_matic_last_price` |
| 107 | + |
| 108 | +</details> |
| 109 | + |
| 110 | +*If you think that a newly-added method is missing, kindly open an [issue](https://github.com/tarsil/polygonscan-python/issues) as a feature request and I will do my best to add it.* |
| 111 | + |
| 112 | +## Installation |
| 113 | + |
| 114 | +Before proceeding, you should register an account on [polygonscan.com](https://polygonscan.com/) |
| 115 | +and [generate a personal API key](https://polygonscan.com/myapikey) to use. |
| 116 | + |
| 117 | +If you wish to have access to the PRO endpoints, you should obtain elevated privileges via PolygonScans's |
| 118 | +subscription service. |
| 119 | + |
| 120 | +Install from source: |
| 121 | + |
| 122 | +``` bash |
| 123 | +pip install git+https://github.com/tarsil/polygonscan-python |
| 124 | +``` |
| 125 | + |
| 126 | +Alternatively, install from [PyPI](https://pypi.org/project/polygonscan-python/): |
| 127 | + |
| 128 | +```bash |
| 129 | +pip install polygonscan-python |
| 130 | +``` |
| 131 | + |
| 132 | +## Unit tests |
| 133 | + |
| 134 | +In `bash`, test that everything looks OK on your end using your `YOUR_API_KEY` (without quotation marks) |
| 135 | +before proceeding: |
| 136 | + |
| 137 | +``` bash |
| 138 | +bash run_tests.sh YOUR_API_KEY |
| 139 | +```` |
| 140 | + |
| 141 | +This will regenerate the logs under `logs/` with the most recent results and the timestamp of the execution. |
| 142 | + |
| 143 | +## Usage |
| 144 | + |
| 145 | +In `python`, create a client with your personal [polygonscan.com](https://polygonscan.com/) API key: |
| 146 | + |
| 147 | +``` python |
| 148 | +from polygonscan import PolygonScan |
| 149 | +matic = PolygonScan(YOUR_API_KEY) # key in quotation marks |
| 150 | +``` |
| 151 | + |
| 152 | +Then you can call all available methods, e.g.: |
| 153 | + |
| 154 | +``` python |
| 155 | +matic.get_matic_balance(address="0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a") |
| 156 | +
|
| 157 | +> '40891631566070000000000' |
| 158 | +``` |
| 159 | +You can also choose one of the other testnets: |
| 160 | +``` python |
| 161 | +matic = PolygonScan(YOUR_API_KEY, net="ropsten") # net name is case-insensitive, default is main |
| 162 | +``` |
| 163 | + |
| 164 | +## Examples |
| 165 | + |
| 166 | +Examples (arguments and results) for all methods may be found as JSON files |
| 167 | +[here](https://github.com/tarsil/polygonscan-python/tree/master/logs). |
| 168 | +For example, if you want to use the method `get_block_number_by_timestamp`, |
| 169 | +you can find the supported arguments and the format of its output in its respective |
| 170 | +[JSON file](logs/standard/get_block_number_by_timestamp.json): |
| 171 | + |
| 172 | +``` json |
| 173 | +{ |
| 174 | + "method": "get_block_number_by_timestamp", |
| 175 | + "module": "blocks", |
| 176 | + "kwargs": { |
| 177 | + "timestamp": "1578638524", |
| 178 | + "closest": "before" |
| 179 | + }, |
| 180 | + "log_timestamp": "2020-10-28-12:34:44", |
| 181 | + "res": "9251482" |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +where `kwargs` refer to the required named arguments and `res` refers to the expected result if you were to run: |
| 186 | + |
| 187 | +``` python |
| 188 | +eth.get_block_number_by_timestamp(timestamp="1578638524", closest="before") |
| 189 | +
|
| 190 | +> '9251482' |
| 191 | +``` |
| 192 | + |
| 193 | +**Disclaimer**: Those examples blindly use the arguments originally showcased |
| 194 | +[here](https://api.polygonscan.com/apis) and the selected wallets/contracts |
| 195 | +do not reflect any personal preference. You should refer to the same source for additional |
| 196 | +information regarding specific argument values. |
| 197 | + |
| 198 | +## Issues |
| 199 | + |
| 200 | +For problems regarding installing or using the package please open an |
| 201 | +[issue](https://github.com/tarsil/polygonscan-python/issues). |
| 202 | +Kindly avoid disclosing potentially sensitive information such as your API keys or your wallet addresses. |
| 203 | + |
| 204 | +Feel free to leave a :star: if you found this package useful. |
| 205 | + |
| 206 | +___ |
| 207 | + |
| 208 | + Powered by [polygonscan.com APIs](https://polygonscan.com/apis). |
0 commit comments