Skip to content

Commit

Permalink
performance increased
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik Schicke committed Oct 21, 2021
1 parent d0a57d7 commit 7c68212
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 59 deletions.
6 changes: 3 additions & 3 deletions C_Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -1092,4 +1092,4 @@ def keep_safe():
return True

if __name__ == '__main__':
keep_safe = keep_safe()
main = main()
4 changes: 2 additions & 2 deletions D_Scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
main()
9 changes: 3 additions & 6 deletions assets/Other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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:
Expand Down Expand Up @@ -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())
return dict((k, self.value_convert_dec_to_float(v, human)) for k, v in input_value.items())
53 changes: 16 additions & 37 deletions assets/Queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions assets/Terra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 1 addition & 7 deletions assets/Transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7c68212

Please sign in to comment.