Skip to content

Commit

Permalink
Merge pull request #357 from ton-blockchain/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
igroman787 authored Sep 9, 2024
2 parents 6cc6705 + 344f861 commit 74e536b
Show file tree
Hide file tree
Showing 19 changed files with 880 additions and 997 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![GitHub stars](https://img.shields.io/github/stars/ton-blockchain/mytonctrl?style=flat-square&logo=github) ![GitHub forks](https://img.shields.io/github/forks/ton-blockchain/mytonctrl?style=flat-square&logo=github) ![GitHub issues](https://img.shields.io/github/issues/ton-blockchain/mytonctrl?style=flat-square&logo=github) ![GitHub pull requests](https://img.shields.io/github/issues-pr/ton-blockchain/mytonctrl?style=flat-square&logo=github) ![GitHub last commit](https://img.shields.io/github/last-commit/ton-blockchain/mytonctrl?style=flat-square&logo=github) ![GitHub license](https://img.shields.io/github/license/ton-blockchain/mytonctrl?style=flat-square&logo=github)

<!-- omit from toc -->
<!-- omit from toc -->
# MyTonCtrl

<!-- omit from toc -->
Expand Down Expand Up @@ -58,12 +58,6 @@ Mytonctrl's documentation can be found at https://docs.ton.org/participate/run-n
- [x] Show offers
- [x] Vote for the proposal
- [x] Automatic voting for previously voted proposals
- [x] Domain management
- [x] Rent a new domain
- [x] Show rented domains
- [x] Show domain status
- [x] Delete domain
- [ ] Automatic domain renewal
- [x] Controlling the validator
- [x] Participate in the election of a validator
- [x] Return bet + reward
Expand Down
1 change: 1 addition & 0 deletions modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Setting:
'useDefaultCustomOverlays': Setting(None, True, 'Participate in default custom overlays node eligible to'),
'defaultCustomOverlaysUrl': Setting(None, 'https://ton-blockchain.github.io/fallback_custom_overlays.json', 'Default custom overlays config url'),
'debug': Setting(None, False, 'Debug mtc console mode. Prints Traceback on errors'),
'subscribe_tg_channel': Setting('validator', False, 'Disables warning about subscribing to the `TON STATUS` channel'),
}


Expand Down
352 changes: 352 additions & 0 deletions modules/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
import base64
import json
import os
import subprocess
import time

from mypylib.mypylib import color_print, print_table, color_text, timeago, bcolors
from modules.module import MtcModule


class UtilitiesModule(MtcModule):

description = ''
default_value = False

def view_account_status(self, args):
try:
addrB64 = args[0]
except:
color_print("{red}Bad args. Usage:{endc} vas <account-addr>")
return
addrB64 = self.ton.get_destination_addr(addrB64)
account = self.ton.GetAccount(addrB64)
version = self.ton.GetVersionFromCodeHash(account.codeHash)
statusTable = list()
statusTable += [["Address", "Status", "Balance", "Version"]]
statusTable += [[addrB64, account.status, account.balance, version]]
codeHashTable = list()
codeHashTable += [["Code hash"]]
codeHashTable += [[account.codeHash]]
historyTable = self.get_history_table(addrB64, 10)
print_table(statusTable)
print()
print_table(codeHashTable)
print()
print_table(historyTable)
# end define

def get_history_table(self, addr, limit):
addr = self.ton.get_destination_addr(addr)
account = self.ton.GetAccount(addr)
history = self.ton.GetAccountHistory(account, limit)
table = list()
typeText = color_text("{red}{bold}{endc}")
table += [["Time", typeText, "Coins", "From/To"]]
for message in history:
if message.srcAddr is None:
continue
srcAddrFull = f"{message.srcWorkchain}:{message.srcAddr}"
destAddFull = f"{message.destWorkchain}:{message.destAddr}"
if srcAddrFull == account.addrFull:
type = color_text("{red}{bold}>>>{endc}")
fromto = destAddFull
else:
type = color_text("{blue}{bold}<<<{endc}")
fromto = srcAddrFull
fromto = self.ton.AddrFull2AddrB64(fromto)
# datetime = timestamp2datetime(message.time, "%Y.%m.%d %H:%M:%S")
datetime = timeago(message.time)
table += [[datetime, type, message.value, fromto]]
return table
# end define

