diff --git a/mytoncore/modes.py b/mytoncore/modes.py new file mode 100644 index 00000000..62a64a67 --- /dev/null +++ b/mytoncore/modes.py @@ -0,0 +1,4 @@ +MODES = { # mode_name : is_enabled_by_default + 'Controller': False, + 'Validator': True +} \ No newline at end of file diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 8b79278a..fa6664dd 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -11,6 +11,7 @@ import requests from fastcrc import crc16 +from mytoncore.modes import MODES from mytoncore.utils import xhex2hex, ng2g from mytoncore.liteclient import LiteClient from mytoncore.validator_console import ValidatorConsole @@ -3207,6 +3208,35 @@ def SetSettings(self, name, data): self.local.save() #end define + def get_modes(self): + current_modes = self.local.db.get('modes', {}) + if 'modes' not in self.local.db: + self.local.db['modes'] = current_modes + for mode in MODES: + if mode not in current_modes: + current_modes[mode] = MODES[mode] # assign default mode value + return current_modes + + def enable_mode(self, name): + if name not in MODES: + raise Exception(f'Unknown module name: {name}. Available modes: {", ".join(MODES)}') + current_modes = self.get_modes() + current_modes[name] = True + self.local.save() + + def disable_mode(self, name): + current_modes = self.get_modes() + if name not in current_modes: + raise Exception(f'Unknown module name: {name}. Available modes: {", ".join(MODES)}') + current_modes[name] = False + self.local.save() + + def get_mode_value(self, name): + current_modes = self.get_modes() + if name not in current_modes: + raise Exception(f'No mode named {name} found in current modes: {current_modes}') + return current_modes[name] + def Tlb2Json(self, text): # Заменить скобки start = 0 diff --git a/mytonctrl/modules/__init__.py b/mytonctrl/modules/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mytonctrl/modules/controller.py b/mytonctrl/modules/controller.py new file mode 100644 index 00000000..04b2dd5e --- /dev/null +++ b/mytonctrl/modules/controller.py @@ -0,0 +1,150 @@ +import json +from mypylib.mypylib import color_print, print_table + +from mytonctrl.utils import GetItemFromList +from mytonctrl.modules.module import MtcModule + + +class ControllerModule(MtcModule): + + def create_controllers(self, args): + self.ton.CreateControllers() + color_print("CreateControllers - {green}OK{endc}") + + def print_controllers_list(self, args): + new_controllers = self.ton.GetControllers() + using_controllers = self.ton.GetSettings("using_controllers") + old_controllers = self.ton.GetSettings("old_controllers") + user_controllers_list = self.ton.GetSettings("user_controllers_list") + print("using controllers:") + self.print_controllers_list_process(using_controllers) + if new_controllers != using_controllers: + print() + print("new controllers:") + self.print_controllers_list_process(new_controllers) + if old_controllers is not None and len(old_controllers) > 0: + print() + print("old controllers:") + self.print_controllers_list_process(old_controllers) + if user_controllers_list is not None and len(user_controllers_list) > 0: + print() + print("user controllers:") + self.print_controllers_list_process(user_controllers_list) + + def print_controllers_list_process(self, controllers): + table = list() + table += [["Address", "Status", "Balance", "Approved", "State"]] + for controllerAddr in controllers: + account = self.ton.GetAccount(controllerAddr) + controllerData = self.ton.GetControllerData(controllerAddr) + approved = True if controllerData and controllerData["approved"] == -1 else False + state = controllerData["state"] if controllerData else None + table += [[controllerAddr, account.status, account.balance, approved, state]] + print_table(table) + + def get_controller_data(self, args): + try: + controller_addr = args[0] + except: + color_print("{red}Bad args. Usage:{endc} get_controller_data ") + return + controller_data = self.ton.GetControllerData(controller_addr) + print(json.dumps(controller_data, indent=4)) + + def deposit_to_controller(self, args): + try: + controller_addr = args[0] + amount = float(args[1]) + except: + color_print("{red}Bad args. Usage:{endc} deposit_to_controller ") + return + self.ton.DepositToController(controller_addr, amount) + + def withdraw_from_controller(self, args): + try: + controller_addr = args[0] + amount = GetItemFromList(args, 1) + except: + color_print("{red}Bad args. Usage:{endc} withdraw_from_controller [amount]") + return + self.ton.WithdrawFromController(controller_addr, amount) + + def calculate_annual_controller_percentage(self, args): + try: + percent_per_round = float(args[0]) + except: + percent_per_round = self.ton.GetSettings("max_interest_percent") + config15 = self.ton.GetConfig(15) + roundPeriod = config15["validators_elected_for"] + rounds = 365 * 24 * 3600 / roundPeriod + yearInterest = (1 + percent_per_round / 100) * rounds + yearInterestPercent = round(yearInterest / 100, 2) + print("roundPeriod", roundPeriod) + print("rounds", rounds) + print("percentPerRound", percent_per_round) + print("yearInterest", yearInterest) + print(f"yearInterestPercent: {yearInterestPercent}%") + + def controller_update_validator_set(self, args): + try: + controller_addr = args[0] + except: + color_print("{red}Bad args. Usage:{endc} controller_update_validator_set ") + return + self.ton.ControllerUpdateValidatorSet(controller_addr) + color_print("ControllerUpdateValidatorSet - {green}OK{endc}") + + def stop_controller(self, args): + try: + controller_addr = args[0] + except: + color_print("{red}Bad args. Usage:{endc} stop_controller ") + return + self.ton.StopController(controller_addr) + color_print("StopController - {green}OK{endc}") + + def stop_and_withdraw_controller(self, args): + try: + controller_addr = args[0] + amount = GetItemFromList(args, 1) + except: + color_print("{red}Bad args. Usage:{endc} stop_and_withdraw_controller [amount]") + return + if amount is None: + account = self.ton.GetAccount(controller_addr) + amount = account.balance - 10.1 + self.ton.StopController(controller_addr) + self.ton.WithdrawFromController(controller_addr, amount) + color_print("StopAndWithdrawController - {green}OK{endc}") + + def add_controller(self, args): + try: + controller_addr = args[0] + except: + color_print("{red}Bad args. Usage:{endc} add_controller ") + return + self.ton.AddController(controller_addr) + color_print("AddController - {green}OK{endc}") + + def check_liquid_pool(self, args): + self.ton.CheckLiquidPool() + color_print("CheckLiquidPool - {green}OK{endc}") + + def calculate_loan_amount_test(self, args): + t = self.ton.CalculateLoanAmount_test() + print(t) + + def add_console_commands(self, console): + console.AddItem("create_controllers", self.create_controllers, self.local.translate("_")) + console.AddItem("update_controllers", self.create_controllers, self.local.translate("_")) + console.AddItem("controllers_list", self.print_controllers_list, self.local.translate("_")) + console.AddItem("get_controller_data", self.get_controller_data, self.local.translate("_")) + console.AddItem("deposit_to_controller", self.deposit_to_controller, self.local.translate("_")) + console.AddItem("withdraw_from_controller", self.withdraw_from_controller, self.local.translate("_")) + console.AddItem("calculate_annual_controller_percentage", self.calculate_annual_controller_percentage, self.local.translate("_")) + console.AddItem("controller_update_validator_set", self.controller_update_validator_set, self.local.translate("_")) + console.AddItem("stop_controller", self.stop_controller, self.local.translate("_")) + console.AddItem("stop_and_withdraw_controller", self.stop_and_withdraw_controller, self.local.translate("_")) + console.AddItem("add_controller", self.add_controller, self.local.translate("_")) + console.AddItem("check_liquid_pool", self.check_liquid_pool, self.local.translate("_")) + console.AddItem("test_calculate_loan_amount", self.calculate_loan_amount_test, self.local.translate("_")) diff --git a/mytonctrl/modules/module.py b/mytonctrl/modules/module.py new file mode 100644 index 00000000..6ace8e49 --- /dev/null +++ b/mytonctrl/modules/module.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod +from mytoncore.mytoncore import MyTonCore + + +class MtcModule(ABC): + + def __init__(self, ton, local, *args, **kwargs): + self.ton: MyTonCore = ton + self.local = local + + @abstractmethod + def add_console_commands(self, console): ... diff --git a/mytonctrl/mytonctrl.py b/mytonctrl/mytonctrl.py index 2b47e6a6..babe6fe1 100755 --- a/mytonctrl/mytonctrl.py +++ b/mytonctrl/mytonctrl.py @@ -42,6 +42,7 @@ GetBinGitHash, ) from mytonctrl.migrate import run_migrations +from mytonctrl.utils import GetItemFromList import sys, getopt, os @@ -138,19 +139,10 @@ def inject_globals(func): console.AddItem("activate_single_pool", inject_globals(activate_single_pool), local.translate("activate_single_pool_cmd")) console.AddItem("import_pool", inject_globals(import_pool), local.translate("import_pool_cmd")) - console.AddItem("create_controllers", inject_globals(CreateControllers), local.translate("_")) - console.AddItem("update_controllers", inject_globals(CreateControllers), local.translate("_")) - console.AddItem("controllers_list", inject_globals(PrintControllersList), local.translate("_")) - console.AddItem("get_controller_data", inject_globals(GetControllerData), local.translate("_")) - console.AddItem("deposit_to_controller", inject_globals(DepositToController), local.translate("_")) - console.AddItem("withdraw_from_controller", inject_globals(WithdrawFromController), local.translate("_")) - console.AddItem("calculate_annual_controller_percentage", inject_globals(CalculateAnnualControllerPercentage), local.translate("_")) - console.AddItem("controller_update_validator_set", inject_globals(ControllerUpdateValidatorSet), local.translate("_")) - console.AddItem("stop_controller", inject_globals(StopController), local.translate("_")) - console.AddItem("stop_and_withdraw_controller", inject_globals(StopAndWithdrawController), local.translate("_")) - console.AddItem("add_controller", inject_globals(AddController), local.translate("_")) - console.AddItem("check_liquid_pool", inject_globals(CheckLiquidPool), local.translate("_")) - console.AddItem("test_calculate_loan_amount", inject_globals(CalculateLoanAmount_test), local.translate("_")) + if ton.get_mode_value('Controller'): + from mytonctrl.modules.controller import ControllerModule + module = ControllerModule(ton, local) + module.add_console_commands(console) # console.AddItem("pt", inject_globals(PrintTest), "PrintTest") # console.AddItem("sl", inject_globals(sl), "sl") @@ -199,12 +191,6 @@ def Installer(args): subprocess.run(args) #end define -def GetItemFromList(data, index): - try: - return data[index] - except: pass -#end define - def GetAuthorRepoBranchFromArgs(args): data = dict() arg1 = GetItemFromList(args, 0) @@ -1250,6 +1236,27 @@ def SetSettings(ton, args): color_print("SetSettings - {green}OK{endc}") #end define + +def enable_mode(ton, args): + try: + name = args[0] + except: + color_print("{red}Bad args. Usage:{endc} enable_mode ") + return + ton.enable_mode(name) + color_print("enable_mode - {green}OK{endc}") + + +def disable_mode(ton, args): + try: + name = args[0] + except: + color_print("{red}Bad args. Usage:{endc} disable_mode ") + return + ton.disable_mode(name) + color_print("disable_mode - {green}OK{endc}") + + def Xrestart(inputArgs): if len(inputArgs) < 2: color_print("{red}Bad args. Usage:{endc} xrestart ") @@ -1503,146 +1510,6 @@ def import_pool(ton, args): color_print("import_pool - {green}OK{endc}") #end define -def CreateControllers(ton, args): - ton.CreateControllers() - color_print("CreateControllers - {green}OK{endc}") -#end define - -def PrintControllersList(ton, args): - new_controllers = ton.GetControllers() - using_controllers = ton.GetSettings("using_controllers") - old_controllers = ton.GetSettings("old_controllers") - user_controllers_list = ton.GetSettings("user_controllers_list") - print("using controllers:") - PrintControllersListProcess(ton, using_controllers) - if new_controllers != using_controllers: - print() - print("new controllers:") - PrintControllersListProcess(ton, new_controllers) - if old_controllers is not None and len(old_controllers) > 0: - print() - print("old controllers:") - PrintControllersListProcess(ton, old_controllers) - if user_controllers_list is not None and len(user_controllers_list) > 0: - print() - print("user controllers:") - PrintControllersListProcess(ton, user_controllers_list) -#end define - -def PrintControllersListProcess(ton, controllers): - table = list() - table += [["Address", "Status", "Balance", "Approved", "State"]] - for controllerAddr in controllers: - account = ton.GetAccount(controllerAddr) - controllerData = ton.GetControllerData(controllerAddr) - approved = True if controllerData and controllerData["approved"] == -1 else False - state = controllerData["state"] if controllerData else None - table += [[controllerAddr, account.status, account.balance, approved, state]] - print_table(table) -#end define - -def GetControllerData(ton, args): - try: - controllerAddr = args[0] - except: - color_print("{red}Bad args. Usage:{endc} get_controller_data ") - return - controllerData = ton.GetControllerData(controllerAddr) - print(json.dumps(controllerData, indent=4)) -#end define - -def DepositToController(ton, args): - try: - controllerAddr = args[0] - amount = float(args[1]) - except: - color_print("{red}Bad args. Usage:{endc} deposit_to_controller ") - return - ton.DepositToController(controllerAddr, amount) -#end define - -def WithdrawFromController(ton, args): - try: - controllerAddr = args[0] - amount = GetItemFromList(args, 1) - except: - color_print("{red}Bad args. Usage:{endc} withdraw_from_controller [amount]") - return - ton.WithdrawFromController(controllerAddr, amount) -#end define - -def CalculateAnnualControllerPercentage(ton, args): - try: - percentPerRound = float(args[0]) - except: - percentPerRound = ton.GetSettings("max_interest_percent") - config15 = ton.GetConfig(15) - roundPeriod = config15["validators_elected_for"] - rounds = 365 * 24 * 3600 / roundPeriod - yearInterest = (1 + percentPerRound / 100) * rounds - yearInterestPercent = round(yearInterest / 100, 2) - print("roundPeriod", roundPeriod) - print("rounds", rounds) - print("percentPerRound", percentPerRound) - print("yearInterest", yearInterest) - print(f"yearInterestPercent: {yearInterestPercent}%") -#end define - -def ControllerUpdateValidatorSet(ton, args): - try: - controllerAddr = args[0] - except: - color_print("{red}Bad args. Usage:{endc} controller_update_validator_set ") - return - ton.ControllerUpdateValidatorSet(controllerAddr) - color_print("ControllerUpdateValidatorSet - {green}OK{endc}") -#end define - -def StopController(ton, args): - try: - controllerAddr = args[0] - except: - color_print("{red}Bad args. Usage:{endc} stop_controller ") - return - ton.StopController(controllerAddr) - color_print("StopController - {green}OK{endc}") -#end define - -def StopAndWithdrawController(ton, args): - try: - controllerAddr = args[0] - amount = GetItemFromList(args, 1) - except: - color_print("{red}Bad args. Usage:{endc} stop_and_withdraw_controller [amount]") - return - if amount is None: - account = ton.GetAccount(controllerAddr) - amount = account.balance-10.1 - ton.StopController(controllerAddr) - ton.WithdrawFromController(controllerAddr, amount) - color_print("StopAndWithdrawController - {green}OK{endc}") -#end define - -def AddController(ton, args): - try: - controllerAddr = args[0] - except: - color_print("{red}Bad args. Usage:{endc} add_controller ") - return - ton.AddController(controllerAddr) - color_print("AddController - {green}OK{endc}") -#end define - -def CheckLiquidPool(ton, args): - ton.CheckLiquidPool() - color_print("CheckLiquidPool - {green}OK{endc}") -#end define - -def CalculateLoanAmount_test(ton, args): - t = ton.CalculateLoanAmount_test() - print(t) - - ### ### Start of the program ### diff --git a/mytonctrl/utils.py b/mytonctrl/utils.py new file mode 100644 index 00000000..01db5491 --- /dev/null +++ b/mytonctrl/utils.py @@ -0,0 +1,5 @@ +def GetItemFromList(data, index): + try: + return data[index] + except: + pass