Created
May 10, 2021 15:24
-
-
Save chibie/1b376d0487f89aded5e9b0a5a8fa73ea to your computer and use it in GitHub Desktop.
Nervos Tx Verification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_nervos_txn_status(txnid, network='mainnet'): | |
| if not txnid: | |
| return None | |
| if network != 'mainnet': # TODO: network == 'mainnet' | |
| base_url = 'https://api.explorer.nervos.org/api/v1' | |
| else: | |
| base_url = 'https://api.explorer.nervos.org/testnet/api/v1' | |
| explorer_url = f'{base_url}/transactions/{txnid}' | |
| tip_block_number_url = f'{base_url}/statistics/tip_block_number' | |
| tx_response = requests.get( | |
| explorer_url, headers=HEADERS).json()['data']['attributes'] | |
| tx_data = tx_response.json()['data']['attributes'] | |
| if tx_response.status_code == 200: | |
| tip_block_number = requests.get( | |
| tip_block_number_url, headers=HEADERS | |
| ).json()['data']['attributes']['tip_block_number'] | |
| confirmations = tip_block_number - int(tx_data['block_number']) | |
| output_status = tx_data['display_outputs'][0]['status'] | |
| if ( | |
| confirmations > 0 and | |
| tx_data['tx_status'] == 'committed' and | |
| output_status == 'live' | |
| ): | |
| return True | |
| else: | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment