-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecution.py
84 lines (71 loc) · 3.18 KB
/
execution.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
# Helper class for executing trades against Mycelium Perp Swaps
from mlp_helpers import getTokenPriceWithSpread
referral_code = "0x0000000000000000000000000000000000000000000000000000000000000000"
# increases position usign ERC20 as collat per https://swaps.docs.mycelium.xyz/developer-resources/contract-interactions
# token_in -> token being used to take the position (use stablecoin ERC20 with best borrowing rate)
# target_token -> asset to take the position on (ETH or BTC)
# collateral_amount -> amount of collat (stablecoin) going in (units 10^18)
# leverage -> integer amount of leverage being taken -> todo might be better ways to pass in this param
# size_delta -> USD value of change in position size. 10^30 units
# price -> USD price in 10^30 units
def increasePositionToken(router, vault, token_in, target_token, collateral_amount_in, size_delta, is_long, account, w3):
execution_fee = 150000000000000 #todo
nonce = w3.eth.get_transaction_count(account.address)
price_with_spread = getTokenPriceWithSpread(vault, target_token, is_long)
transaction = router.functions.createIncreasePosition(
[token_in],
target_token,
collateral_amount_in,
0,
size_delta,
is_long,
int(price_with_spread),
execution_fee,
referral_code
).build_transaction({
'chainId': 42161,
'gas': 3000000,
'maxFeePerGas': w3.toWei('0.1', 'gwei'),
'maxPriorityFeePerGas': w3.toWei('0.1', 'gwei'),
'nonce': nonce,
'value': execution_fee
})
signed = account.sign_transaction(transaction)
result = w3.eth.send_raw_transaction(signed.rawTransaction)
# return txn hash
return result
# decreases position using ERC20 as collat per https://swaps.docs.mycelium.xyz/developer-resources/contract-interactions
def decreasePositionToken(router, vault, token_out, target_token, collateral_amount_out, size_delta, is_long, account, w3):
execution_fee = 150000000000000 #todo
nonce = w3.eth.get_transaction_count(account.address)
# use current oracle price + accepted deviation to price order
# todo improve pricing here / verify safety
oracle_price = vault.functions.getMinPrice(target_token).call()
price_with_slippage = oracle_price + (oracle_price * 0.005)
print(int(price_with_slippage))
print(oracle_price)
# invert is long because closing a short -> want max price. Closing a long -> want min price to avoid issues.
price_with_spread = getTokenPriceWithSpread(vault, target_token, not is_long)
transaction = router.functions.createDecreasePosition(
[token_out],
target_token,
collateral_amount_out,
size_delta,
is_long,
account.address,
int(price_with_spread),
0,
execution_fee,
False
).build_transaction({
'chainId': 42161,
'gas': 3000000,
'maxFeePerGas': w3.toWei('0.15', 'gwei'),
'maxPriorityFeePerGas': w3.toWei('0.15', 'gwei'),
'nonce': nonce,
'value': execution_fee
})
signed = account.sign_transaction(transaction)
result = w3.eth.send_raw_transaction(signed.rawTransaction)
# return txn hash
return result