From 7b2a25592ec099fe276f094b447f2b25a843d69d Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Mon, 3 Jun 2024 16:12:46 +1000 Subject: [PATCH] feast: Add transaction history api route --- hsd.py | 41 +++++++++++++++++++++++++++++++++++++---- main.py | 7 +++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/hsd.py b/hsd.py index a8190ef..ab3a53f 100644 --- a/hsd.py +++ b/hsd.py @@ -1,4 +1,5 @@ -from functools import cache +import functools +import time import requests import dotenv import os @@ -41,6 +42,27 @@ if not os.path.exists(wallet_file): with open(wallet_file, 'w') as f: json.dump([], f, indent=4) +def cache_with_ttl(max_age, maxsize=128, typed=False): + """Least-recently-used cache decorator with time-based cache invalidation. + + Args: + max_age: Time to live for cached results (in seconds). + maxsize: Maximum cache size (see `functools.lru_cache`). + typed: Cache on distinct input types (see `functools.lru_cache`). + """ + def _decorator(fn): + @functools.lru_cache(maxsize=maxsize, typed=typed) + def _new(*args, __time_salt, **kwargs): + return fn(*args, **kwargs) + + @functools.wraps(fn) + def _wrapped(*args, **kwargs): + return _new(*args, **kwargs, __time_salt=int(time.time() / max_age)) + + return _wrapped + + return _decorator + def url(walletURL:bool) -> str: if walletURL: @@ -159,7 +181,7 @@ def import_wallet(name:str,userID:str,xpub:str) -> dict: } -@cache +@cache_with_ttl(60*60) def get_wallet_UUID(userID:str, name:str) -> str|None: accounts = get_accounts() @@ -217,7 +239,7 @@ def get_balance(userID:str, name:str) -> dict: return return_json -@cache +@cache_with_ttl(60*1) def get_domains(userID:str, name:str) -> dict: walletID = get_wallet_UUID(userID, name) if not walletID: @@ -327,11 +349,22 @@ def send_to_address(userID:str, name:str, address:str, amount:str) -> dict: return response.json() +@cache_with_ttl(60*1) +def get_transactions(userID:str, name:str) -> dict: + walletID = get_wallet_UUID(userID, name) + if not walletID: + return { + 'error': 'Wallet not found' + } + + request = requests.get(url(True)+'wallet/'+walletID+'/tx/history').json() + return request + # region Auctions -@cache +@cache_with_ttl(60*1) def get_auctions(userID:str, name:str) -> dict: walletID = get_wallet_UUID(userID, name) if not walletID: diff --git a/main.py b/main.py index ea01580..7ec9228 100644 --- a/main.py +++ b/main.py @@ -75,6 +75,13 @@ def get_domains(): domain_json = hsd.get_domains(userID, name) return jsonify(domain_json) +@app.route('/wallet/transactions', methods=['GET']) +def get_transactions(): + userID = request.args.get('uuid') + name = request.args.get('name') + transaction_json = hsd.get_transactions(userID, name) + return jsonify(transaction_json) + @app.route('/wallet/send', methods=['POST']) def send(): userID = request.args.get('uuid')