diff --git a/crypto/sol/modules/storage.py b/crypto/sol/modules/storage.py index 2003934..ae4aaaf 100644 --- a/crypto/sol/modules/storage.py +++ b/crypto/sol/modules/storage.py @@ -37,6 +37,7 @@ async def init_db(): buy_value REAL, closed BOOLEAN DEFAULT 0, details TEXT, + solana_signature TEXT UNIQUE, FOREIGN KEY (wallet_id) REFERENCES wallets(id) ); @@ -71,14 +72,68 @@ async def init_db(): """) await db.commit() -async def store_transaction(wallet_id, transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, details=None): +async def store_transaction(wallet_id, transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, details=None): async with aiosqlite.connect(DATABASE_FILE) as db: await db.execute(""" - INSERT INTO transactions (wallet_id, timestamp, type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, details) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - """, (wallet_id, datetime.now().isoformat(), transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, json.dumps(details or {}))) + INSERT INTO transactions (wallet_id, timestamp, type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, details) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, (wallet_id, datetime.now().isoformat(), transaction_type, sell_currency, sell_amount, sell_value, buy_currency, buy_amount, buy_value, solana_signature, json.dumps(details or {}))) await db.commit() +# async def get_new_transactions(wallet_address, rpc_url): +# async with AsyncClient(rpc_url) as client: +# last_tx = await get_last_stored_transaction(wallet_address) + +# if last_tx: +# last_signature, last_timestamp = last_tx +# else: +# # If no transactions are stored, we'll fetch all transactions +# last_signature = None +# last_timestamp = None + +# new_transactions = [] + +# # Get the transaction history for the wallet +# tx_history = await client.get_signatures_for_address(wallet_address, before=last_signature) + +# for tx in tx_history.value: +# # Check if the transaction is newer than the last stored one +# if not last_timestamp or tx.block_time > datetime.fromisoformat(last_timestamp).timestamp(): +# # Fetch the full transaction details +# tx_details = await client.get_transaction(tx.signature, commitment=Confirmed) +# new_transactions.append(tx_details) + +# return new_transactions + +# async def process_new_transactions(wallet_id, wallet_address, rpc_url): +# new_transactions = await get_new_transactions(wallet_address, rpc_url) + +# for tx in new_transactions: +# # Process the transaction and extract relevant information +# # This is a placeholder - you'll need to implement the actual logic based on your requirements +# transaction_type = "swap" # Determine the type based on the transaction data +# sell_currency = "SOL" # Extract from transaction data +# sell_amount = 1.0 # Extract from transaction data +# sell_value = 100.0 # Extract from transaction data +# buy_currency = "USDC" # Extract from transaction data +# buy_amount = 100.0 # Extract from transaction data +# buy_value = 100.0 # Extract from transaction data +# solana_signature = tx.transaction.signatures[0] + +# # Store the transaction in the database +# await store_transaction( +# wallet_id, transaction_type, sell_currency, sell_amount, sell_value, +# buy_currency, buy_amount, buy_value, solana_signature +# ) + +# # Update holdings +# await update_holdings(wallet_id, sell_currency, -sell_amount) +# await update_holdings(wallet_id, buy_currency, buy_amount) + +# # After processing all new transactions, close completed transactions +# await close_completed_transactions(wallet_id) + + async def update_holdings(wallet_id, currency, amount_change): async with aiosqlite.connect(DATABASE_FILE) as db: cursor = await db.execute("SELECT amount FROM holdings WHERE wallet_id = ? AND currency = ?", (wallet_id, currency))