-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpecunia_ad_vitam.py
executable file
·139 lines (117 loc) · 5.05 KB
/
pecunia_ad_vitam.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
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
import logging
import os
import sys
import config
from package import cmd_args_pecunia, const
from package.announcer import Announcer
from package.chain import Chain
from package.token import Token, Wallet
from package.utils import get_private_key
from package.web3_functions import approve, swap, wait_for_token, wait_for_transaction, connect, show_balance, \
is_approved
log = logging.getLogger(__name__)
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
def main(cfg, announcer):
private_key = None
if cfg["token_swap"] or cfg["token_approve"]:
private_key = get_private_key()
if cfg["token_wait_time"] > const.WEB3_TIMEOUT:
timeout = cfg["token_wait_time"] * 1.2
else:
timeout = const.WEB3_TIMEOUT
try:
web3 = connect(network=cfg["network"], router=cfg["router"], private_key=private_key, timeout=timeout)
except Exception as e:
log.error(e)
announcer.print_and_voice(e)
exit(1)
else:
if cfg["token_wait_time"] > const.WEB3_TIMEOUT:
timeout = cfg["token_wait_time"] * 1.2
else:
timeout = const.WEB3_TIMEOUT
try:
web3 = connect(network=cfg["network"], timeout=timeout)
except Exception as e:
log.error(e)
announcer.print_and_voice(e)
exit(1)
wallet = Wallet(cfg["wallet_address"])
try:
token = Token(cfg["token_address"])
except Exception as e:
exit(1)
if (cfg["token_swap"] or cfg["token_approve"]):
if not is_approved(token, wallet):
log.info(f"[+] Approving {token.get_symbol()} for trading.")
approve(token, wallet)
announcer.print_and_voice(f"[+] Token {token.get_symbol()} approved.")
else:
print(f"[+] Token {token.get_symbol()} already approved.")
log.info(f"[+] Token {token.get_symbol()} already approved.")
if cfg["token_wait"]:
log.info("[+] wait_for_token")
try:
wait_for_token(wallet.get_address(), token.get_address(), token_wait_time=cfg["token_wait_time"])
except Exception as e:
log.error(e)
log.error(f'wait_for_token() exited prematurely!')
announcer.all(f'wait_for_token() exited prematurely!')
exit(1)
if not cfg["token_swap"]:
announcer.all(f'[+] Token {token.get_symbol()} received.')
else:
announcer.voice("Token received")
if cfg["token_swap"]:
log.info("[+] token_swap")
try:
token_dst = Token(cfg["token_dst_address"])
except Exception as e:
exit(1)
show_balance(token, token_dst, wallet)
if cfg["token_swap_all"]:
swap_amount = token.find_balance_full(wallet.get_address())
else:
swap_amount = web3.toWei(cfg["token_swap_amount"], token.get_units())
print(
f'[+] Swapping: {web3.fromWei(swap_amount, token.get_units())} {token.get_symbol()} for '
f'{token_dst.get_symbol()} with {cfg["gas_speed"]} speed and slippage {cfg["slippage"]}')
try:
tx_hash = swap(token, token_dst, swap_amount, wallet_address=wallet.get_address(), slippage = cfg["slippage"],
gas_speed=cfg["gas_speed"])
except Exception as e:
log.error(e)
log.error("[?] swap() exited prematurely! Slippage issues?")
announcer.all(f'[?] swap() exited prematurely! Slippage issues?')
exit(1)
print(f'[+] Swap transaction for {token.get_symbol()}: {Chain().get_url_tx(tx_hash)}')
wait_for_transaction(tx_hash)
announcer.all(f'[+] Token {token.get_symbol()} sold')
show_balance(token, token_dst, wallet)
if __name__ == "__main__":
if len(sys.argv) != 1:
cfg = cmd_args_pecunia.get_arguments(sys.argv)
else:
cfg = {"network": config.network,
"router": config.router_name, "wallet_address": config.wallet_address,
"token_address": config.token_address, "token_swap": config.token_swap,
"token_swap_all": config.token_swap_all, "token_dst_address": config.token_dst_address,
"token_swap_amount": config.token_swap_amount, "token_wait": config.token_wait,
"use_pushsafer": config.use_pushsafer, "use_mac_voice": config.use_mac_voice,
"gas_speed": config.GAS_SPEED, "slippage": config.SLIPPAGE, "token_wait_time": config.TOKEN_WAIT_TIME,
"token_approve": const.APPROVE}
announcer = Announcer(use_pushsafer=cfg["use_pushsafer"], pushsafer_key=config.pushsafer_key,
use_voice=cfg["use_mac_voice"])
try:
main(cfg, announcer)
except KeyboardInterrupt:
print('\n[!] Interrupted')
try:
exit(0)
except SystemExit:
exit(0)
except Exception as e:
print("[+] Exception. Treat it before this moment.")
announcer.all("Exception encountered!")
raise e