Created
September 10, 2020 02:30
-
-
Save evanaze/8742808acd795a0aeb084d8e21f58de9 to your computer and use it in GitHub Desktop.
This file contains 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_transaction_data(txn_hash): | |
"gets the data for the specified transaction hash" | |
# the url with the given hash | |
url = f"https://web3api.io/api/v2/transactions/{txn_hash}" | |
# the headers | |
headers = { | |
"x-amberdata-blockchain-id": "bitcoin-mainnet", | |
"x-api-key": api_key["AMBERDATA_API_KEY"] | |
} | |
# get the response | |
response = get_response(url, headers=headers) | |
# return the response | |
return response | |
def calculate_flow(txn_hash, status): | |
"Calculates the flow on or off the exchange for a given transaction" | |
# the transaction data | |
transaction = get_transaction_data(txn_hash) | |
# input and outputs of the transaction | |
inputs, outputs = transaction["inputs"], transaction["outputs"] | |
# check the status of the transaction | |
if status == "within": | |
# get the address of the sender sending money out of the exchange | |
senders = set([sender['addresses'][0] for sender in inputs]) | |
# search through the outputs for the recipient that is not the sender | |
for output in outputs: | |
# get the address of the recipient | |
recipient = output['addresses'][0] | |
# check if the recipient is a sender | |
if recipient not in senders: | |
# add the value to the output | |
flow = output['value'] | |
elif status == "on": | |
for output in outputs: | |
# get the address of the recipient | |
recipient = output['addresses'][0] | |
# check if the recipient is a bitmex address | |
if (recipient.startswith("3BMEX") or recipient.startswith("3BitMEX")): | |
# add the value to the output | |
flow = output['value'] | |
return flow | |
# calculate the flows across the transactions in our exchange | |
df_exch['flow'] = df_exch.apply(lambda x: calculate_flow(x[1], x[10]), axis=1) | |
# calcule the value in BTC for the flow | |
df_exch['flow_btc'] = df_exch.flow//10**8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment