From fd17cc9c04cea96f908d1e2d1792bafee4b777d4 Mon Sep 17 00:00:00 2001 From: wystan_rin Date: Mon, 11 Nov 2024 15:32:36 +0800 Subject: [PATCH] developing --- api/tronscan.py | 36 ++++++++++++++++++++++-------------- config/settings.py | 7 ++----- repositories/order.py | 5 ++++- services/order.py | 25 ++++++++++++++++++++----- services/payment.py | 20 +++++++++++--------- utils/tronscan.py | 2 ++ 6 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 utils/tronscan.py diff --git a/api/tronscan.py b/api/tronscan.py index 3be1c96..36395dc 100644 --- a/api/tronscan.py +++ b/api/tronscan.py @@ -1,6 +1,7 @@ import requests from custom_decorators import singleton +from utils.tronscan import convert_to_tronscan_timestamp trc20token_info = { "usdt": {"tokenId": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", @@ -51,9 +52,9 @@ class Tronscan: } if start_timestamp is not None: - params["start_timestamp"] = start_timestamp + params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp) if end_timestamp is not None: - params["end_timestamp"] = end_timestamp + params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp) if from_address is not None: params["fromAddress"] = from_address if to_address is not None: @@ -90,7 +91,7 @@ class Tronscan: ) return response.json() - def token_trc20_transfers(self, start=0, limit=10, contract_address=None, + def token_trc20_transfers(self, start=None, limit=None, contract_address=None, start_timestamp=None, end_timestamp=None, confirm=True, related_address=None, from_address=None, to_address=None): """ @@ -107,17 +108,18 @@ class Tronscan: :return: Get the transfer list of TRC20 and TRC721 tokens. """ params = { - "start": start, - "limit": limit, "filterTokenValue": 1 } - + if start is not None: + params["start"] = start + if limit is not None: + params["limit"] = limit if contract_address is not None: params["contract_address"] = contract_address if start_timestamp is not None: - params["start_timestamp"] = start_timestamp + params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp) if end_timestamp is not None: - params["end_timestamp"] = end_timestamp + params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp) if confirm is not None: params["confirm"] = str(confirm).lower() if related_address is not None: @@ -302,8 +304,6 @@ class Tronscan: """ params = { "address": address, - "start_timestamp": start_timestamp, - "end_timestamp": end_timestamp, "start": start, "limit": limit, "direction": direction, @@ -311,6 +311,10 @@ class Tronscan: "reverse": reverse, "fee": fee, } + if start_timestamp is not None: + params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp) + if end_timestamp is not None: + params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp) response = requests.get("https://apilist.tronscanapi.com/api/transfer/trx", headers={'TRON-PRO-API-KEY': self.api_key}, params=params) @@ -335,14 +339,16 @@ class Tronscan: params = { "address": address, "trc10Id": trc10Id, - "start_timestamp": start_timestamp, - "end_timestamp": end_timestamp, "start": start, "limit": limit, "direction": direction, "db_version": db_version, "reverse": reverse, } + if start_timestamp is not None: + params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp) + if end_timestamp is not None: + params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp) response = requests.get("https://apilist.tronscanapi.com/api/transfer/token10", headers={'TRON-PRO-API-KEY': self.api_key}, params=params) @@ -367,14 +373,16 @@ class Tronscan: params = { "address": address, "trc20Id": trc20Id, - "start_timestamp": start_timestamp, - "end_timestamp": end_timestamp, "start": start, "limit": limit, "direction": direction, "db_version": db_version, "reverse": reverse, } + if start_timestamp is not None: + params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp) + if end_timestamp is not None: + params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp) response = requests.get("https://apilist.tronscanapi.com/api/transfer/trc20", headers={'TRON-PRO-API-KEY': self.api_key}, params=params) diff --git a/config/settings.py b/config/settings.py index 67f6c82..4c79049 100644 --- a/config/settings.py +++ b/config/settings.py @@ -98,10 +98,7 @@ def log_config(config): logger.remove() logger.add(sys.stdout, level=config.log_level) logger.add(sys.stderr, level="ERROR") - logger.add(os.path.join(ROOT_DIR, "logs", "recommender_{time}.log"), level="DEBUG", encoding='utf8', rotation="100 MB", retention=3) - - -nacos_url = f"http://{os.getenv('NACOS_SOCKET', '192.168.15.40:38865')}/nacos/v1/cs/configs" + logger.add(os.path.join(ROOT_DIR, "logs", "{time}.log"), level="DEBUG", encoding='utf8', rotation="100 MB", retention=3) def get_config() -> Config: @@ -109,7 +106,7 @@ def get_config() -> Config: configparser = ConfigParser() configparser.read(fr'{ROOT_DIR}/config_file/param.conf') - parser = argparse.ArgumentParser(description='recommendation system') + parser = argparse.ArgumentParser(description='payment system') parser.add_argument("--seed", type=int, default=2024) args = parser.parse_args() config = Config(configparser, args) diff --git a/repositories/order.py b/repositories/order.py index e6fbb9b..cb92484 100644 --- a/repositories/order.py +++ b/repositories/order.py @@ -1,3 +1,5 @@ +from ruamel_yaml.util import create_timestamp + from custom_decorators import singleton @@ -7,11 +9,12 @@ class OrderRepository: self.db = None def create(self, order_id, from_address, to_address): + create_time = create_timestamp() pass def update_status(self, order_id, status): # 更新状态和时间 pass - def get_creation_time(self, order_id): + def get_order_info(self, order_id): pass diff --git a/services/order.py b/services/order.py index c32c7e6..3d944ec 100644 --- a/services/order.py +++ b/services/order.py @@ -2,6 +2,7 @@ import uuid from custom_decorators import singleton from repositories.order import OrderRepository +from services.payment import PaymentService from utils.datetime import current, current_timestamp, is_time_difference_greater_than @@ -9,6 +10,7 @@ from utils.datetime import current, current_timestamp, is_time_difference_greate class OrderService: def __init__(self): self.order_repo = OrderRepository() + self.payment_service = PaymentService() def create_order(self, from_address, to_address, *args, **kwargs): date_str = current().strftime('%Y%m%d%H%M%S') @@ -19,10 +21,23 @@ class OrderService: def finish_order(self, order_id): # 判断支付时间是否超过订单存活时间 - order_creation_time = self.order_repo.get_creation_time(order_id) - if is_time_difference_greater_than(current_timestamp(), order_creation_time, minutes=15): - status = 2 + quant, from_address, to_address, creation_timestamp = self.order_repo.get_order_info(order_id) + current = current_timestamp() + status = 0 + if is_time_difference_greater_than(creation_timestamp, current, minutes=15): + # 订单超时 + status = 4 else: - status = 1 - self.order_repo.update_status(order_id, status) + correct_quant, confirmed = self.payment_service.check_payment(quant, from_address, to_address, creation_timestamp, current) + if correct_quant and confirmed: + # 支付成功 + status = 1 + elif correct_quant < 0: + # 没有转账 + status = 2 + elif confirmed: + # 金额不对 + status = 3 + if status: + self.order_repo.update_status(order_id, status) return status diff --git a/services/payment.py b/services/payment.py index 06fc204..b602376 100644 --- a/services/payment.py +++ b/services/payment.py @@ -4,18 +4,20 @@ from utils.datetime import current_timestamp @singleton -class Payment: +class PaymentService: def __init__(self, api_key): self.tronscan = Tronscan(api_key) - def check_payment(self, quant, from_address, to_address, order_create_timestamp): - result = self.tronscan.token_trc20_transfers(from_address=from_address, - to_address=to_address, - start_timestamp=order_create_timestamp, - end_timestamp=int(current_timestamp() * 1000)) + def check_payment(self, quant, from_address, to_address, order_create_timestamp, end_timestamp=None): + if end_timestamp is None: + end_timestamp = current_timestamp() + result = self.tronscan.token_trc20_transfers(limit=100, + from_address=from_address, to_address=to_address, + start_timestamp=order_create_timestamp, end_timestamp=end_timestamp) + if result['rangeTotal'] == 0: + return -1, 0 token_transfers = result['token_transfers'] token_transfer = token_transfers[-1] confirmed = token_transfer['confirmed'] - correct_quant = quant == (token_transfer['quant'] / 6) - confirmed = confirmed and correct_quant - return confirmed + correct_quant = int(quant == (token_transfer['quant'] / 6)) + return correct_quant, confirmed diff --git a/utils/tronscan.py b/utils/tronscan.py new file mode 100644 index 0000000..65310b4 --- /dev/null +++ b/utils/tronscan.py @@ -0,0 +1,2 @@ +def convert_to_tronscan_timestamp(timestamp): + return int(timestamp * 1000)