forked from wmalgo/LimitBreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dexTrader.py
100 lines (90 loc) · 3.63 KB
/
dexTrader.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
import logging, time
class DexTrader:
def __init__(self,exchange,web3_providor):
self.client = exchange
self.w3 = web3_providor
self.account = self.client.account
def AmountSlippage(self,pair_contract,slippage):
first_token = self.w3.toChecksumAddress(pair_contract.functions.token0().call())
if first_token == self.client.currency:
reserves = pair_contract.functions.getReserves().call()
shitcoin = float(self.w3.fromWei(reserves[1], 'ether'))
limit = (0.01 * shitcoin) / (1 - (slippage/100))
return self.w3.toWei(limit, 'ether')
else:
reserves = pair_contract.functions.getReserves().call()
shitcoin = float(self.w3.fromWei(reserves[0], 'ether'))
limit = (0.01 * shitcoin) / (1 - (slippage/100))
return self.w3.toWei(limit, 'ether')
def getTokenBalance(self,token_contract):
balance = token_contract.functions.balanceOf(self.account).call()
return balance
def getPath(self,pair_contract, mode):
token0 = self.w3.toChecksumAddress(pair_contract.functions.token0().call())
token1 = self.w3.toChecksumAddress(pair_contract.functions.token1().call())
if mode == 'buy':
if token0 == self.client.currency:
path = [token0,token1]
else:
path = [token1,token0]
elif mode == 'sell':
if token0 == self.client.currency:
path = [token1,token0]
else:
path = [token0,token1]
return path
def getBalance(self):
balance = self.w3.eth.getBalance(self.account)
gas_money = self.w3.toWei(0.1,'ether')
return balance - gas_money
def getTradeAmount(self):
balance = self.getBalance()
lowest_amount_allowed = self.w3.toWei(2.5,'ether')
if balance >= lowest_amount_allowed:
return balance
else:
return None
def tx_isValid(self,tx_hash):
hex_hash = self.w3.toHex(tx_hash)
while True:
try:
receipt = self.w3.eth.getTransactionReceipt(hex_hash)
receipt_dict = dict(receipt)
status = receipt_dict.get('status')
break
except:
time.sleep(10)
if status == 1:
return True
else:
return False
def buy(self,pair_contract,price,time):
rsi_value = round(price)
logging.info('BUY: {} at {} RSI'.format(pair_contract,rsi_value))
buy_amount = self.getTradeAmount()
if buy_amount <= self.AmountSlippage(pair_contract,20) and buy_amount != None:
amount = buy_amount
else:
return False
amountl = int(amount/price)
amountmin = amountl - (int(amountl * 0.03))
path = self.getPath(pair_contract, 'buy')
tx_hash = self.client.swapExactETHForTokens(amount,amountmin,path)
time.sleep(10)
validity = tx_hash(self.tx_isValid(tx_hash))
if validity == True:
return True
else:
return False
return True
def sell(self,token_contract,price,time):
if self.client.isApproved(token_contract) == False:
self.client.approve(token_contract)
time.sleep(10)
amount = self.getTokenBalance(token_contract)
amount_min = int(price * (amount - (amount * 0.05)))
path = self.getPath(token_contract,'sell')
tx_hash = self.client.swapExactTokensForETH(amount,amount_min,path)
return tx_hash
def closePositions(self):
return