changed selling mechanism to be same as buyer

This commit is contained in:
Alexander 2024-07-12 14:36:19 -04:00
parent dfeefa01d5
commit 12b95d4f72

View file

@ -1,7 +1,8 @@
from core import *
# set config variables
sell_percent_diff_affection = 0
sell_percent_diff_pdai = 15
sell_percent_diff_pusdc = 25
sell_with_amount_affection = 200
slippage_percent = 5
wallet_min_pls = 20000
@ -24,6 +25,10 @@ wpls_address = '0xA1077a294dDE1B09bB078844df40758a5D0f9a27'
wpls_info = get_token_info(wpls_address)
wpls_contract = load_contract(wpls_address)
# load contract addresses
pdai_address = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
pusdc_address = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
while True:
# log the wallet's pls balance
logging.info("PLS Balance: {:.15f}".format(get_pls_balance(account.address)))
@ -53,29 +58,39 @@ while True:
log_end_loop(loop_delay)
continue
# take a sample of the 1 affection to wpls price
# take samples of 1 pdai/pusdc/affection to wpls price
pdai_sample_result = sample_exchange_rate('PulseX_v2', pdai_address, wpls_address)
pusdc_sample_result = sample_exchange_rate('PulseX_v2', pusdc_address, wpls_address)
affection_sample_result = sample_exchange_rate('PulseX_v2', affection_address, wpls_address)
if not affection_sample_result_last:
affection_sample_result_last = affection_sample_result
# log the current rate
# log the current rates
logging.info("pDAI Rate: 1 = {} PLS".format(pdai_sample_result / 10 ** 18))
logging.info("pUSDC Rate: 1 = {} PLS".format(pusdc_sample_result / 10 ** 18))
logging.info("AFFECTION™ Rate: 1 = {} PLS".format(affection_sample_result / 10 ** 18))
logging.info("AFFECTION™ Balance: {:.15f}".format(affection_balance := get_token_balance(affection_address, wallet_c_address), 2))
# check if wallet c has at least 1 token to sell
# log the balance
affection_balance = get_token_balance(affection_address, wallet_c_address)
logging.info("AFFECTION™ Balance: {:.15f}".format(affection_balance))
# check if wallet c has at least 1 token
if affection_balance > 1:
# check if the affection price spiked since last time
if not sell_percent_diff_affection or affection_sample_result > affection_sample_result_last:
# check the percent difference
percent_diff = ((affection_sample_result_last - affection_sample_result) / affection_sample_result) * 100
# sell if percent is met
if not sell_percent_diff_affection or (percent_diff < 0 and abs(percent_diff) >= sell_percent_diff_affection):
# get amounts of affection to sell
sells = math.floor(affection_balance / sell_with_amount_affection)
selling_amounts = [sell_with_amount_affection] * sells
selling_amounts.append(math.floor(affection_balance - sum(selling_amounts)))
# start selling affection in different amounts
logging.info("Selling {} AFFECTION™...".format(sum(selling_amounts)))
for i, amount in enumerate(selling_amounts):
i = 0
while i < len(selling_amounts):
amount = selling_amounts[i]
# check if the pdai/pusdc price is cheaper than affection price
if (pdai_sample_result < affection_sample_result
or pusdc_sample_result < affection_sample_result):
pdai_percent_diff = ((pdai_sample_result - affection_sample_result) / affection_sample_result) * 100
pusdc_percent_diff = ((pusdc_sample_result - affection_sample_result) / affection_sample_result) * 100
# pdai/pusdc price must be cheaper and over the diff threshold
if (pdai_percent_diff < 0 and abs(pdai_percent_diff) >= sell_percent_diff_pdai) \
or (pusdc_percent_diff < 0 and abs(pusdc_percent_diff) >= sell_percent_diff_pusdc):
estimated_swap_result = estimate_swap_result(
'PulseX_v2',
affection_address,
@ -91,20 +106,26 @@ while True:
slippage_percent
):
logging.info("Swapped {} AFFECTION™ to PLS".format(amount))
i += 1
else:
logging.warning("No estimated swap result from RPC")
break
# delay if amounts remain in the list
if i + 1 != len(selling_amounts):
if i <= len(selling_amounts):
logging.info("Waiting for {} seconds...".format(loop_sell_delay))
time.sleep(loop_sell_delay)
else:
logging.info("AFFECTION™ is not within range to sell yet ({}%)".format(sell_percent_diff_affection))
break
# resample the prices
pdai_sample_result = sample_exchange_rate('PulseX_v2', pdai_address, wpls_address)
pusdc_sample_result = sample_exchange_rate('PulseX_v2', pusdc_address, wpls_address)
affection_sample_result = sample_exchange_rate('PulseX_v2', affection_address, wpls_address)
else:
logging.info("AFFECTION™ price hasn't increased yet")
# save the last sample price
affection_sample_result_last = affection_sample_result
logging.info("AFFECTION™ price is not within targeted range for selling")
break
else:
logging.info("AFFECTION™ price is too low")
break
# wait before next loop
log_end_loop(loop_delay)