Skip to content

Commit ea3fae0

Browse files
committed
add print_offers_btc_teleport_list
1 parent 140b3a6 commit ea3fae0

File tree

3 files changed

+87
-25
lines changed

3 files changed

+87
-25
lines changed

modules/btc_teleport.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import json
12
import os
23
import subprocess
34

45
import pkg_resources
56

67
from modules.module import MtcModule
7-
from mypylib.mypylib import run_as_root, color_print
8+
from mypylib.mypylib import run_as_root, color_print, bcolors, print_table
89

910

1011
class BtcTeleportModule(MtcModule):
@@ -81,6 +82,83 @@ def run_remove_btc_teleport(args):
8182
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/remove_btc_teleport.sh')
8283
return run_as_root(["bash", script_path] + args)
8384

85+
def get_offers_btc_teleport(self):
86+
self.local.add_log("start get_offers_btc_teleport function", "debug")
87+
cmd = f"runmethodfull {self.CONFIGURATOR_ADDRESS} list_proposals"
88+
result = self.ton.liteClient.Run(cmd)
89+
raw_offers = self.ton.Result2List(result)
90+
raw_offers = raw_offers[0]
91+
config34 = self.ton.GetConfig34()
92+
total_weight = config34.get("totalWeight")
93+
offers = []
94+
for offer in raw_offers:
95+
if len(offer) == 0:
96+
continue
97+
item = {}
98+
hash = str(offer[0])
99+
item["hash"] = hash
100+
item["price"] = offer[1]
101+
item["proposal"] = offer[2]
102+
item["votedValidators"] = offer[3]
103+
weight_remaining = offer[4]
104+
item["weightRemaining"] = weight_remaining
105+
item["vset_id"] = offer[5]
106+
item["creator"] = offer[6]
107+
required_weight = total_weight * 3 / 4
108+
if len(item["votedValidators"]) == 0:
109+
weight_remaining = required_weight
110+
available_weight = required_weight - weight_remaining
111+
item["weightRemaining"] = weight_remaining
112+
item["approvedPercent"] = round(available_weight / total_weight * 100, 3)
113+
item["isPassed"] = (weight_remaining < 0)
114+
offers.append(item)
115+
return offers
116+
117+
def vote_offer_btc_teleport(self, args):
118+
if len(args) == 0:
119+
color_print("{red}Bad args. Usage:{endc} vote_offer_btc_teleport <offer-hash> [offer-hash-2 offer-hash-3 ...]")
120+
return
121+
wallet = self.ton.GetValidatorWallet(mode="vote")
122+
validator_key = self.ton.GetValidatorKey()
123+
validator_pubkey_b64 = self.ton.GetPubKeyBase64(validator_key)
124+
validator_index = self.ton.GetValidatorIndex()
125+
for offer_hash in args:
126+
# offer = self.GetOffer(offerHash)
127+
# if validatorIndex in offer.get("votedValidators"):
128+
# self.local.add_log("Proposal already has been voted", "debug")
129+
# return
130+
request_hash = self.ton.CreateConfigProposalRequest(offer_hash, validator_index)
131+
validator_signature = self.ton.GetValidatorSignature(validator_key, request_hash)
132+
path = self.ton.SignProposalVoteRequestWithValidator(offer_hash, validator_index, validator_pubkey_b64,
133+
validator_signature)
134+
path = self.ton.SignBocWithWallet(wallet, path, self.CONFIGURATOR_ADDRESS, 1.5)
135+
self.ton.SendFile(path, wallet)
136+
137+
def print_offers_btc_teleport_list(self, args):
138+
data = self.get_offers_btc_teleport()
139+
if not data:
140+
print("No data")
141+
return
142+
if "--json" in args:
143+
text = json.dumps(data, indent=2)
144+
print(text)
145+
return
146+
table = [["Hash", "Votes", "Approved", "Is passed"]]
147+
for item in data:
148+
hash = item.get("hash")
149+
voted_validators = len(item.get("votedValidators"))
150+
approved_percent_text = f"{item.get('approvedPercent')}%"
151+
is_passed = item.get("isPassed")
152+
if "hash" not in args:
153+
from modules.utilities import UtilitiesModule
154+
hash = UtilitiesModule.reduct(hash)
155+
if is_passed is True:
156+
is_passed = bcolors.green_text("true")
157+
if is_passed is False:
158+
is_passed = bcolors.red_text("false")
159+
table += [[hash, voted_validators, approved_percent_text, is_passed]]
160+
print_table(table)
161+
84162
def remove_btc_teleport(self, args: list):
85163
if len(args) > 1:
86164
color_print("{red}Bad args. Usage:{endc} remove_btc_teleport [--force]")
@@ -96,3 +174,5 @@ def remove_btc_teleport(self, args: list):
96174

