Skip to content

Instantly share code, notes, and snippets.

@mariorz
Last active May 11, 2021
Embed
What would you like to do?
from decimal import Decimal
import itertools
import coinbasepro as cbp
def compute_coinbase_attack_cost():
client = cbp.PublicClient()
#get last 10000 trades, for good measure
trades = list(itertools.islice(client.get_product_trades('DAI-USDC'), 10000))
#filter for off-price trades
relevant = [t for t in trades if t['price'] > Decimal('1.02')]
#sanity check time-frames
print(f"{relevant[-1]['time']} -- {relevant[0]['time']}")
#compute total value paid, and total cost
sum_paid = sum([t['price']*t['size']) for t in relevant])
sum_value = sum([t['size'] for t in relevant])
cost = sum_paid - sum_value
print(f"Sum paid: {sum_paid}")
print(f"Sum value: {sum_value}")
print(f"Attack cost: {cost}")
# >>> import compcb
# >>> compcb.compute_coinbase_attack_cost()
# 2020-11-26 08:36:42.813000 -- 2020-11-26 09:07:02.573000
# Sum paid: 381453.1254952848200000
# Sum value: 359692.471640000
# Attack cost: 21760.6538552848200000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment