forked from daigakuimo/CryptoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HarmonyFairTrade.py
155 lines (118 loc) · 4.2 KB
/
HarmonyFairTrade.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
from web3 import Web3
from datetime import datetime
import time
import config
harmonyRpc = "https://rpc.s0.t.hmny.io"
walletAddress = config.WALLET_A
walletKey = config.KEY_A
STAR = "0xb914e7a183abcd46300584da828f62a39516f33b"
SPEED = "0x2dae9ac8e3195715f308b59e7e9326f115ab4d98"
JOC = "0x22fb638a010e922d53fd2671a598a3334c228b62"
WONE = "0xcf664087a5bb0237a0bad6742852ec6c8d69a27a"
HarmonyRouter = '0x1b02da8cb0d097eb8d57a175b88c7d8b47997506'
HarmonyFactory = '0xc35DADB65012eC5796536bD9864eD8773aBc74C4'
gas = 190000
gasPrice = 31
targetToken = JOC
targetAmountWONE = 4000
web3 = Web3(Web3.HTTPProvider(harmonyRpc))
### Create Pancake Swap Router Contract ###
with open('ABI/UniswapABI.json') as f:
uniswapRouterABI = json.load(f)
uniswapRouterContract = web3.eth.contract(address=web3.toChecksumAddress(HarmonyRouter), abi=uniswapRouterABI)
### Create Pancake Swap Factory Contract ###
with open('ABI/UniswapFactoryABI.json') as f:
uniswapFactoryABI = json.load(f)
uniswapFactoryContract = web3.eth.contract(address=web3.toChecksumAddress(HarmonyFactory), abi=uniswapFactoryABI)
def checkLP( token1, token2 ):
### Get LP address ###
lpAddr = uniswapFactoryContract.functions.getPair( token1, token2 ).call()
if web3.toInt(hexstr=lpAddr) == 0:
return False
else:
return lpAddr
def waitLiquiditySupply( lpAddr, interval ):
with open('ABI/SushiLPABI.json') as f:
uniswapLPABI = json.load(f)
uniswapLPContract = web3.eth.contract(address=lpAddr, abi=uniswapLPABI)
while True:
try:
liq = uniswapLPContract.functions.totalSupply().call()
if liq > 0:
return True
except:
print('error')
time.sleep(interval)
# def buyTokenETH( token, amountWBNB ):
# print('Swap WBNB to target token')
# now = int(datetime.now().timestamp())
# nonce = web3.eth.getTransactionCount(walletAddress)
# funcSwap = uniswapRouterContract.functions.swapExactETHForTokens(
# 0,
# [WBNB, token],
# walletAddress,
# now+300
# )
# tx = funcSwap.buildTransaction({
# 'value': web3.toWei(amountWBNB, 'ether'),
# 'gas': gas,
# 'gasPrice': web3.toWei(gasPrice, 'gwei'),
# 'nonce': nonce
# })
# signedTx = web3.eth.account.sign_transaction(tx, walletKey)
# txHash = web3.eth.sendRawTransaction(signedTx.rawTransaction)
# print('Send transaction')
# print(web3.toHex(txHash))
# result = web3.eth.wait_for_transaction_receipt(txHash)
# status = result['status']
# if status == 1:
# print('Transaction Succeeded')
# else:
# print('Transaction Failed')
# return status
def buyToken( token, amountWONE ):
print('Swap WONE to target token')
now = int(datetime.now().timestamp())
nonce = web3.eth.getTransactionCount(walletAddress)
funcSwap = uniswapRouterContract.functions.swapExactTokensForTokens(
web3.toWei(amountWONE, 'ether'),
0,
[web3.toChecksumAddress(WONE), web3.toChecksumAddress(token)],
walletAddress,
now+300
)
tx = funcSwap.buildTransaction({
'gas': gas,
'gasPrice': web3.toWei(gasPrice, 'gwei'),
'nonce': nonce
})
signedTx = web3.eth.account.sign_transaction(tx, walletKey)
txHash = web3.eth.sendRawTransaction(signedTx.rawTransaction)
print('Send transaction')
print(web3.toHex(txHash))
result = web3.eth.wait_for_transaction_receipt(txHash)
status = result['status']
if status == 1:
print('Transaction Succeeded')
else:
print('Transaction Failed')
return status
if __name__ == '__main__':
lpAddedWBNB = False
lpAddedBUSD = False
print('Wait LP Pair...')
while True:
lpAddrWONE = checkLP(web3.toChecksumAddress(targetToken), web3.toChecksumAddress(WONE))
if lpAddrWONE:
print('Find LP Pair with WONE')
print('Wait Liquidity...')
waitLiquiditySupply(lpAddrWONE, 0.5)
print('Liquidity Added!')
lpAddedWONE = True
break
else:
time.sleep(1)
### Buy Token ###
if lpAddedWONE:
buyToken( targetToken, targetAmountWONE )