def view_account_history(self, args):
try:
addr = args[0]
limit = int(args[1])
except:
color_print("{red}Bad args. Usage:{endc} vah <account-addr> <limit>")
return
table = self.get_history_table(addr, limit)
print_table(table)
# end define

def create_new_bookmark(self, args):
try:
name = args[0]
addr = args[1]
except:
color_print("{red}Bad args. Usage:{endc} nb <bookmark-name> <account-addr>")
return
if not self.ton.IsAddr(addr):
raise Exception("Incorrect address")
# end if

bookmark = dict()
bookmark["name"] = name
bookmark["addr"] = addr
self.ton.AddBookmark(bookmark)
color_print("CreatNewBookmark - {green}OK{endc}")
# end define

def print_bookmarks_list(self, args):
data = self.ton.GetBookmarks()
if data is None or len(data) == 0:
print("No data")
return
table = list()
table += [["Name", "Address", "Balance / Exp. date"]]
for item in data:
name = item.get("name")
addr = item.get("addr")
bookmark_data = item.get("data")
table += [[name, addr, bookmark_data]]
print_table(table)
# end define

def delete_bookmark(self, args):
try:
name = args[0]
except:
color_print("{red}Bad args. Usage:{endc} db <bookmark-name>")
return
self.ton.DeleteBookmark(name)
color_print("DeleteBookmark - {green}OK{endc}")
# end define

@staticmethod
def reduct(item):
item = str(item)
if item is None:
result = None
else:
end = len(item)
result = item[0:6] + "..." + item[end - 6:end]
return result
# end define

def print_offers_list(self, args):
data = self.ton.GetOffers()
if data is None or len(data) == 0:
print("No data")
return
if "--json" in args:
text = json.dumps(data, indent=2)
print(text)
else:
table = list()
table += [["Hash", "Config", "Votes", "W/L", "Approved", "Is passed"]]
for item in data:
hash = item.get("hash")
votedValidators = len(item.get("votedValidators"))
wins = item.get("wins")
losses = item.get("losses")
wl = "{0}/{1}".format(wins, losses)
approvedPercent = item.get("approvedPercent")
approvedPercent_text = "{0}%".format(approvedPercent)
isPassed = item.get("isPassed")
if "hash" not in args:
hash = self.reduct(hash)
if isPassed == True:
isPassed = bcolors.green_text("true")
if isPassed == False:
isPassed = bcolors.red_text("false")
table += [[hash, item.config.id, votedValidators, wl, approvedPercent_text, isPassed]]
print_table(table)
# end define

def get_offer_diff(self, offer_hash):
self.local.add_log("start GetOfferDiff function", "debug")
offer = self.ton.GetOffer(offer_hash)
config_id = offer["config"]["id"]
config_value = offer["config"]["value"]

if '{' in config_value or '}' in config_value:
start = config_value.find('{') + 1
end = config_value.find('}')
config_value = config_value[start:end]
# end if

args = [self.ton.liteClient.appPath, "--global-config", self.ton.liteClient.configPath, "--verbosity", "0"]
process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(1)

fullConfigAddr = self.ton.GetFullConfigAddr()
cmd = "runmethodfull {fullConfigAddr} list_proposals".format(fullConfigAddr=fullConfigAddr)
process.stdin.write(cmd.encode() + b'\n')
process.stdin.flush()
time.sleep(1)

cmd = "dumpcellas ConfigParam{configId} {configValue}".format(configId=config_id, configValue=config_value)
process.stdin.write(cmd.encode() + b'\n')
process.stdin.flush()
time.sleep(1)

process.terminate()
text = process.stdout.read().decode()

lines = text.split('\n')
b = len(lines)
for i in range(b):
line = lines[i]
if "dumping cells as values of TLB type" in line:
a = i + 2
break
# end for

for i in range(a, b):
line = lines[i]
if '(' in line:
start = i
break
# end for

for i in range(a, b):
line = lines[i]
if '>' in line:
end = i
break
# end for

buff = lines[start:end]
text = "".join(buff)
newData = self.ton.Tlb2Json(text)
newFileName = self.ton.tempDir + "data1diff"
file = open(newFileName, 'wt')
newText = json.dumps(newData, indent=2)
file.write(newText)
file.close()

oldData = self.ton.GetConfig(config_id)
oldFileName = self.ton.tempDir + "data2diff"
file = open(oldFileName, 'wt')
oldText = json.dumps(oldData, indent=2)
file.write(oldText)
file.close()