97175
def add_console_commands(self, console):
98176
console.AddItem("remove_btc_teleport", self.remove_btc_teleport, self.local.translate("remove_btc_teleport_cmd"))
177+
console.AddItem("vote_offer_btc_teleport", self.vote_offer_btc_teleport, self.local.translate("vote_offer_btc_teleport_cmd"))
178+
console.AddItem("print_offers_btc_teleport_list", self.print_offers_btc_teleport_list, self.local.translate("print_offers_btc_teleport_list_cmd"))

modules/validator.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,6 @@ def vote_offer(self, args):
2020
self.ton.VoteOffer(offerHash)
2121
color_print("VoteOffer - {green}OK{endc}")
2222

23-
def vote_offer_btc_teleport(self, args):
24-
if len(args) == 0:
25-
color_print("{red}Bad args. Usage:{endc} vote_offer_btc_teleport <offer-hash> [offer-hash-2 offer-hash-3 ...]")
26-
return
27-
from modules.btc_teleport import BtcTeleportModule
28-
coordinator_addr = BtcTeleportModule.CONFIGURATOR_ADDRESS
29-
wallet = self.ton.GetValidatorWallet(mode="vote")
30-
validator_key = self.ton.GetValidatorKey()
31-
validator_pubkey_b64 = self.ton.GetPubKeyBase64(validator_key)
32-
validator_index = self.ton.GetValidatorIndex()
33-
for offer_hash in args:
34-
# offer = self.GetOffer(offerHash)
35-
# if validatorIndex in offer.get("votedValidators"):
36-
# self.local.add_log("Proposal already has been voted", "debug")
37-
# return
38-
request_hash = self.ton.CreateConfigProposalRequest(offer_hash, validator_index)
39-
validator_signature = self.ton.GetValidatorSignature(validator_key, request_hash)
40-
path = self.ton.SignProposalVoteRequestWithValidator(offer_hash, validator_index, validator_pubkey_b64,
41-
validator_signature)
42-
path = self.ton.SignBocWithWallet(wallet, path, coordinator_addr, 1.5)
43-
self.ton.SendFile(path, wallet)
44-
4523
def vote_election_entry(self, args):
4624
from mytoncore.functions import Elections
4725
Elections(self.ton.local, self.ton)
@@ -126,7 +104,6 @@ def get_my_complaint(self):
126104

127105
def add_console_commands(self, console):
128106
console.AddItem("vo", self.vote_offer, self.local.translate("vo_cmd"))
129-
console.AddItem("vote_offer_btc_teleport", self.vote_offer_btc_teleport, self.local.translate("vote_offer_btc_teleport_cmd"))
130107
console.AddItem("ve", self.vote_election_entry, self.local.translate("ve_cmd"))
131108
console.AddItem("vc", self.vote_complaint, self.local.translate("vc_cmd"))
132109
console.AddItem("check_ef", self.check_efficiency, self.local.translate("check_ef_cmd"))

mytonctrl/resources/translate.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@
140140
"zh_TW": "投票支持優惠"
141141
},
142142
"vote_offer_btc_teleport_cmd": {
143-
"en": "Vote for BTC teleport offer",
143+
"en": "Vote for BTC teleport proposal",
144144
"ru": "Голосовать за предложение BTC Teleport",
145145
"zh_TW": "投票支持 BTC Teleport 優惠"
146146
},
147+
"print_offers_btc_teleport_list_cmd": {
148+
"en": "Show BTC teleport proposals list",
149+
"ru": "Показать список предложений BTC Teleport",
150+
"zh_TW": "顯示 BTC Teleport 優惠列表"
151+
},
147152
"od_cmd": {
148153
"en": "Show offer diff",
149154
"ru": "Показать разницу предложений",

0 commit comments

Comments
 (0)