#!/usr/bin/env python3
import subprocess
import json
def check_balance(token):
"""Check token balance"""
result = subprocess.run(
["cavos", "balance", "--token", token, "--json"],
capture_output=True, text=True
)
data = json.loads(result.stdout)
return float(data["balance"])
def swap_tokens(amount_in, token_in, token_out, swap_contract):
"""Approve + Swap tokens atomically"""
calls = [
{
"contract": token_in,
"entrypoint": "approve",
"calldata": f"{swap_contract},{amount_in},0"
},
{
"contract": swap_contract,
"entrypoint": "swap",
"calldata": f"{token_in},{token_out},{amount_in}"
}
]
result = subprocess.run([
"cavos", "multicall",
"--calls", json.dumps(calls),
"--wait",
"--json"
], capture_output=True, text=True)
return json.loads(result.stdout)
# Main loop
while True:
price = get_price_from_oracle() # Your price oracle
if price > TARGET_PRICE:
balance = check_balance("STRK")
if balance > 10:
result = swap_tokens(
amount_in="10000000000000000000", # 10 STRK
token_in="0x049d36...",
token_out="0x053c91...",
swap_contract="0x041fd..."
)
print(f"Swap executed: {result['txHash']}")
time.sleep(60)