From 569893b0aa665fba34ccb72b64efcc43bec715ee Mon Sep 17 00:00:00 2001 From: user Date: Wed, 21 Aug 2024 23:49:56 +0300 Subject: [PATCH] add monitor script & edit default license type --- app/api/routes/_blockchain.py | 111 ++++++++++++------------- app/core/background/indexer_service.py | 5 ++ scripts/constants.py | 4 + scripts/fetch_status.py | 82 ++++++++++++++++++ 4 files changed, 146 insertions(+), 56 deletions(-) create mode 100644 scripts/constants.py create mode 100644 scripts/fetch_status.py diff --git a/app/api/routes/_blockchain.py b/app/api/routes/_blockchain.py index 455a67d..2159a10 100644 --- a/app/api/routes/_blockchain.py +++ b/app/api/routes/_blockchain.py @@ -49,9 +49,8 @@ async def s_api_v1_blockchain_send_new_content_message(request): assert field_key in request.json, f"No {field_key} provided" assert field_value(request.json[field_key]), f"Invalid {field_key} provided" - ton_connect, ton_connection = TonConnect.by_user(request.ctx.db_session, request.ctx.user, callback_fn=()) - await ton_connect.restore_connection() - assert ton_connect.connected, "No connected wallet" + wallet_connection = request.ctx.user.wallet_connection(request.ctx.db_session) + assert wallet_connection, "No wallet connection found" decrypted_content_cid, err = resolve_content(request.json['content']) assert not err, f"Invalid content CID" @@ -93,66 +92,45 @@ async def s_api_v1_blockchain_send_new_content_message(request): ) i += 1 - request.app.add_task(ton_connect._sdk_client.send_transaction({ - 'valid_until': int(datetime.now().timestamp() + 120), - 'messages': [ - { - 'address': platform.address.to_string(1, 1, 1), - 'amount': str(int(0.15 * 10 ** 9)), - 'payload': b64encode( + return response.json({ + 'address': platform.address.to_string(1, 1, 1), + 'amount': str(int(0.15 * 10 ** 9)), + 'payload': b64encode( + begin_cell() + .store_uint(0x5491d08c, 32) + .store_uint(int.from_bytes(encrypted_content_cid.content_hash, "big", signed=False), 256) + .store_ref( begin_cell() - .store_uint(0x5491d08c, 32) - .store_uint(int.from_bytes(encrypted_content_cid.content_hash, "big", signed=False), 256) .store_ref( begin_cell() - .store_ref( - begin_cell() - .store_coins(int(request.json['price'])) - .store_coins(int(100000000 * 10 ** 9)) - .store_coins(int(500000000 * 10 ** 9)) - .end_cell() - ) - .store_maybe_ref(royalties_dict.end_dict()) - .store_uint(0, 1) + .store_coins(int(800000000 * 10 ** 9)) + .store_coins(int(800000000 * 10 ** 9)) + .store_coins(int(request.json['price'])) + .end_cell() + ) + .store_maybe_ref(royalties_dict.end_dict()) + .store_uint(0, 1) + .end_cell() + ) + .store_ref( + begin_cell() + .store_ref( + begin_cell() + .store_bytes(f"{PROJECT_HOST}/api/v1/storage/{metadata_content.cid.serialize_v2(include_accept_type=True)}".encode()) .end_cell() ) .store_ref( begin_cell() - .store_ref( - begin_cell() - .store_bytes(f"{PROJECT_HOST}/api/v1/storage/{metadata_content.cid.serialize_v2(include_accept_type=True)}".encode()) - .end_cell() - ) - .store_ref( - begin_cell() - .store_ref(begin_cell().store_bytes(f"{encrypted_content_cid.serialize_v2()}".encode()).end_cell()) - .store_ref(begin_cell().store_bytes(f"{image_content_cid.serialize_v2() if image_content_cid else ''}".encode()).end_cell()) - .store_ref(begin_cell().store_bytes(f"{metadata_content.cid.serialize_v2()}".encode()).end_cell()) - .end_cell() - ) + .store_ref(begin_cell().store_bytes(f"{encrypted_content_cid.serialize_v2()}".encode()).end_cell()) + .store_ref(begin_cell().store_bytes(f"{image_content_cid.serialize_v2() if image_content_cid else ''}".encode()).end_cell()) + .store_ref(begin_cell().store_bytes(f"{metadata_content.cid.serialize_v2()}".encode()).end_cell()) .end_cell() ) - .end_cell().to_boc(False) - ).decode() - } - ] - })) - - await request.ctx.user_uploader_wrapper.send_message( - request.ctx.user.translated('p_tonconnectTransactionRequested'), - reply_markup=get_inline_keyboard([ - [{ - 'text': request.ctx.user.translated('gotoWallet_button'), - 'url': wallet_obj_by_name(ton_connection.wallet_key.split('==')[0])['universal_url'] - }], - [{ - 'text': request.ctx.user.translated('home_button'), - 'callback_data': 'home' - }] - ]) - ) - - return response.json({"message": "Transaction requested"}) + .end_cell() + ) + .end_cell().to_boc(False) + ).hex() + }) except BaseException as e: make_log("Blockchain", f"Error while sending new content message: {e}" + '\n' + traceback.format_exc(), level='error') return response.json({"error": str(e)}, status=400) @@ -163,9 +141,30 @@ async def s_api_v1_blockchain_send_purchase_content_message(request): for field_key, field_value in { 'content_address': lambda x: isinstance(x, str), - # 'price': lambda x: (isinstance(x, str) and x.isdigit()), + 'license_type': lambda x: x in ['resale'] }.items(): assert field_key in request.json, f"No {field_key} provided" assert field_value(request.json[field_key]), f"Invalid {field_key} provided" - return response.json({"message": "Transaction requested"}) + r_content = StoredContent.from_cid(request.ctx.db_session, request.json['content_address']) + content = r_content.open_content(request.ctx.db_session) + + licenses_cost = content['encrypted_content'].json_format()['license'] + assert request.json['lisense_type'] in licenses_cost + + return response.json({ + 'address': request.json['content_address'], + 'amount': str(int(licenses_cost['resale']['price'])), + 'payload': ( + begin_cell() + .store_uint(0x2a319593, 32) + .store_uint(0, 64) + .store_uint(3, 8) + # .store_uint({ + # 'listen': 1, + # 'resale': 3 + # }[request.json['license_type']], 8) + .store_uint(0, 256) + .end_cell() + ).to_boc(False).hex() + }) diff --git a/app/core/background/indexer_service.py b/app/core/background/indexer_service.py index 019d023..a4d25e1 100644 --- a/app/core/background/indexer_service.py +++ b/app/core/background/indexer_service.py @@ -91,6 +91,8 @@ async def indexer_loop(memory, platform_found: bool, seqno: int) -> [bool, int]: item_distribution_slice = item_distribution.begin_parse() item_prices_slice = item_distribution_slice.refs[0].begin_parse() item_listen_license_price = item_prices_slice.read_coins() + item_use_license_price = item_prices_slice.read_coins() + item_resale_license_price = item_prices_slice.read_coins() item_values_slice = item_values.begin_parse() item_content_hash_int = item_values_slice.read_uint(256) @@ -118,6 +120,9 @@ async def indexer_loop(memory, platform_found: bool, seqno: int) -> [bool, int]: 'license': { 'listen': { 'price': str(item_listen_license_price) + }, + 'resale': { + 'price': str(item_listen_license_price) } } } diff --git a/scripts/constants.py b/scripts/constants.py new file mode 100644 index 0000000..5e4042b --- /dev/null +++ b/scripts/constants.py @@ -0,0 +1,4 @@ + +NODES_URL_LIST = [ + 'https://my-public-node-1.projscale.dev' +] diff --git a/scripts/fetch_status.py b/scripts/fetch_status.py new file mode 100644 index 0000000..dec89bc --- /dev/null +++ b/scripts/fetch_status.py @@ -0,0 +1,82 @@ +import string +from datetime import datetime + +from constants import NODES_URL_LIST +from colorama import Fore, Back, Style +import httpx +import time +import re + + +def print_node_status(i, node_url): + result_print = '' + + if i == 0: + result_print += '\n' * 50 + else: + result_print += '=' * 50 + '\n' + + result_print += f"{Back.MAGENTA}{Fore.WHITE}Node #{i} {Fore.YELLOW}{node_url.split('://')[-1]}{Fore.WHITE} status ({datetime.now().strftime('%H:%M:%S %d.%m')}):{Back.BLUE}{Fore.WHITE}\n" + node_info = httpx.get(f"{node_url}/api/v1/node").json() + services_status = '' + for service_key in node_info['services']: + services_status += ' ' + f"{service_key}:" + '\n' + services_status += ' ' + f"Status: {node_info['services'][service_key]['status']}" + '\n' + services_status += ' ' + f"Delay: {node_info['services'][service_key]['delay']}" + '\n' + + result_print += f"""ID: {node_info['id']} +Blockchain: + Node address: {node_info['node_address']} + Master address: {node_info['master_address']} + Master state: + Indexer height: {node_info['indexer_height']} +Services: +{services_status} +""" + + content_print = 'Known content:\n' + content_list = httpx.get(f"{node_url}/api/v1/content.list?store=onchain").json() + for content_cid in content_list: + if content_list[content_cid]['onchain_index'] < (node_info['indexer_height'] - 15): + continue + + content_metadata = httpx.get(f"{node_url}/api/v1/content.view/{content_cid}").json() + if 'error' in content_metadata: + continue + + content_print += f"{content_list[content_cid]['onchain_index']}. {content_metadata['display_options']['metadata']['name']}" + '\n' + content_print += '\t ' + f"https://tonviewer.com/{content_metadata['content_address']}" + '\n' + content_print += '\t ' + f"CID: {content_cid}" + '\n' + content_print += '\t ' + f"Indexed at {datetime.fromisoformat(content_metadata['encrypted']['created']).strftime('%H:%M:%S %d.%m.%Y')}" + '\n' + + content_print = content_print.splitlines() + if result_print: + passed_id_line = False + for i, line in enumerate(result_print.splitlines()): + if line.startswith(f"ID: "): + passed_id_line = True + + if not line: + print('') + else: + content_line = ('| ' + content_print.pop(0)) if (content_print and passed_id_line) else '' + line_chars = len(re.sub(r'\x1b\[[0-9;]*m', '', line)) + line = line.ljust(66 + (len(line) - line_chars)) + print(f"{line} {content_line}") + + print(f"{Style.RESET_ALL}") + + +def main(): + while True: + for i, node_url in enumerate(NODES_URL_LIST): + try: + print_node_status(i, node_url) + except BaseException as e: + print(f"Error while fetching node status /api/v1/node: {e}") + + time.sleep(5) + + +if __name__ == '__main__': + main()