print(oldText)
args = ["diff", "--color", oldFileName, newFileName]
subprocess.run(args)
# end define

def offer_diff(self, args):
try:
offer_hash = args[0]
offer_hash = offer_hash
except:
color_print("{red}Bad args. Usage:{endc} od <offer-hash>")
return
self.get_offer_diff(offer_hash)
# end define

def print_complaints_list(self, args):
past = "past" in args
data = self.ton.GetComplaints(past=past)
if data is None or len(data) == 0:
print("No data")
return
if "--json" in args:
text = json.dumps(data, indent=2)
print(text)
else:
table = list()
table += [["Election id", "ADNL", "Fine (part)", "Votes", "Approved", "Is passed"]]
for key, item in data.items():
electionId = item.get("electionId")
adnl = item.get("adnl")
suggestedFine = item.get("suggestedFine")
suggestedFinePart = item.get("suggestedFinePart")
Fine_text = "{0} ({1})".format(suggestedFine, suggestedFinePart)
votedValidators = len(item.get("votedValidators"))
approvedPercent = item.get("approvedPercent")
approvedPercent_text = "{0}%".format(approvedPercent)
isPassed = item.get("isPassed")
if "adnl" not in args:
adnl = self.reduct(adnl)
if isPassed:
isPassed = bcolors.green_text("true")
if not isPassed:
isPassed = bcolors.red_text("false")
table += [[electionId, adnl, Fine_text, votedValidators, approvedPercent_text, isPassed]]
print_table(table)
# end define

def print_election_entries_list(self, args):
past = "past" in args
data = self.ton.GetElectionEntries(past=past)
if data is None or len(data) == 0:
print("No data")
return
if "--json" in args:
text = json.dumps(data, indent=2)
print(text)
else:
table = list()
table += [["ADNL", "Pubkey", "Wallet", "Stake", "Max-factor"]]
for key, item in data.items():
adnl = item.get("adnlAddr")
pubkey = item.get("pubkey")
walletAddr = item.get("walletAddr")
stake = item.get("stake")
maxFactor = item.get("maxFactor")
if "adnl" not in args:
adnl = self.reduct(adnl)
if "pubkey" not in args:
pubkey = self.reduct(pubkey)
if "wallet" not in args:
walletAddr = self.reduct(walletAddr)
table += [[adnl, pubkey, walletAddr, stake, maxFactor]]
print_table(table)
# end define

def print_validator_list(self, args):
past = "past" in args
fast = "fast" in args
data = self.ton.GetValidatorsList(past=past, fast=fast)
if data is None or len(data) == 0:
print("No data")
return
if "--json" in args:
text = json.dumps(data, indent=2)
print(text)
else:
table = list()
table += [["id", "ADNL", "Pubkey", "Wallet", "Efficiency", "Online"]]
for i, item in enumerate(data):
adnl = item.get("adnlAddr")
pubkey = item.get("pubkey")
walletAddr = item.get("walletAddr")
efficiency = item.get("efficiency")
online = item.get("online")
if "adnl" not in args:
adnl = self.reduct(adnl)
if "pubkey" not in args:
pubkey = self.reduct(pubkey)
if "wallet" not in args:
walletAddr = self.reduct(walletAddr)
if "offline" in args and online != False:
continue
if online:
online = bcolors.green_text("true")
if not online:
online = bcolors.red_text("false")
table += [[str(i), adnl, pubkey, walletAddr, efficiency, online]]
print_table(table)
# end define

def add_console_commands(self, console):
console.AddItem("vas", self.view_account_status, self.local.translate("vas_cmd"))
console.AddItem("vah", self.view_account_history, self.local.translate("vah_cmd"))

console.AddItem("nb", self.create_new_bookmark, self.local.translate("nb_cmd"))
console.AddItem("bl", self.print_bookmarks_list, self.local.translate("bl_cmd"))
console.AddItem("db", self.delete_bookmark, self.local.translate("db_cmd"))

console.AddItem("ol", self.print_offers_list, self.local.translate("ol_cmd"))
console.AddItem("od", self.offer_diff, self.local.translate("od_cmd"))

console.AddItem("el", self.print_election_entries_list, self.local.translate("el_cmd"))
console.AddItem("vl", self.print_validator_list, self.local.translate("vl_cmd"))
console.AddItem("cl", self.print_complaints_list, self.local.translate("cl_cmd"))

Loading

0 comments on commit 74e536b

Please sign in to comment.