From c651f6eb43e68bfdbd2c1e5c6a539efeba588f21 Mon Sep 17 00:00:00 2001 From: yungwine Date: Fri, 22 Mar 2024 20:39:59 +0800 Subject: [PATCH] add download pool scripts for single-nom, import-pool; move CreatePool to modules --- modules/nominator_pool.py | 33 ++++++++++++++++++++++++++++++++- modules/pool.py | 18 +++++++++++++++++- modules/single_pool.py | 2 ++ mytoncore/mytoncore.py | 38 -------------------------------------- 4 files changed, 51 insertions(+), 40 deletions(-) diff --git a/modules/nominator_pool.py b/modules/nominator_pool.py index 0f3b90f5..dc2eec53 100644 --- a/modules/nominator_pool.py +++ b/modules/nominator_pool.py @@ -7,6 +7,37 @@ class NominatorPoolModule(PoolModule): + def do_create_pool(self, pool_name, validator_reward_share_percent, max_nominators_count, min_validator_stake, + min_nominator_stake): + self.ton.local.add_log("start CreatePool function", "debug") + validator_reward_share = int(validator_reward_share_percent * 100) + + self.check_download_pool_contract_scripts() + + file_path = self.ton.poolsDir + pool_name + if os.path.isfile(file_path + ".addr"): + self.ton.local.add_log("CreatePool warning: Pool already exists: " + file_path, "warning") + return + # end if + + fift_script = self.ton.contractsDir + "nominator-pool/func/new-pool.fif" + wallet = self.ton.GetValidatorWallet() + args = [fift_script, wallet.addrB64, validator_reward_share, max_nominators_count, min_validator_stake, + min_nominator_stake, file_path] + result = self.ton.fift.Run(args) + if "Saved pool" not in result: + raise Exception("CreatePool error: " + result) + # end if + + pools = self.ton.GetPools() + new_pool = self.ton.GetLocalPool(pool_name) + for pool in pools: + if pool.name != new_pool.name and pool.addrB64 == new_pool.addrB64: + new_pool.Delete() + raise Exception("CreatePool error: Pool with the same parameters already exists.") + # end for + # end define + def new_pool(self, args): try: pool_name = args[0] @@ -17,7 +48,7 @@ def new_pool(self, args): except: color_print("{red}Bad args. Usage:{endc} new_pool ") return - self.ton.CreatePool(pool_name, validator_reward_share_percent, max_nominators_count, min_validator_stake, min_nominator_stake) + self.do_create_pool(pool_name, validator_reward_share_percent, max_nominators_count, min_validator_stake, min_nominator_stake) color_print("NewPool - {green}OK{endc}") def do_activate_pool(self, pool, ex=True): diff --git a/modules/pool.py b/modules/pool.py index a9086900..e5d90735 100644 --- a/modules/pool.py +++ b/modules/pool.py @@ -1,3 +1,5 @@ +import os + from mypylib.mypylib import color_print, print_table from modules.module import MtcModule @@ -28,6 +30,15 @@ def delete_pool(self, args): pool = self.ton.GetLocalPool(pool_name) pool.Delete() color_print("DeletePool - {green}OK{endc}") + # end define + + def do_import_pool(self, pool_name, addr_b64): + self.check_download_pool_contract_scripts() + addr_bytes = self.ton.addr_b64_to_bytes(addr_b64) + pool_path = self.ton.poolsDir + pool_name + with open(pool_path + ".addr", 'wb') as file: + file.write(addr_bytes) + # end define def import_pool(self, args): try: @@ -36,9 +47,14 @@ def import_pool(self, args): except: color_print("{red}Bad args. Usage:{endc} import_pool ") return - self.ton.import_pool(pool_name, pool_addr) + self.do_import_pool(pool_name, pool_addr) color_print("import_pool - {green}OK{endc}") + def check_download_pool_contract_scripts(self): + contract_path = self.ton.contractsDir + "nominator-pool/" + if not os.path.isdir(contract_path): + self.ton.DownloadContract("https://github.com/ton-blockchain/nominator-pool") + def add_console_commands(self, console): console.AddItem("pools_list", self.print_pools_list, self.local.translate("pools_list_cmd")) console.AddItem("delete_pool", self.delete_pool, self.local.translate("delete_pool_cmd")) diff --git a/modules/single_pool.py b/modules/single_pool.py index 5b419f4c..490ebbc6 100644 --- a/modules/single_pool.py +++ b/modules/single_pool.py @@ -11,6 +11,8 @@ class SingleNominatorModule(PoolModule): def do_create_single_pool(self, pool_name, owner_address): self.ton.local.add_log("start create_single_pool function", "debug") + self.check_download_pool_contract_scripts() + file_path = self.ton.poolsDir + pool_name if os.path.isfile(file_path + ".addr"): self.ton.local.add_log("create_single_pool warning: Pool already exists: " + file_path, "warning") diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 53b2e5a9..ee5447f7 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -1733,13 +1733,6 @@ def ExportWallet(self, walletName): return wallet.addrB64, key #end define - def import_pool(self, pool_name, addr_b64): - addr_bytes = self.addr_b64_to_bytes(addr_b64) - pool_path = self.poolsDir + pool_name - with open(pool_path + ".addr", 'wb') as file: - file.write(addr_bytes) - #end define - def GetWalletsNameList(self): self.local.add_log("start GetWalletsNameList function", "debug") walletsNameList = list() @@ -3396,37 +3389,6 @@ def DownloadContract(self, url, branch=None): #end if #end define - def CreatePool(self, poolName, validatorRewardSharePercent, maxNominatorsCount, minValidatorStake, minNominatorStake): - self.local.add_log("start CreatePool function", "debug") - validatorRewardShare = int(validatorRewardSharePercent * 100) - contractPath = self.contractsDir + "nominator-pool/" - if not os.path.isdir(contractPath): - self.DownloadContract("https://github.com/ton-blockchain/nominator-pool") - #end if - - filePath = self.poolsDir + poolName - if os.path.isfile(filePath + ".addr"): - self.local.add_log("CreatePool warning: Pool already exists: " + filePath, "warning") - return - #end if - - fiftScript = self.contractsDir + "nominator-pool/func/new-pool.fif" - wallet = self.GetValidatorWallet() - args = [fiftScript, wallet.addrB64, validatorRewardShare, maxNominatorsCount, minValidatorStake, minNominatorStake, filePath] - result = self.fift.Run(args) - if "Saved pool" not in result: - raise Exception("CreatePool error: " + result) - #end if - - pools = self.GetPools() - newPool = self.GetLocalPool(poolName) - for pool in pools: - if pool.name != newPool.name and pool.addrB64 == newPool.addrB64: - newPool.Delete() - raise Exception("CreatePool error: Pool with the same parameters already exists.") - #end for - #end define - def WithdrawFromPoolProcess(self, poolAddr, amount): self.local.add_log("start WithdrawFromPoolProcess function", "debug") wallet = self.GetValidatorWallet()