Skip to content

Instantly share code, notes, and snippets.

@chibie
Created May 10, 2021 15:24
Show Gist options
  • Select an option

  • Save chibie/1b376d0487f89aded5e9b0a5a8fa73ea to your computer and use it in GitHub Desktop.

Select an option

Save chibie/1b376d0487f89aded5e9b0a5a8fa73ea to your computer and use it in GitHub Desktop.
Nervos Tx Verification
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