From 7c682124cb2abcbaf8b546c8f218e36f11682a27 Mon Sep 17 00:00:00 2001 From: Hendrik Schicke Date: Thu, 21 Oct 2021 16:08:03 +0800 Subject: [PATCH] performance increased --- C_Main.py | 6 ++--- D_Scheduler.py | 4 ++-- assets/Other.py | 9 +++---- assets/Queries.py | 53 +++++++++++++----------------------------- assets/Terra.py | 9 +++---- assets/Transactions.py | 8 +------ 6 files changed, 30 insertions(+), 59 deletions(-) diff --git a/C_Main.py b/C_Main.py index 78ab980..78a3945 100644 --- a/C_Main.py +++ b/C_Main.py @@ -35,9 +35,9 @@ Transaction_class, Queries_class, Cooldown_class, Logger_class, Terra_class, Prettify_class, Notifications_class = Transaction(), Queries(), Cooldown(), Logger(), Terra, Prettify(), Notifications() default_logger, report_logger, report_array = Logger_class.default_logger, Logger_class.report_logger, Logger_class.report_array -def keep_safe(): +def main(): - if config.Debug_mode: print(f'keep_safe() started.') + if config.Debug_mode: print(f'main() started.') begin_time = time() @@ -1092,4 +1092,4 @@ def keep_safe(): return True if __name__ == '__main__': - keep_safe = keep_safe() \ No newline at end of file + main = main() \ No newline at end of file diff --git a/D_Scheduler.py b/D_Scheduler.py index 5d42200..4440a46 100644 --- a/D_Scheduler.py +++ b/D_Scheduler.py @@ -2,7 +2,7 @@ # Other assets import B_Config as config -from C_Main import keep_safe +from C_Main import main # Other imports import threading @@ -14,4 +14,4 @@ if __name__ == "__main__": print(f'[Scheduler] Started. First interval will start in {config.Run_interval_for_Scheduler:.0f} min.') while not main_thread.wait(wait_time_in_seconds): - keep_safe() \ No newline at end of file + main() \ No newline at end of file diff --git a/assets/Other.py b/assets/Other.py index 2997432..7da954d 100644 --- a/assets/Other.py +++ b/assets/Other.py @@ -20,12 +20,10 @@ class Cooldown: def write_cooldown(self, cooldowns): with open('X_Cooldowns.json', 'w') as fp: # Stringify dates otherwise cannot be written into json - for index in cooldowns: - cooldowns[index] = f'{cooldowns[index]:%Y-%m-%d %H:%M}' + cooldowns = dict((i, f'{cooldowns[i]:%Y-%m-%d %H:%M}') for i in cooldowns) self.default_logger.debug(f'[Script] {cooldowns} has been written to X_Cooldowns.json') dump(cooldowns, fp) fp.close - pass def read_cooldown(self): @@ -42,8 +40,7 @@ def read_cooldown(self): self.default_logger.debug(f'[Script] X_Cooldowns.json existed and has been read: {cooldowns}') f.close - for index in cooldowns: - cooldowns[index] = datetime.strptime(cooldowns[index], '%Y-%m-%d %H:%M') + cooldowns = dict((i, datetime.strptime(cooldowns[i], '%Y-%m-%d %H:%M')) for i in cooldowns) return cooldowns except: @@ -80,4 +77,4 @@ def dict_value_convert_dec_to_float(self, input_value, human=False): # If it is just a plain dict elif type(input_value) is dict: - return dict((k, self.value_convert_dec_to_float(v, human)) for k, v in input_value.items()) \ No newline at end of file + return dict((k, self.value_convert_dec_to_float(v, human)) for k, v in input_value.items()) \ No newline at end of file diff --git a/assets/Queries.py b/assets/Queries.py index c4bdc94..ada774f 100644 --- a/assets/Queries.py +++ b/assets/Queries.py @@ -72,17 +72,10 @@ def get_pool_info(self, token_UST_pair_address:str): query = { "pool": {} } - query_result = Terra.terra.wasm.contract_query(token_UST_pair_address, query) - for asset in query_result['assets']: - # If ['info']['token'] does not exists, the current asset is uusd - if asset['info'].get('token') is None: - UST_in_pool = Dec(asset['amount']) - - # Otherwise it is the token - else: - token_in_pool = Dec(asset['amount']) + UST_in_pool = sum(Dec(asset['amount']) for asset in query_result['assets'] if asset['info'].get('token') is None) + token_in_pool = sum(Dec(asset['amount']) for asset in query_result['assets'] if asset['info'].get('token') is not None) total_share = Dec(query_result['total_share']) return [token_in_pool, UST_in_pool, total_share] @@ -274,9 +267,8 @@ def get_claimable_MIR(self): query_result = Terra.terra.wasm.contract_query(Terra.MirrorStaking, query) - # Sum up all claimable rewards for this account_address - for reward in query_result['reward_infos']: - claimable += Dec(reward['pending_reward']) + # Sum up all claimable rewards for this account_address + claimable = sum(Dec(reward['pending_reward']) for reward in query_result['reward_infos']) return Dec(claimable) @@ -300,8 +292,7 @@ def get_claimable_SPEC(self): query_result_mirrorFarm = Terra.terra.wasm.contract_query(Terra.mirrorFarm, query) # print(f'mirrorFarm: {query_result_mirrorFarm}') # Sum up all claimable rewards for this account_address - for reward in query_result_mirrorFarm['reward_infos']: - claimable_mirrorFarm += Dec(reward['pending_spec_reward']) + claimable_mirrorFarm = sum(Dec(reward['pending_spec_reward']) for reward in query_result_mirrorFarm['reward_infos']) # Query for the Anchor related claimable SPEC query = { @@ -313,8 +304,7 @@ def get_claimable_SPEC(self): query_result_anchorFarm = Terra.terra.wasm.contract_query(Terra.anchorFarm, query) # print(f'anchorFarm: {query_result_anchorFarm}') # Sum up all claimable rewards for this account_address - for reward in query_result_anchorFarm['reward_infos']: - claimable_anchorFarm += Dec(reward['pending_spec_reward']) + claimable_anchorFarm = sum(Dec(reward['pending_spec_reward']) for reward in query_result_anchorFarm['reward_infos']) # Query for the Spec related claimable SPEC query = { @@ -326,8 +316,7 @@ def get_claimable_SPEC(self): query_result_specFarm = Terra.terra.wasm.contract_query(Terra.specFarm, query) # print(f'specFarm: {query_result_specFarm}') # Sum up all claimable rewards for this account_address - for reward in query_result_specFarm['reward_infos']: - claimable_specFarm += Dec(reward['pending_spec_reward']) + claimable_specFarm = sum(Dec(reward['pending_spec_reward']) for reward in query_result_specFarm['reward_infos']) # Query for the Pylon related claimable SPEC query = { @@ -339,8 +328,7 @@ def get_claimable_SPEC(self): query_result_pylonFarm = Terra.terra.wasm.contract_query(Terra.pylonFarm, query) # print(f'pylonFarm: {query_result_pylonFarm}') # Sum up all claimable rewards for this account_address - for reward in query_result_pylonFarm['reward_infos']: - claimable_pylonFarm += Dec(reward['pending_spec_reward']) + claimable_pylonFarm = sum(Dec(reward['pending_spec_reward']) for reward in query_result_pylonFarm['reward_infos']) # claimable_SPEC_dict = { # "claimable_mirrorFarm": Dec(claimable_mirrorFarm)/1000000, @@ -445,27 +433,22 @@ def get_available_LP_token_for_withdrawal(self, token_farm_address:str, token_ad } query_result = Terra.terra.wasm.contract_query(token_farm_address, query) - if not query_result == []: - for reward_info in query_result['reward_infos']: - if reward_info['asset_token'] == token_address: - LP_token_available = reward_info['bond_amount'] + if query_result != []: + LP_token_available = sum(Dec(reward_info['bond_amount']) for reward_info in query_result['reward_infos'] if reward_info['asset_token'] == token_address) return Dec(LP_token_available) def Anchor_get_max_ltv_ratio(self): - max_ltv_ratio:dict - max_ltv_ratio = {} + # max_ltv_ratio:dict + # max_ltv_ratio = {} query = { "whitelist": {}, } query_result = Terra.terra.wasm.contract_query(Terra.mmOverseer, query) - for elem in query_result['elems']: - max_ltv_ratio[elem['symbol']] = Dec(elem['max_ltv']) - - return max_ltv_ratio + return dict((elem['symbol'], Dec(elem['max_ltv'])) for elem in query_result['elems']) def Anchor_get_borrow_info(self): @@ -501,9 +484,7 @@ def Anchor_get_borrow_info(self): loan_amount = Dec(loan_amount_result['loan_amount']) - collateral_dict = {} - for collateral in query_msg_collateral_result['collaterals']: - collateral_dict[collateral[0]] = Dec(collateral[1]) + collateral_dict = dict((collateral[0], Dec(collateral[1])) for collateral in query_msg_collateral_result['collaterals']) if collateral_dict.get(Terra.bETH_token) is not None: amount_bETH_collateral = collateral_dict[Terra.bETH_token] @@ -629,10 +610,8 @@ def get_native_balance(self, denom:str): # Todo: Return a dict with all natives to be incl in the wallet_balance dict provided balance_native = Terra.terra.bank.balance(address=account_address).to_data() - for item in balance_native: - if item['denom'] == denom: - return Dec(item['amount']) - return 0 + + return sum(Dec(item['amount']) for item in balance_native if item['denom'] == denom) def get_non_native_balance(self, token_address): # Todo: Return a dict with all natives to be incl in the wallet_balance dict provided diff --git a/assets/Terra.py b/assets/Terra.py index e7f0699..d5be040 100644 --- a/assets/Terra.py +++ b/assets/Terra.py @@ -33,21 +33,22 @@ def get_terra_gas_prices(retry=0): else: raise err -terra_gas_prices = get_terra_gas_prices() - class Terra: if config.Debug_mode: print(f'Terra Class loaded.') + + terra_gas_prices = get_terra_gas_prices() + if config.NETWORK == 'MAINNET': chain_id = 'columbus-5' public_node_url = 'https://lcd.terra.dev' - tx_look_up = f'https://finder.terra.money/{chain_id}/tx/' + # tx_look_up = f'https://finder.terra.money/{chain_id}/tx/' contact_addresses = Contract_addresses.contact_addresses(network='MAINNET') rev_Contract_addresses = Contract_addresses.rev_contact_addresses(contact_addresses) else: chain_id = 'bombay-12' public_node_url = 'https://bombay-lcd.terra.dev' - tx_look_up = f'https://finder.terra.money/{chain_id}/tx/' + # tx_look_up = f'https://finder.terra.money/{chain_id}/tx/' contact_addresses = Contract_addresses.contact_addresses(network='bombay-12') rev_Contract_addresses = Contract_addresses.rev_contact_addresses(contact_addresses) diff --git a/assets/Transactions.py b/assets/Transactions.py index 3c9ab3f..d25191f 100644 --- a/assets/Transactions.py +++ b/assets/Transactions.py @@ -166,16 +166,10 @@ def Mirror_withdraw_collateral_for_position(self, idx:str, collateral_amount_in_ def Mirror_claim_unlocked_UST(self, Mirror_position_info:dict): - def position_idxs_to_be_claimed(): - position_idxs_to_be_claimed = [] - for position in Mirror_position_info: - position_idxs_to_be_claimed.append(position['position_idx']) - return position_idxs_to_be_claimed - contract=Terra.Lock execute_msg={ "unlock_position_funds": { - "positions_idx": position_idxs_to_be_claimed() + "positions_idx": [position['position_idx'] for position in Mirror_position_info] } } coins=Coins()