#!/usr/bin/env python3 """Stablecoin local run: bootstrap with a person-fed oracle, position lifecycle, and the controller fed by the real twap_oracle price account (Q64.64).""" import sys, json sys.path.insert(0, "/home/vpavlin/devel/m1-local/harness") import h I = h.I; Q = 2**64; RAY = 10**27; DAY = 86400000 def sc_snap(label): s = { "params": h.inspect("stablecoin", I["PROTO_PARAMS"], "ProtocolParameters"), "redemption": h.inspect("stablecoin", I["REDEMPTION"], "RedemptionPriceState"), "fee_acc": h.inspect("stablecoin", I["FEE_ACC"], "StabilityFeeAccumulator"), "position": h.inspect("stablecoin", I["POSITION"], "Position"), "oracle": h.inspect("twap_oracle", I["SCO_PRICE"], "OraclePriceAccount"), "pos_vault": h.holding(I["POS_VAULT"]), "user_sc": h.holding(I["SC_USER_HOLD"]), "hold_a": h.holding(I["HOLD_A"]), } h.log_note("SC snapshot: " + label, s) r = s["redemption"] or {} o = s["oracle"] or {} p = s["position"] or {} print(" redemption:", {k: r.get(k) for k in list(r)[:6]}) print(" oracle price raw:", o.get("price"), "(= %.6f as Q64.64, = %.3e as RAY)" % (int(o.get("price", 0)) / Q, int(o.get("price", 0)) / RAY) if o.get("price") else None) print(" position:", p, "vault:", s["pos_vault"], "user_sc:", s["user_sc"]) return s def tx(label, idl, prog, instr, **a): r = h.act(label, idl, prog, instr, snap=False, **a) h.wait_next_block() sc_snap(label) return r step = sys.argv[1] if len(sys.argv) > 1 else "all" if step in ("all", "boot"): tx("SC0 person-fed oracle (fresh source, its ONLY possible call): price account base=FUTURE stablecoin PDA, quote=TKA, @0.5 (Q64.64)", "twap_oracle", "twap_oracle", "create_oracle_price_account", oracle_price_account=I["SCO_PRICE"], price_source=I["ORACLE_SRC"], clock=I["CLOCK"], base_asset=I["SC_DEF"], quote_asset=I["DEF_A"], initial_price=Q // 2, window_duration=DAY) if step in ("all", "boot", "init"): tx("SC1 initialize_program (redemption 0.5 RAY, CR 1.5x, Kp=1.0, Ki=0)", "stablecoin", "stablecoin", "initialize_program", admin=I["SC_ADMIN"], protocol_parameters=I["PROTO_PARAMS"], stability_fee_accumulator=I["FEE_ACC"], redemption_price_state=I["REDEMPTION"], stablecoin_definition=I["SC_DEF"], stablecoin_master_holding=I["SC_MASTER"], collateral_definition=I["DEF_A"], market_price_oracle=I["SCO_PRICE"], clock=I["CLOCK"], freeze_authority_account_id=I["SC_FREEZE"], initial_stability_fee_per_millisecond=RAY + 1_500_000_000_000_000, initial_controller_proportional_gain=RAY, initial_controller_integral_gain=0, initial_minimum_collateralization_ratio=RAY * 3 // 2, minimum_milliseconds_between_rate_updates=30000, maximum_oracle_price_age_milliseconds=DAY, initial_redemption_price=RAY // 2, stablecoin_name="M1-STABLE") if step in ("all", "life", "init"): tx("SC2 token initialize_account: user stablecoin holding", "token", "token", "initialize_account", definition_account=I["SC_DEF"], account_to_initialize=I["SC_USER_HOLD"]) tx("SC3 open_position nonce 0 with 1e18 A collateral", "stablecoin", "stablecoin", "open_position", owner=I["POS_OWNER"], position=I["POSITION"], vault=I["POS_VAULT"], user_collateral_holding=I["HOLD_A"], collateral_definition=I["DEF_A"], protocol_parameters=I["PROTO_PARAMS"], clock=I["CLOCK"], position_nonce=0, initial_collateral_amount=10**18) tx("SC4 generate_debt 1e17 (well within 1.5x at redemption 0.5)", "stablecoin", "stablecoin", "generate_debt", owner=I["POS_OWNER"], position=I["POSITION"], stablecoin_definition=I["SC_DEF"], user_stablecoin_holding=I["SC_USER_HOLD"], stability_fee_accumulator=I["FEE_ACC"], redemption_price_state=I["REDEMPTION"], market_price_oracle=I["SCO_PRICE"], protocol_parameters=I["PROTO_PARAMS"], clock=I["CLOCK"], amount=10**17) tx("SC5 accrue_stability_fee (permissionless poke)", "stablecoin", "stablecoin", "accrue_stability_fee", caller=I["HOLD_A"], protocol_parameters=I["PROTO_PARAMS"], stability_fee_accumulator=I["FEE_ACC"], clock=I["CLOCK"]) tx("SC6 update_redemption_rate fed by the REAL oracle account (Q64.64 0.5 vs RAY 0.5)", "stablecoin", "stablecoin", "update_redemption_rate", caller=I["HOLD_A"], protocol_parameters=I["PROTO_PARAMS"], redemption_price_state=I["REDEMPTION"], market_price_oracle=I["SCO_PRICE"], clock=I["CLOCK"])