forked from OpenGradient/BitQuant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap_tracker.py
More file actions
86 lines (74 loc) · 2.71 KB
/
Copy pathswap_tracker.py
File metadata and controls
86 lines (74 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from datetime import datetime, timezone
from typing import Optional
from server.dynamodb_helpers import TableContext
class SwapTracker:
"""
A class for tracking processed JUP swap transactions to prevent duplicates.
"""
def __init__(self, get_table: callable):
"""
Initialize the SwapTracker with a function that returns an async DynamoDB table.
"""
self.get_table = get_table
async def is_swap_processed(self, chain: str, txid: str) -> bool:
"""
Check if a swap transaction has already been processed.
Args:
txid: The transaction ID to check
Returns:
True if the transaction has been processed, False otherwise
"""
try:
async with self.get_table() as table:
response = await table.get_item(Key={"txid": f"{chain}:{txid}"})
return "Item" in response
except Exception as e:
# If there's an error checking, assume it's not processed
# This prevents blocking on DynamoDB errors
return False
async def mark_swap_processed(
self,
chain: str,
txid: str,
user_address: str,
referral_reward: float,
points_awarded: int,
) -> bool:
"""
Mark a swap transaction as processed.
Args:
txid: The transaction ID
user_address: The user's wallet address
referral_reward: The calculated referral reward amount
points_awarded: The points awarded to the user
Returns:
True if successfully marked as processed, False otherwise
"""
try:
async with self.get_table() as table:
await table.put_item(
Item={
"txid": f"{chain}:{txid}",
"user_address": user_address,
"processed_at": datetime.now(timezone.utc).isoformat(),
"referral_reward": referral_reward,
"points_awarded": points_awarded,
}
)
return True
except Exception as e:
return False
async def get_swap_details(self, chain: str, txid: str) -> Optional[dict]:
"""
Get details of a processed swap transaction.
Args:
txid: The transaction ID
Returns:
Dictionary with swap details if found, None otherwise
"""
try:
async with self.get_table() as table:
response = await table.get_item(Key={"txid": f"{chain}:{txid}"})
return response.get("Item")
except Exception:
return None