-
Notifications
You must be signed in to change notification settings - Fork 19
/
voly.py
207 lines (170 loc) · 5.54 KB
/
voly.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# __ Imports and initialisations ______________________________________________
from x import con
import asyncio
from os import getenv
from dotenv import load_dotenv
from time import time
load_dotenv()
w3 = con('POLYGON')
# __ Configuration ____________________________________________________________
# @notice where to store receipts
LOGS = 'logs/receipts.log'
# @notice import account
EOA = '<INSERTADDRESS>'
KEY = getenv('RANGE_KEY')
# @notice hardcode decimals
RES0_DEC = 18
RES1_DEC = 6
# @notice target pool (WMATIC -> USDC)
PAIR_ADDRESS = '0x6e7a5FAFcec6BB1e78bAE2A1F0B612012BF14827'
WMATIC = '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270'
USDC = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174'
# @notice for now: Quickswap router
ROUTER_ADDRESS = '0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff'
# @dev if there is a delay, how long.
POLL_DELAY = 2
# @notice position size in USDC
SIZE = 20000000
# @notice buy n sell ranges
supply_zone = 0.79
demand_zone = 0.74
stop_loss = 0.697
# @notice volatile settings
cached_nonce = w3.eth.get_transaction_count(EOA)
# __ Helpers __________________________________________________________________
def fetch_abi(_file: str) -> str:
with open(f'abi/{_file}') as f:
abi = f.read().strip()
return abi
def c_init(_addr: str, _abi: str) -> object:
return w3.eth.contract(address=_addr, abi=_abi)
def get_reserves(_pair: object) -> (int, int, int):
return _pair.functions.getReserves().call()
async def poll_reserves(_pair: object) -> list:
await asyncio.sleep(POLL_DELAY)
res0, res1, stamp = get_reserves(_pair)
return [res0, res1, stamp]
# __ Filler functions _________________________________________________________
def from_wei(_n: int, _dec: int) -> float:
return _n / 10 ** _dec
def to_wei(_n: int, _dec: int) -> int:
return int(_n * 10 ** _dec)
def calcSlippage(_slippage: float, _amount: int) -> int:
return int(_slippage / 100 * _amount)
def dline() -> int:
return round(time()) + (60 * 5)
def getAmountOut(amountIn: int, reserveIn: int, reserveOut: int) -> int:
amountInWithFee = amountIn * 997
numerator = amountInWithFee * reserveOut
denominator = (reserveIn * 1000) + amountInWithFee
amountOut = numerator / denominator
return int(amountOut)
def getAmountIn(amountOut: int, reserveIn: int, reserveOut: int) -> int:
numerator = (reserveIn * amountOut) * 1000
denominator = (reserveOut - amountOut) * 997
amountIn = (numerator / denominator) + 1
return int(amountIn)
tx = {
'chainId': 137,
'type': '0x2',
'from': EOA,
'value': 0,
'gas': 200000,
'maxFeePerGas': 35000000000,
'maxPriorityFeePerGas': 35000000000,
'nonce': cached_nonce
}
def from_exact(
_router: object,
_amount_in: int,
_amount_out_min: int,
_token_in: str,
_token_out: str
):
return _router.functions.swapExactTokensForTokens(
_amount_in,
_amount_out_min,
[_token_in, _token_out],
EOA,
dline(),
).buildTransaction(tx)
def to_exact(
_router: object,
_amount_out: int,
_amount_in_max: int,
_token_in: str,
_token_out: str
):
return _router.functions.swapTokensForExactTokens(
_amount_out,
_amount_in_max,
[_token_in, _token_out],
EOA,
dline(),
).buildTransaction(tx)
def sign_tx(tx: object, key: str) -> str:
sig = w3.eth.account.sign_transaction
return sig(tx, private_key=key)
def send_tx(signed_tx: object) -> str:
w3.eth.send_raw_transaction(signed_tx.rawTransaction)
return w3.toHex(w3.keccak(signed_tx.rawTransaction))
def log(tx_hash: str):
r = w3.eth.wait_for_transaction_receipt(tx_hash)
s = str({ tx_hash: r})
with open(LOGS, 'w') as f:
f.write(s)
f.close()
def buy(tx: object):
tx_hash = send_tx(sign_tx(tx, KEY))
print(f'[+] loaded: {tx_hash})')
log(tx_hash)
def sell(tx: object):
tx_hash = send_tx(sign_tx(tx, KEY))
print(f'[-] unload: {tx_hash})')
log(tx_hash)
# __ SAFETY ___________________________________________________________________
def halt():
should_buy = 0
should_sell = 0
amount_out = getAmountIn(SIZE, res0, res1)
amount_out += calcSlippage(1, amount_out)
at_loss = to_exact(router, amount_out, SIZE, WMATIC, USDC)
sell(at_loss)
quit()
def safety(_price: int):
if _price < stop_loss:
halt()
# __ Main _____________________________________________________________________
async def main():
pair = c_init(PAIR_ADDRESS, fetch_abi('pair.json'))
router = c_init(ROUTER_ADDRESS, fetch_abi('router.json'))
should_buy = True
should_sell = False
cached_stamp = 0
print('running...')
while(1):
res0, res1, stamp = await poll_reserves(pair)
if stamp > cached_stamp:
matic_reserve = res0 / 10 ** RES0_DEC
usdc_reserve = res1 / 10 ** RES1_DEC
matic_price = usdc_reserve / matic_reserve
safety(matic_price)
if matic_price <= demand_zone and should_buy:
amount_in = getAmountOut(SIZE, res1, res0)
amount_in -= calcSlippage(0.5, amount_in)
at_discount = from_exact(router, SIZE, amount_in, USDC, WMATIC)
buy(at_discount)
cached_nonce = w3.eth.get_transaction_count(EOA)
should_buy = False
should_sell = True
elif matic_price >= supply_zone and should_sell:
amount_out = getAmountIn(SIZE, res0, res1)
amount_out += calcSlippage(0.5, amount_out)
at_premium = to_exact(router, amount_out, SIZE, WMATIC, USDC)
sell(at_premium)
cached_nonce = w3.eth.get_transaction_count(EOA)
should_buy = True
should_sell = False
cached_stamp = stamp
if __name__ == '__main__':
asyncio.run(main())