-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswap.py
128 lines (95 loc) Β· 4.64 KB
/
swap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from classes.common import (
check_database,
check_version,
get_user_choice
)
from constants.constants import (
FULL_COIN_LOOKUP,
ENABLE_TRADING_BOT,
ULUNA,
USER_ACTION_QUIT
)
from classes.swap_transaction import swap_coins
from classes.transaction_core import TransactionResult
from classes.wallet import UserParameters
from classes.wallets import UserWallet, UserWallets
from terra_classic_sdk.core.coin import Coin
def main():
# Check if there is a new version we should be using
check_version()
check_database()
# Get the user wallets
wallets = UserWallets()
user_wallets = wallets.loadUserWallets()
if user_wallets is None:
print ("\n π This password couldn't decrypt any wallets. Make sure it is correct, or rebuild the wallet list by running the configure_user_wallet.py script again.\n")
exit()
if len(user_wallets) > 0:
print (f'You can make swaps on the following wallets:')
wallet:UserWallet
wallet, answer = wallets.getUserSinglechoice("Select a wallet number 1 - " + str(len(user_wallets)) + ", 'X' to continue, or 'Q' to quit: ")
if answer == USER_ACTION_QUIT:
print ('\n π Exiting...\n')
exit()
else:
print ("\n π This password couldn't decrypt any wallets. Make sure it is correct, or rebuild the wallet list by running the configure_user_wallet.py script again.\n")
exit()
# Swaps use LUNC as the denom - if there is no LUNC in the wallet, then exit
if ULUNA not in wallet.balances:
print ('\nYou need LUNC in this wallet to pay for swap transactions.')
print ('Please transfer some LUNC to this address before swapping.')
print ('\n π Exiting...\n')
exit()
# List all the coins in this wallet, with the amounts available:
print ('\nWhat coin do you want to swap FROM?')
coin_count:int = 0
for coin in wallet.balances:
if coin in FULL_COIN_LOOKUP:
coin_count += 1
coin_from, answer, _ = wallet.getCoinSelection("Select a coin number 1 - " + str(coin_count) + ", 'X' to continue, or 'Q' to quit: ", wallet.balances)
if answer == USER_ACTION_QUIT:
print ('\n π Exiting...\n')
exit()
available_balance:float = wallet.formatUluna(wallet.balances[coin_from], coin_from)
print (f'This coin has a maximum of {available_balance} {FULL_COIN_LOOKUP[coin_from]} available.')
user_params:UserParameters = UserParameters()
user_params.max_number = float(available_balance)
user_params.percentages_allowed = True
user_params.target_amount = wallet.formatUluna(wallet.balances[coin_from], coin_from)
user_params.target_denom = coin_from
swap_uluna = wallet.getUserNumber("How much do you want to swap? (Or type 'Q' to quit) ", user_params)
if swap_uluna == USER_ACTION_QUIT:
print ('\n π Exiting...\n')
exit()
else:
swap_uluna = float(swap_uluna)
print ('\nWhat coin do you want to swap TO?')
coin_to, answer, estimated_amount = wallet.getCoinSelection("Select a coin number 1 - " + str(len(FULL_COIN_LOOKUP)) + ", 'X' to continue, or 'Q' to quit: ", wallet.balances, False, {'denom':coin_from, 'amount':swap_uluna})
if answer == USER_ACTION_QUIT:
print ('\n π Exiting...\n')
exit()
estimated_amount = str(("%.6f" % (estimated_amount)).rstrip('0').rstrip('.'))
swap_coin:Coin = wallet.createCoin(swap_uluna, coin_from)
# Get the trading bot options (if any):
log_trade:bool = False
if ENABLE_TRADING_BOT == True:
log_trade = get_user_choice(' β Do you want to add this to the trading bot? (y/n) ', [])
log_trade_params:dict = {}
if log_trade == True:
user_params = UserParameters()
user_params.only_percentages = True
user_params.percentages_allowed = True
exit_profit:float = float(wallet.getUserNumber(' β What is the profit threshold? (10% is recommended) ', user_params))
exit_profit = exit_profit / 100
exit_loss:float = float(wallet.getUserNumber(' β What is the loss threshold? (20% is recommended) ', user_params))
exit_loss = exit_loss / 100
log_trade_params['exit_profit'] = exit_profit
log_trade_params['exit_loss'] = exit_loss
transaction_result:TransactionResult = swap_coins(wallet, swap_coin, coin_to, estimated_amount, False, log_trade, log_trade_params)
transaction_result.showResults()
print ('\n π― Done!\n')
if __name__ == "__main__":
""" This is executed when run from the command line """
main()