|
11 | 11 | class BtcTeleportModule(MtcModule): |
12 | 12 |
|
13 | 13 | COORDINATOR_ADDRESS = 'Ef_q19o4m94xfF-yhYB85Qe6rTHDX-VTSzxBh4XpAfZMaOvk' |
14 | | - CONFIGURATOR_ADDRESS = 'EQAR_I_lQm5wnEePggUKfioQUFs1vN1YYkK1Kl3WVAPiCzDZ' |
15 | 14 |
|
16 | 15 | def __init__(self, ton, local, *args, **kwargs): |
17 | 16 | super().__init__(ton, local, *args, **kwargs) |
@@ -91,132 +90,6 @@ def run_remove_btc_teleport(args): |
91 | 90 | with get_package_resource_path('mytonctrl', 'scripts/remove_btc_teleport.sh') as script_path: |
92 | 91 | return run_as_root(["bash", script_path] + args) |
93 | 92 |
|
94 | | - def get_save_offers(self): |
95 | | - bname = "saveOffersBtcTeleport" |
96 | | - save_offers = self.ton.local.db.get(bname) |
97 | | - if save_offers is None: |
98 | | - save_offers = dict() |
99 | | - self.ton.local.db[bname] = save_offers |
100 | | - return save_offers |
101 | | - |
102 | | - def auto_vote_offers(self): |
103 | | - save_offers = self.get_save_offers() |
104 | | - if not save_offers: |
105 | | - return |
106 | | - current_offers = self.get_offers() |
107 | | - config34 = self.ton.GetConfig34() |
108 | | - for save_offer in list(save_offers.values()): |
109 | | - offer_hash = save_offer['hash'] |
110 | | - if offer_hash not in current_offers: |
111 | | - if save_offer['ttl'] > config34['endWorkTime']: # offer is not expired but not found in current offers, so we assume it has been accepted |
112 | | - save_offers.pop(offer_hash) |
113 | | - self.local.add_log(f'Removing offer {offer_hash} from saved offers', 'debug') |
114 | | - continue |
115 | | - offer = current_offers[save_offer['hash']] |
116 | | - if offer['created_at'] != save_offer['created_at'] and save_offer['ttl'] > config34['endWorkTime']: # offer is not expired but has been changed, so it has been accepted and launched again |
117 | | - save_offers.pop(offer_hash) |
118 | | - self.local.add_log(f'Removing offer {offer_hash} from saved offers', 'debug') |
119 | | - continue |
120 | | - self.vote_offer_btc_teleport([offer_hash]) |
121 | | - |
122 | | - def get_offers(self): |
123 | | - self.local.add_log("start get_offers_btc_teleport function", "debug") |
124 | | - cmd = f"runmethodfull {self.CONFIGURATOR_ADDRESS} list_proposals" |
125 | | - result = self.ton.liteClient.Run(cmd) |
126 | | - raw_offers = self.ton.Result2List(result) |
127 | | - raw_offers = raw_offers[0] |
128 | | - validators = self.ton.GetValidatorsList(fast=True) |
129 | | - total_weight = 0 |
130 | | - for v in validators: |
131 | | - if v['is_masterchain'] is False: |
132 | | - continue |
133 | | - total_weight += v['weight'] |
134 | | - |
135 | | - offers = {} |
136 | | - for offer in raw_offers: |
137 | | - if len(offer) == 0: |
138 | | - continue |
139 | | - item = {} |
140 | | - o_hash = str(offer[0]) |
141 | | - item["hash"] = o_hash |
142 | | - item["remaining_losses"] = offer[1] |
143 | | - item["price"] = offer[2] |
144 | | - item["proposal"] = offer[3] |
145 | | - item["votedValidators"] = offer[4] |
146 | | - weight_remaining = offer[5] |
147 | | - item["weightRemaining"] = weight_remaining |
148 | | - item["vset_id"] = offer[6] |
149 | | - item["creator"] = offer[7] |
150 | | - item["created_at"] = offer[-1] # todo: bug in parsing slice in get method output |
151 | | - required_weight = total_weight * 2 / 3 |
152 | | - if len(item["votedValidators"]) == 0: |
153 | | - weight_remaining = required_weight |
154 | | - available_weight = required_weight - weight_remaining |
155 | | - item["approvedPercent"] = round(available_weight / total_weight * 100, 3) |
156 | | - item["isPassed"] = (weight_remaining < 0) |
157 | | - offers[o_hash] = item |
158 | | - return offers |
159 | | - |
160 | | - def add_save_offer(self, offer: dict): |
161 | | - config15 = self.ton.GetConfig15() |
162 | | - offer['ttl'] = offer['created_at'] + config15['validatorsElectedFor'] * 3 # proposal lives 3 rounds |
163 | | - self.get_save_offers()[offer['hash']] = offer |
164 | | - self.ton.local.save() |
165 | | - |
166 | | - def vote_offer_btc_teleport(self, args): |
167 | | - if len(args) == 0: |
168 | | - color_print("{red}Bad args. Usage:{endc} vote_offer_btc_teleport <offer-hash> [offer-hash-2 offer-hash-3 ...]") |
169 | | - return |
170 | | - wallet = self.ton.GetValidatorWallet(mode="vote") |
171 | | - validator_key = self.ton.GetValidatorKey() |
172 | | - validator_pubkey_b64 = self.ton.GetPubKeyBase64(validator_key) |
173 | | - validator_index = self.ton.GetValidatorIndex() |
174 | | - config34 = self.ton.GetConfig34() |
175 | | - if validator_index == -1 or validator_index >= config34['mainValidators']: |
176 | | - self.local.add_log("Can not vote for BTC Teleport proposal from non-masterchain validator", "error") |
177 | | - return |
178 | | - for offer_hash in args: |
179 | | - current_offers = self.get_offers() |
180 | | - if offer_hash not in current_offers: |
181 | | - self.local.add_log("Offer not found, skip", "warning") |
182 | | - continue |
183 | | - offer = current_offers[offer_hash] |
184 | | - if validator_index in offer.get("votedValidators"): |
185 | | - self.local.add_log("Proposal already has been voted", "debug") |
186 | | - continue |
187 | | - self.add_save_offer(offer) |
188 | | - request_hash = self.ton.CreateConfigProposalRequest(offer_hash, validator_index) |
189 | | - validator_signature = self.ton.GetValidatorSignature(validator_key, request_hash) |
190 | | - path = self.ton.SignProposalVoteRequestWithValidator(offer_hash, validator_index, validator_pubkey_b64, |
191 | | - validator_signature) |
192 | | - path = self.ton.SignBocWithWallet(wallet, path, self.CONFIGURATOR_ADDRESS, 1.5) |
193 | | - self.ton.SendFile(path, wallet) |
194 | | - |
195 | | - def print_offers_btc_teleport_list(self, args): |
196 | | - data = self.get_offers() |
197 | | - if not data: |
198 | | - print("No data") |
199 | | - return |
200 | | - if "--json" in args: |
201 | | - text = json.dumps(data, indent=2) |
202 | | - print(text) |
203 | | - return |
204 | | - table = [["Hash", "Votes", "Approved", "Is passed"]] |
205 | | - for item in data.values(): |
206 | | - o_hash = item.get("hash") |
207 | | - voted_validators = len(item.get("votedValidators")) |
208 | | - approved_percent_text = f"{item.get('approvedPercent')}%" |
209 | | - is_passed = item.get("isPassed") |
210 | | - if "hash" not in args: |
211 | | - from modules.utilities import UtilitiesModule |
212 | | - o_hash = UtilitiesModule.reduct(o_hash) |
213 | | - if is_passed is True: |
214 | | - is_passed = bcolors.green_text("true") |
215 | | - if is_passed is False: |
216 | | - is_passed = bcolors.red_text("false") |
217 | | - table += [[o_hash, voted_validators, approved_percent_text, is_passed]] |
218 | | - print_table(table) |
219 | | - |
220 | 93 | def remove_btc_teleport(self, args: list): |
221 | 94 | if len(args) > 1: |
222 | 95 | color_print("{red}Bad args. Usage:{endc} remove_btc_teleport [--force]") |
|
0 commit comments