Files
hyper/clp_auto_hedger/check_stuck_position.py

72 lines
3.1 KiB
Python

#!/usr/bin/env python3
import os
import json
from web3 import Web3
from eth_account import Account
from dotenv import load_dotenv
# Load environment
load_dotenv()
# Configuration
RPC_URL = os.environ.get("MAINNET_RPC_URL")
PRIVATE_KEY = os.environ.get("MAIN_WALLET_PRIVATE_KEY") or os.environ.get("PRIVATE_KEY")
# ABI (minimal for positions function)
NONFUNGIBLE_POSITION_MANAGER_ABI = json.loads('''
[
{"inputs": [{"internalType": "uint256", "name": "tokenId", "type": "uint256"}], "name": "positions", "outputs": [{"internalType": "uint96", "name": "nonce", "type": "uint96"}, {"internalType": "address", "name": "operator", "type": "address"}, {"internalType": "address", "name": "token0", "type": "address"}, {"internalType": "address", "name": "token1", "type": "address"}, {"internalType": "uint24", "name": "fee", "type": "uint24"}, {"internalType": "int24", "name": "tickLower", "type": "int24"}, {"internalType": "int24", "name": "tickUpper", "type": "int24"}, {"internalType": "uint128", "name": "liquidity", "type": "uint128"}, {"internalType": "uint256", "name": "feeGrowthInside0LastX128", "type": "uint256"}, {"internalType": "uint256", "name": "feeGrowthInside1LastX128", "type": "uint256"}, {"internalType": "uint128", "name": "tokensOwed0", "type": "uint128"}, {"internalType": "uint128", "name": "tokensOwed1", "type": "uint128"}], "stateMutability": "view", "type": "function"}
]
''')
NONFUNGIBLE_POSITION_MANAGER_ADDRESS = "0xC36442b4a4522E871399CD71a7BDD847Ab11FE88"
def main():
if not RPC_URL:
print("Missing RPC URL")
return
w3 = Web3(Web3.HTTPProvider(RPC_URL))
if not w3.is_connected():
print("Failed to connect to RPC")
return
print(f"Connected to Chain ID: {w3.eth.chain_id}")
npm_contract = w3.eth.contract(address=NONFUNGIBLE_POSITION_MANAGER_ADDRESS, abi=NONFUNGIBLE_POSITION_MANAGER_ABI)
# Check the stuck position
token_id = 5167004
print(f"Checking position {token_id}...")
try:
position_data = npm_contract.functions.positions(token_id).call()
liquidity = position_data[7]
print(f"Position {token_id} liquidity: {liquidity}")
if liquidity == 0:
print("✅ Position has 0 liquidity - should be marked CLOSED")
# Update hedge_status.json
with open('hedge_status.json', 'r') as f:
data = json.load(f)
for entry in data:
if entry.get('token_id') == token_id and entry.get('status') == 'CLOSING':
entry['status'] = 'CLOSED'
entry['timestamp_close'] = int(time.time())
print(f"Updated position {token_id} to CLOSED")
break
with open('hedge_status.json', 'w') as f:
json.dump(data, f, indent=2)
else:
print(f"❌ Position still has {liquidity} liquidity")
except Exception as e:
print(f"Error checking position: {e}")
if __name__ == "__main__":
import time
main()