Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mytoncore/modes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODES = { # mode_name : is_enabled_by_default
'Controller': False,
'Validator': True
}
30 changes: 30 additions & 0 deletions mytoncore/mytoncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Empty file added mytonctrl/modules/__init__.py
Empty file.
150 changes: 150 additions & 0 deletions mytonctrl/modules/controller.py
Original file line number Diff line number Diff line change
@@ -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 <controller-addr>")
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 <controller-addr> <amount>")
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 <controller-addr> [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 <controller-addr>")
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 <controller-addr>")
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 <controller-addr> [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 <controller-addr>")
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("_"))
12 changes: 12 additions & 0 deletions mytonctrl/modules/module.py
Original file line number Diff line number Diff line change
@@ -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): ...
Loading